Make naming consistent regarding synchronization
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / repository / Ric.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.repository;
22
23 import com.google.common.collect.ImmutableList;
24
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import lombok.Getter;
31 import lombok.Setter;
32
33 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
34 import org.oransc.policyagent.configuration.RicConfig;
35
36 /**
37  * Represents the dynamic information about a NearRealtime-RIC.
38  */
39 public class Ric {
40
41     private final RicConfig ricConfig;
42     private final ImmutableList<String> managedElementIds;
43
44     private RicState state = RicState.UNDEFINED;
45     private Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
46     @Getter
47     @Setter
48     private A1ProtocolType protocolVersion = A1ProtocolType.UNKNOWN;
49
50     @Getter
51     private final Lock lock = new Lock();
52
53     /**
54      * Creates the Ric. Initial state is {@link RicState.UNDEFINED}.
55      *
56      * @param ricConfig The {@link RicConfig} for this Ric.
57      */
58     public Ric(RicConfig ricConfig) {
59         this.ricConfig = ricConfig;
60         this.managedElementIds = ricConfig.managedElementIds();
61     }
62
63     public String name() {
64         return ricConfig.name();
65     }
66
67     public RicConfig getConfig() {
68         return this.ricConfig;
69     }
70
71     public synchronized RicState getState() {
72         return this.state;
73     }
74
75     public synchronized void setState(RicState state) {
76         this.state = state;
77     }
78
79     /**
80      * Gets the nodes managed by this Ric.
81      *
82      * @return a vector containing the nodes managed by this Ric.
83      */
84     public synchronized Collection<String> getManagedElementIds() {
85         return managedElementIds;
86     }
87
88     /**
89      * Determines if the given node is managed by this Ric.
90      *
91      * @param managedElementId the node name to check.
92      * @return true if the given node is managed by this Ric.
93      */
94     public synchronized boolean isManaging(String managedElementId) {
95         return managedElementIds.contains(managedElementId);
96     }
97
98     /**
99      * Gets the policy types supported by this Ric.
100      *
101      * @return the policy types supported by this Ric in an unmodifiable list.
102      */
103     public synchronized Collection<PolicyType> getSupportedPolicyTypes() {
104         return new Vector<>(supportedPolicyTypes.values());
105     }
106
107     public synchronized Collection<String> getSupportedPolicyTypeNames() {
108         return new Vector<>(supportedPolicyTypes.keySet());
109     }
110
111     /**
112      * Adds a policy type as supported by this Ric.
113      *
114      * @param type the policy type to support.
115      */
116     public synchronized void addSupportedPolicyType(PolicyType type) {
117         supportedPolicyTypes.put(type.name(), type);
118     }
119
120     /**
121      * Removes all policy type as supported by this Ric.
122      */
123     public synchronized void clearSupportedPolicyTypes() {
124         supportedPolicyTypes.clear();
125     }
126
127     /**
128      * Checks if a type is supported by this Ric.
129      *
130      * @param typeName the name of the type to check if it is supported.
131      *
132      * @return true if the given type is supported by this Ric, false otherwise.
133      */
134     public synchronized boolean isSupportingType(String typeName) {
135         return supportedPolicyTypes.containsKey(typeName);
136     }
137
138     @Override
139     public synchronized String toString() {
140         return Ric.class.getSimpleName() + ": " + "name: " + name() + ", state: " + state + ", baseUrl: "
141             + ricConfig.baseUrl() + ", managedNodes: " + managedElementIds;
142     }
143
144     /**
145      * Represents the states possible for a Ric.
146      */
147     public enum RicState {
148         /**
149          * The agent view of the Ric may be inconsistent.
150          */
151         UNDEFINED,
152         /**
153          * The normal state. Policies can be configured.
154          */
155         IDLE,
156         /**
157          * The agent is synchronizing the view of the Ric.
158          */
159         SYNCHRONIZING
160     }
161 }