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