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