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.Collections;
25 import java.util.HashMap;
28 import java.util.Vector;
30 import org.oransc.policyagent.exceptions.ServiceException;
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<>();
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);
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);
49 private void multiMapRemove(Map<String, Map<String, Policy>> multiMap, String key, Policy value) {
50 Map<String, Policy> map = multiMap.get(key);
52 map.remove(value.id());
59 private Collection<Policy> multiMapGet(Map<String, Map<String, Policy>> multiMap, String key) {
60 Map<String, Policy> map = multiMap.get(key);
62 return Collections.emptyList();
64 return new Vector<>(map.values());
67 public synchronized boolean containsPolicy(String id) {
68 return policiesId.containsKey(id);
71 public synchronized Policy get(String id) {
72 return policiesId.get(id);
75 public synchronized Policy getPolicy(String id) throws ServiceException {
76 Policy p = policiesId.get(id);
78 throw new ServiceException("Could not find policy: " + id);
83 public synchronized Collection<Policy> getAll() {
84 return new Vector<>(policiesId.values());
87 public synchronized Collection<Policy> getForService(String service) {
88 return multiMapGet(policiesService, service);
91 public synchronized Collection<Policy> getForRic(String ric) {
92 return multiMapGet(policiesRic, ric);
95 public synchronized Collection<Policy> getForType(String type) {
96 return multiMapGet(policiesType, type);
99 public synchronized Policy removeId(String id) {
100 Policy p = policiesId.get(id);
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);
114 public synchronized void removePoliciesForRic(String ricName) {
115 Collection<Policy> policiesForRic = getForRic(ricName);
116 for (Policy policy : policiesForRic) {
121 public synchronized int size() {
122 return policiesId.size();
125 public synchronized void clear() {
126 while (policiesId.size() > 0) {
127 Set<String> keys = policiesId.keySet();
128 removeId(keys.iterator().next());