Technical debt reduction
[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 java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26
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; 
33
34 public class MatchStore {
35         private static HashSet<Matches> matchStore = new HashSet<>();
36         private static Logger logger = FlexLogger.getLogger(MatchStore.class.getName());
37         
38         private MatchStore() {
39                 // Empty Constructor
40         }
41         
42         public static Set<Matches> getMatchStore() {
43                 return matchStore;
44         }
45
46         public static void storeMatch(Matches newMatch){
47                 // Initialization..
48                 if(newMatch!=null){
49                         if(matchStore.isEmpty()){
50                                 matchStore.add(newMatch);
51                         }else{
52                                 // Check if it is a new Match
53                                 Boolean match = false;
54                                 for(Matches oldMatch: matchStore){
55                                         // Compare ONAPName
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..
65                                                                                 int count= 0;
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))){
71                                                                                                                 check = true;
72                                                                                                         }
73                                                                                                 }
74                                                                                         }
75                                                                                         if(check){
76                                                                                                 count++;
77                                                                                         }else{
78                                                                                                 break;
79                                                                                         }
80                                                                                 }
81                                                                                 if(count==oldMatch.getConfigAttributes().size()){
82                                                                                         match = true;
83                                                                                         break;
84                                                                                 }
85                                                                         }
86                                                                 }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){
87                                                                         match = true;
88                                                                         break;
89                                                                 }
90                                                         }
91                                                 }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){
92                                                         match = true;
93                                                         break;
94                                                 }       
95                                         }
96                                         
97                                 }
98                                 // IF not a match then add it to the MatchStore
99                                 if(! match){
100                                         matchStore.add(newMatch);
101                                 }
102                         }
103                 }
104         }
105         
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){
111                         return null;
112                 }
113                 StdPDPNotification newNotification = new StdPDPNotification();
114                 if(matchStore.isEmpty()) {
115                         logger.debug("No Success Config Calls made yet.. ");
116                         return null;
117                 } 
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);
127                         }
128                         newNotification.setRemovedPolicies(removedPolicies);
129                         removed = true;
130                 }
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()){
137                                         boolean matched;
138                                         for(Matches match : matchStore){
139                                                 matched = false;
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;
152                                                                                 }else {
153                                                                                         matchAttributes = false;
154                                                                                         break;
155                                                                                 }
156                                                                         }else if("ConfigName".equals(newKey)) {
157                                                                                 if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){
158                                                                                         matchAttributes = true;
159                                                                                 }else{
160                                                                                         matchAttributes = false;
161                                                                                         break;
162                                                                                 }
163                                                                         }else {
164                                                                                 if(match.getConfigAttributes().containsKey(newKey)){
165                                                                                         if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){
166                                                                                                 matchAttributes = true;
167                                                                                         }else{
168                                                                                                 matchAttributes = false;
169                                                                                                 break;
170                                                                                         }
171                                                                                 }else{
172                                                                                         matchAttributes = false;
173                                                                                         break;
174                                                                                 }
175                                                                         }
176                                                                 }
177                                                                 if(matchAttributes){
178                                                                         // Match..
179                                                                         matched = true;
180                                                                 }else{
181                                                                         break;
182                                                                 }
183                                                         }else {
184                                                                 break;
185                                                         }
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())){
191                                                                                 // Match..
192                                                                                 matched = true;
193                                                                         }else{
194                                                                                 break;
195                                                                         }
196                                                                 }else {
197                                                                         break;
198                                                                 }
199                                                         }else{
200                                                                 break;
201                                                         }
202                                                 }else {
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())){
206                                                                         // Match.. 
207                                                                         matched = true;
208                                                                 }else {
209                                                                         break;
210                                                                 }
211                                                         }else{
212                                                                 break;
213                                                         }
214                                                 }
215                                                 // Add logic to add the policy. 
216                                                 if(matched){
217                                                         newUpdatedPolicy = new StdLoadedPolicy();
218                                                         newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName());
219                                                         newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo());
220                                                         newUpdatedPolicy.setMatches(updatedPolicy.getMatches());
221                                                         updatedPolicies.add(newUpdatedPolicy);
222                                                         updated = true;
223                                                 }
224                                         }
225                                         
226                                 }else {
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);
232                                         updated = true;
233                                 }
234                         }
235                         newNotification.setLoadedPolicies(updatedPolicies);
236                 }
237                 // Need to set the type of Update.. 
238                 if(removed && updated) {
239                         newNotification.setNotificationType(NotificationType.BOTH);
240                 }else if(removed){
241                         newNotification.setNotificationType(NotificationType.REMOVE);
242                 }else if(updated){
243                         newNotification.setNotificationType(NotificationType.UPDATE);
244                 }
245                 return newNotification;
246         }
247         
248 }