Refactor code for nested stmts in policy std
[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 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;
28 import java.util.Map;
29 import java.util.Set;
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;
36
37 public class MatchStore {
38
39     private static final Set<Matches> MATCH_STORE = new HashSet<>();
40
41     private static final Logger LOGGER = FlexLogger.getLogger(MatchStore.class.getName());
42
43     private MatchStore() {
44         // Empty Constructor
45     }
46
47     public static Set<Matches> getMatchStore() {
48         return MATCH_STORE;
49     }
50
51     public static void storeMatch(final Matches newMatch) {
52         if (newMatch != null && !MATCH_STORE.contains(newMatch)) {
53             MATCH_STORE.add(newMatch);
54         }
55     }
56
57     // Logic changes for Requested Policies notifications..
58     public static PDPNotification checkMatch(final PDPNotification oldNotification) {
59         if (oldNotification == null) {
60             return null;
61         }
62         if (MATCH_STORE.isEmpty()) {
63             LOGGER.debug("No Success Config Calls made yet.. ");
64             return null;
65         }
66         return getPDPNotification(oldNotification);
67     }
68
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()));
76             removed = true;
77         }
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);
82             }
83             newNotification.setLoadedPolicies(updatedPolicies);
84         }
85         // Need to set the type of Update..
86         if (removed && updated) {
87             newNotification.setNotificationType(NotificationType.BOTH);
88         } else if (removed) {
89             newNotification.setNotificationType(NotificationType.REMOVE);
90         } else if (updated) {
91             newNotification.setNotificationType(NotificationType.UPDATE);
92         }
93         return newNotification;
94     }
95
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);
109                     updated = true;
110                 } else {
111                     break;
112                 }
113             }
114
115         } else {
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);
121             updated = true;
122         }
123         return updated;
124     }
125
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));
130
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);
135
136         matches.setConfigAttributes(configAttributes);
137
138         return matches;
139     }
140
141     private static boolean isValid(final Map<String, String> map) {
142         return map != null && !map.isEmpty();
143     }
144
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);
152         }
153         return removedPolicies;
154     }
155
156     private static boolean isValid(final Collection<?> collection) {
157         return collection != null && !collection.isEmpty();
158     }
159
160 }