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.openecomp.policy.std;
23 import java.util.Collection;
24 import java.util.HashSet;
26 import org.openecomp.policy.api.LoadedPolicy;
27 import org.openecomp.policy.api.NotificationType;
28 import org.openecomp.policy.api.PDPNotification;
29 import org.openecomp.policy.api.RemovedPolicy;
30 import org.openecomp.policy.std.StdLoadedPolicy;
31 import org.openecomp.policy.std.StdPDPNotification;
32 import org.openecomp.policy.std.StdRemovedPolicy;
34 import org.openecomp.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 public static HashSet<Matches> getMatchStore() {
44 public static void storeMatch(Matches newMatch){
47 if(matchStore.isEmpty()){
48 matchStore.add(newMatch);
50 // Check if it is a new Match
51 Boolean match = false;
52 for(Matches oldMatch: matchStore){
54 if(oldMatch.getEcompName().equals(newMatch.getEcompName())){
55 // Compare ConfigName if it exists.
56 if(newMatch.getConfigName()!=null && oldMatch.getConfigName()!=null){
57 if(oldMatch.getConfigName().equals(newMatch.getConfigName())){
58 // Compare the Config Attributes if they exist.
59 if(newMatch.getConfigAttributes()!= null && oldMatch.getConfigAttributes()!=null) {
60 //Simple thing would be comparing their size.
61 if(newMatch.getConfigAttributes().size()==oldMatch.getConfigAttributes().size()){
62 // Now need to compare each of them..
64 for(String oldkey: oldMatch.getConfigAttributes().keySet()){
65 boolean check = false;
66 for(String newKey: newMatch.getConfigAttributes().keySet()){
67 if(oldkey.equals(newKey)){
68 if(oldMatch.getConfigAttributes().get(oldkey).equals(newMatch.getConfigAttributes().get(newKey))){
79 if(count==oldMatch.getConfigAttributes().size()){
84 }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){
89 }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){
96 // IF not a match then add it to the MatchStore
98 matchStore.add(newMatch);
104 //Logic changes for Requested Policies notifications..
105 public static PDPNotification checkMatch(PDPNotification oldNotification) {
106 boolean removed = false, updated = false;
107 if(oldNotification==null){
110 StdPDPNotification newNotification = new StdPDPNotification();
111 if(matchStore.isEmpty()) {
112 logger.debug("No Success Config Calls made yet.. ");
113 System.out.println("No success Config calls made yet. ");
116 if(oldNotification.getRemovedPolicies()!=null && !oldNotification.getRemovedPolicies().isEmpty()){
117 // send all removed policies to client.
118 Collection<StdRemovedPolicy> removedPolicies = new HashSet<>();
119 StdRemovedPolicy newRemovedPolicy;
120 for(RemovedPolicy removedPolicy: oldNotification.getRemovedPolicies()){
121 newRemovedPolicy = new StdRemovedPolicy();
122 newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName());
123 newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo());
124 removedPolicies.add(newRemovedPolicy);
126 newNotification.setRemovedPolicies(removedPolicies);
129 if(oldNotification.getLoadedPolicies()!=null && !oldNotification.getLoadedPolicies().isEmpty()){
130 Collection<StdLoadedPolicy> updatedPolicies = new HashSet<>();
131 StdLoadedPolicy newUpdatedPolicy;
132 for(LoadedPolicy updatedPolicy: oldNotification.getLoadedPolicies()){
133 // if it is config policies check their matches..
134 if(updatedPolicy.getMatches()!=null && !updatedPolicy.getMatches().isEmpty()){
135 boolean matched = false;
136 for(Matches match : matchStore){
138 // Again Better way would be comparing sizes first.
139 // Matches are different need to check if has configAttributes
140 if(match.getConfigAttributes()!=null && !match.getConfigAttributes().isEmpty()){
141 // adding ecomp and config to config-attributes.
142 int compValues = match.getConfigAttributes().size() + 2;
143 if(updatedPolicy.getMatches().size()== compValues){
144 // Comparing both the values..
145 boolean matchAttributes = false;
146 for(String newKey: updatedPolicy.getMatches().keySet()){
147 if(newKey.equals("ECOMPName")){
148 if(updatedPolicy.getMatches().get(newKey).equals(match.getEcompName())){
149 matchAttributes = true;
151 matchAttributes = false;
154 }else if(newKey.equals("ConfigName")) {
155 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){
156 matchAttributes = true;
158 matchAttributes = false;
162 if(match.getConfigAttributes().containsKey(newKey)){
163 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){
164 matchAttributes = true;
166 matchAttributes = false;
170 matchAttributes = false;
184 }else if(match.getConfigName()!=null){
185 // If there are no config Attributes then check if it has Config Name
186 if(updatedPolicy.getMatches().size()== 2){
187 if(updatedPolicy.getMatches().get("ECOMPName").equals(match.getEcompName())){
188 if(updatedPolicy.getMatches().get("ConfigName").equals(match.getConfigName())){
201 // If non exist then assuming the ECOMP Name to be there.
202 if(updatedPolicy.getMatches().size()== 1){
203 if(updatedPolicy.getMatches().get("ECOMPName").equals(match.getEcompName())){
213 // Add logic to add the policy.
215 newUpdatedPolicy = new StdLoadedPolicy();
216 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
217 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
218 newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
219 updatedPolicies.add(newUpdatedPolicy);
225 //send all non config notifications to client.
226 newUpdatedPolicy = new StdLoadedPolicy();
227 newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
228 newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
229 updatedPolicies.add(newUpdatedPolicy);
233 newNotification.setLoadedPolicies(updatedPolicies);
235 // Need to set the type of Update..
236 if(removed && updated) {
237 newNotification.setNotificationType(NotificationType.BOTH);
239 newNotification.setNotificationType(NotificationType.REMOVE);
241 newNotification.setNotificationType(NotificationType.UPDATE);
243 return newNotification;