a669d82a0fb1f0f0485275fabe9c5954a8459862
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / BRMSDictionaryController.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.controller;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.io.IOException;
28 import java.io.StringWriter;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Date;
31 import java.util.List;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.apache.commons.io.IOUtils;
37 import org.onap.policy.api.PEDependency;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.pap.xacml.rest.components.CreateBRMSRuleTemplate;
41 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.BRMSController;
44 import org.onap.policy.rest.jpa.BRMSDependency;
45 import org.onap.policy.rest.jpa.BRMSParamTemplate;
46 import org.onap.policy.rest.jpa.OnapName;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.onap.policy.utils.PolicyUtils;
49 import org.onap.policy.xacml.api.XACMLErrorConstants;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.http.MediaType;
52 import org.springframework.stereotype.Controller;
53 import org.springframework.web.bind.annotation.RequestMapping;
54 import org.springframework.web.bind.annotation.RequestMethod;
55 import org.springframework.web.servlet.ModelAndView;
56
57 @Controller
58 public class BRMSDictionaryController {
59
60     private static final Logger LOGGER = FlexLogger.getLogger(BRMSDictionaryController.class);
61
62     private static final String VALIDATIONRESPONSE = "Validation";
63
64     private static CommonClassDao commonClassDao;
65     private static String rule;
66     private static String successMsg = "Success";
67     private static String duplicateResponseString = "Duplicate";
68     private static String ruleName = "ruleName";
69     private static String errorMessage = "Error";
70     private static String operation = "operation";
71     private static String dictionaryFields = "dictionaryFields";
72     private static String userid = "userid";
73     private static String dependencyName = "dependencyName";
74     private static String controllerName = "controllerName";
75     private static String brmsParamDatas = "brmsParamDictionaryDatas";
76     private static String brmsDependencyDatas = "brmsDependencyDictionaryDatas";
77     private static String brmsControllerDatas = "brmsControllerDictionaryDatas";
78
79     @Autowired
80     public BRMSDictionaryController(CommonClassDao commonClassDao) {
81         BRMSDictionaryController.commonClassDao = commonClassDao;
82     }
83
84     public static void setCommonClassDao(CommonClassDao commonClassDao2) {
85         BRMSDictionaryController.commonClassDao = commonClassDao2;
86     }
87
88     public BRMSDictionaryController() {
89         super();
90     }
91
92     private DictionaryUtils getDictionaryUtilsInstance() {
93         return DictionaryUtils.getDictionaryUtils();
94     }
95
96     @RequestMapping(
97             value = {"/get_BRMSParamDataByName"},
98             method = {RequestMethod.GET},
99             produces = MediaType.APPLICATION_JSON_VALUE)
100     public void getBRMSParamDictionaryByNameEntityData(HttpServletResponse response) {
101         DictionaryUtils utils = getDictionaryUtilsInstance();
102         utils.getDataByEntity(response, brmsParamDatas, ruleName, BRMSParamTemplate.class);
103     }
104
105     @RequestMapping(
106             value = {"/get_BRMSParamData"},
107             method = {RequestMethod.GET},
108             produces = MediaType.APPLICATION_JSON_VALUE)
109     public void getBRMSParamDictionaryEntityData(HttpServletResponse response) {
110         DictionaryUtils utils = getDictionaryUtilsInstance();
111         utils.getData(response, brmsParamDatas, BRMSParamTemplate.class);
112     }
113
114     @RequestMapping(value = {"/brms_dictionary/set_BRMSParamData"}, method = {RequestMethod.POST})
115     public static void setRuleData(HttpServletRequest request) throws IOException {
116         StringWriter writer = new StringWriter();
117         IOUtils.copy(request.getInputStream(), writer, StandardCharsets.UTF_8);
118         String cleanStreamBoundary = writer.toString().replaceFirst("------(.*)(?s).*octet-stream", "");
119         rule = cleanStreamBoundary.substring(0, cleanStreamBoundary.lastIndexOf("end") + 4);
120     }
121
122     @RequestMapping(value = {"/brms_dictionary/save_BRMSParam"}, method = {RequestMethod.POST})
123     public ModelAndView saveBRMSParamDictionary(HttpServletRequest request, HttpServletResponse response)
124             throws IOException {
125         DictionaryUtils utils = getDictionaryUtilsInstance();
126         try {
127             boolean fromAPI = utils.isRequestFromAPI(request);
128             ObjectMapper mapper = new ObjectMapper();
129             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
130             JsonNode root = mapper.readTree(request.getReader());
131
132             BRMSParamTemplate bRMSParamTemplateData;
133             String userId = null;
134             if (fromAPI) {
135                 bRMSParamTemplateData =
136                         mapper.readValue(root.get(dictionaryFields).toString(), BRMSParamTemplate.class);
137                 userId = "API";
138             } else {
139                 bRMSParamTemplateData =
140                         mapper.readValue(root.get("brmsParamDictionaryData").toString(), BRMSParamTemplate.class);
141                 userId = root.get(userid).textValue();
142             }
143             UserInfo userInfo = utils.getUserInfo(userId);
144
145             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(bRMSParamTemplateData.getRuleName(),
146                     ruleName, BRMSParamTemplate.class);
147             boolean duplicateflag = false;
148             if (!duplicateData.isEmpty()) {
149                 BRMSParamTemplate data = (BRMSParamTemplate) duplicateData.get(0);
150                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
151                     bRMSParamTemplateData.setId(data.getId());
152                 } else if ((request.getParameter(operation) != null
153                         && !"update".equals(request.getParameter(operation)))
154                         || (request.getParameter(operation) == null
155                                 && (data.getId() != bRMSParamTemplateData.getId()))) {
156                     duplicateflag = true;
157                 }
158             }
159             String responseString = null;
160             boolean validation = false;
161             if (rule != null && CreateBRMSRuleTemplate.validateRuleParams(rule)) {
162                 bRMSParamTemplateData.setRule(rule);
163                 validation = true;
164                 if (!duplicateflag) {
165                     if (bRMSParamTemplateData.getId() == 0) {
166                         bRMSParamTemplateData.setUserCreatedBy(userInfo);
167                         commonClassDao.save(bRMSParamTemplateData);
168                     } else {
169                         commonClassDao.update(bRMSParamTemplateData);
170                     }
171                     responseString = mapper.writeValueAsString(commonClassDao.getData(BRMSParamTemplate.class));
172                 } else {
173                     responseString = duplicateResponseString;
174                 }
175             }
176
177             if (!validation) {
178                 responseString = VALIDATIONRESPONSE;
179             }
180             if (fromAPI) {
181                 if (responseString != null && !(duplicateResponseString).equals(responseString)
182                         && !VALIDATIONRESPONSE.equals(responseString)) {
183                     responseString = successMsg;
184                 }
185                 ModelAndView result = new ModelAndView();
186                 result.setViewName(responseString);
187                 return result;
188             } else {
189                 utils.setResponseData(response, brmsParamDatas, responseString);
190             }
191         } catch (Exception e) {
192             utils.setErrorResponseData(response, e);
193         }
194         return null;
195     }
196
197     @RequestMapping(value = {"/brms_dictionary/remove_brmsParam"}, method = {RequestMethod.POST})
198     public void removeBRMSParamDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
199         DictionaryUtils utils = getDictionaryUtilsInstance();
200         utils.removeData(request, response, brmsParamDatas, BRMSParamTemplate.class);
201     }
202
203     @RequestMapping(
204             value = {"/get_BRMSDependencyDataByName"},
205             method = {RequestMethod.GET},
206             produces = MediaType.APPLICATION_JSON_VALUE)
207     public void getBRMSDependencyDictionaryByNameEntityData(HttpServletResponse response) {
208         DictionaryUtils utils = getDictionaryUtilsInstance();
209         utils.getDataByEntity(response, brmsDependencyDatas, dependencyName, BRMSDependency.class);
210     }
211
212     @RequestMapping(
213             value = {"/get_BRMSDependencyData"},
214             method = {RequestMethod.GET},
215             produces = MediaType.APPLICATION_JSON_VALUE)
216     public void getBRMSDependencyDictionaryEntityData(HttpServletResponse response) {
217         DictionaryUtils utils = getDictionaryUtilsInstance();
218         utils.getData(response, brmsDependencyDatas, BRMSDependency.class);
219     }
220
221     @RequestMapping(value = {"/brms_dictionary/save_BRMSDependencyData"}, method = {RequestMethod.POST})
222     public ModelAndView saveBRMSDependencyDictionary(HttpServletRequest request, HttpServletResponse response)
223             throws IOException {
224         DictionaryUtils utils = getDictionaryUtilsInstance();
225         try {
226             LOGGER.debug("DictionaryController:  saveBRMSDependencyDictionary() is called");
227             boolean fromAPI = utils.isRequestFromAPI(request);
228             ObjectMapper mapper = new ObjectMapper();
229             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
230             JsonNode root = mapper.readTree(request.getReader());
231
232             BRMSDependency brmsDependency;
233             String userId = null;
234             if (fromAPI) {
235                 brmsDependency = mapper.readValue(root.get(dictionaryFields).toString(), BRMSDependency.class);
236                 userId = "API";
237             } else {
238                 brmsDependency =
239                         mapper.readValue(root.get("brmsDependencyDictionaryData").toString(), BRMSDependency.class);
240                 userId = root.get(userid).textValue();
241             }
242             UserInfo userInfo = utils.getUserInfo(userId);
243
244             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(brmsDependency.getDependencyName(),
245                     dependencyName, BRMSDependency.class);
246             boolean duplicateflag = false;
247             if (!duplicateData.isEmpty()) {
248                 BRMSDependency data = (BRMSDependency) duplicateData.get(0);
249                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
250                     brmsDependency.setId(data.getId());
251                 } else if ((request.getParameter(operation) != null
252                         && !"update".equals(request.getParameter(operation)))
253                         || (request.getParameter(operation) == null && (data.getId() != brmsDependency.getId()))) {
254                     duplicateflag = true;
255                 }
256             }
257             LOGGER.audit("the userId from the onap portal is: " + userId);
258             String responseString = null;
259             if (brmsDependency.getDependency() != null && !("").equals(brmsDependency.getDependency().trim())) {
260                 PEDependency dependency = null;
261                 try {
262                     dependency = PolicyUtils.jsonStringToObject(brmsDependency.getDependency(), PEDependency.class);
263                 } catch (Exception e) {
264                     LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID
265                             + "wrong data given for BRMS PEDependency Dictionary : " + brmsDependency.getDependency(),
266                             e);
267                 }
268                 if (dependency == null) {
269                     responseString = errorMessage;
270                 } else {
271                     if (!duplicateflag) {
272                         brmsDependency.setUserModifiedBy(userInfo);
273                         if (brmsDependency.getId() == 0) {
274                             brmsDependency.setUserCreatedBy(userInfo);
275                             commonClassDao.save(brmsDependency);
276                         } else {
277                             brmsDependency.setModifiedDate(new Date());
278                             commonClassDao.update(brmsDependency);
279                         }
280                         responseString = mapper.writeValueAsString(commonClassDao.getData(BRMSDependency.class));
281                     } else {
282                         responseString = duplicateResponseString;
283                     }
284                 }
285             }
286
287             if (fromAPI) {
288                 if (responseString != null && !duplicateResponseString.equals(responseString)
289                         && !errorMessage.equals(responseString)) {
290                     responseString = successMsg;
291                 }
292                 ModelAndView result = new ModelAndView();
293                 result.setViewName(responseString);
294                 return result;
295             } else {
296                 utils.setResponseData(response, brmsDependencyDatas, responseString);
297             }
298         } catch (Exception e) {
299             utils.setErrorResponseData(response, e);
300         }
301         return null;
302     }
303
304     @RequestMapping(value = {"/brms_dictionary/remove_brmsDependency"}, method = {RequestMethod.POST})
305     public void removeBRMSDependencyDictionary(HttpServletRequest request, HttpServletResponse response)
306             throws IOException {
307         DictionaryUtils utils = getDictionaryUtilsInstance();
308         utils.removeData(request, response, brmsDependencyDatas, BRMSDependency.class);
309     }
310
311     @RequestMapping(
312             value = {"/get_BRMSControllerDataByName"},
313             method = {RequestMethod.GET},
314             produces = MediaType.APPLICATION_JSON_VALUE)
315     public void getBRMSControllerDictionaryByNameEntityData(HttpServletResponse response) {
316         DictionaryUtils utils = getDictionaryUtilsInstance();
317         utils.getDataByEntity(response, brmsControllerDatas, controllerName, BRMSController.class);
318     }
319
320     @RequestMapping(
321             value = {"/get_BRMSControllerData"},
322             method = {RequestMethod.GET},
323             produces = MediaType.APPLICATION_JSON_VALUE)
324     public void getBRMSControllerDictionaryEntityData(HttpServletResponse response) {
325         DictionaryUtils utils = getDictionaryUtilsInstance();
326         utils.getData(response, brmsControllerDatas, BRMSController.class);
327     }
328
329     @RequestMapping(value = {"/brms_dictionary/save_BRMSControllerData"}, method = {RequestMethod.POST})
330     public ModelAndView saveBRMSControllerDictionary(HttpServletRequest request, HttpServletResponse response)
331             throws IOException {
332         DictionaryUtils utils = getDictionaryUtilsInstance();
333         try {
334             LOGGER.debug("DictionaryController:  saveBRMSControllerDictionary() is called");
335             boolean fromAPI = utils.isRequestFromAPI(request);
336             ObjectMapper mapper = new ObjectMapper();
337             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
338             JsonNode root = mapper.readTree(request.getReader());
339             BRMSController brmsController;
340             String userId = null;
341             if (fromAPI) {
342                 brmsController = mapper.readValue(root.get(dictionaryFields).toString(), BRMSController.class);
343                 userId = "API";
344             } else {
345                 brmsController =
346                         mapper.readValue(root.get("brmsControllerDictionaryData").toString(), BRMSController.class);
347                 userId = root.get(userid).textValue();
348             }
349             UserInfo userInfo = utils.getUserInfo(userId);
350
351             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(brmsController.getControllerName(),
352                     controllerName, BRMSController.class);
353             boolean duplicateflag = false;
354             if (!duplicateData.isEmpty()) {
355                 BRMSController data = (BRMSController) duplicateData.get(0);
356                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
357                     brmsController.setId(data.getId());
358                 } else if ((request.getParameter(operation) != null
359                         && !"update".equals(request.getParameter(operation)))
360                         || (request.getParameter(operation) == null && (data.getId() != brmsController.getId()))) {
361                     duplicateflag = true;
362                 }
363             }
364             String responseString = null;
365             if (brmsController.getController() != null && !("").equals(brmsController.getController().trim())) {
366                 PEDependency dependency = null;
367                 try {
368                     dependency = PolicyUtils.jsonStringToObject(brmsController.getController(), PEDependency.class);
369                 } catch (Exception e) {
370                     LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID
371                             + "wrong data given for BRMS Controller Dictionary : " + brmsController.getController(), e);
372                 }
373                 if (dependency == null) {
374                     responseString = errorMessage;
375                 } else {
376                     if (!duplicateflag) {
377                         brmsController.setUserModifiedBy(userInfo);
378                         if (brmsController.getId() == 0) {
379                             brmsController.setUserCreatedBy(userInfo);
380                             commonClassDao.save(brmsController);
381                         } else {
382                             brmsController.setModifiedDate(new Date());
383                             commonClassDao.update(brmsController);
384                         }
385                         responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
386                     } else {
387                         responseString = duplicateResponseString;
388                     }
389                 }
390             }
391             if (fromAPI) {
392                 if (responseString != null && !(duplicateResponseString).equals(responseString)
393                         && !(errorMessage).equals(responseString)) {
394                     responseString = successMsg;
395                 }
396                 ModelAndView result = new ModelAndView();
397                 result.setViewName(responseString);
398                 return result;
399             } else {
400                 utils.setResponseData(response, brmsControllerDatas, responseString);
401             }
402         } catch (Exception e) {
403             utils.setErrorResponseData(response, e);
404         }
405         return null;
406     }
407
408     @RequestMapping(value = {"/brms_dictionary/remove_brmsController"}, method = {RequestMethod.POST})
409     public void removeBRMSControllerDictionary(HttpServletRequest request, HttpServletResponse response)
410             throws IOException {
411         DictionaryUtils utils = getDictionaryUtilsInstance();
412         utils.removeData(request, response, brmsControllerDatas, BRMSController.class);
413     }
414
415     public BRMSDependency getDependencyDataByID(String dependencyName) {
416         return (BRMSDependency) commonClassDao.getEntityItem(BRMSDependency.class,
417                 BRMSDictionaryController.dependencyName, dependencyName);
418     }
419
420     public BRMSController getControllerDataByID(String controllerName) {
421         return (BRMSController) commonClassDao.getEntityItem(BRMSController.class,
422                 BRMSDictionaryController.controllerName, controllerName);
423     }
424 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use