a8320093eaaf1bfd0bb09ebeec62dced04f268a9
[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-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
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 package org.onap.policy.pap.xacml.rest.elk.client;
21
22
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;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
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;
68
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;
73
74 import io.searchbox.client.JestResult;
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,
84                 clSite, clService, clVarbind, clVnf, clVSCL, decision, 
85                 fwTerm, msDCAEUUID, msConfigName, msLocation, msModels,
86                 psGroupPolicy, safeRisk, safePolicyWarning
87         }
88
89         protected static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() {
90                 private static final long serialVersionUID = 1L;
91         };
92         
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";
100
101         @Autowired
102         public PolicyElasticSearchController(CommonClassDao commonClassDao) {
103                 PolicyElasticSearchController.commonClassDao = commonClassDao;
104         }
105
106         public PolicyElasticSearchController() {
107                 super();
108         }
109
110         public ElkConnector.PolicyIndexType toPolicyIndexType(String type) throws IllegalArgumentException {
111                 if (type == null || type.isEmpty()){
112                         return PolicyIndexType.all;
113                 }
114                 return PolicyIndexType.valueOf(type);
115         }
116
117         public boolean updateElk(PolicyRestAdapter policyData) {
118                 boolean success = true;
119                 try {
120                         success = ElkConnector.singleton.update(policyData);
121                         if (!success) {
122                                 if (LOGGER.isWarnEnabled()) {
123                                         LOGGER.warn("FAILURE to create ELK record created for " + policyData.getNewFileName());
124                                 }
125                         } else {
126                                 if (LOGGER.isInfoEnabled()) {
127                                         LOGGER.warn("SUCCESS creating ELK record created for " + policyData.getNewFileName());
128                                 }                                                                       
129                         }                                                                       
130                 } catch (Exception e) {
131                         LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
132                         success = false;
133                 }
134                 return success;
135         }
136
137         public boolean deleteElk(PolicyRestAdapter policyData) {
138                 boolean success = true;
139                 try {
140                         success = ElkConnector.singleton.delete(policyData);
141                         if (!success) {
142                                 if (LOGGER.isWarnEnabled()) {
143                                         LOGGER.warn("FAILURE to delete ELK record created for " + policyData.getNewFileName());
144                                 }
145                         } else {
146                                 if (LOGGER.isInfoEnabled()) {
147                                         LOGGER.warn("SUCCESS deleting ELK record created for " + policyData.getNewFileName());
148                                 }                                                                       
149                         }                                                                       
150                 } catch (Exception e) {
151                         LOGGER.warn(XACMLErrorConstants.ERROR_DATA_ISSUE + ": " + e.getMessage(), e);
152                         success = false;
153                 }
154                 return success;
155         }
156
157         
158         @RequestMapping(value="/searchPolicy", method= RequestMethod.POST)
159         public void searchPolicy(HttpServletRequest request, HttpServletResponse response) {
160                 try{
161                         String message="";
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);
176                                 }else{
177                                         result = controller.updateElk(policyData);
178                                 }
179                         }
180                         if("search".equalsIgnoreCase(request.getParameter(action))){
181                                 try {
182                                         JsonNode root = mapper.readTree(request.getReader());
183                                         SearchData searchData = mapper.readValue(root.get("searchdata").toString(), SearchData.class);
184
185                                         String policyType = searchData.getPolicyType();
186                                         
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;
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().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);
281                                                         }
282                                                 }else{
283                                                         LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
284                                                 }
285                                         }
286                                 }catch(Exception e){
287                                         LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e);
288                                 }
289                         }
290                         if(validationCheck){
291                                 if(result){
292                                         message = "Elastic Server Transaction is success";
293                                 }else{
294                                         message = "Elastic Server Transaction is failed, please check the logs";
295                                 }
296                         }
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); 
301                         if(policyResult){
302                                 JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
303                                 response.getWriter().write(k.toString());
304                         }else{
305                                 response.getWriter().write(j.toString());
306                         }
307                 }catch(Exception e){
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);
311                 }
312         }
313         
314         @RequestMapping(value={"/searchDictionary"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
315         public ModelAndView searchDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
316                 try{
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;
322                         
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);
328                         String value; 
329                         List<String> policyList = new ArrayList<>();
330                         switch (mode){
331                         case attribute :
332                                 Attribute attributedata = mapper.readValue(root.get("data").toString(), Attribute.class);
333                                 value = attributedata.getXacmlId();
334                                 policyList = searchElkDatabase(all, pholder,value);
335                                 break;
336                         case onapName :
337                                 OnapName onapName = mapper.readValue(root.get("data").toString(), OnapName.class);
338                                 value = onapName.getOnapName();
339                                 policyList = searchElkDatabase(all, "onapName",value);
340                                 break;
341                         case actionPolicy :
342                                 ActionPolicyDict actionPolicyDict = mapper.readValue(root.get("data").toString(), ActionPolicyDict.class);
343                                 value = actionPolicyDict.getAttributeName();
344                                 policyList = searchElkDatabase(action, "actionAttributeValue",value);
345                                 break;
346                         case brmsParam :
347                                 BRMSParamTemplate bRMSParamTemplate = mapper.readValue(root.get("data").toString(), BRMSParamTemplate.class);
348                                 value = bRMSParamTemplate.getRuleName();
349                                 policyList = searchElkDatabase(config, "ruleName",value);
350                                 break;
351                         case pepOptions :
352                                 PEPOptions pEPOptions = mapper.readValue(root.get("data").toString(), PEPOptions.class);
353                                 value = pEPOptions.getPepName();
354                                 policyList = searchElkDatabase(closedloop,"jsonBodyData.pepName",value);
355                                 break;
356                         case clSite :
357                                 ClosedLoopSite closedLoopSite = mapper.readValue(root.get("data").toString(), ClosedLoopSite.class);
358                                 value = closedLoopSite.getSiteName();
359                                 policyList = searchElkDatabase(closedloop,"siteNames",value);
360                                 break;
361                         case clService :
362                                 ClosedLoopD2Services closedLoopD2Services = mapper.readValue(root.get("data").toString(), ClosedLoopD2Services.class);
363                                 value = closedLoopD2Services.getServiceName();
364                                 policyList = searchElkDatabase(closedloop, pholder,value);
365                                 break;
366                         case clVarbind :
367                                 VarbindDictionary varbindDictionary = mapper.readValue(root.get("data").toString(), VarbindDictionary.class);
368                                 value = varbindDictionary.getVarbindName();
369                                 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
370                                 break;
371                         case clVnf :
372                                 VNFType vNFType = mapper.readValue(root.get("data").toString(), VNFType.class);
373                                 value = vNFType.getVnftype();
374                                 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
375                                 break;
376                         case clVSCL :
377                                 VSCLAction vsclAction = mapper.readValue(root.get("data").toString(), VSCLAction.class);
378                                 value = vsclAction.getVsclaction();
379                                 policyList = searchElkDatabase(closedloop, jsonBodyData,"*"+value+"*");
380                                 break;
381                         case decision :
382                                 DecisionSettings decisionSettings = mapper.readValue(root.get("data").toString(), DecisionSettings.class);
383                                 value = decisionSettings.getXacmlId();
384                                 policyList = searchElkDatabase(decision,pholder,value);
385                                 break;  
386                         case fwTerm :
387                                 TermList term = mapper.readValue(root.get("data").toString(), TermList.class);
388                                 value = term.getTermName();
389                                 policyList = searchElkDatabase(config, pholder,value);
390                                 break;
391                         case msDCAEUUID :
392                                 DCAEuuid dcaeUUID = mapper.readValue(root.get("data").toString(), DCAEuuid.class);
393                                 value = dcaeUUID.getName();
394                                 policyList = searchElkDatabase(config, "uuid",value);
395                                 break;
396                         case msLocation :
397                                 MicroServiceLocation mslocation = mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
398                                 value = mslocation.getName();
399                                 policyList = searchElkDatabase(config, "location",value);
400                                 break;
401                         case msModels :
402                                 MicroServiceModels msModels = mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
403                                 value = msModels.getModelName();
404                                 policyList = searchElkDatabase(config, "serviceType",value);
405                                 break;
406                         case psGroupPolicy :
407                                 GroupPolicyScopeList groupPoilicy = mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
408                                 value = groupPoilicy.getGroupName();
409                                 policyList = searchElkDatabase(config, pholder,value);
410                                 break;
411                         case safeRisk :
412                                 RiskType riskType= mapper.readValue(root.get("data").toString(), RiskType.class);
413                                 value = riskType.getRiskName();
414                                 policyList = searchElkDatabase(config, "riskType",value);
415                                 break;
416                         case safePolicyWarning :
417                                 SafePolicyWarning safePolicy = mapper.readValue(root.get("data").toString(), SafePolicyWarning.class);
418                                 value = safePolicy.getName();
419                                 policyList = searchElkDatabase(config, pholder,value);
420                                 break;
421                         default:                
422                         }
423                         
424                         response.setStatus(HttpServletResponse.SC_OK);
425                         response.addHeader(success, success); 
426                         JSONObject k = new JSONObject("{policyresult: " + policyList + "}");
427                         response.getWriter().write(k.toString());
428                 }catch(Exception e){
429                         response.setCharacterEncoding("UTF-8");
430                         request.setCharacterEncoding("UTF-8");
431                         PrintWriter out = response.getWriter();
432                         out.write(PolicyUtils.CATCH_EXCEPTION);
433                         LOGGER.error(e);
434                 }
435                 return null;
436         }
437
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);
444                 }
445                 
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);
453                         }
454                 }else{
455                         LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs");
456                 }
457                 return policyList;
458         }
459         
460         public JestResult search(PolicyIndexType type, String text, Map<String, String> searchKeyValue) {
461                  return ElkConnector.singleton.search(type, text, searchKeyValue);
462         }
463         
464 }