2 * ============LICENSE_START=======================================================
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
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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
36 import org.onap.policy.rest.jpa.OnapName;
37 import org.onap.policy.rest.jpa.PolicyScopeClosedLoop;
38 import org.onap.policy.rest.jpa.PolicyScopeResource;
39 import org.onap.policy.rest.jpa.PolicyScopeService;
40 import org.onap.policy.rest.jpa.PolicyScopeType;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.MediaType;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.servlet.ModelAndView;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
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;
86 @RequestMapping(value={"/get_GroupPolicyScopeDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
87 public void getGroupPolicyScopeEntityDataByName(HttpServletResponse response){
88 DictionaryUtils utils = getDictionaryUtilsInstance();
89 utils.getDataByEntity(response, groupPolicyScopeDatas, "name", GroupPolicyScopeList.class);
92 @RequestMapping(value={"/get_GroupPolicyScopeData"}, method={RequestMethod.GET} , 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"}, method={RequestMethod.POST})
99 public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
100 DictionaryUtils utils = getDictionaryUtilsInstance();
102 boolean fromAPI = utils.isRequestFromAPI(request);
103 ObjectMapper mapper = new ObjectMapper();
104 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
105 JsonNode root = mapper.readTree(request.getReader());
106 GroupPolicyScopeList gpdata = null;
107 GroupPolicyScope groupData = null;
108 boolean duplicateGroupFlag = false;
110 gpdata = mapper.readValue(root.get(dictionaryFields).toString(), GroupPolicyScopeList.class);
112 groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
114 groupData = new GroupPolicyScope();
115 groupData.setResource(root.get(dictionaryFields).get("resource").toString().replace("\"", ""));
116 groupData.setClosedloop(root.get(dictionaryFields).get("closedloop").toString().replace("\"", ""));
117 groupData.setService(root.get(dictionaryFields).get("service").toString().replace("\"", ""));
118 groupData.setType(root.get(dictionaryFields).get("type").toString().replace("\"", ""));
122 gpdata = mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
124 groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
127 groupData = new GroupPolicyScope();
128 groupData.setResource(root.get(groupPolicyScopeListData1).get("resource").toString().replace("\"", ""));
129 groupData.setClosedloop(root.get(groupPolicyScopeListData1).get("closedloop").toString().replace("\"", ""));
130 groupData.setService(root.get(groupPolicyScopeListData1).get("service").toString().replace("\"", ""));
131 groupData.setType(root.get(groupPolicyScopeListData1).get("type").toString().replace("\"", ""));
134 if(!gpdata.getGroupName().startsWith(policyScope)){
135 String name = "PolicyScope_" + gpdata.getGroupName();
136 gpdata.setGroupName(name);
138 ArrayList<String> valueList = new ArrayList<>();
139 String resourceValue = groupData.getResource();
140 String typeValue = groupData.getType();
141 String serviceValue = groupData.getService();
142 String closedLoopValue = groupData.getClosedloop();
143 valueList.add("resource=" + resourceValue);
144 valueList.add("service=" + serviceValue);
145 valueList.add("type=" + typeValue);
146 valueList.add("closedLoopControlName=" + closedLoopValue);
147 String list = StringUtils.replaceEach(valueList.toString(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
148 gpdata.setGroupList(list);
150 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
151 if(duplicateData.isEmpty()){
152 duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupList(), "groupList", GroupPolicyScopeList.class);
153 if(duplicateData.isEmpty()){
154 duplicateGroupFlag = true;
157 boolean duplicateflag = false;
158 if(!duplicateData.isEmpty()){
159 GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0);
160 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
161 gpdata.setId(data.getId());
162 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
163 (request.getParameter(operation) == null && (data.getId() != gpdata.getId()))){
164 duplicateflag = true;
167 String responseString = null;
168 if(!duplicateflag && !duplicateGroupFlag){
169 if(gpdata.getId() == 0){
170 commonClassDao.save(gpdata);
172 commonClassDao.update(gpdata);
174 responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
175 }else if(duplicateGroupFlag){
176 responseString = "DuplicateGroup";
178 responseString = duplicateResponseString;
181 return utils.getResultForApi(responseString);
183 utils.setResponseData(response, groupPolicyScopeDatas, responseString);
185 }catch (Exception e){
186 utils.setErrorResponseData(response, e);
191 @RequestMapping(value={"/ps_dictionary/remove_GroupPolicyScope"}, method={RequestMethod.POST})
192 public void removePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
193 DictionaryUtils utils = getDictionaryUtilsInstance();
194 utils.removeData(request, response, groupPolicyScopeDatas, GroupPolicyScopeList.class);
197 @RequestMapping(value={"/get_PSClosedLoopDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
198 public void getPSClosedLoopEntityDataByName(HttpServletResponse response){
199 DictionaryUtils utils = getDictionaryUtilsInstance();
200 utils.getDataByEntity(response, psCLDatas, "name", OnapName.class);
203 @RequestMapping(value={"/get_PSClosedLoopData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
204 public void getPSClosedLoopEntityData(HttpServletResponse response){
205 DictionaryUtils utils = getDictionaryUtilsInstance();
206 utils.getData(response, psCLDatas, PolicyScopeClosedLoop.class);
209 @RequestMapping(value={"/ps_dictionary/save_psClosedLoop"}, method={RequestMethod.POST})
210 public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
211 DictionaryUtils utils = getDictionaryUtilsInstance();
213 boolean fromAPI = utils.isRequestFromAPI(request);
214 ObjectMapper mapper = new ObjectMapper();
215 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
216 JsonNode root = mapper.readTree(request.getReader());
217 PolicyScopeClosedLoop onapData;
219 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeClosedLoop.class);
221 onapData = mapper.readValue(root.get("psClosedLoopDictionaryData").toString(), PolicyScopeClosedLoop.class);
224 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
225 boolean duplicateflag = false;
226 if(!duplicateData.isEmpty()){
227 PolicyScopeClosedLoop data = (PolicyScopeClosedLoop) duplicateData.get(0);
228 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
229 onapData.setId(data.getId());
230 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
231 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
232 duplicateflag = true;
235 String responseString = null;
237 if(onapData.getId() == 0){
238 commonClassDao.save(onapData);
240 commonClassDao.update(onapData);
242 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
244 responseString = duplicateResponseString;
247 return utils.getResultForApi(responseString);
249 utils.setResponseData(response, psCLDatas, responseString);
251 }catch (Exception e){
252 utils.setErrorResponseData(response, e);
257 @RequestMapping(value={"/ps_dictionary/remove_PSClosedLoop"}, method={RequestMethod.POST})
258 public void removePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
259 DictionaryUtils utils = getDictionaryUtilsInstance();
260 utils.removeData(request, response, psCLDatas, PolicyScopeClosedLoop.class);
263 @RequestMapping(value={"/get_PSServiceDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
264 public void getPSServiceEntityDataByName(HttpServletResponse response){
265 DictionaryUtils utils = getDictionaryUtilsInstance();
266 utils.getDataByEntity(response, psServiceDatas, "name", PolicyScopeService.class);
269 @RequestMapping(value={"/get_PSServiceData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
270 public void getPSServiceEntityData(HttpServletResponse response){
271 DictionaryUtils utils = getDictionaryUtilsInstance();
272 utils.getData(response, psServiceDatas, PolicyScopeService.class);
275 @RequestMapping(value={"/ps_dictionary/save_psService"}, method={RequestMethod.POST})
276 public ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
277 DictionaryUtils utils = getDictionaryUtilsInstance();
279 boolean fromAPI = utils.isRequestFromAPI(request);
280 ObjectMapper mapper = new ObjectMapper();
281 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
282 JsonNode root = mapper.readTree(request.getReader());
283 PolicyScopeService onapData;
285 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeService.class);
287 onapData = mapper.readValue(root.get("psServiceDictionaryData").toString(), PolicyScopeService.class);
290 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
291 boolean duplicateflag = false;
292 if(!duplicateData.isEmpty()){
293 PolicyScopeService data = (PolicyScopeService) duplicateData.get(0);
294 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
295 onapData.setId(data.getId());
296 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
297 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
298 duplicateflag = true;
301 String responseString = null;
303 if(onapData.getId() == 0){
304 commonClassDao.save(onapData);
306 commonClassDao.update(onapData);
308 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
310 responseString = duplicateResponseString;
313 return utils.getResultForApi(responseString);
315 utils.setResponseData(response, psServiceDatas, responseString);
317 }catch (Exception e){
318 utils.setErrorResponseData(response, e);
323 @RequestMapping(value={"/ps_dictionary/remove_PSService"}, method={RequestMethod.POST})
324 public void removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
325 DictionaryUtils utils = getDictionaryUtilsInstance();
326 utils.removeData(request, response, psServiceDatas, PolicyScopeService.class);
329 @RequestMapping(value={"/get_PSTypeDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
330 public void getPSTypeEntityDataByName(HttpServletResponse response){
331 DictionaryUtils utils = getDictionaryUtilsInstance();
332 utils.getDataByEntity(response, psTypeDatas, "name", PolicyScopeType.class);
335 @RequestMapping(value={"/get_PSTypeData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
336 public void getPSTypeEntityData(HttpServletResponse response){
337 DictionaryUtils utils = getDictionaryUtilsInstance();
338 utils.getData(response, psTypeDatas, PolicyScopeType.class);
341 @RequestMapping(value={"/ps_dictionary/save_psType"}, method={RequestMethod.POST})
342 public ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
343 DictionaryUtils utils = getDictionaryUtilsInstance();
345 boolean fromAPI = utils.isRequestFromAPI(request);
346 ObjectMapper mapper = new ObjectMapper();
347 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
348 JsonNode root = mapper.readTree(request.getReader());
349 PolicyScopeType onapData;
351 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeType.class);
353 onapData = mapper.readValue(root.get("psTypeDictionaryData").toString(), PolicyScopeType.class);
356 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
357 boolean duplicateflag = false;
358 if(!duplicateData.isEmpty()){
359 PolicyScopeType data = (PolicyScopeType) duplicateData.get(0);
360 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
361 onapData.setId(data.getId());
362 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
363 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
364 duplicateflag = true;
367 String responseString = null;
369 if(onapData.getId() == 0){
370 commonClassDao.save(onapData);
372 commonClassDao.update(onapData);
374 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
376 responseString = duplicateResponseString;
379 return utils.getResultForApi(responseString);
381 utils.setResponseData(response, psTypeDatas, responseString);
383 }catch (Exception e){
384 utils.setErrorResponseData(response, e);
389 @RequestMapping(value={"/ps_dictionary/remove_PSType"}, method={RequestMethod.POST})
390 public void removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
391 DictionaryUtils utils = getDictionaryUtilsInstance();
392 utils.removeData(request, response, psTypeDatas, PolicyScopeType.class);
395 @RequestMapping(value={"/get_PSResourceDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
396 public void getPSResourceEntityDataByName(HttpServletResponse response){
397 DictionaryUtils utils = getDictionaryUtilsInstance();
398 utils.getDataByEntity(response, psResourceDatas, "name", PolicyScopeResource.class);
401 @RequestMapping(value={"/get_PSResourceData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
402 public void getPSResourceEntityData(HttpServletResponse response){
403 DictionaryUtils utils = getDictionaryUtilsInstance();
404 utils.getData(response, psResourceDatas, PolicyScopeResource.class);
407 @RequestMapping(value={"/ps_dictionary/save_psResource"}, method={RequestMethod.POST})
408 public ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
409 DictionaryUtils utils = getDictionaryUtilsInstance();
411 boolean fromAPI = utils.isRequestFromAPI(request);
412 ObjectMapper mapper = new ObjectMapper();
413 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
414 JsonNode root = mapper.readTree(request.getReader());
415 PolicyScopeResource onapData;
417 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeResource.class);
419 onapData = mapper.readValue(root.get("psResourceDictionaryData").toString(), PolicyScopeResource.class);
422 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
423 boolean duplicateflag = false;
424 if(!duplicateData.isEmpty()){
425 PolicyScopeResource data = (PolicyScopeResource) duplicateData.get(0);
426 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
427 onapData.setId(data.getId());
428 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
429 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
430 duplicateflag = true;
433 String responseString = null;
435 if(onapData.getId() == 0){
436 commonClassDao.save(onapData);
438 commonClassDao.update(onapData);
440 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
442 responseString = duplicateResponseString;
445 return utils.getResultForApi(responseString);
447 utils.setResponseData(response, psResourceDatas, responseString);
449 }catch (Exception e){
450 utils.setErrorResponseData(response, e);
455 @RequestMapping(value={"/ps_dictionary/remove_PSResource"}, method={RequestMethod.POST})
456 public void removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
457 DictionaryUtils utils = getDictionaryUtilsInstance();
458 utils.removeData(request, response, psResourceDatas, PolicyScopeResource.class);
462 class GroupPolicyScope{
467 public String getResource() {
470 public void setResource(String resource) {
471 this.resource = resource;
473 public String getType() {
476 public void setType(String type) {
479 public String getService() {
482 public void setService(String service) {
483 this.service = service;
485 public String getClosedloop() {
488 public void setClosedloop(String closedloop) {
489 this.closedloop = closedloop;