Remove code smells and increase code coverage
[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 import lombok.Getter;
30 import lombok.Setter;
31 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
32 import org.oransc.policyagent.configuration.RicConfig;
33
34 /**
35  * Represents the dynamic information about a NearRealtime-RIC.
36  */
37 public class Ric {
38     private final RicConfig ricConfig;
39     private final List<String> managedElementIds;
40
41     private RicState state = RicState.UNDEFINED;
42     private Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
43     @Getter
44     @Setter
45     private A1ProtocolType protocolVersion = A1ProtocolType.UNKNOWN;
46
47
48     /**
49      * Creates the Ric. Initial state is {@link RicState.NOT_INITIATED}.
50      *
51      * @param ricConfig The {@link RicConfig} for this Ric.
52      */
53     public Ric(RicConfig ricConfig) {
54         this.ricConfig = ricConfig;
55         this.managedElementIds = new ArrayList<>(ricConfig.managedElementIds());
56     }
57
58     public String name() {
59         return ricConfig.name();
60     }
61
62     public RicConfig getConfig() {
63         return this.ricConfig;
64     }
65
66     public synchronized RicState getState() {
67         return this.state;
68     }
69
70     public synchronized void setState(RicState state) {
71         this.state = state;
72     }
73
74     /**
75      * Gets the nodes managed by this Ric.
76      *
77      * @return a vector containing the nodes managed by this Ric.
78      */
79     public synchronized Collection<String> getManagedElementIds() {
80         return managedElementIds;
81     }
82
83     /**
84      * Determines if the given node is managed by this Ric.
85      *
86      * @param managedElementId the node name to check.
87      * @return true if the given node is managed by this Ric.
88      */
89     public synchronized boolean isManaging(String managedElementId) {
90         return managedElementIds.contains(managedElementId);
91     }
92
93     /**
94      * Adds the given node as managed by this Ric.
95      *
96      * @param managedElementId the node to add.
97      */
98     public synchronized void addManagedElement(String managedElementId) {
99         if (!managedElementIds.contains(managedElementId)) {
100             managedElementIds.add(managedElementId);
101         }
102     }
103
104     /**
105      * Removes the given node as managed by this Ric.
106      *
107      * @param managedElementId the node to remove.
108      */
109     public synchronized void removeManagedElement(String managedElementId) {
110         managedElementIds.remove(managedElementId);
111     }
112
113     /**
114      * Gets the policy types supported by this Ric.
115      *
116      * @return the policy types supported by this Ric in an unmodifiable list.
117      */
118     public synchronized Collection<PolicyType> getSupportedPolicyTypes() {
119         return new Vector<>(supportedPolicyTypes.values());
120     }
121
122     public synchronized Collection<String> getSupportedPolicyTypeNames() {
123         return new Vector<>(supportedPolicyTypes.keySet());
124     }
125
126     /**
127      * Adds a policy type as supported by this Ric.
128      *
129      * @param type the policy type to support.
130      */
131     public synchronized void addSupportedPolicyType(PolicyType type) {
132         supportedPolicyTypes.put(type.name(), type);
133     }
134
135     /**
136      * Removes all policy type as supported by this Ric.
137      */
138     public synchronized void clearSupportedPolicyTypes() {
139         supportedPolicyTypes.clear();
140     }
141
142     /**
143      * Checks if a type is supported by this Ric.
144      *
145      * @param typeName the name of the type to check if it is supported.
146      *
147      * @return true if the given type is supported by this Ric, false otherwise.
148      */
149     public synchronized boolean isSupportingType(String typeName) {
150         return supportedPolicyTypes.containsKey(typeName);
151     }
152
153     @Override
154     public synchronized String toString() {
155         return Ric.class.getSimpleName() + ": " + "name: " + name() + ", state: " + state + ", baseUrl: "
156             + ricConfig.baseUrl() + ", managedNodes: " + managedElementIds;
157     }
158
159     /**
160      * Represents the states possible for a Ric.
161      */
162     public enum RicState {
163         /**
164          * The agent view of the Ric may be inconsistent.
165          */
166         UNDEFINED,
167         /**
168          * The normal state. Policies can be configured.
169          */
170         IDLE,
171         /**
172          * The agent is synchronizing the view of the Ric.
173          */
174         SYNCHRONIZING
175     }
176 }