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