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