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;
26 import org.onap.policy.api.LoadedPolicy;
27 import org.onap.policy.api.NotificationType;
28 import org.onap.policy.api.PDPNotification;
29 import org.onap.policy.api.RemovedPolicy;
30 import org.onap.policy.std.StdLoadedPolicy;
31 import org.onap.policy.std.StdPDPNotification;
32 import org.onap.policy.std.StdRemovedPolicy;
34 import org.onap.policy.common.logging.flexlogger.*;
36 public class MatchStore {
37 private static HashSet<Matches> matchStore = new HashSet<>();
38 private static Logger logger = FlexLogger.getLogger(MatchStore.class.getName());
40 private MatchStore() {
44 public static HashSet<Matches> getMatchStore() {
48 public static void storeMatch(Matches newMatch){
51 if(matchStore.isEmpty()){
52 matchStore.add(newMatch);
54 // Check if it is a new Match
55 Boolean match = false;
56 for(Matches oldMatch: matchStore){
58 if(oldMatch.getOnapName().equals(newMatch.getOnapName())){
59 // Compare ConfigName if it exists.
60 if(newMatch.getConfigName()!=null && oldMatch.getConfigName()!=null){
61 if(oldMatch.getConfigName().equals(newMatch.getConfigName())){
62 // Compare the Config Attributes if they exist.
63 if(newMatch.getConfigAttributes()!= null && oldMatch.getConfigAttributes()!=null) {
64 //Simple thing would be comparing their size.
65 if(newMatch.getConfigAttributes().size()==oldMatch.getConfigAttributes().size()){
66 // Now need to compare each of them..
68 for(String oldkey: oldMatch.getConfigAttributes().keySet()){
69 boolean check = false;
70 for(String newKey: newMatch.getConfigAttributes().keySet()){
71 if(oldkey.equals(newKey)){
72 if(oldMatch.getConfigAttributes().get(oldkey).equals(newMatch.getConfigAttributes().get(newKey))){
83 if(count==oldMatch.getConfigAttributes().size()){
88 }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){
93 }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){
100 // IF not a match then add it to the MatchStore
102 matchStore.add(newMatch);
108 //Logic changes for Requested Policies notifications..
109 public static PDPNotification checkMatch(PDPNotification oldNotification) {
110 boolean removed = false;
111 boolean updated = false;
112 if(oldNotification==null){
115 StdPDPNotification newNotification = new StdPDPNotification();
116 if(matchStore.isEmpty()) {
117 logger.debug("No Success Config Calls made yet.. ");
120 if(oldNotification.getRemovedPolicies()!=null && !oldNotification.getRemovedPolicies().isEmpty()){
121 // send all removed policies to client.
122 Collection<StdRemovedPolicy> removedPolicies = new HashSet<>();
123 StdRemovedPolicy newRemovedPolicy;
124 for(RemovedPolicy removedPolicy: oldNotification.getRemovedPolicies()){
125 newRemovedPolicy = new StdRemovedPolicy();
126 newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName());
127 newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo());
128 removedPolicies.add(newRemovedPolicy);
130 newNotification.setRemovedPolicies(removedPolicies);
133 if(oldNotification.getLoadedPolicies()!=null && !oldNotification.getLoadedPolicies().isEmpty()){
134 Collection<StdLoadedPolicy> updatedPolicies = new HashSet<>();
135 StdLoadedPolicy newUpdatedPolicy;
136 for(LoadedPolicy updatedPolicy: oldNotification.getLoadedPolicies()){
137 // if it is config policies check their matches..
138 if(updatedPolicy.getMatches()!=null && !updatedPolicy.getMatches().isEmpty()){
140 for(Matches match : matchStore){
142 // Again Better way would be comparing sizes first.
143 // Matches are different need to check if has configAttributes
144 if(match.getConfigAttributes()!=null && !match.getConfigAttributes().isEmpty()){
145 // adding onap and config to config-attributes.
146 int compValues = match.getConfigAttributes().size() + 2;
147 if(updatedPolicy.getMatches().size()== compValues){
148 // Comparing both the values..
149 boolean matchAttributes = false;
150 for(String newKey: updatedPolicy.getMatches().keySet()){
151 if("ONAPName".equals(newKey)){
152 if(updatedPolicy.getMatches().get(newKey).equals(match.getOnapName())){
153 matchAttributes = true;
155 matchAttributes = false;
158 }else if("ConfigName".equals(newKey)) {
159 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){
160 matchAttributes = true;
162 matchAttributes = false;
166 if(match.getConfigAttributes().containsKey(newKey)){
167 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){
168 matchAttributes = true;
170 matchAttributes = false;
174 matchAttributes = false;
188 }else if(match.getConfigName()!=null){
189 // If there are no config Attributes then check if it has Config Name
190 if(updatedPolicy.getMatches().size()== 2){
191 if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){
192 if(updatedPolicy.getMatches().get("ConfigName").equals(match.getConfigName())){
205 // If non exist then assuming the ONAP Name to be there.
206 if(updatedPolicy.getMatches().size()== 1){
207 if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){
217 // Add logic to add the policy.
219 newUpdatedPolicy = new StdLoadedPolicy();
220 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
221 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
222 newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
223 updatedPolicies.add(newUpdatedPolicy);
229 //send all non config notifications to client.
230 newUpdatedPolicy = new StdLoadedPolicy();
231 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
232 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
233 updatedPolicies.add(newUpdatedPolicy);
237 newNotification.setLoadedPolicies(updatedPolicies);
239 // Need to set the type of Update..
240 if(removed && updated) {
241 newNotification.setNotificationType(NotificationType.BOTH);
243 newNotification.setNotificationType(NotificationType.REMOVE);
245 newNotification.setNotificationType(NotificationType.UPDATE);
247 return newNotification;