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.components;
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;
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;
40 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
41 import org.openecomp.policy.common.logging.flexlogger.Logger;
44 public class ElasticSearchComponent {
45 private static final Logger logger = FlexLogger.getLogger(ElasticSearchComponent.class);
48 private static PolicyVersionDao policyVersionDao;
49 private static DescriptiveScopeDao descriptiveScopeDao;
52 public ElasticSearchComponent(PolicyVersionDao policyVersionDao){
53 ElasticSearchComponent.policyVersionDao = policyVersionDao;
57 public ElasticSearchComponent(DescriptiveScopeDao descriptiveScopeDao){
58 ElasticSearchComponent.descriptiveScopeDao = descriptiveScopeDao;
62 private volatile static HashMap<Path, String> filteredPolicies = new HashMap<Path, String>();
64 public static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
65 private static final long serialVersionUID = 1L;
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);
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);
79 put(CLFAULT_UIFIELD_POLICY_STATUS_INACTIVE, CLFAULT_UIJSON_POLICY_STATUS_ACTIVE);
80 put(CLFAULT_UIFIELD_POLICY_STATUS_ACTIVE, CLFAULT_UIJSON_POLICY_STATUS_INACTIVE);
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);
88 //For AND and OR logical connector AND=0 and OR=1
89 private static int connectorSelected;
91 public static final String CLFAULT_UIFIELD_D2_SERVICES_TRINITY = "Hosted Voice (Trinity)";
92 public static final String CLFAULT_UIJSON_D2_SERVICES_TRINITY = "trinity";
94 public static final String CLFAULT_UIFIELD_D2_SERVICES_VUSP = "vUSP";
95 public static final String CLFAULT_UIJSON_D2_SERVICES_VUSP = "vUSP";
97 public static final String CLFAULT_UIFIELD_D2_SERVICES_MCR = "MCR";
98 public static final String CLFAULT_UIJSON_D2_SERVICES_MCR = "mcr";
100 public static final String CLFAULT_UIFIELD_D2_SERVICES_GAMMA = "Gamma";
101 public static final String CLFAULT_UIJSON_D2_SERVICES_GAMMA = "gama";
103 public static final String CLFAULT_UIFIELD_D2_SERVICES_VDNS = "vDNS";
104 public static final String CLFAULT_UIJSON_D2_SERVICES_VDNS = "vDNS";
106 public static final String CLFAULT_UIFIELD_EMAIL_ADDRESS = "Email Address";
107 public static final String CLFAULT_UIJSON_EMAIL_ADDRESS = "emailAddress";
109 public static final String CLFAULT_UIFIELD_TRIGGER_SIGNATURE = "Trigger Signature";
110 public static final String CLFAULT_UIJSON_TRIGGER_SIGNATURE = "triggerSignaturesUsedForUI.signatures";
112 public static final String CLFAULT_UIFIELD_VERIFICATION_SIGNATURE = "Verification Signature";
113 public static final String CLFAULT_UIJSON_VERIFICATION_SIGNATURE = "verificationSignaturesUsedForUI.signatures";
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";
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";
121 public static final String CLFAULT_UIFIELD_POLICY_STATUS_ACTIVE = "Active";
122 public static final String CLFAULT_UIJSON_POLICY_STATUS_ACTIVE = "ACTIVE";
124 public static final String CLFAULT_UIFIELD_POLICY_STATUS_INACTIVE = "InActive";
125 public static final String CLFAULT_UIJSON_POLICY_STATUS_INACTIVE = "INACTIVE";
128 public static final String CLPM_UIFIELD_ONSET_MESSAGE = "Onset Message";
129 public static final String CLPM_UIJSON_ONSET_MESSAGE = "attributes.OnsetMessage";
131 public static final String CLPM_UIFIELD_POLICY_NAME = "PolicyName";
132 public static final String CLPM_UIJSON_POLICY_NAME = "attributes.PolicyName";
134 public static final String CLPM_UIFIELD_ABATEMENT_MESSAGE = "Abatement Message";
135 public static final String CLPM_UIJSON_ABATEMENT_MESSAGE = "attributes.AbatementMessage";
137 public static final String CLPM_UIFIELD_GEOLINK = "Geo Link";
138 public static final String CLPM_UIJSON_GEOLINK = "geoLink";
140 public static void search(String value){
141 String policyType = "all";//(String) self.searchPolicyType.getValue()
143 ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s = new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>();
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.");
155 searchText = searchText.trim();
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.
162 if(searchText.contains("Descriptive-Scope="))
164 if (logger.isDebugEnabled()) {
165 logger.debug("Inside the Descriptive Scope");
168 First item is always String "Descriptive-Scope" before the "=",
169 So taking the second item of "split using ="
171 String[] dsName= searchText.split("=",2);
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.
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 );
187 if(searchText.contains(":"))
189 String connector="&";
191 if(searchText.contains("&"))
196 else if(searchText.contains("|"))
198 connector=Pattern.quote("|");
201 for (String retval: searchText.split(connector)){
203 int index= retval.indexOf(':');
204 String filterKey=null;
205 String filterValue=null;
207 filterKey=retval.substring(0,index);
208 filterValue= retval.substring(index+1);
210 String clSearchBoxFilter=filterKey;
212 ArrayList<String> clSearchBoxFilterField_s = new ArrayList<String>();
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);
220 ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>();
221 clSearchBoxFilterValue_s.add(filterValue);
223 filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s));
228 if (policyType != null && !policyType.isEmpty() &&
229 policyType.equals(ElkConnector.PolicyIndexType.closedloop.toString())) {
231 /* closed loop policy type */
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");
238 ArrayList<String> clPolicyTypeValue_s = new ArrayList<String>();
239 clPolicyTypeValue_s.add(clPolicyType);
241 filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clPolicyTypeField_s, clPolicyTypeValue_s));
244 String clEcompName = "";//(String) self.cbSearchCLEcompName.getValue()
245 if (clEcompName != null && !clEcompName.isEmpty()) {
246 clSearchBody(clPolicyType, "ecompname", clEcompName, filter_s);
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);
260 if (logger.isWarnEnabled())
261 logger.warn("Unexpected D2 Service: " + clD2Services);
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);
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);
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);
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);
294 String clSearchBoxFilter = "";//(String) self.cbSearchCLTextFilter.getValue()
295 if (clSearchBoxFilter != null && !clSearchBoxFilter.isEmpty() &&
296 searchText != null && !searchText.isEmpty()) {
298 if (name2jsonPath.containsKey(clSearchBoxFilter)) {
299 clSearchBoxFilter = name2jsonPath.get(clSearchBoxFilter);
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);
307 clSearchBoxFilterField_s.add("Policy.Body." + clPolicyType + "_Body." + clSearchBoxFilter);
310 ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>();
311 clSearchBoxFilterValue_s.add(searchText);
313 filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s));
315 // deactivate search all fields in case a searchbox filter is provided
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");
329 ArrayList<PolicyLocator> locators;
331 locators = ElkConnector.singleton.policyLocators(toPolicyIndexType(policyType),
332 searchText, filter_s,connectorSelected);
333 } catch (Exception ise) {
334 /*AdminNotification.warn("Search is unavailable: " + ise.getMessage());*/
338 synchronized(filteredPolicies) {
339 if (locators.isEmpty()) {
340 if (logger.isInfoEnabled()) {
341 logger.info("No match has been found");
343 //AdminNotification.warn("No match has been found");
347 // Retrieve active versions
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);
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);
368 if (logger.isInfoEnabled())
369 logger.info("Inactive Version Policy found in search: " + dbPolicyName);
373 if (filteredPolicies.isEmpty()) {
374 if (logger.isInfoEnabled()) {
375 logger.info("No match has been found for active versions");
377 //AdminNotification.warn("No match has been found for active versions");
381 //self.policyContainer.setFilter(self.filteredPolicies);
383 /* self.policyContainer.refresh();*/
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);
392 final ArrayList<String> clBodyField_s = new ArrayList<String>();
393 final ArrayList<String> clBodyValue_s = new ArrayList<String>();
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);
400 clBodyField_s.add("Policy.Body." + clPolicyType + "_Body." + bodyField);
401 clBodyValue_s.add(bodyValue);
403 filter_s.add(new Pair<ArrayList<String>, ArrayList<String>>(clBodyField_s, clBodyValue_s));
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);
411 ArrayList<String> clSearchField_s = new ArrayList<String>();
412 clSearchField_s.add("Policy.Body." + clType + "_Body." + clField);
414 ArrayList<String> clSearchValue_s = new ArrayList<String>();
415 clSearchValue_s.add(clValue);
417 filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchField_s, clSearchValue_s));
420 public static ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
421 if (type == null || type.isEmpty())
422 return PolicyIndexType.all;
424 return PolicyIndexType.valueOf(type);