1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.pcc.chronicle.api
18 
19 import java.time.Duration
20 
21 /**
22  * Declares the strategy a given [ConnectionProvider] uses to manage data provided via the
23  * [Connection]s it supports.
24  */
25 sealed class ManagementStrategy {
26   /**
27    * Declares that the data accessed via [Connection]s governed by [PassThru] are not held in memory
28    * or stored on disk but simply passed from writer to reader.
29    *
30    * Common use-case example: data processing pipelines.
31    */
32   object PassThru : ManagementStrategy()
33 
34   /** Declares that the data accessed via [Connection]s are held, and how they're held. */
35   data class Stored(
36     /** Whether or not the data is encrypted at rest. */
37     val encrypted: Boolean,
38     /** The storage location. */
39     val media: StorageMedia,
40     /** The maximum allowable age of data being stored. */
41     val ttl: Duration?,
42     /** Connected deletion triggers for this data. */
43     val deletionTriggers: Set<DeletionTrigger> = emptySet(),
44   ) : ManagementStrategy()
45 }
46 
47 /**
48  * General type of storage technology being used to manage data.
49  *
50  * The value of [danger] implies the risk associated with storing data using the given
51  * [StorageMedia], while the ordinal value of the enum is related to the [danger], they should not
52  * be used interchangeably.
53  */
54 enum class StorageMedia(val danger: Int) {
55   /** The data is not persisted, and is only held in memory for at most: the life of the process. */
56   MEMORY(0),
57 
58   /** The data is stored local to the device, persisted to disk. */
59   LOCAL_DISK(1),
60 
61   /** The data is persisted, but to an external device. */
62   REMOTE_DISK(2)
63 }
64 
65 /**
66  * A helper method to return the TTL for a [ManagementStrategy].
67  *
68  * If the [ManagementStrategy] returns a null TTL, the provided `default` value will be returned.
69  * The `default` parameter itself defaults to [Duration.ZERO].
70  */
ManagementStrategynull71 fun ManagementStrategy.ttl(default: Duration = Duration.ZERO): Duration {
72   return when (this) {
73     ManagementStrategy.PassThru -> Duration.ZERO
74     is ManagementStrategy.Stored -> ttl ?: default
75   }
76 }
77 
78 /** A helper method that returns whether or not a [ManagementStrategy] is persisted to disk. */
ManagementStrategynull79 fun ManagementStrategy.isPersisted(): Boolean {
80   return when (this) {
81     ManagementStrategy.PassThru -> false
82     is ManagementStrategy.Stored -> this.media != StorageMedia.MEMORY
83   }
84 }
85