Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / components / ElasticSearchComponent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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.components;
22
23
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.regex.Pattern;
29
30 import org.openecomp.policy.dao.PolicyVersionDao;
31 import org.openecomp.policy.elk.client.ElkConnector;
32 import org.openecomp.policy.elk.client.ElkConnector.PolicyIndexType;
33 import org.openecomp.policy.rest.dao.DescriptiveScopeDao;
34 import org.openecomp.policy.rest.jpa.DescriptiveScope;
35 import org.openecomp.policy.rest.jpa.PolicyVersion;
36 import org.openecomp.policy.elk.client.Pair;
37 import org.openecomp.policy.elk.client.PolicyLocator;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
41 import org.openecomp.policy.common.logging.flexlogger.Logger;
42
43
44 public class ElasticSearchComponent {
45         private static final Logger logger = FlexLogger.getLogger(ElasticSearchComponent.class);
46
47
48         private static PolicyVersionDao policyVersionDao;
49         private static DescriptiveScopeDao descriptiveScopeDao;
50
51         @Autowired
52         public ElasticSearchComponent(PolicyVersionDao policyVersionDao){
53                 ElasticSearchComponent.policyVersionDao = policyVersionDao;
54         }
55
56         @Autowired
57         public ElasticSearchComponent(DescriptiveScopeDao descriptiveScopeDao){
58                 ElasticSearchComponent.descriptiveScopeDao = descriptiveScopeDao;
59         }
60
61
62         private volatile static HashMap<Path, String> filteredPolicies = new HashMap<Path, String>();
63
64         public static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
65                 private static final long serialVersionUID = 1L;
66                 {
67                         put(CLFAULT_UIFIELD_D2_SERVICES_TRINITY, CLFAULT_UIJSON_D2_SERVICES_TRINITY);
68                         put(CLFAULT_UIFIELD_D2_SERVICES_VUSP, CLFAULT_UIJSON_D2_SERVICES_VUSP);
69                         put(CLFAULT_UIFIELD_D2_SERVICES_MCR, CLFAULT_UIJSON_D2_SERVICES_MCR);
70                         put(CLFAULT_UIFIELD_D2_SERVICES_GAMMA, CLFAULT_UIJSON_D2_SERVICES_GAMMA);       
71                         put(CLFAULT_UIFIELD_D2_SERVICES_VDNS, CLFAULT_UIJSON_D2_SERVICES_VDNS);
72
73                         put(CLFAULT_UIFIELD_EMAIL_ADDRESS, CLFAULT_UIJSON_EMAIL_ADDRESS);
74                         put(CLFAULT_UIFIELD_TRIGGER_SIGNATURE, CLFAULT_UIJSON_TRIGGER_SIGNATURE);
75                         put(CLFAULT_UIFIELD_VERIFICATION_SIGNATURE, CLFAULT_UIJSON_VERIFICATION_SIGNATURE);
76                         put(CLFAULT_UIFIELD_CONNECT_ALL_TRAPS, CLFAULT_UIJSON_CONNECT_ALL_TRAPS);       
77                         put(CLFAULT_UIFIELD_CONNECT_ALL_FAULTS, CLFAULT_UIJSON_CONNECT_ALL_FAULTS);
78
79                         put(CLFAULT_UIFIELD_POLICY_STATUS_INACTIVE, CLFAULT_UIJSON_POLICY_STATUS_ACTIVE);
80                         put(CLFAULT_UIFIELD_POLICY_STATUS_ACTIVE, CLFAULT_UIJSON_POLICY_STATUS_INACTIVE);
81
82                         put(CLPM_UIFIELD_ONSET_MESSAGE, CLPM_UIJSON_ONSET_MESSAGE);
83                         put(CLPM_UIFIELD_POLICY_NAME, CLPM_UIJSON_POLICY_NAME);
84                         put(CLPM_UIFIELD_ABATEMENT_MESSAGE, CLPM_UIJSON_ABATEMENT_MESSAGE);
85                         put(CLPM_UIFIELD_GEOLINK, CLPM_UIJSON_GEOLINK); 
86                 }};
87
88                 //For AND and OR logical connector AND=0 and OR=1
89                 private static int connectorSelected;
90
91                 public static final String CLFAULT_UIFIELD_D2_SERVICES_TRINITY = "Hosted Voice (Trinity)";
92                 public static final String CLFAULT_UIJSON_D2_SERVICES_TRINITY = "trinity";
93
94                 public static final String CLFAULT_UIFIELD_D2_SERVICES_VUSP = "vUSP";
95                 public static final String CLFAULT_UIJSON_D2_SERVICES_VUSP = "vUSP";
96
97                 public static final String CLFAULT_UIFIELD_D2_SERVICES_MCR = "MCR";
98                 public static final String CLFAULT_UIJSON_D2_SERVICES_MCR = "mcr";
99
100                 public static final String CLFAULT_UIFIELD_D2_SERVICES_GAMMA = "Gamma";
101                 public static final String CLFAULT_UIJSON_D2_SERVICES_GAMMA = "gama";
102
103                 public static final String CLFAULT_UIFIELD_D2_SERVICES_VDNS = "vDNS";
104                 public static final String CLFAULT_UIJSON_D2_SERVICES_VDNS = "vDNS";
105
106                 public static final String CLFAULT_UIFIELD_EMAIL_ADDRESS = "Email Address";
107                 public static final String CLFAULT_UIJSON_EMAIL_ADDRESS = "emailAddress";
108
109                 public static final String CLFAULT_UIFIELD_TRIGGER_SIGNATURE = "Trigger Signature";
110                 public static final String CLFAULT_UIJSON_TRIGGER_SIGNATURE = "triggerSignaturesUsedForUI.signatures";
111
112                 public static final String CLFAULT_UIFIELD_VERIFICATION_SIGNATURE = "Verification Signature";
113                 public static final String CLFAULT_UIJSON_VERIFICATION_SIGNATURE = "verificationSignaturesUsedForUI.signatures";
114
115                 public static final String CLFAULT_UIFIELD_CONNECT_ALL_TRAPS = "Connect All Traps";
116                 public static final String CLFAULT_UIJSON_CONNECT_ALL_TRAPS = "triggerSignaturesUsedForUI.connectSignatures";
117
118                 public static final String CLFAULT_UIFIELD_CONNECT_ALL_FAULTS = "Connect All Faults";
119                 public static final String CLFAULT_UIJSON_CONNECT_ALL_FAULTS = "verificationSignaturesUsedForUI.connectSignatures";
120
121                 public static final String CLFAULT_UIFIELD_POLICY_STATUS_ACTIVE = "Active";
122                 public static final String CLFAULT_UIJSON_POLICY_STATUS_ACTIVE = "ACTIVE";
123
124                 public static final String CLFAULT_UIFIELD_POLICY_STATUS_INACTIVE = "InActive";
125                 public static final String CLFAULT_UIJSON_POLICY_STATUS_INACTIVE = "INACTIVE";
126
127
128                 public static final String CLPM_UIFIELD_ONSET_MESSAGE = "Onset Message";
129                 public static final String CLPM_UIJSON_ONSET_MESSAGE = "attributes.OnsetMessage";
130
131                 public static final String CLPM_UIFIELD_POLICY_NAME = "PolicyName";
132                 public static final String CLPM_UIJSON_POLICY_NAME = "attributes.PolicyName";
133
134                 public static final String CLPM_UIFIELD_ABATEMENT_MESSAGE = "Abatement Message";
135                 public static final String CLPM_UIJSON_ABATEMENT_MESSAGE = "attributes.AbatementMessage";
136
137                 public static final String CLPM_UIFIELD_GEOLINK = "Geo Link";   
138                 public static final String CLPM_UIJSON_GEOLINK = "geoLink";
139
140                 public static void search(String value){
141                         String policyType = "all";//(String) self.searchPolicyType.getValue()
142
143                         ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s =  new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>();
144
145                         String searchText = "";//self.searchTextBox.getValue()
146                         if (searchText == null || searchText.isEmpty()) {
147                                 if (policyType == null || policyType.isEmpty() &&
148                                                 !policyType.equals(ElkConnector.PolicyIndexType.closedloop.toString())) {
149                                         if (logger.isDebugEnabled()) {
150                                                 logger.debug("Clearing search filters, nothing to search and not closed loop.");
151                                         }
152                                         return;                                         
153                                 }
154                         } else {
155                                 searchText = searchText.trim();
156                                 //Descriptive Scope. 
157                                 /*
158                           When a item is selected in the "descriptiveScope" comboBox, the name of the item 
159                           is added to the Search-Text Box with the prefix "Descriptive-Scope"
160                           User needs to press the "Search" button to perform the search. 
161                                  */
162                                 if(searchText.contains("Descriptive-Scope="))
163                                 { 
164                                         if (logger.isDebugEnabled()) {
165                                                 logger.debug("Inside the Descriptive Scope");
166                                         }
167                                         /*
168                                         First item is always String "Descriptive-Scope" before the "=",
169                                         So taking the second item of "split using ="
170                                          */
171                                         String[] dsName= searchText.split("=",2);
172                                         /*
173                                         Trying to find the search String by traversing different items from the dictionary by Scope-Name
174                                         Once when the the "scope-name" is found, we get the search string from dictionary. 
175                                          */
176                                         for (int i = 0; i < descriptiveScopeDao.getDescriptiveScope().size(); i++) {
177                                                 DescriptiveScope dsSearch=descriptiveScopeDao.getDescriptiveScope().get(i);
178                                                 if(dsSearch.getScopeName().equals(dsName[1])){
179                                                         searchText=dsSearch.getSearch();
180                                                         if (logger.isDebugEnabled()) {
181                                                                 logger.debug("DescriptiveScope Search String is " +searchText );
182                                                         }
183                                                 }
184                                         }
185                                 }
186
187                                 if(searchText.contains(":"))
188                                 {
189                                         String connector="&";
190
191                                         if(searchText.contains("&"))
192                                         {
193                                                 connector="&";
194                                                 connectorSelected=0;
195                                         }
196                                         else if(searchText.contains("|"))
197                                         {
198                                                 connector=Pattern.quote("|");
199                                                 connectorSelected=1;
200                                         }
201                                         for (String retval: searchText.split(connector)){
202
203                                                 int index= retval.indexOf(':');
204                                                 String filterKey=null;
205                                                 String filterValue=null;
206
207                                                 filterKey=retval.substring(0,index);
208                                                 filterValue= retval.substring(index+1);
209
210                                                 String clSearchBoxFilter=filterKey;
211
212                                                 ArrayList<String> clSearchBoxFilterField_s = new ArrayList<String>();
213
214                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_Fault.name() + "_Body." + clSearchBoxFilter);
215                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_PM.name() + "_Body." + clSearchBoxFilter);
216                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_FW.name() + "_Body." + clSearchBoxFilter);
217                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_MS.name() + "_Body." + clSearchBoxFilter);
218
219
220                                                 ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>();
221                                                 clSearchBoxFilterValue_s.add(filterValue);
222
223                                                 filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s));
224                                         }
225                                 }
226                         }
227
228                         if (policyType != null && !policyType.isEmpty() && 
229                                         policyType.equals(ElkConnector.PolicyIndexType.closedloop.toString())) {
230
231                                 /* closed loop policy type */
232
233                                 String clPolicyType = "";//(String) self.cbSearchCLPolicyType.getValue()
234                                 if (clPolicyType != null && !clPolicyType.isEmpty()) {                                                  
235                                         ArrayList<String> clPolicyTypeField_s = new ArrayList<String>();
236                                         clPolicyTypeField_s.add("Policy.PolicyType");
237
238                                         ArrayList<String> clPolicyTypeValue_s = new ArrayList<String>();
239                                         clPolicyTypeValue_s.add(clPolicyType);
240
241                                         filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clPolicyTypeField_s, clPolicyTypeValue_s));
242                                 }
243
244                                 String clEcompName = "";//(String) self.cbSearchCLEcompName.getValue()
245                                 if (clEcompName != null && !clEcompName.isEmpty()) {
246                                         clSearchBody(clPolicyType, "ecompname", clEcompName, filter_s);
247                                 }
248
249                                 String clD2Services = "";//(String) self.cbSearchCLD2Services.getValue()
250                                 if (clD2Services != null && !clD2Services.isEmpty()) {
251                                         switch (clD2Services) {
252                                         case CLFAULT_UIFIELD_D2_SERVICES_TRINITY:
253                                         case CLFAULT_UIFIELD_D2_SERVICES_VUSP:                                          
254                                         case CLFAULT_UIFIELD_D2_SERVICES_MCR:                                           
255                                         case CLFAULT_UIFIELD_D2_SERVICES_GAMMA:                                                 
256                                         case CLFAULT_UIFIELD_D2_SERVICES_VDNS:  
257                                                 clSearchBody(clPolicyType, name2jsonPath.get(clD2Services), "true", filter_s);
258                                                 break;
259                                         default:
260                                                 if (logger.isWarnEnabled())
261                                                         logger.warn("Unexpected D2 Service: " + clD2Services);
262                                                 break;
263                                         }
264                                 }
265
266                                 String clFaultAction = "";//(String) self.cbSearchCLFaultAction.getValue()
267                                 if (clFaultAction != null && !clFaultAction.isEmpty()) {
268                                         if (clPolicyType == null || clPolicyType.equals(ElkConnector.PolicyType.Config_Fault.name())) {
269                                                 clSearchFilter(ElkConnector.PolicyType.Config_Fault.name(), "actions", clFaultAction, filter_s);
270                                         }
271                                 }
272
273                                 String clFaultStatus = "";//(String) self.cbSearchCLFaultStatus.getValue()
274                                 if (clFaultStatus != null && !clFaultStatus.isEmpty()) {
275                                         if (clPolicyType == null || clPolicyType.equals(ElkConnector.PolicyType.Config_Fault.name())) {
276                                                 clSearchFilter(ElkConnector.PolicyType.Config_Fault.name(), "closedLoopPolicyStatus", clFaultStatus, filter_s);
277                                         }
278                                 }
279
280                                 String clFaultVnfTypes = "";//(String) self.cbSearchCLFaultVnfTypes.getValue()
281                                 if (clFaultVnfTypes != null && !clFaultVnfTypes.isEmpty()) {
282                                         if (clPolicyType == null || clPolicyType.equals(ElkConnector.PolicyType.Config_Fault.name())) {
283                                                 clSearchFilter(ElkConnector.PolicyType.Config_Fault.name(), "vnfType", clFaultVnfTypes, filter_s);
284                                         }
285                                 }
286
287                                 String clPMServiceType = "";//(String) self.cbSearchCLPMServiceType.getValue()
288                                 if (clPMServiceType != null && !clPMServiceType.isEmpty()) {
289                                         if (clPolicyType == null || clPolicyType.equals(ElkConnector.PolicyType.Config_PM.name())) {
290                                                 clSearchFilter(ElkConnector.PolicyType.Config_PM.name(), "serviceTypePolicyName", clPMServiceType, filter_s);
291                                         }
292                                 }
293
294                                 String clSearchBoxFilter = "";//(String) self.cbSearchCLTextFilter.getValue()
295                                 if (clSearchBoxFilter != null && !clSearchBoxFilter.isEmpty() && 
296                                                 searchText != null && !searchText.isEmpty()) {
297
298                                         if (name2jsonPath.containsKey(clSearchBoxFilter)) {
299                                                 clSearchBoxFilter = name2jsonPath.get(clSearchBoxFilter);
300                                         }                                               
301
302                                         ArrayList<String> clSearchBoxFilterField_s = new ArrayList<String>();
303                                         if (clPolicyType == null || clPolicyType.isEmpty()) {
304                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_Fault.name() + "_Body." + clSearchBoxFilter);
305                                                 clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_PM.name() + "_Body." + clSearchBoxFilter);
306                                         } else {
307                                                 clSearchBoxFilterField_s.add("Policy.Body." + clPolicyType + "_Body." + clSearchBoxFilter);
308                                         }
309
310                                         ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>();
311                                         clSearchBoxFilterValue_s.add(searchText);
312
313                                         filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s));
314
315                                         // deactivate search all fields in case a searchbox filter is provided
316                                         searchText = "";
317                                 }
318                         }
319
320                         if ((searchText == null || searchText.isEmpty()) && 
321                                         (filter_s == null || filter_s.size() <=0) ) {
322                                 if (logger.isWarnEnabled()) {
323                                         logger.warn("Clearing search filters, closed loop but nothing to search nor filters");
324                                 }
325
326                                 return;
327                         }
328
329                         ArrayList<PolicyLocator> locators;
330                         try {
331                                 locators = ElkConnector.singleton.policyLocators(toPolicyIndexType(policyType), 
332                                                 searchText, filter_s,connectorSelected);        
333                         } catch (Exception ise) {
334                                 /*AdminNotification.warn("Search is unavailable: " + ise.getMessage());*/
335                                 return;
336                         }
337
338                         synchronized(filteredPolicies) {
339                                 if (locators.isEmpty()) {
340                                         if (logger.isInfoEnabled()) {
341                                                 logger.info("No match has been found");
342                                         }
343                                         //AdminNotification.warn("No match has been found");
344                                         return;
345                                 }
346
347                                 // Retrieve active versions
348
349                                 HashMap<String, Boolean> policyVersion_s = new HashMap<String, Boolean>();
350                                 for(int i = 0; i < policyVersionDao.getPolicyVersionData().size(); i++) {
351                                         PolicyVersion entityVersion = policyVersionDao.getPolicyVersionData().get(i);
352                                         String dbPolicy = entityVersion.getPolicyName() + "." + entityVersion.getActiveVersion();
353                                         policyVersion_s.put(dbPolicy, true);
354                                         if (logger.isDebugEnabled())
355                                                 logger.debug("Map addition: DB Policy Name: " + dbPolicy);
356                                 }
357
358                                 filteredPolicies.clear();
359                                 for (PolicyLocator p: locators) {
360                                         String dbPolicyName = p.scope + "/" + p.policyType + "_" + p.policyName;
361                                         if (policyVersion_s.containsKey(dbPolicyName)) {
362                                                 String filterPolicyName = dbPolicyName + ".xml";
363                                                 filteredPolicies.put(Paths.get(filterPolicyName), filterPolicyName);                                                    
364                                                 if (logger.isInfoEnabled())
365                                                         logger.info("Active Version Policy found in search: " + 
366                                                                         dbPolicyName + " -> " + filterPolicyName);
367                                         } else {
368                                                 if (logger.isInfoEnabled()) 
369                                                         logger.info("Inactive Version Policy found in search: " + dbPolicyName);                                                
370                                         }
371                                 }
372
373                                 if (filteredPolicies.isEmpty()) {
374                                         if (logger.isInfoEnabled()) {
375                                                 logger.info("No match has been found for active versions");
376                                         }
377                                         //AdminNotification.warn("No match has been found for active versions");
378                                         return;
379                                 }
380
381                                 //self.policyContainer.setFilter(self.filteredPolicies);
382                         }
383                         /*      self.policyContainer.refresh();*/
384                 }
385
386
387                 protected static void clSearchBody(String clPolicyType, String bodyField, String bodyValue,
388                                 ArrayList<Pair<ArrayList<String>, ArrayList<String>>> filter_s) {
389                         if (logger.isDebugEnabled())
390                                 logger.debug("ENTER: " + clPolicyType + ":" + bodyField + ":" + bodyValue);
391
392                         final ArrayList<String> clBodyField_s = new ArrayList<String>();
393                         final ArrayList<String> clBodyValue_s = new ArrayList<String>();
394
395                         if (clPolicyType == null || clPolicyType.isEmpty()) {
396                                 clBodyField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_Fault.name() + "_Body." + bodyField);
397                                 clBodyField_s.add("Policy.Body."+ ElkConnector.PolicyType.Config_PM.name() + "_Body." + bodyField);
398                                 clBodyValue_s.add(bodyValue);
399                         } else {
400                                 clBodyField_s.add("Policy.Body." + clPolicyType + "_Body." + bodyField);
401                                 clBodyValue_s.add(bodyValue);
402                         }
403                         filter_s.add(new Pair<ArrayList<String>, ArrayList<String>>(clBodyField_s, clBodyValue_s));
404                 }
405
406                 protected static void clSearchFilter(String clType, String clField, String clValue, 
407                                 ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s) {
408                         if (logger.isDebugEnabled())
409                                 logger.debug("ENTER: " + clType + ":" + clField + ":" + clValue);
410
411                         ArrayList<String> clSearchField_s = new ArrayList<String>();
412                         clSearchField_s.add("Policy.Body." + clType + "_Body." + clField);
413
414                         ArrayList<String> clSearchValue_s = new ArrayList<String>();
415                         clSearchValue_s.add(clValue);
416
417                         filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchField_s, clSearchValue_s));
418                 }
419
420                 public static ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
421                         if (type == null || type.isEmpty())
422                                 return PolicyIndexType.all;
423
424                         return PolicyIndexType.valueOf(type);
425                 }
426 }