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;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.lang.StringUtils;
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.util.DictionaryUtils;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
40 import org.onap.policy.rest.jpa.OnapName;
41 import org.onap.policy.rest.jpa.PolicyScopeClosedLoop;
42 import org.onap.policy.rest.jpa.PolicyScopeResource;
43 import org.onap.policy.rest.jpa.PolicyScopeService;
44 import org.onap.policy.rest.jpa.PolicyScopeType;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.MediaType;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.servlet.ModelAndView;
53 public class PolicyScopeDictionaryController {
55 private static final Logger LOGGER = FlexLogger.getLogger(PolicyScopeDictionaryController.class);
57 private static CommonClassDao commonClassDao;
58 private static String operation = "operation";
59 private static String groupPolicyScopeListData1 = "groupPolicyScopeListData1";
60 private static String policyScope = "PolicyScope";
61 private static String duplicateResponseString = "Duplicate";
62 private static String groupPolicyScopeDatas = "groupPolicyScopeListDatas";
63 private static String dictionaryFields = "dictionaryFields";
64 private static String psCLDatas = "psClosedLoopDictionaryDatas";
65 private static String psServiceDatas = "psServiceDictionaryDatas";
66 private static String psTypeDatas = "psTypeDictionaryDatas";
67 private static String psResourceDatas = "psResourceDictionaryDatas";
69 public PolicyScopeDictionaryController() {
73 private DictionaryUtils getDictionaryUtilsInstance() {
74 return DictionaryUtils.getDictionaryUtils();
78 public PolicyScopeDictionaryController(CommonClassDao commonClassDao) {
79 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
82 public void setCommonClassDao(CommonClassDao commonClassDao) {
83 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
87 value = {"/get_GroupPolicyScopeDataByName"},
88 method = {RequestMethod.GET},
89 produces = MediaType.APPLICATION_JSON_VALUE)
90 public void getGroupPolicyScopeEntityDataByName(HttpServletResponse response) {
91 DictionaryUtils utils = getDictionaryUtilsInstance();
92 utils.getDataByEntity(response, groupPolicyScopeDatas, "name", GroupPolicyScopeList.class);
96 value = {"/get_GroupPolicyScopeData"},
97 method = {RequestMethod.GET},
98 produces = MediaType.APPLICATION_JSON_VALUE)
99 public void getGroupPolicyScopeEntityData(HttpServletResponse response) {
100 DictionaryUtils utils = getDictionaryUtilsInstance();
101 utils.getData(response, groupPolicyScopeDatas, GroupPolicyScopeList.class);
104 @RequestMapping(value = {"/ps_dictionary/save_psGroupPolicyScope"}, method = {RequestMethod.POST})
105 public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response)
107 DictionaryUtils utils = getDictionaryUtilsInstance();
109 boolean fromAPI = utils.isRequestFromAPI(request);
110 ObjectMapper mapper = new ObjectMapper();
111 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
112 JsonNode root = mapper.readTree(request.getReader());
113 GroupPolicyScopeList gpdata = null;
114 GroupPolicyScope groupData = null;
115 boolean duplicateGroupFlag = false;
117 gpdata = mapper.readValue(root.get(dictionaryFields).toString(), GroupPolicyScopeList.class);
120 mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
121 } catch (Exception e) {
122 groupData = new GroupPolicyScope();
123 groupData.setResource(root.get(dictionaryFields).get("resource").toString().replace("\"", ""));
124 groupData.setClosedloop(root.get(dictionaryFields).get("closedloop").toString().replace("\"", ""));
125 groupData.setService(root.get(dictionaryFields).get("service").toString().replace("\"", ""));
126 groupData.setType(root.get(dictionaryFields).get("type").toString().replace("\"", ""));
130 gpdata = mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
133 mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
134 } catch (Exception e) {
136 groupData = new GroupPolicyScope();
137 groupData.setResource(
138 root.get(groupPolicyScopeListData1).get("resource").toString().replace("\"", ""));
139 groupData.setClosedloop(
140 root.get(groupPolicyScopeListData1).get("closedloop").toString().replace("\"", ""));
141 groupData.setService(
142 root.get(groupPolicyScopeListData1).get("service").toString().replace("\"", ""));
143 groupData.setType(root.get(groupPolicyScopeListData1).get("type").toString().replace("\"", ""));
146 if (!gpdata.getGroupName().startsWith(policyScope)) {
147 String name = "PolicyScope_" + gpdata.getGroupName();
148 gpdata.setGroupName(name);
150 ArrayList<String> valueList = new ArrayList<>();
151 String resourceValue = groupData.getResource();
152 String typeValue = groupData.getType();
153 String serviceValue = groupData.getService();
154 String closedLoopValue = groupData.getClosedloop();
155 valueList.add("resource=" + resourceValue);
156 valueList.add("service=" + serviceValue);
157 valueList.add("type=" + typeValue);
158 valueList.add("closedLoopControlName=" + closedLoopValue);
159 String list = StringUtils.replaceEach(valueList.toString(), new String[] {"[", "]", " "},
160 new String[] {"", "", ""});
161 gpdata.setGroupList(list);
163 List<Object> duplicateData =
164 commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
165 if (duplicateData.isEmpty()) {
166 duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupList(), "groupList",
167 GroupPolicyScopeList.class);
168 if (duplicateData.isEmpty()) {
169 duplicateGroupFlag = true;
172 boolean duplicateflag = false;
173 if (!duplicateData.isEmpty()) {
174 GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0);
175 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
176 gpdata.setId(data.getId());
177 } else if ((request.getParameter(operation) != null
178 && !"update".equals(request.getParameter(operation)))
179 || (request.getParameter(operation) == null && (data.getId() != gpdata.getId()))) {
180 duplicateflag = true;
183 String responseString = null;
184 if (!duplicateflag && !duplicateGroupFlag) {
185 if (gpdata.getId() == 0) {
186 commonClassDao.save(gpdata);
188 commonClassDao.update(gpdata);
190 responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
191 } else if (duplicateGroupFlag) {
192 responseString = "DuplicateGroup";
194 responseString = duplicateResponseString;
197 return utils.getResultForApi(responseString);
199 utils.setResponseData(response, groupPolicyScopeDatas, responseString);
201 } catch (Exception e) {
202 utils.setErrorResponseData(response, e);
207 @RequestMapping(value = {"/ps_dictionary/remove_GroupPolicyScope"}, method = {RequestMethod.POST})
208 public void removePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response)
210 DictionaryUtils utils = getDictionaryUtilsInstance();
211 utils.removeData(request, response, groupPolicyScopeDatas, GroupPolicyScopeList.class);
215 value = {"/get_PSClosedLoopDataByName"},
216 method = {RequestMethod.GET},
217 produces = MediaType.APPLICATION_JSON_VALUE)
218 public void getPSClosedLoopEntityDataByName(HttpServletResponse response) {
219 DictionaryUtils utils = getDictionaryUtilsInstance();
220 utils.getDataByEntity(response, psCLDatas, "name", OnapName.class);
224 value = {"/get_PSClosedLoopData"},
225 method = {RequestMethod.GET},
226 produces = MediaType.APPLICATION_JSON_VALUE)
227 public void getPSClosedLoopEntityData(HttpServletResponse response) {
228 DictionaryUtils utils = getDictionaryUtilsInstance();
229 utils.getData(response, psCLDatas, PolicyScopeClosedLoop.class);
232 @RequestMapping(value = {"/ps_dictionary/save_psClosedLoop"}, method = {RequestMethod.POST})
233 public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response)
235 DictionaryUtils utils = getDictionaryUtilsInstance();
237 boolean fromAPI = utils.isRequestFromAPI(request);
238 ObjectMapper mapper = new ObjectMapper();
239 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
240 JsonNode root = mapper.readTree(request.getReader());
241 PolicyScopeClosedLoop onapData;
243 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeClosedLoop.class);
245 onapData = mapper.readValue(root.get("psClosedLoopDictionaryData").toString(),
246 PolicyScopeClosedLoop.class);
249 List<Object> duplicateData =
250 commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
251 boolean duplicateflag = false;
252 if (!duplicateData.isEmpty()) {
253 PolicyScopeClosedLoop data = (PolicyScopeClosedLoop) duplicateData.get(0);
254 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
255 onapData.setId(data.getId());
256 } else if ((request.getParameter(operation) != null
257 && !"update".equals(request.getParameter(operation)))
258 || (request.getParameter(operation) == null && (data.getId() != onapData.getId()))) {
259 duplicateflag = true;
262 String responseString = null;
263 if (!duplicateflag) {
264 if (onapData.getId() == 0) {
265 commonClassDao.save(onapData);
267 commonClassDao.update(onapData);
269 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
271 responseString = duplicateResponseString;
274 return utils.getResultForApi(responseString);
276 utils.setResponseData(response, psCLDatas, responseString);
278 } catch (Exception e) {
279 utils.setErrorResponseData(response, e);
284 @RequestMapping(value = {"/ps_dictionary/remove_PSClosedLoop"}, method = {RequestMethod.POST})
285 public void removePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response)
287 DictionaryUtils utils = getDictionaryUtilsInstance();
288 utils.removeData(request, response, psCLDatas, PolicyScopeClosedLoop.class);
292 value = {"/get_PSServiceDataByName"},
293 method = {RequestMethod.GET},
294 produces = MediaType.APPLICATION_JSON_VALUE)
295 public void getPSServiceEntityDataByName(HttpServletResponse response) {
296 DictionaryUtils utils = getDictionaryUtilsInstance();
297 utils.getDataByEntity(response, psServiceDatas, "name", PolicyScopeService.class);
301 value = {"/get_PSServiceData"},
302 method = {RequestMethod.GET},
303 produces = MediaType.APPLICATION_JSON_VALUE)
304 public void getPSServiceEntityData(HttpServletResponse response) {
305 DictionaryUtils utils = getDictionaryUtilsInstance();
306 utils.getData(response, psServiceDatas, PolicyScopeService.class);
309 @RequestMapping(value = {"/ps_dictionary/save_psService"}, method = {RequestMethod.POST})
310 public ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response)
312 DictionaryUtils utils = getDictionaryUtilsInstance();
314 boolean fromAPI = utils.isRequestFromAPI(request);
315 ObjectMapper mapper = new ObjectMapper();
316 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
317 JsonNode root = mapper.readTree(request.getReader());
318 PolicyScopeService onapData;
320 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeService.class);
322 onapData = mapper.readValue(root.get("psServiceDictionaryData").toString(), PolicyScopeService.class);
325 List<Object> duplicateData =
326 commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
327 boolean duplicateflag = false;
328 if (!duplicateData.isEmpty()) {
329 PolicyScopeService data = (PolicyScopeService) duplicateData.get(0);
330 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
331 onapData.setId(data.getId());
332 } else if ((request.getParameter(operation) != null
333 && !"update".equals(request.getParameter(operation)))
334 || (request.getParameter(operation) == null && (data.getId() != onapData.getId()))) {
335 duplicateflag = true;
338 String responseString = null;
339 if (!duplicateflag) {
340 if (onapData.getId() == 0) {
341 commonClassDao.save(onapData);
343 commonClassDao.update(onapData);
345 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
347 responseString = duplicateResponseString;
350 return utils.getResultForApi(responseString);
352 utils.setResponseData(response, psServiceDatas, responseString);
354 } catch (Exception e) {
355 utils.setErrorResponseData(response, e);
360 @RequestMapping(value = {"/ps_dictionary/remove_PSService"}, method = {RequestMethod.POST})
361 public void removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
362 DictionaryUtils utils = getDictionaryUtilsInstance();
363 utils.removeData(request, response, psServiceDatas, PolicyScopeService.class);
367 value = {"/get_PSTypeDataByName"},
368 method = {RequestMethod.GET},
369 produces = MediaType.APPLICATION_JSON_VALUE)
370 public void getPSTypeEntityDataByName(HttpServletResponse response) {
371 DictionaryUtils utils = getDictionaryUtilsInstance();
372 utils.getDataByEntity(response, psTypeDatas, "name", PolicyScopeType.class);
376 value = {"/get_PSTypeData"},
377 method = {RequestMethod.GET},
378 produces = MediaType.APPLICATION_JSON_VALUE)
379 public void getPSTypeEntityData(HttpServletResponse response) {
380 DictionaryUtils utils = getDictionaryUtilsInstance();
381 utils.getData(response, psTypeDatas, PolicyScopeType.class);
384 @RequestMapping(value = {"/ps_dictionary/save_psType"}, method = {RequestMethod.POST})
385 public ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response)
387 DictionaryUtils utils = getDictionaryUtilsInstance();
389 boolean fromAPI = utils.isRequestFromAPI(request);
390 ObjectMapper mapper = new ObjectMapper();
391 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
392 JsonNode root = mapper.readTree(request.getReader());
393 PolicyScopeType onapData;
395 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeType.class);
397 onapData = mapper.readValue(root.get("psTypeDictionaryData").toString(), PolicyScopeType.class);
400 List<Object> duplicateData =
401 commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
402 boolean duplicateflag = false;
403 if (!duplicateData.isEmpty()) {
404 PolicyScopeType data = (PolicyScopeType) duplicateData.get(0);
405 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
406 onapData.setId(data.getId());
407 } else if ((request.getParameter(operation) != null
408 && !"update".equals(request.getParameter(operation)))
409 || (request.getParameter(operation) == null && (data.getId() != onapData.getId()))) {
410 duplicateflag = true;
413 String responseString = null;
414 if (!duplicateflag) {
415 if (onapData.getId() == 0) {
416 commonClassDao.save(onapData);
418 commonClassDao.update(onapData);
420 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
422 responseString = duplicateResponseString;
425 return utils.getResultForApi(responseString);
427 utils.setResponseData(response, psTypeDatas, responseString);
429 } catch (Exception e) {
430 utils.setErrorResponseData(response, e);
435 @RequestMapping(value = {"/ps_dictionary/remove_PSType"}, method = {RequestMethod.POST})
436 public void removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
437 DictionaryUtils utils = getDictionaryUtilsInstance();
438 utils.removeData(request, response, psTypeDatas, PolicyScopeType.class);
442 value = {"/get_PSResourceDataByName"},
443 method = {RequestMethod.GET},
444 produces = MediaType.APPLICATION_JSON_VALUE)
445 public void getPSResourceEntityDataByName(HttpServletResponse response) {
446 DictionaryUtils utils = getDictionaryUtilsInstance();
447 utils.getDataByEntity(response, psResourceDatas, "name", PolicyScopeResource.class);
451 value = {"/get_PSResourceData"},
452 method = {RequestMethod.GET},
453 produces = MediaType.APPLICATION_JSON_VALUE)
454 public void getPSResourceEntityData(HttpServletResponse response) {
455 DictionaryUtils utils = getDictionaryUtilsInstance();
456 utils.getData(response, psResourceDatas, PolicyScopeResource.class);
459 @RequestMapping(value = {"/ps_dictionary/save_psResource"}, method = {RequestMethod.POST})
460 public ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response)
462 DictionaryUtils utils = getDictionaryUtilsInstance();
464 boolean fromAPI = utils.isRequestFromAPI(request);
465 ObjectMapper mapper = new ObjectMapper();
466 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
467 JsonNode root = mapper.readTree(request.getReader());
468 PolicyScopeResource onapData;
470 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeResource.class);
472 onapData = mapper.readValue(root.get("psResourceDictionaryData").toString(), PolicyScopeResource.class);
475 List<Object> duplicateData =
476 commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
477 boolean duplicateflag = false;
478 if (!duplicateData.isEmpty()) {
479 PolicyScopeResource data = (PolicyScopeResource) duplicateData.get(0);
480 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
481 onapData.setId(data.getId());
482 } else if ((request.getParameter(operation) != null
483 && !"update".equals(request.getParameter(operation)))
484 || (request.getParameter(operation) == null && (data.getId() != onapData.getId()))) {
485 duplicateflag = true;
488 String responseString = null;
489 if (!duplicateflag) {
490 if (onapData.getId() == 0) {
491 commonClassDao.save(onapData);
493 commonClassDao.update(onapData);
495 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
497 responseString = duplicateResponseString;
500 return utils.getResultForApi(responseString);
502 utils.setResponseData(response, psResourceDatas, responseString);
504 } catch (Exception e) {
505 utils.setErrorResponseData(response, e);
510 @RequestMapping(value = {"/ps_dictionary/remove_PSResource"}, method = {RequestMethod.POST})
511 public void removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response)
513 DictionaryUtils utils = getDictionaryUtilsInstance();
514 utils.removeData(request, response, psResourceDatas, PolicyScopeResource.class);
519 class GroupPolicyScope {
525 public String getResource() {
529 public void setResource(String resource) {
530 this.resource = resource;
533 public String getType() {
537 public void setType(String type) {
541 public String getService() {
545 public void setService(String service) {
546 this.service = service;
549 public String getClosedloop() {
553 public void setClosedloop(String closedloop) {
554 this.closedloop = closedloop;