JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / elk / client / PolicyElasticSearchController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.pap.xacml.rest.elk.client;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.JsonArray;
27
28 import io.searchbox.client.JestResult;
29
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.io.UnsupportedEncodingException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import org.json.JSONObject;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.adapters.SearchData;
45 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
46 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
47 import org.onap.policy.rest.adapter.PolicyRestAdapter;
48 import org.onap.policy.rest.dao.CommonClassDao;
49 import org.onap.policy.rest.jpa.ActionPolicyDict;
50 import org.onap.policy.rest.jpa.Attribute;
51 import org.onap.policy.rest.jpa.BrmsParamTemplate;
52 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
53 import org.onap.policy.rest.jpa.ClosedLoopSite;
54 import org.onap.policy.rest.jpa.DcaeUuid;
55 import org.onap.policy.rest.jpa.DecisionSettings;
56 import org.onap.policy.rest.jpa.DescriptiveScope;
57 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
58 import org.onap.policy.rest.jpa.MicroServiceLocation;
59 import org.onap.policy.rest.jpa.MicroServiceModels;
60 import org.onap.policy.rest.jpa.OnapName;
61 import org.onap.policy.rest.jpa.PEPOptions;
62 import org.onap.policy.rest.jpa.RiskType;
63 import org.onap.policy.rest.jpa.SafePolicyWarning;
64 import org.onap.policy.rest.jpa.TermList;
65 import org.onap.policy.rest.jpa.VNFType;
66 import org.onap.policy.rest.jpa.VSCLAction;
67 import org.onap.policy.rest.jpa.VarbindDictionary;
68 import org.onap.policy.utils.PolicyUtils;
69 import org.onap.policy.xacml.api.XACMLErrorConstants;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.stereotype.Controller;
72 import org.springframework.web.bind.annotation.RequestMapping;
73 import org.springframework.web.bind.annotation.RequestMethod;
74 import org.springframework.web.servlet.ModelAndView;
75
76 @Controller
77 @RequestMapping({"/"})
78 public class PolicyElasticSearchController {
79
80     private static final Logger LOGGER = FlexLogger.getLogger(PolicyElasticSearchController.class);
81
82     enum Mode {
83         attribute, onapName, actionPolicy, brmsParam, pepOptions, clSite, clService, clVarbind, clVnf, clVSCL, decision, fwTerm, msDCAEUUID, msConfigName, msLocation, msModels, psGroupPolicy, safeRisk, safePolicyWarning
84     }
85
86     protected static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
87         private static final long serialVersionUID = 1L;
88     };
89
90     private static CommonClassDao commonClassDao;
91     private static final String action = "action";
92     private static final String config = "config";
93     private static final String decision = "decision";
94     private static final String pholder = "pholder";
95     private static final String jsonBodyData = "jsonBodyData";
96     private static final String success = "success";
97
98     @Autowired
99     public PolicyElasticSearchController(CommonClassDao commonClassDao) {
100         PolicyElasticSearchController.commonClassDao = commonClassDao;
101     }
102
103     public PolicyElasticSearchController() {
104         super();
105     }
106
107     public ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
108         if (type == null || type.isEmpty()) {
109             return PolicyIndexType.all;
110         }
111         return PolicyIndexType.valueOf(type);
112     }
113
114     public boolean updateElk(PolicyRestAdapter policyData) {
115         boolean success = true;
116         try {
117             success = ElkConnector.singleton.update(policyData);
118             if (!success) {
119                 if (LOGGER.isWarnEnabled()) {
120                     LOGGER.warn("FAILURE to create ELK record created for " + policyData.getNewFileName());
121                 }
122             } else {
123                 if (LOGGER.isInfoEnabled()) {
124                     LOGGER.warn("SUCCESS creating ELK record created for " + policyData.getNewFileName());
125                 }
126             }
127         } catch (Exception e) {
128             LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
129             success = false;
130         }
131         return success;
132     }
133
134     public boolean deleteElk(PolicyRestAdapter policyData) {
135         boolean success = true;
136         try {
137             success = ElkConnector.singleton.delete(policyData);
138             if (!success) {
139                 if (LOGGER.isWarnEnabled()) {
140                     LOGGER.warn("FAILURE to delete ELK record created for " + policyData.getNewFileName());
141                 }
142             } else {
143                 if (LOGGER.isInfoEnabled()) {
144                     LOGGER.warn("SUCCESS deleting ELK record created for " + policyData.getNewFileName());
145                 }
146             }
147         } catch (Exception e) {
148             LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
149             success = false;
150         }
151         return success;
152     }
153
154     @RequestMapping(value = "/searchPolicy", method = RequestMethod.POST)
155     public void searchPolicy(HttpServletRequest request, HttpServletResponse response) {
156         try {
157             String message = "";
158             boolean result = false;
159             boolean policyResult = false;
160             boolean validationCheck = true;
161             ObjectMapper mapper = new ObjectMapper();
162             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
163             PolicyRestAdapter policyData = new PolicyRestAdapter();
164             PolicyElasticSearchController controller = new PolicyElasticSearchController();
165             Map<String, String> searchKeyValue = new HashMap<>();
166             List<String> policyList = new ArrayList<>();
167             if (request.getParameter("policyName") != null) {
168                 String policyName = request.getParameter("policyName");
169                 policyData.setNewFileName(policyName);
170                 if ("delete".equalsIgnoreCase(request.getParameter(action))) {
171                     result = controller.deleteElk(policyData);
172                 } else {
173                     result = controller.updateElk(policyData);
174                 }
175             }
176             if ("search".equalsIgnoreCase(request.getParameter(action))) {
177                 try {
178                     JsonNode root = mapper.readTree(request.getReader());
179                     SearchData searchData = mapper.readValue(root.get("searchdata").toString(), SearchData.class);
180
181                     String policyType = searchData.getPolicyType();
182
183                     String searchText = searchData.getQuery();
184                     String descriptivevalue = searchData.getDescriptiveScope();
185                     if (descriptivevalue != null) {
186                         DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao
187                                 .getEntityItem(DescriptiveScope.class, "descriptiveScopeName", descriptivevalue);
188                         if (dsSearch != null) {
189                             String[] descriptiveList = dsSearch.getSearch().split("AND");
190                             for (String keyValue : descriptiveList) {
191                                 String[] entry = keyValue.split(":");
192                                 if (searchData.getPolicyType() != null
193                                         && "closedLoop".equals(searchData.getPolicyType())) {
194                                     if (!PolicyUtils.policySpecialCharValidator(entry[1]).contains(success)) {
195                                         message =
196                                                 "The Descriptive Scope Dictionary value contains space and it is invalid for Search :   "
197                                                         + entry[1];
198                                         validationCheck = false;
199                                     }
200                                     searchKeyValue.put(jsonBodyData, "*" + entry[1] + "*");
201                                 } else {
202                                     searchText = entry[1];
203                                 }
204                             }
205                         }
206                     }
207
208                     if (!PolicyUtils.policySpecialCharValidator(searchText).contains(success)) {
209                         message = "The Search value contains space and it is invalid for Search :   " + searchText;
210                         validationCheck = false;
211                     }
212
213                     if (searchData.getClosedLooppolicyType() != null) {
214                         String closedLoopType;
215                         if ("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())) {
216                             closedLoopType = "ClosedLoop_Fault";
217                         } else {
218                             closedLoopType = "ClosedLoop_PM";
219                         }
220                         searchKeyValue.put("configPolicyType", closedLoopType);
221                     }
222                     if (searchData.getOnapName() != null) {
223                         searchKeyValue.put("onapName", searchData.getOnapName());
224                     }
225                     if (searchData.getD2Service() != null) {
226                         String d2Service = searchData.getD2Service().trim();
227                         if ("Hosted Voice (Trinity)".equalsIgnoreCase(d2Service)) {
228                             d2Service = "trinity";
229                         } else if ("vUSP".equalsIgnoreCase(d2Service)) {
230                             d2Service = "vUSP";
231                         } else if ("MCR".equalsIgnoreCase(d2Service)) {
232                             d2Service = "mcr";
233                         } else if ("Gamma".equalsIgnoreCase(d2Service)) {
234                             d2Service = "gamma";
235                         } else if ("vDNS".equalsIgnoreCase(d2Service)) {
236                             d2Service = "vDNS";
237                         }
238                         searchKeyValue.put("jsonBodyData." + d2Service + "", "true");
239                     }
240                     if (searchData.getVnfType() != null) {
241                         searchKeyValue.put(jsonBodyData, "*" + searchData.getVnfType() + "*");
242                     }
243                     if (searchData.getPolicyStatus() != null) {
244                         searchKeyValue.put(jsonBodyData, "*" + searchData.getPolicyStatus() + "*");
245                     }
246                     if (searchData.getVproAction() != null) {
247                         searchKeyValue.put(jsonBodyData, "*" + searchData.getVproAction() + "*");
248                     }
249                     if (searchData.getServiceType() != null) {
250                         searchKeyValue.put("serviceType", searchData.getServiceType());
251                     }
252                     if (searchData.getBindTextSearch() != null) {
253                         searchKeyValue.put(searchData.getBindTextSearch(), searchText);
254                         searchText = null;
255                     }
256                     PolicyIndexType type = null;
257                     if (policyType != null) {
258                         if (action.equalsIgnoreCase(policyType)) {
259                             type = ElkConnector.PolicyIndexType.action;
260                         } else if (decision.equalsIgnoreCase(policyType)) {
261                             type = ElkConnector.PolicyIndexType.decision;
262                         } else if (config.equalsIgnoreCase(policyType)) {
263                             type = ElkConnector.PolicyIndexType.config;
264                         } else if ("closedloop".equalsIgnoreCase(policyType)) {
265                             type = ElkConnector.PolicyIndexType.closedloop;
266                         } else {
267                             type = ElkConnector.PolicyIndexType.all;
268                         }
269                     } else {
270                         type = ElkConnector.PolicyIndexType.all;
271                     }
272                     if (validationCheck) {
273                         JestResult policyResultList = controller.search(type, searchText, searchKeyValue);
274                         if (policyResultList.isSucceeded()) {
275                             result = true;
276                             policyResult = true;
277                             JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject()
278                                     .get("hits").getAsJsonArray();
279                             for (int i = 0; i < resultObject.size(); i++) {
280                                 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
281                                 policyList.add(policyName);
282                             }
283                         } else {
284                             LOGGER.error(
285                                     "Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
286                         }
287                     }
288                 } catch (Exception e) {
289                     LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
290                 }
291             }
292             if (validationCheck) {
293                 if (result) {
294                     message = "Elastic Server Transaction is success";
295                 } else {
296                     message = "Elastic Server Transaction is failed, please check the logs";
297                 }
298             }
299             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(message));
300             JSONObject j = new JSONObject(msg);
301             response.setStatus(HttpServletResponse.SC_OK);
302             response.addHeader(success, success);
303             if (policyResult) {
304                 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
305                 response.getWriter().write(k.toString());
306             } else {
307                 response.getWriter().write(j.toString());
308             }
309         } catch (Exception e) {
310             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
311             response.addHeader("error", "Exception Occured While Performing Elastic Transaction");
312             LOGGER.error("Exception Occured While Performing Elastic Transaction" + e.getMessage(), e);
313         }
314     }
315
316     @RequestMapping(
317             value = {"/searchDictionary"},
318             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
319     public ModelAndView searchDictionary(HttpServletRequest request, HttpServletResponse response)
320             throws UnsupportedEncodingException, IOException {
321         try {
322             PolicyIndexType config = PolicyIndexType.config;
323             PolicyIndexType closedloop = PolicyIndexType.closedloop;
324             PolicyIndexType action = PolicyIndexType.action;
325             PolicyIndexType decision = PolicyIndexType.decision;
326             PolicyIndexType all = PolicyIndexType.all;
327
328             ObjectMapper mapper = new ObjectMapper();
329             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
330             JsonNode root = mapper.readTree(request.getReader());
331             String dictionaryType = root.get("type").textValue();
332             Mode mode = Mode.valueOf(dictionaryType);
333             String value;
334             List<String> policyList = new ArrayList<>();
335             switch (mode) {
336                 case attribute:
337                     Attribute attributedata = mapper.readValue(root.get("data").toString(), Attribute.class);
338                     value = attributedata.getXacmlId();
339                     policyList = searchElkDatabase(all, pholder, value);
340                     break;
341                 case onapName:
342                     OnapName onapName = mapper.readValue(root.get("data").toString(), OnapName.class);
343                     value = onapName.getOnapName();
344                     policyList = searchElkDatabase(all, "onapName", value);
345                     break;
346                 case actionPolicy:
347                     ActionPolicyDict actionPolicyDict =
348                             mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
349                     value = actionPolicyDict.getAttributeName();
350                     policyList = searchElkDatabase(action, "actionAttributeValue", value);
351                     break;
352                 case brmsParam:
353                     BrmsParamTemplate bRMSParamTemplate =
354                             mapper.readValue(root.get("data").toString(), BrmsParamTemplate.class);
355                     value = bRMSParamTemplate.getRuleName();
356                     policyList = searchElkDatabase(config, "ruleName", value);
357                     break;
358                 case pepOptions:
359                     PEPOptions pEPOptions = mapper.readValue(root.get("data").toString(), PEPOptions.class);
360                     value = pEPOptions.getPepName();
361                     policyList = searchElkDatabase(closedloop, "jsonBodyData.pepName", value);
362                     break;
363                 case clSite:
364                     ClosedLoopSite closedLoopSite = mapper.readValue(root.get("data").toString(), ClosedLoopSite.class);
365                     value = closedLoopSite.getSiteName();
366                     policyList = searchElkDatabase(closedloop, "siteNames", value);
367                     break;
368                 case clService:
369                     ClosedLoopD2Services closedLoopD2Services =
370                             mapper.readValue(root.get("data").toString(), ClosedLoopD2Services.class);
371                     value = closedLoopD2Services.getServiceName();
372                     policyList = searchElkDatabase(closedloop, pholder, value);
373                     break;
374                 case clVarbind:
375                     VarbindDictionary varbindDictionary =
376                             mapper.readValue(root.get("data").toString(), VarbindDictionary.class);
377                     value = varbindDictionary.getVarbindName();
378                     policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
379                     break;
380                 case clVnf:
381                     VNFType vNFType = mapper.readValue(root.get("data").toString(), VNFType.class);
382                     value = vNFType.getVnftype();
383                     policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
384                     break;
385                 case clVSCL:
386                     VSCLAction vsclAction = mapper.readValue(root.get("data").toString(), VSCLAction.class);
387                     value = vsclAction.getVsclaction();
388                     policyList = searchElkDatabase(closedloop, jsonBodyData, "*" + value + "*");
389                     break;
390                 case decision:
391                     DecisionSettings decisionSettings =
392                             mapper.readValue(root.get("data").toString(), DecisionSettings.class);
393                     value = decisionSettings.getXacmlId();
394                     policyList = searchElkDatabase(decision, pholder, value);
395                     break;
396                 case fwTerm:
397                     TermList term = mapper.readValue(root.get("data").toString(), TermList.class);
398                     value = term.getTermName();
399                     policyList = searchElkDatabase(config, pholder, value);
400                     break;
401                 case msDCAEUUID:
402                     DcaeUuid dcaeUUID = mapper.readValue(root.get("data").toString(), DcaeUuid.class);
403                     value = dcaeUUID.getName();
404                     policyList = searchElkDatabase(config, "uuid", value);
405                     break;
406                 case msLocation:
407                     MicroServiceLocation mslocation =
408                             mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
409                     value = mslocation.getName();
410                     policyList = searchElkDatabase(config, "location", value);
411                     break;
412                 case msModels:
413                     MicroServiceModels msModels =
414                             mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
415                     value = msModels.getModelName();
416                     policyList = searchElkDatabase(config, "serviceType", value);
417                     break;
418                 case psGroupPolicy:
419                     GroupPolicyScopeList groupPoilicy =
420                             mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
421                     value = groupPoilicy.getGroupName();
422                     policyList = searchElkDatabase(config, pholder, value);
423                     break;
424                 case safeRisk:
425                     RiskType riskType = mapper.readValue(root.get("data").toString(), RiskType.class);
426                     value = riskType.getRiskName();
427                     policyList = searchElkDatabase(config, "riskType", value);
428                     break;
429                 case safePolicyWarning:
430                     SafePolicyWarning safePolicy =
431                             mapper.readValue(root.get("data").toString(), SafePolicyWarning.class);
432                     value = safePolicy.getName();
433                     policyList = searchElkDatabase(config, pholder, value);
434                     break;
435                 default:
436             }
437
438             response.setStatus(HttpServletResponse.SC_OK);
439             response.addHeader(success, success);
440             JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
441             response.getWriter().write(k.toString());
442         } catch (Exception e) {
443             response.setCharacterEncoding("UTF-8");
444             request.setCharacterEncoding("UTF-8");
445             PrintWriter out = response.getWriter();
446             out.write(PolicyUtils.CATCH_EXCEPTION);
447             LOGGER.error(e);
448         }
449         return null;
450     }
451
452     // Search the Elk database
453     public List<String> searchElkDatabase(PolicyIndexType type, String key, String value) {
454         PolicyElasticSearchController controller = new PolicyElasticSearchController();
455         Map<String, String> searchKeyValue = new HashMap<>();
456         if (!pholder.equals(key)) {
457             searchKeyValue.put(key, value);
458         }
459
460         List<String> policyList = new ArrayList<>();
461         JestResult policyResultList = controller.search(type, value, searchKeyValue);
462         if (policyResultList.isSucceeded()) {
463             JsonArray resultObject =
464                     policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
465             for (int i = 0; i < resultObject.size(); i++) {
466                 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
467                 policyList.add(policyName);
468             }
469         } else {
470             LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
471         }
472         return policyList;
473     }
474
475     public JestResult search(PolicyIndexType type, String text, Map<String, String> searchKeyValue) {
476         return ElkConnector.singleton.search(type, text, searchKeyValue);
477     }
478
479 }