ac3c9e968de7fc97ed44cbca60401df77ba10b34
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / openecomp / 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.openecomp.policy.std;
22
23 import java.util.Collection;
24 import java.util.HashSet;
25
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;
33
34 import org.openecomp.policy.common.logging.flexlogger.*; 
35
36 public class MatchStore {
37         private static HashSet<Matches> matchStore = new HashSet<Matches>();
38         private static Logger logger = FlexLogger.getLogger(MatchStore.class.getName());
39         
40         public static HashSet<Matches> getMatchStore() {
41                 return matchStore;
42         }
43
44         public static void storeMatch(Matches newMatch){
45                 // Initialization..
46                 if(newMatch!=null){
47                         if(matchStore.isEmpty()){
48                                 matchStore.add(newMatch);
49                         }else{
50                                 // Check if it is a new Match
51                                 Boolean match = false;
52                                 for(Matches oldMatch: matchStore){
53                                         // Compare ECOMPName
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..
63                                                                                 int count= 0;
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))){
69                                                                                                                 check = true;
70                                                                                                         }
71                                                                                                 }
72                                                                                         }
73                                                                                         if(check){
74                                                                                                 count++;
75                                                                                         }else{
76                                                                                                 break;
77                                                                                         }
78                                                                                 }
79                                                                                 if(count==oldMatch.getConfigAttributes().size()){
80                                                                                         match = true;
81                                                                                         break;
82                                                                                 }
83                                                                         }
84                                                                 }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){
85                                                                         match = true;
86                                                                         break;
87                                                                 }
88                                                         }
89                                                 }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){
90                                                         match = true;
91                                                         break;
92                                                 }       
93                                         }
94                                         
95                                 }
96                                 // IF not a match then add it to the MatchStore
97                                 if(match==false){
98                                         matchStore.add(newMatch);
99                                 }
100                         }
101                 }
102         }
103         
104         //Logic changes for Requested Policies notifications.. 
105         public static PDPNotification checkMatch(PDPNotification oldNotification) {
106                 boolean removed = false, updated = false;
107                 if(oldNotification==null){
108                         return null;
109                 }
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. ");
114                         return null;
115                 } 
116                 if(oldNotification.getRemovedPolicies()!=null && !oldNotification.getRemovedPolicies().isEmpty()){
117                         // send all removed policies to client.
118                         Collection<StdRemovedPolicy> removedPolicies = new HashSet<StdRemovedPolicy>();
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);
125                         }
126                         newNotification.setRemovedPolicies(removedPolicies);
127                         removed = true;
128                 }
129                 if(oldNotification.getLoadedPolicies()!=null && !oldNotification.getLoadedPolicies().isEmpty()){
130                         Collection<StdLoadedPolicy> updatedPolicies = new HashSet<StdLoadedPolicy>();
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){
137                                                 matched = false;
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;
150                                                                                 }else {
151                                                                                         matchAttributes = false;
152                                                                                         break;
153                                                                                 }
154                                                                         }else if(newKey.equals("ConfigName")) {
155                                                                                 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){
156                                                                                         matchAttributes = true;
157                                                                                 }else{
158                                                                                         matchAttributes = false;
159                                                                                         break;
160                                                                                 }
161                                                                         }else {
162                                                                                 if(match.getConfigAttributes().containsKey(newKey)){
163                                                                                         if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){
164                                                                                                 matchAttributes = true;
165                                                                                         }else{
166                                                                                                 matchAttributes = false;
167                                                                                                 break;
168                                                                                         }
169                                                                                 }else{
170                                                                                         matchAttributes = false;
171                                                                                         break;
172                                                                                 }
173                                                                         }
174                                                                 }
175                                                                 if(matchAttributes){
176                                                                         // Match..
177                                                                         matched = true;
178                                                                 }else{
179                                                                         break;
180                                                                 }
181                                                         }else {
182                                                                 break;
183                                                         }
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())){
189                                                                                 // Match..
190                                                                                 matched = true;
191                                                                         }else{
192                                                                                 break;
193                                                                         }
194                                                                 }else {
195                                                                         break;
196                                                                 }
197                                                         }else{
198                                                                 break;
199                                                         }
200                                                 }else {
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())){
204                                                                         // Match.. 
205                                                                         matched = true;
206                                                                 }else {
207                                                                         break;
208                                                                 }
209                                                         }else{
210                                                                 break;
211                                                         }
212                                                 }
213                                                 // Add logic to add the policy. 
214                                                 if(matched){
215                                                         newUpdatedPolicy = new StdLoadedPolicy();
216                                                         newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
217                                                         newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
218                                                         newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
219                                                         updatedPolicies.add(newUpdatedPolicy);
220                                                         updated = true;
221                                                 }
222                                         }
223                                         
224                                 }else {
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);
230                                         updated = true;
231                                 }
232                         }
233                         newNotification.setLoadedPolicies(updatedPolicies);
234                 }
235                 // Need to set the type of Update.. 
236                 if(removed && updated) {
237                         newNotification.setNotificationType(NotificationType.BOTH);
238                 }else if(removed){
239                         newNotification.setNotificationType(NotificationType.REMOVE);
240                 }else if(updated){
241                         newNotification.setNotificationType(NotificationType.UPDATE);
242                 }
243                 return newNotification;
244         }
245         
246 }