Merge "Add Error Handling in A1 Client"
[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.Collections;
24 import java.util.List;
25 import java.util.Vector;
26
27 import org.oransc.policyagent.configuration.RicConfig;
28 import org.oransc.policyagent.repository.Ric.RicState;
29
30 /**
31  * Represents the dynamic information about a NearRealtime-RIC.
32  */
33 public class Ric {
34     private final RicConfig ricConfig;
35     private RicState state = RicState.NOT_INITIATED;
36     private Vector<PolicyType> supportedPolicyTypes = new Vector<>();
37
38     /**
39      * Creates the Ric. Initial state is {@link RicState.NOT_INITIATED}.
40      *
41      * @param ricConfig The {@link RicConfig} for this Ric.
42      */
43     public Ric(RicConfig ricConfig) {
44         this.ricConfig = ricConfig;
45     }
46
47     public String name() {
48         return ricConfig.name();
49     }
50
51     public RicState state() {
52         return state;
53     }
54
55     public void setState(RicState newState) {
56         state = newState;
57     }
58
59     /**
60      * Gets the nodes managed by this Ric.
61      *
62      * @return a vector containing the nodes managed by this Ric.
63      */
64     public Vector<String> getManagedNodes() {
65         return ricConfig.managedElementIds();
66     }
67
68     /**
69      * Determines if the given node is managed by this Ric.
70      *
71      * @param nodeName the node name to check.
72      * @return true if the given node is managed by this Ric.
73      */
74     public boolean isManaging(String nodeName) {
75         return ricConfig.managedElementIds().contains(nodeName);
76     }
77
78     /**
79      * Adds the given node as managed by this Ric.
80      *
81      * @param nodeName the node to add.
82      */
83     public void addManagedNode(String nodeName) {
84         if (!ricConfig.managedElementIds().contains(nodeName)) {
85             ricConfig.managedElementIds().add(nodeName);
86         }
87     }
88
89     /**
90      * Removes the given node as managed by this Ric.
91      *
92      * @param nodeName the node to remove.
93      */
94     public void removeManagedNode(String nodeName) {
95         ricConfig.managedElementIds().remove(nodeName);
96     }
97
98     /**
99      * Gets the policy types supported by this Ric.
100      *
101      * @return the policy types supported by this Ric in an unmodifiable list.
102      */
103     public List<PolicyType> getSupportedPolicyTypes() {
104         return Collections.unmodifiableList(supportedPolicyTypes);
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 void addSupportedPolicyType(PolicyType type) {
113         if (!supportedPolicyTypes.contains(type)) {
114             supportedPolicyTypes.add(type);
115         }
116     }
117
118     /**
119      * Adds policy types as supported by this Ric.
120      *
121      * @param types the policy types to support.
122      */
123     public void addSupportedPolicyTypes(Vector<PolicyType> types) {
124         for (PolicyType type : types) {
125             addSupportedPolicyType(type);
126         }
127     }
128
129     /**
130      * Removes a policy type as supported by this Ric.
131      *
132      * @param type the policy type to remove as supported by this Ric.
133      */
134     public void removeSupportedPolicyType(PolicyType type) {
135         supportedPolicyTypes.remove(type);
136     }
137
138     /**
139      * Checks if a type is supported by this Ric.
140      *
141      * @param type the type to check if it is supported.
142      *
143      * @return true if the given type issupported by this Ric, false otherwise.
144      */
145     public boolean isSupportingType(PolicyType type) {
146         return supportedPolicyTypes.contains(type);
147     }
148
149     /**
150      * Represents the states possible for a Ric.
151      */
152     public static enum RicState {
153         /**
154          * The Ric has not been initiated yet.
155          */
156         NOT_INITIATED,
157         /**
158          * The Ric is working fine.
159          */
160         ACTIVE,
161         /**
162          * Something is wrong with the Ric.
163          */
164         FAULTY,
165         /**
166          * The node is unreachable at the moment.
167          */
168         UNREACHABLE
169     }
170 }