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