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 static org.onap.policy.std.utils.PolicyCommonConfigConstants.CONFIG_NAME;
24 import static org.onap.policy.std.utils.PolicyCommonConfigConstants.ONAP_NAME;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.HashSet;
30 import org.onap.policy.api.LoadedPolicy;
31 import org.onap.policy.api.NotificationType;
32 import org.onap.policy.api.PDPNotification;
33 import org.onap.policy.api.RemovedPolicy;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
37 public class MatchStore {
39 private static final Set<Matches> MATCH_STORE = new HashSet<>();
41 private static final Logger LOGGER = FlexLogger.getLogger(MatchStore.class.getName());
43 private MatchStore() {
47 public static Set<Matches> getMatchStore() {
51 public static void storeMatch(final Matches newMatch) {
52 if (newMatch != null && !MATCH_STORE.contains(newMatch)) {
53 MATCH_STORE.add(newMatch);
57 // Logic changes for Requested Policies notifications..
58 public static PDPNotification checkMatch(final PDPNotification oldNotification) {
59 if (oldNotification == null) {
62 if (MATCH_STORE.isEmpty()) {
63 LOGGER.debug("No Success Config Calls made yet.. ");
66 return getPDPNotification(oldNotification);
69 private static PDPNotification getPDPNotification(final PDPNotification oldNotification) {
70 boolean removed = false;
71 boolean updated = false;
72 final StdPDPNotification newNotification = new StdPDPNotification();
73 if (isValid(oldNotification.getRemovedPolicies())) {
74 // send all removed policies to client.
75 newNotification.setRemovedPolicies(getStdRemovedPolicies(oldNotification.getRemovedPolicies()));
78 if (isValid(oldNotification.getLoadedPolicies())) {
79 final Collection<StdLoadedPolicy> updatedPolicies = new HashSet<>();
80 for (final LoadedPolicy updatedPolicy : oldNotification.getLoadedPolicies()) {
81 updated = updateStdLoadedPolicy(updated, updatedPolicies, updatedPolicy);
83 newNotification.setLoadedPolicies(updatedPolicies);
85 // Need to set the type of Update..
86 if (removed && updated) {
87 newNotification.setNotificationType(NotificationType.BOTH);
89 newNotification.setNotificationType(NotificationType.REMOVE);
91 newNotification.setNotificationType(NotificationType.UPDATE);
93 return newNotification;
96 private static boolean updateStdLoadedPolicy(boolean updated, final Collection<StdLoadedPolicy> updatedPolicies,
97 final LoadedPolicy updatedPolicy) {
98 // if it is config policies check their matches
99 if (isValid(updatedPolicy.getMatches())) {
100 final Map<String, String> matchesMap = updatedPolicy.getMatches();
101 final Matches policyMatches = getMatches(matchesMap);
102 for (final Matches match : MATCH_STORE) {
103 if (match.equals(policyMatches)) {
104 final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy();
105 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
106 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
107 newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
108 updatedPolicies.add(newUpdatedPolicy);
116 // send all non config notifications to client.
117 final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy();
118 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
119 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
120 updatedPolicies.add(newUpdatedPolicy);
126 private static Matches getMatches(final Map<String, String> attributes) {
127 final Matches matches = new Matches();
128 matches.setOnapName(attributes.get(ONAP_NAME));
129 matches.setConfigName(attributes.get(CONFIG_NAME));
131 final Map<String, String> configAttributes = new HashMap<>(attributes);
132 // remove onap and config to config-attributes.
133 configAttributes.remove(ONAP_NAME);
134 configAttributes.remove(CONFIG_NAME);
136 matches.setConfigAttributes(configAttributes);
141 private static boolean isValid(final Map<String, String> map) {
142 return map != null && !map.isEmpty();
145 private static Collection<StdRemovedPolicy> getStdRemovedPolicies(final Collection<RemovedPolicy> policies) {
146 final Set<StdRemovedPolicy> removedPolicies = new HashSet<>();
147 for (final RemovedPolicy removedPolicy : policies) {
148 final StdRemovedPolicy newRemovedPolicy = new StdRemovedPolicy();
149 newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName());
150 newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo());
151 removedPolicies.add(newRemovedPolicy);
153 return removedPolicies;
156 private static boolean isValid(final Collection<?> collection) {
157 return collection != null && !collection.isEmpty();