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.HashSet;
27 import org.onap.policy.api.LoadedPolicy;
28 import org.onap.policy.api.NotificationType;
29 import org.onap.policy.api.PDPNotification;
30 import org.onap.policy.api.RemovedPolicy;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
34 public class MatchStore {
35 private static HashSet<Matches> matchStore = new HashSet<>();
36 private static Logger logger = FlexLogger.getLogger(MatchStore.class.getName());
38 private MatchStore() {
42 public static Set<Matches> getMatchStore() {
46 public static void storeMatch(Matches newMatch){
49 if(matchStore.isEmpty()){
50 matchStore.add(newMatch);
52 // Check if it is a new Match
53 Boolean match = false;
54 for(Matches oldMatch: matchStore){
56 if(oldMatch.getOnapName().equals(newMatch.getOnapName())){
57 // Compare ConfigName if it exists.
58 if(newMatch.getConfigName()!=null && oldMatch.getConfigName()!=null){
59 if(oldMatch.getConfigName().equals(newMatch.getConfigName())){
60 // Compare the Config Attributes if they exist.
61 if(newMatch.getConfigAttributes()!= null && oldMatch.getConfigAttributes()!=null) {
62 //Simple thing would be comparing their size.
63 if(newMatch.getConfigAttributes().size()==oldMatch.getConfigAttributes().size()){
64 // Now need to compare each of them..
66 for(String oldkey: oldMatch.getConfigAttributes().keySet()){
67 boolean check = false;
68 for(String newKey: newMatch.getConfigAttributes().keySet()){
69 if(oldkey.equals(newKey)){
70 if(oldMatch.getConfigAttributes().get(oldkey).equals(newMatch.getConfigAttributes().get(newKey))){
81 if(count==oldMatch.getConfigAttributes().size()){
86 }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){
91 }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){
98 // IF not a match then add it to the MatchStore
100 matchStore.add(newMatch);
106 //Logic changes for Requested Policies notifications..
107 public static PDPNotification checkMatch(PDPNotification oldNotification) {
108 boolean removed = false;
109 boolean updated = false;
110 if(oldNotification==null){
113 StdPDPNotification newNotification = new StdPDPNotification();
114 if(matchStore.isEmpty()) {
115 logger.debug("No Success Config Calls made yet.. ");
118 if(oldNotification.getRemovedPolicies()!=null && !oldNotification.getRemovedPolicies().isEmpty()){
119 // send all removed policies to client.
120 Collection<StdRemovedPolicy> removedPolicies = new HashSet<>();
121 StdRemovedPolicy newRemovedPolicy;
122 for(RemovedPolicy removedPolicy: oldNotification.getRemovedPolicies()){
123 newRemovedPolicy = new StdRemovedPolicy();
124 newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName());
125 newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo());
126 removedPolicies.add(newRemovedPolicy);
128 newNotification.setRemovedPolicies(removedPolicies);
131 if(oldNotification.getLoadedPolicies()!=null && !oldNotification.getLoadedPolicies().isEmpty()){
132 Collection<StdLoadedPolicy> updatedPolicies = new HashSet<>();
133 StdLoadedPolicy newUpdatedPolicy;
134 for(LoadedPolicy updatedPolicy: oldNotification.getLoadedPolicies()){
135 // if it is config policies check their matches..
136 if(updatedPolicy.getMatches()!=null && !updatedPolicy.getMatches().isEmpty()){
138 for(Matches match : matchStore){
140 // Again Better way would be comparing sizes first.
141 // Matches are different need to check if has configAttributes
142 if(match.getConfigAttributes()!=null && !match.getConfigAttributes().isEmpty()){
143 // adding onap and config to config-attributes.
144 int compValues = match.getConfigAttributes().size() + 2;
145 if(updatedPolicy.getMatches().size()== compValues){
146 // Comparing both the values..
147 boolean matchAttributes = false;
148 for(String newKey: updatedPolicy.getMatches().keySet()){
149 if("ONAPName".equals(newKey)){
150 if(updatedPolicy.getMatches().get(newKey).equals(match.getOnapName())){
151 matchAttributes = true;
153 matchAttributes = false;
156 }else if("ConfigName".equals(newKey)) {
157 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){
158 matchAttributes = true;
160 matchAttributes = false;
164 if(match.getConfigAttributes().containsKey(newKey)){
165 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){
166 matchAttributes = true;
168 matchAttributes = false;
172 matchAttributes = false;
186 }else if(match.getConfigName()!=null){
187 // If there are no config Attributes then check if it has Config Name
188 if(updatedPolicy.getMatches().size()== 2){
189 if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){
190 if(updatedPolicy.getMatches().get("ConfigName").equals(match.getConfigName())){
203 // If non exist then assuming the ONAP Name to be there.
204 if(updatedPolicy.getMatches().size()== 1){
205 if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){
215 // Add logic to add the policy.
217 newUpdatedPolicy = new StdLoadedPolicy();
218 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
219 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
220 newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
221 updatedPolicies.add(newUpdatedPolicy);
227 //send all non config notifications to client.
228 newUpdatedPolicy = new StdLoadedPolicy();
229 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
230 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
231 updatedPolicies.add(newUpdatedPolicy);
235 newNotification.setLoadedPolicies(updatedPolicies);
237 // Need to set the type of Update..
238 if(removed && updated) {
239 newNotification.setNotificationType(NotificationType.BOTH);
241 newNotification.setNotificationType(NotificationType.REMOVE);
243 newNotification.setNotificationType(NotificationType.UPDATE);
245 return newNotification;