2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
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.onap.policy.std;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
29 import org.onap.policy.api.LoadedPolicy;
30 import org.onap.policy.api.NotificationType;
31 import org.onap.policy.api.PDPNotification;
32 import org.onap.policy.api.RemovedPolicy;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
36 public class MatchStore {
37 private static final String CONFIG_NAME = "ConfigName";
39 private static final String ONAP_NAME = "ONAPName";
41 private static final Set<Matches> MATCH_STORE = new HashSet<>();
43 private static final Logger LOGGER = FlexLogger.getLogger(MatchStore.class.getName());
45 private MatchStore() {
49 public static Set<Matches> getMatchStore() {
53 public static void storeMatch(final Matches newMatch) {
54 if (newMatch != null && !MATCH_STORE.contains(newMatch)) {
55 MATCH_STORE.add(newMatch);
59 // Logic changes for Requested Policies notifications..
60 public static PDPNotification checkMatch(final PDPNotification oldNotification) {
61 if (oldNotification == null) {
64 if (MATCH_STORE.isEmpty()) {
65 LOGGER.debug("No Success Config Calls made yet.. ");
68 return getPDPNotification(oldNotification);
71 private static PDPNotification getPDPNotification(final PDPNotification oldNotification) {
72 boolean removed = false;
73 boolean updated = false;
74 final StdPDPNotification newNotification = new StdPDPNotification();
75 if (isValid(oldNotification.getRemovedPolicies())) {
76 // send all removed policies to client.
77 newNotification.setRemovedPolicies(getStdRemovedPolicies(oldNotification.getRemovedPolicies()));
80 if (isValid(oldNotification.getLoadedPolicies())) {
81 final Collection<StdLoadedPolicy> updatedPolicies = new HashSet<>();
82 for (final LoadedPolicy updatedPolicy : oldNotification.getLoadedPolicies()) {
83 updated = updateStdLoadedPolicy(updated, updatedPolicies, updatedPolicy);
85 newNotification.setLoadedPolicies(updatedPolicies);
87 // Need to set the type of Update..
88 if (removed && updated) {
89 newNotification.setNotificationType(NotificationType.BOTH);
91 newNotification.setNotificationType(NotificationType.REMOVE);
93 newNotification.setNotificationType(NotificationType.UPDATE);
95 return newNotification;
98 private static boolean updateStdLoadedPolicy(boolean updated, final Collection<StdLoadedPolicy> updatedPolicies,
99 final LoadedPolicy updatedPolicy) {
100 // if it is config policies check their matches
101 if (isValid(updatedPolicy.getMatches())) {
102 final Map<String, String> matchesMap = updatedPolicy.getMatches();
103 final Matches policyMatches = getMatches(matchesMap);
104 for (final Matches match : MATCH_STORE) {
105 if (match.equals(policyMatches)) {
106 final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy();
107 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
108 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
109 newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
110 updatedPolicies.add(newUpdatedPolicy);
118 // send all non config notifications to client.
119 final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy();
120 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
121 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
122 updatedPolicies.add(newUpdatedPolicy);
128 private static Matches getMatches(final Map<String, String> attributes) {
129 final Matches matches = new Matches();
130 matches.setOnapName(attributes.get(ONAP_NAME));
131 matches.setConfigName(attributes.get(CONFIG_NAME));
133 final Map<String, String> configAttributes = new HashMap<>(attributes);
134 // remove onap and config to config-attributes.
135 configAttributes.remove(ONAP_NAME);
136 configAttributes.remove(CONFIG_NAME);
138 matches.setConfigAttributes(configAttributes);
143 private static boolean isValid(final Map<String, String> map) {
144 return map != null && !map.isEmpty();
147 private static Collection<StdRemovedPolicy> getStdRemovedPolicies(final Collection<RemovedPolicy> policies) {
148 final Set<StdRemovedPolicy> removedPolicies = new HashSet<>();
149 for (final RemovedPolicy removedPolicy : policies) {
150 final StdRemovedPolicy newRemovedPolicy = new StdRemovedPolicy();
151 newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName());
152 newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo());
153 removedPolicies.add(newRemovedPolicy);
155 return removedPolicies;
158 private static boolean isValid(final Collection<?> collection) {
159 return collection != null && !collection.isEmpty();