2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.repository;
23 import java.util.Collection;
24 import java.util.HashMap;
27 import java.util.Vector;
29 import org.oransc.policyagent.exceptions.ServiceException;
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<>();
40 public synchronized void put(Policy policy) {
41 policiesId.put(policy.id(), policy);
42 multiMapPut(policiesRic, policy.ric().name(), policy);
43 multiMapPut(policiesService, policy.ownerServiceName(), policy);
44 multiMapPut(policiesType, policy.type().name(), policy);
47 private void multiMapPut(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
48 Map<String, Policy> map = multiMap.get(key);
50 map = new HashMap<>();
51 multiMap.put(key, map);
53 map.put(value.id(), value);
56 private void multiMapRemove(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
57 Map<String, Policy> map = multiMap.get(key);
59 map.remove(value.id());
66 private Collection<Policy> multiMapGet(Map<String, Map<String, Policy>> multiMap, String key) {
67 Map<String, Policy> map = multiMap.get(key);
69 return new Vector<Policy>();
74 public synchronized boolean containsPolicy(String id) {
75 return policiesId.containsKey(id);
78 public synchronized Policy getPolicy(String id) throws ServiceException {
79 Policy p = policiesId.get(id);
81 throw new ServiceException("Could not find policy: " + id);
86 public synchronized Collection<Policy> getAll() {
87 return policiesId.values();
90 public synchronized Collection<Policy> getForService(String service) {
91 return multiMapGet(policiesService, service);
94 public synchronized Collection<Policy> getForRic(String ric) {
95 return multiMapGet(policiesRic, ric);
98 public synchronized Collection<Policy> getForType(String type) {
99 return multiMapGet(policiesType, type);
102 public synchronized Policy removeId(String id) {
103 Policy p = policiesId.get(id);
110 public synchronized void remove(Policy policy) {
111 policiesId.remove(policy.id());
112 multiMapRemove(policiesRic, policy.ric().name(), policy);
113 multiMapRemove(policiesService, policy.ownerServiceName(), policy);
114 multiMapRemove(policiesType, policy.type().name(), policy);
117 public synchronized int size() {
118 return policiesId.size();
121 public synchronized void clear() {
122 while (policiesId.size() > 0) {
123 Set<String> keys = policiesId.keySet();
124 removeId(keys.iterator().next());