2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.io.StringWriter;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Date;
30 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.apache.commons.io.IOUtils;
34 import org.onap.policy.api.PEDependency;
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.components.CreateBRMSRuleTemplate;
38 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.BRMSController;
41 import org.onap.policy.rest.jpa.BRMSDependency;
42 import org.onap.policy.rest.jpa.BRMSParamTemplate;
43 import org.onap.policy.rest.jpa.OnapName;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.onap.policy.utils.PolicyUtils;
46 import org.onap.policy.xacml.api.XACMLErrorConstants;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.http.MediaType;
49 import org.springframework.stereotype.Controller;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RequestMethod;
52 import org.springframework.web.servlet.ModelAndView;
56 public class BRMSDictionaryController {
58 private static final Logger LOGGER = FlexLogger.getLogger(BRMSDictionaryController.class);
60 private static final String VALIDATIONRESPONSE = "Validation";
62 private static CommonClassDao commonClassDao;
63 private static String rule;
64 private static String successMsg = "Success";
65 private static String duplicateResponseString = "Duplicate";
66 private static String ruleName = "ruleName";
67 private static String errorMessage = "Error";
68 private static String operation = "operation";
69 private static String dictionaryFields = "dictionaryFields";
70 private static String userid = "userid";
71 private static String dependencyName = "dependencyName";
72 private static String controllerName = "controllerName";
73 private static String brmsParamDatas = "brmsParamDictionaryDatas";
74 private static String brmsDependencyDatas = "brmsDependencyDictionaryDatas";
75 private static String brmsControllerDatas = "brmsControllerDictionaryDatas";
78 public BRMSDictionaryController(CommonClassDao commonClassDao) {
79 BRMSDictionaryController.commonClassDao = commonClassDao;
82 public static void setCommonClassDao(CommonClassDao commonClassDao2) {
83 BRMSDictionaryController.commonClassDao = commonClassDao2;
86 public BRMSDictionaryController() {
90 private DictionaryUtils getDictionaryUtilsInstance() {
91 return DictionaryUtils.getDictionaryUtils();
94 @RequestMapping(value = {"/get_BRMSParamDataByName"}, method = {RequestMethod.GET},
95 produces = MediaType.APPLICATION_JSON_VALUE)
96 public void getBRMSParamDictionaryByNameEntityData(HttpServletResponse response) {
97 DictionaryUtils utils = getDictionaryUtilsInstance();
98 utils.getDataByEntity(response, brmsParamDatas, ruleName, BRMSParamTemplate.class);
101 @RequestMapping(value = {"/get_BRMSParamData"}, method = {RequestMethod.GET},
102 produces = MediaType.APPLICATION_JSON_VALUE)
103 public void getBRMSParamDictionaryEntityData(HttpServletResponse response) {
104 DictionaryUtils utils = getDictionaryUtilsInstance();
105 utils.getData(response, brmsParamDatas, BRMSParamTemplate.class);
108 @RequestMapping(value = {"/brms_dictionary/set_BRMSParamData"}, method = {RequestMethod.POST})
109 public static void setRuleData(HttpServletRequest request) throws IOException {
110 StringWriter writer = new StringWriter();
111 IOUtils.copy(request.getInputStream(), writer, StandardCharsets.UTF_8);
112 String cleanStreamBoundary =
113 writer.toString().replaceFirst("------(.*)(?s).*octet-stream", "");
114 rule = cleanStreamBoundary.substring(0, cleanStreamBoundary.lastIndexOf("end") + 4);
117 @RequestMapping(value = {"/brms_dictionary/save_BRMSParam"}, method = {RequestMethod.POST})
118 public ModelAndView saveBRMSParamDictionary(HttpServletRequest request,
119 HttpServletResponse response) throws IOException {
120 DictionaryUtils utils = getDictionaryUtilsInstance();
122 boolean fromAPI = utils.isRequestFromAPI(request);
123 ObjectMapper mapper = new ObjectMapper();
124 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
125 JsonNode root = mapper.readTree(request.getReader());
127 BRMSParamTemplate bRMSParamTemplateData;
128 String userId = null;
130 bRMSParamTemplateData = mapper.readValue(root.get(dictionaryFields).toString(),
131 BRMSParamTemplate.class);
134 bRMSParamTemplateData = mapper.readValue(
135 root.get("brmsParamDictionaryData").toString(), BRMSParamTemplate.class);
136 userId = root.get(userid).textValue();
138 UserInfo userInfo = utils.getUserInfo(userId);
140 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
141 bRMSParamTemplateData.getRuleName(), ruleName, BRMSParamTemplate.class);
142 boolean duplicateflag = false;
143 if (!duplicateData.isEmpty()) {
144 BRMSParamTemplate data = (BRMSParamTemplate) duplicateData.get(0);
145 if (request.getParameter(operation) != null
146 && "update".equals(request.getParameter(operation))) {
147 bRMSParamTemplateData.setId(data.getId());
148 } else if ((request.getParameter(operation) != null
149 && !"update".equals(request.getParameter(operation)))
150 || (request.getParameter(operation) == null
151 && (data.getId() != bRMSParamTemplateData.getId()))) {
152 duplicateflag = true;
155 String responseString = null;
156 boolean validation = false;
157 if (rule != null && CreateBRMSRuleTemplate.validateRuleParams(rule)) {
158 bRMSParamTemplateData.setRule(rule);
160 if (!duplicateflag) {
161 if (bRMSParamTemplateData.getId() == 0) {
162 bRMSParamTemplateData.setUserCreatedBy(userInfo);
163 commonClassDao.save(bRMSParamTemplateData);
165 commonClassDao.update(bRMSParamTemplateData);
167 responseString = mapper
168 .writeValueAsString(commonClassDao.getData(BRMSParamTemplate.class));
170 responseString = duplicateResponseString;
175 responseString = VALIDATIONRESPONSE;
178 if (responseString != null && !(duplicateResponseString).equals(responseString)
179 && !VALIDATIONRESPONSE.equals(responseString)) {
180 responseString = successMsg;
182 ModelAndView result = new ModelAndView();
183 result.setViewName(responseString);
186 utils.setResponseData(response, brmsParamDatas, responseString);
188 } catch (Exception e) {
189 utils.setErrorResponseData(response, e);
194 @RequestMapping(value = {"/brms_dictionary/remove_brmsParam"}, method = {RequestMethod.POST})
195 public void removeBRMSParamDictionary(HttpServletRequest request, HttpServletResponse response)
197 DictionaryUtils utils = getDictionaryUtilsInstance();
198 utils.removeData(request, response, brmsParamDatas, BRMSParamTemplate.class);
201 @RequestMapping(value = {"/get_BRMSDependencyDataByName"}, method = {RequestMethod.GET},
202 produces = MediaType.APPLICATION_JSON_VALUE)
203 public void getBRMSDependencyDictionaryByNameEntityData(HttpServletResponse response) {
204 DictionaryUtils utils = getDictionaryUtilsInstance();
205 utils.getDataByEntity(response, brmsDependencyDatas, dependencyName, BRMSDependency.class);
208 @RequestMapping(value = {"/get_BRMSDependencyData"}, method = {RequestMethod.GET},
209 produces = MediaType.APPLICATION_JSON_VALUE)
210 public void getBRMSDependencyDictionaryEntityData(HttpServletResponse response) {
211 DictionaryUtils utils = getDictionaryUtilsInstance();
212 utils.getData(response, brmsDependencyDatas, BRMSDependency.class);
215 @RequestMapping(value = {"/brms_dictionary/save_BRMSDependencyData"},
216 method = {RequestMethod.POST})
217 public ModelAndView saveBRMSDependencyDictionary(HttpServletRequest request,
218 HttpServletResponse response) throws IOException {
219 DictionaryUtils utils = getDictionaryUtilsInstance();
221 LOGGER.debug("DictionaryController: saveBRMSDependencyDictionary() is called");
222 boolean fromAPI = utils.isRequestFromAPI(request);
223 ObjectMapper mapper = new ObjectMapper();
224 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
225 JsonNode root = mapper.readTree(request.getReader());
227 BRMSDependency brmsDependency;
228 String userId = null;
230 brmsDependency = mapper.readValue(root.get(dictionaryFields).toString(),
231 BRMSDependency.class);
234 brmsDependency = mapper.readValue(
235 root.get("brmsDependencyDictionaryData").toString(), BRMSDependency.class);
236 userId = root.get(userid).textValue();
238 UserInfo userInfo = utils.getUserInfo(userId);
240 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
241 brmsDependency.getDependencyName(), dependencyName, BRMSDependency.class);
242 boolean duplicateflag = false;
243 if (!duplicateData.isEmpty()) {
244 BRMSDependency data = (BRMSDependency) duplicateData.get(0);
245 if (request.getParameter(operation) != null
246 && "update".equals(request.getParameter(operation))) {
247 brmsDependency.setId(data.getId());
248 } else if ((request.getParameter(operation) != null
249 && !"update".equals(request.getParameter(operation)))
250 || (request.getParameter(operation) == null
251 && (data.getId() != brmsDependency.getId()))) {
252 duplicateflag = true;
255 LOGGER.audit("the userId from the onap portal is: " + userId);
256 String responseString = null;
257 if (brmsDependency.getDependency() != null
258 && !("").equals(brmsDependency.getDependency().trim())) {
259 PEDependency dependency = null;
261 dependency = PolicyUtils.jsonStringToObject(brmsDependency.getDependency(),
263 } catch (Exception e) {
264 LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID
265 + "wrong data given for BRMS PEDependency Dictionary : "
266 + brmsDependency.getDependency(), e);
268 if (dependency == null) {
269 responseString = errorMessage;
271 if (!duplicateflag) {
272 brmsDependency.setUserModifiedBy(userInfo);
273 if (brmsDependency.getId() == 0) {
274 brmsDependency.setUserCreatedBy(userInfo);
275 commonClassDao.save(brmsDependency);
277 brmsDependency.setModifiedDate(new Date());
278 commonClassDao.update(brmsDependency);
280 responseString = mapper
281 .writeValueAsString(commonClassDao.getData(BRMSDependency.class));
283 responseString = duplicateResponseString;
289 if (responseString != null && !duplicateResponseString.equals(responseString)
290 && !errorMessage.equals(responseString)) {
291 responseString = successMsg;
293 ModelAndView result = new ModelAndView();
294 result.setViewName(responseString);
297 utils.setResponseData(response, brmsDependencyDatas, responseString);
299 } catch (Exception e) {
300 utils.setErrorResponseData(response, e);
305 @RequestMapping(value = {"/brms_dictionary/remove_brmsDependency"},
306 method = {RequestMethod.POST})
307 public void removeBRMSDependencyDictionary(HttpServletRequest request,
308 HttpServletResponse response) throws IOException {
309 DictionaryUtils utils = getDictionaryUtilsInstance();
310 utils.removeData(request, response, brmsDependencyDatas, BRMSDependency.class);
314 @RequestMapping(value = {"/get_BRMSControllerDataByName"}, method = {RequestMethod.GET},
315 produces = MediaType.APPLICATION_JSON_VALUE)
316 public void getBRMSControllerDictionaryByNameEntityData(HttpServletResponse response) {
317 DictionaryUtils utils = getDictionaryUtilsInstance();
318 utils.getDataByEntity(response, brmsControllerDatas, controllerName, BRMSController.class);
321 @RequestMapping(value = {"/get_BRMSControllerData"}, method = {RequestMethod.GET},
322 produces = MediaType.APPLICATION_JSON_VALUE)
323 public void getBRMSControllerDictionaryEntityData(HttpServletResponse response) {
324 DictionaryUtils utils = getDictionaryUtilsInstance();
325 utils.getData(response, brmsControllerDatas, BRMSController.class);
328 @RequestMapping(value = {"/brms_dictionary/save_BRMSControllerData"},
329 method = {RequestMethod.POST})
330 public ModelAndView saveBRMSControllerDictionary(HttpServletRequest request,
331 HttpServletResponse response) throws IOException {
332 DictionaryUtils utils = getDictionaryUtilsInstance();
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;
342 brmsController = mapper.readValue(root.get(dictionaryFields).toString(),
343 BRMSController.class);
346 brmsController = mapper.readValue(
347 root.get("brmsControllerDictionaryData").toString(), BRMSController.class);
348 userId = root.get(userid).textValue();
350 UserInfo userInfo = utils.getUserInfo(userId);
352 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
353 brmsController.getControllerName(), controllerName, BRMSController.class);
354 boolean duplicateflag = false;
355 if (!duplicateData.isEmpty()) {
356 BRMSController data = (BRMSController) duplicateData.get(0);
357 if (request.getParameter(operation) != null
358 && "update".equals(request.getParameter(operation))) {
359 brmsController.setId(data.getId());
360 } else if ((request.getParameter(operation) != null
361 && !"update".equals(request.getParameter(operation)))
362 || (request.getParameter(operation) == null
363 && (data.getId() != brmsController.getId()))) {
364 duplicateflag = true;
367 String responseString = null;
368 if (brmsController.getController() != null
369 && !("").equals(brmsController.getController().trim())) {
370 PEDependency dependency = null;
372 dependency = PolicyUtils.jsonStringToObject(brmsController.getController(),
374 } catch (Exception e) {
375 LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID
376 + "wrong data given for BRMS Controller Dictionary : "
377 + brmsController.getController(), e);
379 if (dependency == null) {
380 responseString = errorMessage;
382 if (!duplicateflag) {
383 brmsController.setUserModifiedBy(userInfo);
384 if (brmsController.getId() == 0) {
385 brmsController.setUserCreatedBy(userInfo);
386 commonClassDao.save(brmsController);
388 brmsController.setModifiedDate(new Date());
389 commonClassDao.update(brmsController);
392 mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
394 responseString = duplicateResponseString;
399 if (responseString != null && !(duplicateResponseString).equals(responseString)
400 && !(errorMessage).equals(responseString)) {
401 responseString = successMsg;
403 ModelAndView result = new ModelAndView();
404 result.setViewName(responseString);
407 utils.setResponseData(response, brmsControllerDatas, responseString);
409 } catch (Exception e) {
410 utils.setErrorResponseData(response, e);
415 @RequestMapping(value = {"/brms_dictionary/remove_brmsController"},
416 method = {RequestMethod.POST})
417 public void removeBRMSControllerDictionary(HttpServletRequest request,
418 HttpServletResponse response) throws IOException {
419 DictionaryUtils utils = getDictionaryUtilsInstance();
420 utils.removeData(request, response, brmsControllerDatas, BRMSController.class);
423 public BRMSDependency getDependencyDataByID(String dependencyName) {
424 return (BRMSDependency) commonClassDao.getEntityItem(BRMSDependency.class,
425 BRMSDictionaryController.dependencyName, dependencyName);
428 public BRMSController getControllerDataByID(String controllerName) {
429 return (BRMSController) commonClassDao.getEntityItem(BRMSController.class,
430 BRMSDictionaryController.controllerName, controllerName);