Dashboard using policy agent NBI
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / repository / Policies.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.exceptions.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class Policies {
33     private static final Logger logger = LoggerFactory.getLogger(Policies.class);
34
35     private Map<String, Policy> policiesId = new HashMap<>();
36     private Map<String, Map<String, Policy>> policiesRic = new HashMap<>();
37     private Map<String, Map<String, Policy>> policiesService = new HashMap<>();
38     private Map<String, Map<String, Policy>> policiesType = new HashMap<>();
39
40     public Policies() {
41     }
42
43     public synchronized void put(Policy policy) {
44         policiesId.put(policy.id(), policy);
45         multiMapPut(policiesRic, policy.ric().name(), policy);
46         multiMapPut(policiesService, policy.ownerServiceName(), policy);
47         multiMapPut(policiesType, policy.type().name(), policy);
48     }
49
50     private void multiMapPut(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
51         Map<String, Policy> map = multiMap.get(key);
52         if (map == null) {
53             map = new HashMap<>();
54             multiMap.put(key, map);
55         }
56         map.put(value.id(), value);
57     }
58
59     private void multiMapRemove(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
60         Map<String, Policy> map = multiMap.get(key);
61         if (map != null) {
62             map.remove(value.id());
63             if (map.isEmpty()) {
64                 multiMap.remove(key);
65             }
66         }
67     }
68
69     private Collection<Policy> multiMapGet(Map<String, Map<String, Policy>> multiMap, String key) {
70         Map<String, Policy> map = multiMap.get(key);
71         if (map == null) {
72             return new Vector<Policy>();
73         }
74         return map.values();
75     }
76
77     public synchronized Policy get(String id) throws ServiceException {
78         Policy p = policiesId.get(id);
79         if (p == null) {
80             throw new ServiceException("Could not find policy: " + id);
81         }
82         return p;
83     }
84
85     public synchronized Collection<Policy> getAll() {
86         return policiesId.values();
87     }
88
89     public synchronized Collection<Policy> getForService(String service) {
90         return multiMapGet(policiesService, service);
91     }
92
93     public synchronized Collection<Policy> getForRic(String ric) {
94         return multiMapGet(policiesRic, ric);
95     }
96
97     public synchronized Collection<Policy> getForType(String type) {
98         return multiMapGet(policiesType, type);
99     }
100
101     public synchronized Policy removeId(String id) {
102         Policy p = policiesId.get(id);
103         if (p != null) {
104             remove(p);
105         }
106         return p;
107     }
108
109     public synchronized void remove(Policy policy) {
110         policiesId.remove(policy.id());
111         multiMapRemove(policiesRic, policy.ric().name(), policy);
112         multiMapRemove(policiesService, policy.ownerServiceName(), policy);
113         multiMapRemove(policiesType, policy.type().name(), policy);
114     }
115
116     public synchronized int size() {
117         return policiesId.size();
118     }
119
120     public void clear() {
121         for (String id : policiesId.keySet()) {
122             removeId(id);
123         }
124     }
125
126 }