b6ec7491bc089bc4ece3f393ab5e08446c4e1e6f
[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.Optional;
27 import java.util.Vector;
28
29 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
30 import org.springframework.lang.Nullable;
31
32 /**
33  * Dynamic representation of all Rics in the system.
34  */
35 public class Rics {
36     Map<String, Ric> registeredRics = new HashMap<>();
37
38     public synchronized void put(Ric ric) {
39         registeredRics.put(ric.id(), ric);
40     }
41
42     public synchronized Collection<Ric> getRics() {
43         return new Vector<>(registeredRics.values());
44     }
45
46     public synchronized Ric getRic(String ricId) throws ServiceException {
47         Ric ric = registeredRics.get(ricId);
48         if (ric == null) {
49             throw new ServiceException("Could not find ric: " + ricId);
50         }
51         return ric;
52     }
53
54     public synchronized Ric get(String ricId) {
55         return registeredRics.get(ricId);
56     }
57
58     public synchronized @Nullable Ric remove(String ricId) {
59         return registeredRics.remove(ricId);
60     }
61
62     public synchronized int size() {
63         return registeredRics.size();
64     }
65
66     public synchronized void clear() {
67         this.registeredRics.clear();
68     }
69
70     public synchronized Optional<Ric> lookupRicForManagedElement(String managedElementId) {
71         for (Ric ric : this.registeredRics.values()) {
72             if (ric.getManagedElementIds().contains(managedElementId)) {
73                 return Optional.of(ric);
74             }
75         }
76         return Optional.empty();
77     }
78 }