Fixed concurrency problems
[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 java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
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 List<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.NOT_INITIATED}.
55      *
56      * @param ricConfig The {@link RicConfig} for this Ric.
57      */
58     public Ric(RicConfig ricConfig) {
59         this.ricConfig = ricConfig;
60         this.managedElementIds = new ArrayList<>(ricConfig.managedElementIds()); // TODO, this is config why is it
61                                                                                  // copied here?
62     }
63
64     public String name() {
65         return ricConfig.name();
66     }
67
68     public RicConfig getConfig() {
69         return this.ricConfig;
70     }
71
72     public synchronized RicState getState() {
73         return this.state;
74     }
75
76     public synchronized void setState(RicState state) {
77         this.state = state;
78     }
79
80     /**
81      * Gets the nodes managed by this Ric.
82      *
83      * @return a vector containing the nodes managed by this Ric.
84      */
85     public synchronized Collection<String> getManagedElementIds() {
86         return managedElementIds;
87     }
88
89     /**
90      * Determines if the given node is managed by this Ric.
91      *
92      * @param managedElementId the node name to check.
93      * @return true if the given node is managed by this Ric.
94      */
95     public synchronized boolean isManaging(String managedElementId) {
96         return managedElementIds.contains(managedElementId);
97     }
98
99     /**
100      * Adds the given node as managed by this Ric.
101      *
102      * @param managedElementId the node to add.
103      */
104     public synchronized void addManagedElement(String managedElementId) {
105         if (!managedElementIds.contains(managedElementId)) {
106             managedElementIds.add(managedElementId);
107         }
108     }
109
110     /**
111      * Removes the given node as managed by this Ric.
112      *
113      * @param managedElementId the node to remove.
114      */
115     public synchronized void removeManagedElement(String managedElementId) {
116         managedElementIds.remove(managedElementId);
117     }
118
119     /**
120      * Gets the policy types supported by this Ric.
121      *
122      * @return the policy types supported by this Ric in an unmodifiable list.
123      */
124     public synchronized Collection<PolicyType> getSupportedPolicyTypes() {
125         return new Vector<>(supportedPolicyTypes.values());
126     }
127
128     public synchronized Collection<String> getSupportedPolicyTypeNames() {
129         return new Vector<>(supportedPolicyTypes.keySet());
130     }
131
132     /**
133      * Adds a policy type as supported by this Ric.
134      *
135      * @param type the policy type to support.
136      */
137     public synchronized void addSupportedPolicyType(PolicyType type) {
138         supportedPolicyTypes.put(type.name(), type);
139     }
140
141     /**
142      * Removes all policy type as supported by this Ric.
143      */
144     public synchronized void clearSupportedPolicyTypes() {
145         supportedPolicyTypes.clear();
146     }
147
148     /**
149      * Checks if a type is supported by this Ric.
150      *
151      * @param typeName the name of the type to check if it is supported.
152      *
153      * @return true if the given type is supported by this Ric, false otherwise.
154      */
155     public synchronized boolean isSupportingType(String typeName) {
156         return supportedPolicyTypes.containsKey(typeName);
157     }
158
159     @Override
160     public synchronized String toString() {
161         return Ric.class.getSimpleName() + ": " + "name: " + name() + ", state: " + state + ", baseUrl: "
162             + ricConfig.baseUrl() + ", managedNodes: " + managedElementIds;
163     }
164
165     /**
166      * Represents the states possible for a Ric.
167      */
168     public enum RicState {
169         /**
170          * The agent view of the Ric may be inconsistent.
171          */
172         UNDEFINED,
173         /**
174          * The normal state. Policies can be configured.
175          */
176         IDLE,
177         /**
178          * The agent is synchronizing the view of the Ric.
179          */
180         SYNCHRONIZING
181     }
182 }