2971feb2b0f0ee448aa38d992ac1ba11edf3928b
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.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.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client.A1ProtocolType;
32 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Holds information about a Near-RT RIC.
38  */
39 public class Ric {
40     private static final Logger logger = LoggerFactory.getLogger(Ric.class);
41
42     @Setter
43     private RicConfig ricConfig;
44     private RicState state = RicState.UNAVAILABLE;
45     private Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
46
47     @Setter
48     private A1ProtocolType protocolVersion = A1ProtocolType.UNKNOWN;
49
50     @Getter
51     private final Lock lock;
52
53     /**
54      * Creates the Ric. Initial state is {@link RicState.UNDEFINED}.
55      *
56      * @param ricConfig The {@link RicConfig} for this Ric.
57      */
58     public Ric(RicConfig ricConfig) {
59         this.ricConfig = ricConfig;
60         this.lock = new Lock(ricConfig.getRicId());
61     }
62
63     public String id() {
64         return ricConfig.getRicId();
65     }
66
67     public RicConfig getConfig() {
68         return this.ricConfig;
69     }
70
71     public synchronized RicState getState() {
72         return this.state;
73     }
74
75     public synchronized void setState(RicState state) {
76         logger.debug("Ric {} state set to {}", getConfig().getRicId(), state);
77         this.state = state;
78     }
79
80     public synchronized A1ProtocolType getProtocolVersion() {
81         if (this.ricConfig.getCustomAdapterClass().isEmpty()) {
82             return this.protocolVersion;
83         } else {
84             return A1ProtocolType.CUSTOM_PROTOCOL;
85         }
86     }
87
88     /**
89      * Gets the nodes managed by this Ric.
90      *
91      * @return a vector containing the nodes managed by this Ric.
92      */
93     public synchronized Collection<String> getManagedElementIds() {
94         return new Vector<>(ricConfig.getManagedElementIds());
95     }
96
97     /**
98      * Determines if the given node is managed by this Ric.
99      *
100      * @param managedElementId the node name to check.
101      * @return true if the given node is managed by this Ric.
102      */
103     public synchronized boolean isManaging(String managedElementId) {
104         return ricConfig.getManagedElementIds().contains(managedElementId);
105     }
106
107     /**
108      * Gets the policy types supported by this Ric.
109      *
110      * @return the policy types supported by this Ric in an unmodifiable list.
111      */
112     public synchronized Collection<PolicyType> getSupportedPolicyTypes() {
113         return new Vector<>(supportedPolicyTypes.values());
114     }
115
116     public synchronized Collection<String> getSupportedPolicyTypeNames() {
117         return new Vector<>(supportedPolicyTypes.keySet());
118     }
119
120     /**
121      * Adds a policy type as supported by this Ric.
122      *
123      * @param type the policy type to support.
124      */
125     public synchronized void addSupportedPolicyType(PolicyType type) {
126         supportedPolicyTypes.put(type.getId(), type);
127     }
128
129     /**
130      * Removes all policy type as supported by this Ric.
131      */
132     public synchronized void clearSupportedPolicyTypes() {
133         supportedPolicyTypes.clear();
134     }
135
136     /**
137      * Checks if a type is supported by this Ric.
138      *
139      * @param typeId the identity of the type to check if it is supported.
140      *
141      * @return true if the given type is supported by this Ric, false otherwise.
142      */
143     public synchronized boolean isSupportingType(String typeId) {
144         return supportedPolicyTypes.containsKey(typeId);
145     }
146
147     @Override
148     public synchronized String toString() {
149         return Ric.class.getSimpleName() + ": " + "name: " + id() + ", state: " + state + ", baseUrl: "
150                 + ricConfig.getBaseUrl() + ", managedNodes: " + ricConfig.getManagedElementIds();
151     }
152
153     /**
154      * Represents the states possible for a Ric.
155      */
156     public enum RicState {
157         /**
158          * The Policy Management Service's view of the Near-RT RIC may be inconsistent.
159          */
160         UNAVAILABLE,
161         /**
162          * The normal state. Policies can be configured.
163          */
164         AVAILABLE,
165         /**
166          * The Policy Management Service is synchronizing the view of the Near-RT RIC.
167          */
168         SYNCHRONIZING,
169
170         /**
171          * A consistency check between the Policy Management Service and the Near-RT RIC
172          * is done
173          */
174         CONSISTENCY_CHECK
175     }
176 }