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.policy.annotation
18 
19 /**
20  * An Arcs annotations containing additional information on an Arcs manifest element.
21  * An Annotation may be attached to a plan, particle, handle, type etc.
22  */
23 data class Annotation(val name: String, val params: Map<String, AnnotationParam> = emptyMap()) {
24 
getParamnull25   fun getParam(name: String): AnnotationParam {
26     return requireNotNull(params[name]) {
27       "Annotation '$this.name' missing '$name' parameter"
28     }
29   }
30 
getStringParamnull31   fun getStringParam(paramName: String): String {
32     val paramValue = getParam(paramName)
33     require(paramValue is AnnotationParam.Str) {
34       "Annotation param $paramName must be string, instead got $paramValue"
35     }
36     return paramValue.value
37   }
38 
getOptionalStringParamnull39   fun getOptionalStringParam(paramName: String): String? {
40     return if (params.containsKey(paramName)) getStringParam(paramName) else null
41   }
42 
43   companion object {
createArcIdnull44     fun createArcId(id: String) = Annotation("arcId", mapOf("id" to AnnotationParam.Str(id)))
45 
46     fun createTtl(value: String) = Annotation(
47       "ttl",
48       mapOf("value" to AnnotationParam.Str(value))
49     )
50 
51     fun createCapability(name: String) = Annotation(name)
52 
53     /**
54      * Returns an annotation indicating that a particle is an egress particle.
55      *
56      * @param egressType optional egress type for the particle
57      */
58     fun createEgress(egressType: String? = null): Annotation {
59       val params = mutableMapOf<String, AnnotationParam>()
60       if (egressType != null) {
61         params["type"] = AnnotationParam.Str(egressType)
62       }
63       return Annotation("egress", params)
64     }
65 
66     /** Returns an annotation indicating the name of the policy which governs a recipe. */
createPolicynull67     fun createPolicy(policyName: String): Annotation {
68       return Annotation("policy", mapOf("name" to AnnotationParam.Str(policyName)))
69     }
70 
71     /** Annotation indicating that a particle is isolated. */
72     val isolated = Annotation("isolated")
73 
74     /** Annotation indicating that a particle has ingress. */
75     val ingress = Annotation("ingress")
76   }
77 }
78