Merge "Change docker image name"
[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.configuration.RicConfig;
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 Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
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     public RicConfig getConfig() {
60         return this.ricConfig;
61     }
62
63     /**
64      * Gets the nodes managed by this Ric.
65      *
66      * @return a vector containing the nodes managed by this Ric.
67      */
68     public Vector<String> getManagedElementIds() {
69         return ricConfig.managedElementIds();
70     }
71
72     /**
73      * Determines if the given node is managed by this Ric.
74      *
75      * @param managedElementId the node name to check.
76      * @return true if the given node is managed by this Ric.
77      */
78     public boolean isManaging(String managedElementId) {
79         return ricConfig.managedElementIds().contains(managedElementId);
80     }
81
82     /**
83      * Adds the given node as managed by this Ric.
84      *
85      * @param managedElementId the node to add.
86      */
87     public void addManagedElement(String managedElementId) {
88         if (!ricConfig.managedElementIds().contains(managedElementId)) {
89             ricConfig.managedElementIds().add(managedElementId);
90         }
91     }
92
93     /**
94      * Removes the given node as managed by this Ric.
95      *
96      * @param managedElementId the node to remove.
97      */
98     public void removeManagedElement(String managedElementId) {
99         ricConfig.managedElementIds().remove(managedElementId);
100     }
101
102     /**
103      * Gets the policy types supported by this Ric.
104      *
105      * @return the policy types supported by this Ric in an unmodifiable list.
106      */
107     public Collection<PolicyType> getSupportedPolicyTypes() {
108         return supportedPolicyTypes.values();
109     }
110
111     public Collection<String> getSupportedPolicyTypeNames() {
112         return supportedPolicyTypes.keySet();
113     }
114
115     /**
116      * Adds a policy type as supported by this Ric.
117      *
118      * @param type the policy type to support.
119      */
120     public void addSupportedPolicyType(PolicyType type) {
121         supportedPolicyTypes.put(type.name(), type);
122     }
123
124     /**
125      * Removes all policy type as supported by this Ric.
126      */
127     public void clearSupportedPolicyTypes() {
128         supportedPolicyTypes.clear();
129     }
130
131     /**
132      * Checks if a type is supported by this Ric.
133      *
134      * @param typeName the name of the type to check if it is supported.
135      *
136      * @return true if the given type is supported by this Ric, false otherwise.
137      */
138     public boolean isSupportingType(String typeName) {
139         return supportedPolicyTypes.containsKey(typeName);
140     }
141
142     @Override
143     public String toString() {
144         return Ric.class.getSimpleName() + ": " + "name: " + name() + ", state: " + state + ", baseUrl: "
145             + ricConfig.baseUrl() + ", managedNodes: " + ricConfig.managedElementIds();
146     }
147
148     /**
149      * Represents the states possible for a Ric.
150      */
151     public static enum RicState {
152         /**
153          * The Ric has not been initiated yet.
154          */
155         NOT_INITIATED,
156         /**
157          * The Ric is working fine.
158          */
159         ACTIVE,
160         /**
161          * The Ric cannot be contacted.
162          */
163         NOT_REACHABLE, RECOVERING
164     }
165 }