2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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=========================================================
20 package org.onap.policy.pap.xacml.rest.elk.client;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.json.JSONObject;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pap.xacml.rest.adapters.SearchData;
38 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
39 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
40 import org.onap.policy.rest.adapter.PolicyRestAdapter;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.ActionPolicyDict;
43 import org.onap.policy.rest.jpa.Attribute;
44 import org.onap.policy.rest.jpa.BRMSParamTemplate;
45 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
46 import org.onap.policy.rest.jpa.ClosedLoopSite;
47 import org.onap.policy.rest.jpa.DCAEuuid;
48 import org.onap.policy.rest.jpa.DecisionSettings;
49 import org.onap.policy.rest.jpa.DescriptiveScope;
50 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
51 import org.onap.policy.rest.jpa.MicroServiceLocation;
52 import org.onap.policy.rest.jpa.MicroServiceModels;
53 import org.onap.policy.rest.jpa.OnapName;
54 import org.onap.policy.rest.jpa.PEPOptions;
55 import org.onap.policy.rest.jpa.RiskType;
56 import org.onap.policy.rest.jpa.SafePolicyWarning;
57 import org.onap.policy.rest.jpa.TermList;
58 import org.onap.policy.rest.jpa.VNFType;
59 import org.onap.policy.rest.jpa.VSCLAction;
60 import org.onap.policy.rest.jpa.VarbindDictionary;
61 import org.onap.policy.utils.PolicyUtils;
62 import org.onap.policy.xacml.api.XACMLErrorConstants;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.stereotype.Controller;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.servlet.ModelAndView;
69 import com.fasterxml.jackson.databind.DeserializationFeature;
70 import com.fasterxml.jackson.databind.JsonNode;
71 import com.fasterxml.jackson.databind.ObjectMapper;
72 import com.google.gson.JsonArray;
74 import io.searchbox.client.JestResult;
77 @RequestMapping({"/"})
78 public class PolicyElasticSearchController{
80 private static final Logger LOGGER = FlexLogger.getLogger(PolicyElasticSearchController.class);
83 attribute, onapName, actionPolicy, brmsParam, pepOptions,
84 clSite, clService, clVarbind, clVnf, clVSCL, decision,
85 fwTerm, msDCAEUUID, msConfigName, msLocation, msModels,
86 psGroupPolicy, safeRisk, safePolicyWarning
89 protected static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
90 private static final long serialVersionUID = 1L;
93 private static CommonClassDao commonClassDao;
94 private static final String action = "action";
95 private static final String config = "config";
96 private static final String decision = "decision";
97 private static final String pholder = "pholder";
98 private static final String jsonBodyData = "jsonBodyData";
99 private static final String success = "success";
102 public PolicyElasticSearchController(CommonClassDao commonClassDao) {
103 PolicyElasticSearchController.commonClassDao = commonClassDao;
106 public PolicyElasticSearchController() {
110 public ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
111 if (type == null || type.isEmpty()){
112 return PolicyIndexType.all;
114 return PolicyIndexType.valueOf(type);
117 public boolean updateElk(PolicyRestAdapter policyData) {
118 boolean success = true;
120 success = ElkConnector.singleton.update(policyData);
122 if (LOGGER.isWarnEnabled()) {
123 LOGGER.warn("FAILURE to create ELK record created for " + policyData.getNewFileName());
126 if (LOGGER.isInfoEnabled()) {
127 LOGGER.warn("SUCCESS creating ELK record created for " + policyData.getNewFileName());
130 } catch (Exception e) {
131 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
137 public boolean deleteElk(PolicyRestAdapter policyData) {
138 boolean success = true;
140 success = ElkConnector.singleton.delete(policyData);
142 if (LOGGER.isWarnEnabled()) {
143 LOGGER.warn("FAILURE to delete ELK record created for " + policyData.getNewFileName());
146 if (LOGGER.isInfoEnabled()) {
147 LOGGER.warn("SUCCESS deleting ELK record created for " + policyData.getNewFileName());
150 } catch (Exception e) {
151 LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
158 @RequestMapping(value="/searchPolicy", method= RequestMethod.POST)
159 public void searchPolicy(HttpServletRequest request, HttpServletResponse response) {
162 boolean result = false;
163 boolean policyResult = false;
164 boolean validationCheck = true;
165 ObjectMapper mapper = new ObjectMapper();
166 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
167 PolicyRestAdapter policyData = new PolicyRestAdapter();
168 PolicyElasticSearchController controller = new PolicyElasticSearchController();
169 Map<String, String> searchKeyValue = new HashMap<>();
170 List<String> policyList = new ArrayList<>();
171 if(request.getParameter("policyName") != null){
172 String policyName = request.getParameter("policyName");
173 policyData.setNewFileName(policyName);
174 if("delete".equalsIgnoreCase(request.getParameter(action))){
175 result = controller.deleteElk(policyData);
177 result = controller.updateElk(policyData);
180 if("search".equalsIgnoreCase(request.getParameter(action))){
182 JsonNode root = mapper.readTree(request.getReader());
183 SearchData searchData = mapper.readValue(root.get("searchdata").toString(), SearchData.class);
185 String policyType = searchData.getPolicyType();
187 String searchText = searchData.getQuery();
188 String descriptivevalue = searchData.getDescriptiveScope();
189 if(descriptivevalue != null){
190 DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao.getEntityItem(DescriptiveScope.class, "descriptiveScopeName", descriptivevalue);
191 if(dsSearch != null){
192 String[] descriptiveList = dsSearch.getSearch().split("AND");
193 for(String keyValue : descriptiveList){
194 String[] entry = keyValue.split(":");
195 if(searchData.getPolicyType() != null && "closedLoop".equals(searchData.getPolicyType())){
196 if(!PolicyUtils.policySpecialCharValidator(entry[1]).contains(success)){
197 message = "The Descriptive Scope Dictionary value contains space and it is invalid for Search : "+entry[1];
198 validationCheck = false;
200 searchKeyValue.put(jsonBodyData, "*" +entry[1] +"*");
202 searchText = entry[1];
208 if(!PolicyUtils.policySpecialCharValidator(searchText).contains(success)){
209 message = "The Search value contains space and it is invalid for Search : "+searchText;
210 validationCheck = false;
213 if(searchData.getClosedLooppolicyType() != null){
214 String closedLoopType;
215 if("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())){
216 closedLoopType = "ClosedLoop_Fault";
218 closedLoopType = "ClosedLoop_PM";
220 searchKeyValue.put("configPolicyType", closedLoopType);
222 if(searchData.getOnapName() != null){
223 searchKeyValue.put("onapName", searchData.getOnapName());
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)){
231 }else if("MCR".equalsIgnoreCase(d2Service)){
233 }else if("Gamma".equalsIgnoreCase(d2Service)){
235 }else if("vDNS".equalsIgnoreCase(d2Service)){
238 searchKeyValue.put("jsonBodyData."+d2Service+"", "true");
240 if(searchData.getVnfType() != null){
241 searchKeyValue.put(jsonBodyData, "*"+searchData.getVnfType()+"*");
243 if(searchData.getPolicyStatus() != null){
244 searchKeyValue.put(jsonBodyData, "*"+searchData.getPolicyStatus()+"*");
246 if(searchData.getVproAction() != null){
247 searchKeyValue.put(jsonBodyData, "*"+searchData.getVproAction()+"*");
249 if(searchData.getServiceType() != null){
250 searchKeyValue.put("serviceType", searchData.getServiceType());
252 if(searchData.getBindTextSearch() != null){
253 searchKeyValue.put(searchData.getBindTextSearch(), searchText);
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;
267 type = ElkConnector.PolicyIndexType.all;
270 type = ElkConnector.PolicyIndexType.all;
273 JestResult policyResultList = controller.search(type, searchText, searchKeyValue);
274 if(policyResultList.isSucceeded()){
277 JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
278 for(int i =0; i < resultObject.size(); i++){
279 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
280 policyList.add(policyName);
283 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
287 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
292 message = "Elastic Server Transaction is success";
294 message = "Elastic Server Transaction is failed, please check the logs";
297 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(message));
298 JSONObject j = new JSONObject(msg);
299 response.setStatus(HttpServletResponse.SC_OK);
300 response.addHeader(success, success);
302 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
303 response.getWriter().write(k.toString());
305 response.getWriter().write(j.toString());
308 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
309 response.addHeader("error", "Exception Occured While Performing Elastic Transaction");
310 LOGGER.error("Exception Occured While Performing Elastic Transaction"+e.getMessage(),e);
314 @RequestMapping(value={"/searchDictionary"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
315 public ModelAndView searchDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
317 PolicyIndexType config = PolicyIndexType.config;
318 PolicyIndexType closedloop = PolicyIndexType.closedloop;
319 PolicyIndexType action = PolicyIndexType.action;
320 PolicyIndexType decision = PolicyIndexType.decision;
321 PolicyIndexType all = PolicyIndexType.all;
323 ObjectMapper mapper = new ObjectMapper();
324 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
325 JsonNode root = mapper.readTree(request.getReader());
326 String dictionaryType = root.get("type").textValue();
327 Mode mode = Mode.valueOf(dictionaryType);
329 List<String> policyList = new ArrayList<>();
332 Attribute attributedata = mapper.readValue(root.get("data").toString(), Attribute.class);
333 value = attributedata.getXacmlId();
334 policyList = searchElkDatabase(all, pholder,value);
337 OnapName onapName = mapper.readValue(root.get("data").toString(), OnapName.class);
338 value = onapName.getOnapName();
339 policyList = searchElkDatabase(all, "onapName",value);
342 ActionPolicyDict actionPolicyDict = mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
343 value = actionPolicyDict.getAttributeName();
344 policyList = searchElkDatabase(action, "actionAttributeValue",value);
347 BRMSParamTemplate bRMSParamTemplate = mapper.readValue(root.get("data").toString(), BRMSParamTemplate.class);
348 value = bRMSParamTemplate.getRuleName();
349 policyList = searchElkDatabase(config, "ruleName",value);
352 PEPOptions pEPOptions = mapper.readValue(root.get("data").toString(), PEPOptions.class);
353 value = pEPOptions.getPepName();
354 policyList = searchElkDatabase(closedloop,"jsonBodyData.pepName",value);
357 ClosedLoopSite closedLoopSite = mapper.readValue(root.get("data").toString(), ClosedLoopSite.class);
358 value = closedLoopSite.getSiteName();
359 policyList = searchElkDatabase(closedloop,"siteNames",value);
362 ClosedLoopD2Services closedLoopD2Services = mapper.readValue(root.get("data").toString(), ClosedLoopD2Services.class);
363 value = closedLoopD2Services.getServiceName();
364 policyList = searchElkDatabase(closedloop, pholder,value);
367 VarbindDictionary varbindDictionary = mapper.readValue(root.get("data").toString(), VarbindDictionary.class);
368 value = varbindDictionary.getVarbindName();
369 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
372 VNFType vNFType = mapper.readValue(root.get("data").toString(), VNFType.class);
373 value = vNFType.getVnftype();
374 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
377 VSCLAction vsclAction = mapper.readValue(root.get("data").toString(), VSCLAction.class);
378 value = vsclAction.getVsclaction();
379 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
382 DecisionSettings decisionSettings = mapper.readValue(root.get("data").toString(), DecisionSettings.class);
383 value = decisionSettings.getXacmlId();
384 policyList = searchElkDatabase(decision,pholder,value);
387 TermList term = mapper.readValue(root.get("data").toString(), TermList.class);
388 value = term.getTermName();
389 policyList = searchElkDatabase(config, pholder,value);
392 DCAEuuid dcaeUUID = mapper.readValue(root.get("data").toString(), DCAEuuid.class);
393 value = dcaeUUID.getName();
394 policyList = searchElkDatabase(config, "uuid",value);
397 MicroServiceLocation mslocation = mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
398 value = mslocation.getName();
399 policyList = searchElkDatabase(config, "location",value);
402 MicroServiceModels msModels = mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
403 value = msModels.getModelName();
404 policyList = searchElkDatabase(config, "serviceType",value);
407 GroupPolicyScopeList groupPoilicy = mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
408 value = groupPoilicy.getGroupName();
409 policyList = searchElkDatabase(config, pholder,value);
412 RiskType riskType= mapper.readValue(root.get("data").toString(), RiskType.class);
413 value = riskType.getRiskName();
414 policyList = searchElkDatabase(config, "riskType",value);
416 case safePolicyWarning :
417 SafePolicyWarning safePolicy = mapper.readValue(root.get("data").toString(), SafePolicyWarning.class);
418 value = safePolicy.getName();
419 policyList = searchElkDatabase(config, pholder,value);
424 response.setStatus(HttpServletResponse.SC_OK);
425 response.addHeader(success, success);
426 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
427 response.getWriter().write(k.toString());
429 response.setCharacterEncoding("UTF-8");
430 request.setCharacterEncoding("UTF-8");
431 PrintWriter out = response.getWriter();
432 out.write(PolicyUtils.CATCH_EXCEPTION);
438 //Search the Elk database
439 public List<String> searchElkDatabase(PolicyIndexType type, String key, String value){
440 PolicyElasticSearchController controller = new PolicyElasticSearchController();
441 Map<String, String> searchKeyValue = new HashMap<>();
442 if(!pholder.equals(key)){
443 searchKeyValue.put(key, value);
446 List<String> policyList = new ArrayList<>();
447 JestResult policyResultList = controller.search(type, value, searchKeyValue);
448 if(policyResultList.isSucceeded()){
449 JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
450 for(int i =0; i < resultObject.size(); i++){
451 String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString();
452 policyList.add(policyName);
455 LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
460 public JestResult search(PolicyIndexType type, String text, Map<String, String> searchKeyValue) {
461 return ElkConnector.singleton.search(type, text, searchKeyValue);