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