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