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