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