Remove policies from repository when Ric is removed from configuration
[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.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.oransc.policyagent.exceptions.ServiceException;
30
31 public class Policies {
32     private Map<String, Policy> policiesId = new HashMap<>();
33     private Map<String, Map<String, Policy>> policiesRic = new HashMap<>();
34     private Map<String, Map<String, Policy>> policiesService = new HashMap<>();
35     private Map<String, Map<String, Policy>> policiesType = new HashMap<>();
36
37     public synchronized void put(Policy policy) {
38         policiesId.put(policy.id(), policy);
39         multiMapPut(policiesRic, policy.ric().name(), policy);
40         multiMapPut(policiesService, policy.ownerServiceName(), policy);
41         multiMapPut(policiesType, policy.type().name(), policy);
42     }
43
44     private void multiMapPut(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
45         multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value);
46     }
47
48     private void multiMapRemove(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
49         Map<String, Policy> map = multiMap.get(key);
50         if (map != null) {
51             map.remove(value.id());
52             if (map.isEmpty()) {
53                 multiMap.remove(key);
54             }
55         }
56     }
57
58     private Collection<Policy> multiMapGet(Map<String, Map<String, Policy>> multiMap, String key) {
59         Map<String, Policy> map = multiMap.get(key);
60         if (map == null) {
61             return Collections.emptyList();
62         }
63         return Collections.unmodifiableCollection(map.values());
64     }
65
66     public synchronized boolean containsPolicy(String id) {
67         return policiesId.containsKey(id);
68     }
69
70     public synchronized Policy get(String id) {
71         return policiesId.get(id);
72     }
73
74     public synchronized Policy getPolicy(String id) throws ServiceException {
75         Policy p = policiesId.get(id);
76         if (p == null) {
77             throw new ServiceException("Could not find policy: " + id);
78         }
79         return p;
80     }
81
82     public synchronized Collection<Policy> getAll() {
83         return Collections.unmodifiableCollection(policiesId.values());
84     }
85
86     public synchronized Collection<Policy> getForService(String service) {
87         return multiMapGet(policiesService, service);
88     }
89
90     public synchronized Collection<Policy> getForRic(String ric) {
91         return multiMapGet(policiesRic, ric);
92     }
93
94     public synchronized Collection<Policy> getForType(String type) {
95         return multiMapGet(policiesType, type);
96     }
97
98     public synchronized Policy removeId(String id) {
99         Policy p = policiesId.get(id);
100         if (p != null) {
101             remove(p);
102         }
103         return p;
104     }
105
106     public synchronized void remove(Policy policy) {
107         policiesId.remove(policy.id());
108         multiMapRemove(policiesRic, policy.ric().name(), policy);
109         multiMapRemove(policiesService, policy.ownerServiceName(), policy);
110         multiMapRemove(policiesType, policy.type().name(), policy);
111     }
112
113     public synchronized void removePoliciesForRic(String ricName) {
114         Collection<Policy> policiesForRic = getForRic(ricName);
115         for (Policy policy : policiesForRic) {
116             remove(policy);
117         }
118     }
119
120     public synchronized int size() {
121         return policiesId.size();
122     }
123
124     public synchronized void clear() {
125         while (policiesId.size() > 0) {
126             Set<String> keys = policiesId.keySet();
127             removeId(keys.iterator().next());
128         }
129     }
130 }