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