cd4d2570f2afda3c659213c505380cded48a2eda
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.controlloop;
22
23 import java.io.Serializable;
24 import java.util.Map;
25 import java.util.UUID;
26 import java.util.concurrent.ConcurrentHashMap;
27 import lombok.AccessLevel;
28 import lombok.Getter;
29 import lombok.NonNull;
30 import lombok.Setter;
31 import org.onap.policy.controlloop.VirtualControlLoopEvent;
32
33 /**
34  * Context associated with a control loop event.
35  */
36 @Getter
37 @Setter
38 public class ControlLoopEventContext implements Serializable {
39     private static final long serialVersionUID = 1L;
40
41
42     private final VirtualControlLoopEvent event;
43
44     /**
45      * Enrichment data extracted from the event. Never {@code null}, though it may be
46      * immutable.
47      */
48     private final Map<String, String> enrichment;
49
50     @Getter(AccessLevel.NONE)
51     @Setter(AccessLevel.NONE)
52     private Map<String, Serializable> properties = new ConcurrentHashMap<>();
53
54     /**
55      * Request ID extracted from the event, or a generated value if the event has no
56      * request id; never {@code null}.
57      */
58     private final UUID requestId;
59
60
61     /**
62      * Constructs the object.
63      *
64      * @param event event with which this is associated
65      */
66     public ControlLoopEventContext(@NonNull VirtualControlLoopEvent event) {
67         this.event = event;
68         this.requestId = (event.getRequestId() != null ? event.getRequestId() : UUID.randomUUID());
69         this.enrichment = (event.getAai() != null ? event.getAai() : Map.of());
70     }
71
72     /**
73      * Determines if the context contains a property.
74      *
75      * @param name name of the property of interest
76      * @return {@code true} if the context contains the property, {@code false} otherwise
77      */
78     public boolean contains(String name) {
79         return properties.containsKey(name);
80     }
81
82     /**
83      * Gets a property, casting it to the desired type.
84      *
85      * @param <T> desired type
86      * @param name name of the property whose value is to be retrieved
87      * @return the property's value, or {@code null} if it does not yet have a value
88      */
89     @SuppressWarnings("unchecked")
90     public <T> T getProperty(String name) {
91         return (T) properties.get(name);
92     }
93
94     /**
95      * Sets a property's value.
96      *
97      * @param name property name
98      * @param value new property value
99      */
100     public void setProperty(String name, Serializable value) {
101         properties.put(name, value);
102     }
103 }