JUnit test for policy/engine PolicyEngineAPI
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / MatchStore.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
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.onap.policy.std;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
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;
35
36 public class MatchStore {
37     private static final String CONFIG_NAME = "ConfigName";
38
39     private static final String ONAP_NAME = "ONAPName";
40
41     private static final Set<Matches> MATCH_STORE = new HashSet<>();
42
43     private static final Logger LOGGER = FlexLogger.getLogger(MatchStore.class.getName());
44
45     private MatchStore() {
46         // Empty Constructor
47     }
48
49     public static Set<Matches> getMatchStore() {
50         return MATCH_STORE;
51     }
52
53     public static void storeMatch(final Matches newMatch) {
54         if (newMatch != null && !MATCH_STORE.contains(newMatch)) {
55             MATCH_STORE.add(newMatch);
56         }
57     }
58
59     // Logic changes for Requested Policies notifications..
60     public static PDPNotification checkMatch(final PDPNotification oldNotification) {
61         if (oldNotification == null) {
62             return null;
63         }
64         if (MATCH_STORE.isEmpty()) {
65             LOGGER.debug("No Success Config Calls made yet.. ");
66             return null;
67         }
68         return getPDPNotification(oldNotification);
69     }
70
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()));
78             removed = true;
79         }
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);
84             }
85             newNotification.setLoadedPolicies(updatedPolicies);
86         }
87         // Need to set the type of Update..
88         if (removed && updated) {
89             newNotification.setNotificationType(NotificationType.BOTH);
90         } else if (removed) {
91             newNotification.setNotificationType(NotificationType.REMOVE);
92         } else if (updated) {
93             newNotification.setNotificationType(NotificationType.UPDATE);
94         }
95         return newNotification;
96     }
97
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);
111                     updated = true;
112                 } else {
113                     break;
114                 }
115             }
116
117         } else {
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);
123             updated = true;
124         }
125         return updated;
126     }
127
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));
132
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);
137
138         matches.setConfigAttributes(configAttributes);
139
140         return matches;
141     }
142
143     private static boolean isValid(final Map<String, String> map) {
144         return map != null && !map.isEmpty();
145     }
146
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);
154         }
155         return removedPolicies;
156     }
157
158     private static boolean isValid(final Collection<?> collection) {
159         return collection != null && !collection.isEmpty();
160     }
161
162 }