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.util.ArrayList;
28 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.lang.StringUtils;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
37 import org.onap.policy.rest.jpa.OnapName;
38 import org.onap.policy.rest.jpa.PolicyScopeClosedLoop;
39 import org.onap.policy.rest.jpa.PolicyScopeResource;
40 import org.onap.policy.rest.jpa.PolicyScopeService;
41 import org.onap.policy.rest.jpa.PolicyScopeType;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.servlet.ModelAndView;
50 public class PolicyScopeDictionaryController {
52 private static final Logger LOGGER =
53 FlexLogger.getLogger(PolicyScopeDictionaryController.class);
55 private static CommonClassDao commonClassDao;
56 private static String operation = "operation";
57 private static String groupPolicyScopeListData1 = "groupPolicyScopeListData1";
58 private static String policyScope = "PolicyScope";
59 private static String duplicateResponseString = "Duplicate";
60 private static String groupPolicyScopeDatas = "groupPolicyScopeListDatas";
61 private static String dictionaryFields = "dictionaryFields";
62 private static String psCLDatas = "psClosedLoopDictionaryDatas";
63 private static String psServiceDatas = "psServiceDictionaryDatas";
64 private static String psTypeDatas = "psTypeDictionaryDatas";
65 private static String psResourceDatas = "psResourceDictionaryDatas";
67 public PolicyScopeDictionaryController() {
71 private DictionaryUtils getDictionaryUtilsInstance() {
72 return DictionaryUtils.getDictionaryUtils();
76 public PolicyScopeDictionaryController(CommonClassDao commonClassDao) {
77 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
80 public void setCommonClassDao(CommonClassDao commonClassDao) {
81 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
84 @RequestMapping(value = {"/get_GroupPolicyScopeDataByName"}, method = {RequestMethod.GET},
85 produces = MediaType.APPLICATION_JSON_VALUE)
86 public void getGroupPolicyScopeEntityDataByName(HttpServletResponse response) {
87 DictionaryUtils utils = getDictionaryUtilsInstance();
88 utils.getDataByEntity(response, groupPolicyScopeDatas, "name", GroupPolicyScopeList.class);
91 @RequestMapping(value = {"/get_GroupPolicyScopeData"}, method = {RequestMethod.GET},
92 produces = MediaType.APPLICATION_JSON_VALUE)
93 public void getGroupPolicyScopeEntityData(HttpServletResponse response) {
94 DictionaryUtils utils = getDictionaryUtilsInstance();
95 utils.getData(response, groupPolicyScopeDatas, GroupPolicyScopeList.class);
98 @RequestMapping(value = {"/ps_dictionary/save_psGroupPolicyScope"},
99 method = {RequestMethod.POST})
100 public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request,
101 HttpServletResponse response) throws IOException {
102 DictionaryUtils utils = getDictionaryUtilsInstance();
104 boolean fromAPI = utils.isRequestFromAPI(request);
105 ObjectMapper mapper = new ObjectMapper();
106 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
107 JsonNode root = mapper.readTree(request.getReader());
108 GroupPolicyScopeList gpdata = null;
109 GroupPolicyScope groupData = null;
110 boolean duplicateGroupFlag = false;
112 gpdata = mapper.readValue(root.get(dictionaryFields).toString(),
113 GroupPolicyScopeList.class);
115 groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(),
116 GroupPolicyScope.class);
117 } catch (Exception e) {
118 groupData = new GroupPolicyScope();
119 groupData.setResource(root.get(dictionaryFields).get("resource").toString()
121 groupData.setClosedloop(root.get(dictionaryFields).get("closedloop").toString()
123 groupData.setService(
124 root.get(dictionaryFields).get("service").toString().replace("\"", ""));
126 root.get(dictionaryFields).get("type").toString().replace("\"", ""));
130 gpdata = mapper.readValue(root.get("groupPolicyScopeListData").toString(),
131 GroupPolicyScopeList.class);
133 groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(),
134 GroupPolicyScope.class);
135 } catch (Exception e) {
137 groupData = new GroupPolicyScope();
138 groupData.setResource(root.get(groupPolicyScopeListData1).get("resource")
139 .toString().replace("\"", ""));
140 groupData.setClosedloop(root.get(groupPolicyScopeListData1).get("closedloop")
141 .toString().replace("\"", ""));
142 groupData.setService(root.get(groupPolicyScopeListData1).get("service")
143 .toString().replace("\"", ""));
144 groupData.setType(root.get(groupPolicyScopeListData1).get("type").toString()
148 if (!gpdata.getGroupName().startsWith(policyScope)) {
149 String name = "PolicyScope_" + gpdata.getGroupName();
150 gpdata.setGroupName(name);
152 ArrayList<String> valueList = new ArrayList<>();
153 String resourceValue = groupData.getResource();
154 String typeValue = groupData.getType();
155 String serviceValue = groupData.getService();
156 String closedLoopValue = groupData.getClosedloop();
157 valueList.add("resource=" + resourceValue);
158 valueList.add("service=" + serviceValue);
159 valueList.add("type=" + typeValue);
160 valueList.add("closedLoopControlName=" + closedLoopValue);
161 String list = StringUtils.replaceEach(valueList.toString(),
162 new String[] {"[", "]", " "}, new String[] {"", "", ""});
163 gpdata.setGroupList(list);
165 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupName(),
166 "name", GroupPolicyScopeList.class);
167 if (duplicateData.isEmpty()) {
168 duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupList(),
169 "groupList", GroupPolicyScopeList.class);
170 if (duplicateData.isEmpty()) {
171 duplicateGroupFlag = true;
174 boolean duplicateflag = false;
175 if (!duplicateData.isEmpty()) {
176 GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0);
177 if (request.getParameter(operation) != null
178 && "update".equals(request.getParameter(operation))) {
179 gpdata.setId(data.getId());
180 } else if ((request.getParameter(operation) != null
181 && !"update".equals(request.getParameter(operation)))
182 || (request.getParameter(operation) == null
183 && (data.getId() != gpdata.getId()))) {
184 duplicateflag = true;
187 String responseString = null;
188 if (!duplicateflag && !duplicateGroupFlag) {
189 if (gpdata.getId() == 0) {
190 commonClassDao.save(gpdata);
192 commonClassDao.update(gpdata);
194 responseString = mapper
195 .writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
196 } else if (duplicateGroupFlag) {
197 responseString = "DuplicateGroup";
199 responseString = duplicateResponseString;
202 return utils.getResultForApi(responseString);
204 utils.setResponseData(response, groupPolicyScopeDatas, responseString);
206 } catch (Exception e) {
207 utils.setErrorResponseData(response, e);
212 @RequestMapping(value = {"/ps_dictionary/remove_GroupPolicyScope"},
213 method = {RequestMethod.POST})
214 public void removePSGroupScopeDictionary(HttpServletRequest request,
215 HttpServletResponse response) throws IOException {
216 DictionaryUtils utils = getDictionaryUtilsInstance();
217 utils.removeData(request, response, groupPolicyScopeDatas, GroupPolicyScopeList.class);
220 @RequestMapping(value = {"/get_PSClosedLoopDataByName"}, method = {RequestMethod.GET},
221 produces = MediaType.APPLICATION_JSON_VALUE)
222 public void getPSClosedLoopEntityDataByName(HttpServletResponse response) {
223 DictionaryUtils utils = getDictionaryUtilsInstance();
224 utils.getDataByEntity(response, psCLDatas, "name", OnapName.class);
227 @RequestMapping(value = {"/get_PSClosedLoopData"}, method = {RequestMethod.GET},
228 produces = MediaType.APPLICATION_JSON_VALUE)
229 public void getPSClosedLoopEntityData(HttpServletResponse response) {
230 DictionaryUtils utils = getDictionaryUtilsInstance();
231 utils.getData(response, psCLDatas, PolicyScopeClosedLoop.class);
234 @RequestMapping(value = {"/ps_dictionary/save_psClosedLoop"}, method = {RequestMethod.POST})
235 public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request,
236 HttpServletResponse response) throws IOException {
237 DictionaryUtils utils = getDictionaryUtilsInstance();
239 boolean fromAPI = utils.isRequestFromAPI(request);
240 ObjectMapper mapper = new ObjectMapper();
241 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
242 JsonNode root = mapper.readTree(request.getReader());
243 PolicyScopeClosedLoop onapData;
245 onapData = mapper.readValue(root.get(dictionaryFields).toString(),
246 PolicyScopeClosedLoop.class);
248 onapData = mapper.readValue(root.get("psClosedLoopDictionaryData").toString(),
249 PolicyScopeClosedLoop.class);
252 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(),
253 "name", PolicyScopeClosedLoop.class);
254 boolean duplicateflag = false;
255 if (!duplicateData.isEmpty()) {
256 PolicyScopeClosedLoop data = (PolicyScopeClosedLoop) duplicateData.get(0);
257 if (request.getParameter(operation) != null
258 && "update".equals(request.getParameter(operation))) {
259 onapData.setId(data.getId());
260 } else if ((request.getParameter(operation) != null
261 && !"update".equals(request.getParameter(operation)))
262 || (request.getParameter(operation) == null
263 && (data.getId() != onapData.getId()))) {
264 duplicateflag = true;
267 String responseString = null;
268 if (!duplicateflag) {
269 if (onapData.getId() == 0) {
270 commonClassDao.save(onapData);
272 commonClassDao.update(onapData);
274 responseString = mapper
275 .writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
277 responseString = duplicateResponseString;
280 return utils.getResultForApi(responseString);
282 utils.setResponseData(response, psCLDatas, responseString);
284 } catch (Exception e) {
285 utils.setErrorResponseData(response, e);
290 @RequestMapping(value = {"/ps_dictionary/remove_PSClosedLoop"}, method = {RequestMethod.POST})
291 public void removePSClosedLoopDictionary(HttpServletRequest request,
292 HttpServletResponse response) throws IOException {
293 DictionaryUtils utils = getDictionaryUtilsInstance();
294 utils.removeData(request, response, psCLDatas, PolicyScopeClosedLoop.class);
297 @RequestMapping(value = {"/get_PSServiceDataByName"}, method = {RequestMethod.GET},
298 produces = MediaType.APPLICATION_JSON_VALUE)
299 public void getPSServiceEntityDataByName(HttpServletResponse response) {
300 DictionaryUtils utils = getDictionaryUtilsInstance();
301 utils.getDataByEntity(response, psServiceDatas, "name", PolicyScopeService.class);
304 @RequestMapping(value = {"/get_PSServiceData"}, method = {RequestMethod.GET},
305 produces = MediaType.APPLICATION_JSON_VALUE)
306 public void getPSServiceEntityData(HttpServletResponse response) {
307 DictionaryUtils utils = getDictionaryUtilsInstance();
308 utils.getData(response, psServiceDatas, PolicyScopeService.class);
311 @RequestMapping(value = {"/ps_dictionary/save_psService"}, method = {RequestMethod.POST})
312 public ModelAndView savePSServiceDictionary(HttpServletRequest request,
313 HttpServletResponse response) throws IOException {
314 DictionaryUtils utils = getDictionaryUtilsInstance();
316 boolean fromAPI = utils.isRequestFromAPI(request);
317 ObjectMapper mapper = new ObjectMapper();
318 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
319 JsonNode root = mapper.readTree(request.getReader());
320 PolicyScopeService onapData;
322 onapData = mapper.readValue(root.get(dictionaryFields).toString(),
323 PolicyScopeService.class);
325 onapData = mapper.readValue(root.get("psServiceDictionaryData").toString(),
326 PolicyScopeService.class);
329 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(),
330 "name", PolicyScopeService.class);
331 boolean duplicateflag = false;
332 if (!duplicateData.isEmpty()) {
333 PolicyScopeService data = (PolicyScopeService) duplicateData.get(0);
334 if (request.getParameter(operation) != null
335 && "update".equals(request.getParameter(operation))) {
336 onapData.setId(data.getId());
337 } else if ((request.getParameter(operation) != null
338 && !"update".equals(request.getParameter(operation)))
339 || (request.getParameter(operation) == null
340 && (data.getId() != onapData.getId()))) {
341 duplicateflag = true;
344 String responseString = null;
345 if (!duplicateflag) {
346 if (onapData.getId() == 0) {
347 commonClassDao.save(onapData);
349 commonClassDao.update(onapData);
352 mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
354 responseString = duplicateResponseString;
357 return utils.getResultForApi(responseString);
359 utils.setResponseData(response, psServiceDatas, responseString);
361 } catch (Exception e) {
362 utils.setErrorResponseData(response, e);
367 @RequestMapping(value = {"/ps_dictionary/remove_PSService"}, method = {RequestMethod.POST})
368 public void removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response)
370 DictionaryUtils utils = getDictionaryUtilsInstance();
371 utils.removeData(request, response, psServiceDatas, PolicyScopeService.class);
374 @RequestMapping(value = {"/get_PSTypeDataByName"}, method = {RequestMethod.GET},
375 produces = MediaType.APPLICATION_JSON_VALUE)
376 public void getPSTypeEntityDataByName(HttpServletResponse response) {
377 DictionaryUtils utils = getDictionaryUtilsInstance();
378 utils.getDataByEntity(response, psTypeDatas, "name", PolicyScopeType.class);
381 @RequestMapping(value = {"/get_PSTypeData"}, method = {RequestMethod.GET},
382 produces = MediaType.APPLICATION_JSON_VALUE)
383 public void getPSTypeEntityData(HttpServletResponse response) {
384 DictionaryUtils utils = getDictionaryUtilsInstance();
385 utils.getData(response, psTypeDatas, PolicyScopeType.class);
388 @RequestMapping(value = {"/ps_dictionary/save_psType"}, method = {RequestMethod.POST})
389 public ModelAndView savePSTypeDictionary(HttpServletRequest request,
390 HttpServletResponse response) throws IOException {
391 DictionaryUtils utils = getDictionaryUtilsInstance();
393 boolean fromAPI = utils.isRequestFromAPI(request);
394 ObjectMapper mapper = new ObjectMapper();
395 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
396 JsonNode root = mapper.readTree(request.getReader());
397 PolicyScopeType onapData;
399 onapData = mapper.readValue(root.get(dictionaryFields).toString(),
400 PolicyScopeType.class);
402 onapData = mapper.readValue(root.get("psTypeDictionaryData").toString(),
403 PolicyScopeType.class);
406 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(),
407 "name", PolicyScopeType.class);
408 boolean duplicateflag = false;
409 if (!duplicateData.isEmpty()) {
410 PolicyScopeType data = (PolicyScopeType) duplicateData.get(0);
411 if (request.getParameter(operation) != null
412 && "update".equals(request.getParameter(operation))) {
413 onapData.setId(data.getId());
414 } else if ((request.getParameter(operation) != null
415 && !"update".equals(request.getParameter(operation)))
416 || (request.getParameter(operation) == null
417 && (data.getId() != onapData.getId()))) {
418 duplicateflag = true;
421 String responseString = null;
422 if (!duplicateflag) {
423 if (onapData.getId() == 0) {
424 commonClassDao.save(onapData);
426 commonClassDao.update(onapData);
429 mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
431 responseString = duplicateResponseString;
434 return utils.getResultForApi(responseString);
436 utils.setResponseData(response, psTypeDatas, responseString);
438 } catch (Exception e) {
439 utils.setErrorResponseData(response, e);
444 @RequestMapping(value = {"/ps_dictionary/remove_PSType"}, method = {RequestMethod.POST})
445 public void removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response)
447 DictionaryUtils utils = getDictionaryUtilsInstance();
448 utils.removeData(request, response, psTypeDatas, PolicyScopeType.class);
451 @RequestMapping(value = {"/get_PSResourceDataByName"}, method = {RequestMethod.GET},
452 produces = MediaType.APPLICATION_JSON_VALUE)
453 public void getPSResourceEntityDataByName(HttpServletResponse response) {
454 DictionaryUtils utils = getDictionaryUtilsInstance();
455 utils.getDataByEntity(response, psResourceDatas, "name", PolicyScopeResource.class);
458 @RequestMapping(value = {"/get_PSResourceData"}, method = {RequestMethod.GET},
459 produces = MediaType.APPLICATION_JSON_VALUE)
460 public void getPSResourceEntityData(HttpServletResponse response) {
461 DictionaryUtils utils = getDictionaryUtilsInstance();
462 utils.getData(response, psResourceDatas, PolicyScopeResource.class);
465 @RequestMapping(value = {"/ps_dictionary/save_psResource"}, method = {RequestMethod.POST})
466 public ModelAndView savePSResourceDictionary(HttpServletRequest request,
467 HttpServletResponse response) throws IOException {
468 DictionaryUtils utils = getDictionaryUtilsInstance();
470 boolean fromAPI = utils.isRequestFromAPI(request);
471 ObjectMapper mapper = new ObjectMapper();
472 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
473 JsonNode root = mapper.readTree(request.getReader());
474 PolicyScopeResource onapData;
476 onapData = mapper.readValue(root.get(dictionaryFields).toString(),
477 PolicyScopeResource.class);
479 onapData = mapper.readValue(root.get("psResourceDictionaryData").toString(),
480 PolicyScopeResource.class);
483 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(),
484 "name", PolicyScopeResource.class);
485 boolean duplicateflag = false;
486 if (!duplicateData.isEmpty()) {
487 PolicyScopeResource data = (PolicyScopeResource) duplicateData.get(0);
488 if (request.getParameter(operation) != null
489 && "update".equals(request.getParameter(operation))) {
490 onapData.setId(data.getId());
491 } else if ((request.getParameter(operation) != null
492 && !"update".equals(request.getParameter(operation)))
493 || (request.getParameter(operation) == null
494 && (data.getId() != onapData.getId()))) {
495 duplicateflag = true;
498 String responseString = null;
499 if (!duplicateflag) {
500 if (onapData.getId() == 0) {
501 commonClassDao.save(onapData);
503 commonClassDao.update(onapData);
505 responseString = mapper
506 .writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
508 responseString = duplicateResponseString;
511 return utils.getResultForApi(responseString);
513 utils.setResponseData(response, psResourceDatas, responseString);
515 } catch (Exception e) {
516 utils.setErrorResponseData(response, e);
521 @RequestMapping(value = {"/ps_dictionary/remove_PSResource"}, method = {RequestMethod.POST})
522 public void removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response)
524 DictionaryUtils utils = getDictionaryUtilsInstance();
525 utils.removeData(request, response, psResourceDatas, PolicyScopeResource.class);
530 class GroupPolicyScope {
536 public String getResource() {
540 public void setResource(String resource) {
541 this.resource = resource;
544 public String getType() {
548 public void setType(String type) {
552 public String getService() {
556 public void setService(String service) {
557 this.service = service;
560 public String getClosedloop() {
564 public void setClosedloop(String closedloop) {
565 this.closedloop = closedloop;