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.builder 18 19 import com.android.libraries.pcc.chronicle.api.DeletionTrigger 20 import com.android.libraries.pcc.chronicle.api.Trigger 21 import com.android.libraries.pcc.chronicle.api.policy.PolicyTarget 22 import com.android.libraries.pcc.chronicle.api.policy.annotation.Annotation 23 import com.android.libraries.pcc.chronicle.api.policy.annotation.AnnotationParam 24 25 /** The constants used to encode a [DeletionTrigger] onto a target of the [Policy]. */ 26 object DeletionTriggerPolicyAnnotations { 27 const val ANNOTATION_NAME = "deletionTrigger" 28 const val TRIGGER_KEY = "trigger" 29 const val FIELD_KEY = "field" 30 } 31 32 /** Retrieve [DeletionTrigger] from a [PolicyTarget]. */ deletionTriggersnull33fun PolicyTarget.deletionTriggers(): Set<DeletionTrigger> { 34 return annotations.deletionTriggers() 35 } 36 37 /** Encodes a trigger as a Policy annotation */ toAnnotationnull38internal fun Trigger.toAnnotation(field: String): Annotation { 39 return Annotation( 40 DeletionTriggerPolicyAnnotations.ANNOTATION_NAME, 41 mapOf( 42 DeletionTriggerPolicyAnnotations.TRIGGER_KEY to AnnotationParam.Str(this.name), 43 DeletionTriggerPolicyAnnotations.FIELD_KEY to AnnotationParam.Str(field), 44 ) 45 ) 46 } 47 deletionTriggersnull48private fun Collection<Annotation>.deletionTriggers(): Set<DeletionTrigger> { 49 return this.filter { it.name == DeletionTriggerPolicyAnnotations.ANNOTATION_NAME } 50 .map { 51 DeletionTrigger( 52 Trigger.valueOf(it.getStringParam(DeletionTriggerPolicyAnnotations.TRIGGER_KEY)), 53 it.getStringParam(DeletionTriggerPolicyAnnotations.FIELD_KEY) 54 ) 55 } 56 .toSet() 57 } 58