Convert tabs to space in ONAP PAP REST1
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / PolicyScopeDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
47
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 @Controller
53 public class PolicyScopeDictionaryController {
54
55     private static final Logger LOGGER  = FlexLogger.getLogger(PolicyScopeDictionaryController.class);
56
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";
68
69     public PolicyScopeDictionaryController(){
70         super();
71     }
72
73     private DictionaryUtils getDictionaryUtilsInstance(){
74         return DictionaryUtils.getDictionaryUtils();
75     }
76
77     @Autowired
78     public PolicyScopeDictionaryController(CommonClassDao commonClassDao){
79         PolicyScopeDictionaryController.commonClassDao = commonClassDao;
80     }
81
82     public void setCommonClassDao(CommonClassDao commonClassDao){
83         PolicyScopeDictionaryController.commonClassDao = commonClassDao;
84     }
85
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);
90     }
91
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);
96     }
97
98     @RequestMapping(value={"/ps_dictionary/save_psGroupPolicyScope"}, method={RequestMethod.POST})
99     public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
100         DictionaryUtils utils = getDictionaryUtilsInstance();
101         try {
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;
109             if (fromAPI) {
110                 gpdata = mapper.readValue(root.get(dictionaryFields).toString(), GroupPolicyScopeList.class);
111                 try{
112                     groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
113                 }catch(Exception e){
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("\"", ""));
119                     LOGGER.error(e);
120                 }
121             } else {
122                 gpdata = mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
123                 try{
124                     groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
125                 }catch(Exception e){
126                     LOGGER.error(e);
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("\"", ""));
132                 }
133             }
134             if(!gpdata.getGroupName().startsWith(policyScope)){
135                 String name = "PolicyScope_" + gpdata.getGroupName();
136                 gpdata.setGroupName(name);
137             }
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);
149
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;
155                 }
156             }
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;
165                 }
166             }
167             String responseString = null;
168             if(!duplicateflag && !duplicateGroupFlag){
169                 if(gpdata.getId() == 0){
170                     commonClassDao.save(gpdata);
171                 }else{
172                     commonClassDao.update(gpdata);
173                 }
174                 responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
175             }else if(duplicateGroupFlag){
176                 responseString = "DuplicateGroup";
177             }else{
178                 responseString = duplicateResponseString;
179             }
180             if(fromAPI){
181                 return utils.getResultForApi(responseString);
182             }else{
183                 utils.setResponseData(response, groupPolicyScopeDatas, responseString);
184             }
185         }catch (Exception e){
186             utils.setErrorResponseData(response, e);
187         }
188         return null;
189     }
190
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);
195     }
196
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);
201     }
202
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);
207     }
208
209     @RequestMapping(value={"/ps_dictionary/save_psClosedLoop"}, method={RequestMethod.POST})
210     public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
211         DictionaryUtils utils = getDictionaryUtilsInstance();
212         try {
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;
218             if(fromAPI){
219                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeClosedLoop.class);
220             }else{
221                 onapData = mapper.readValue(root.get("psClosedLoopDictionaryData").toString(), PolicyScopeClosedLoop.class);
222             }
223
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;
233                 }
234             }
235             String responseString = null;
236             if(!duplicateflag){
237                 if(onapData.getId() == 0){
238                     commonClassDao.save(onapData);
239                 }else{
240                     commonClassDao.update(onapData);
241                 }
242                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
243             }else{
244                 responseString = duplicateResponseString;
245             }
246             if(fromAPI){
247                 return utils.getResultForApi(responseString);
248             }else{
249                 utils.setResponseData(response, psCLDatas, responseString);
250             }
251         }catch (Exception e){
252             utils.setErrorResponseData(response, e);
253         }
254         return null;
255     }
256
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);
261     }
262
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);
267     }
268
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);
273     }
274
275     @RequestMapping(value={"/ps_dictionary/save_psService"}, method={RequestMethod.POST})
276     public ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
277         DictionaryUtils utils = getDictionaryUtilsInstance();
278         try {
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;
284             if (fromAPI) {
285                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeService.class);
286             } else {
287                 onapData = mapper.readValue(root.get("psServiceDictionaryData").toString(), PolicyScopeService.class);
288             }
289
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;
299                 }
300             }
301             String responseString = null;
302             if(!duplicateflag){
303                 if(onapData.getId() == 0){
304                     commonClassDao.save(onapData);
305                 }else{
306                     commonClassDao.update(onapData);
307                 }
308                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
309             }else{
310                 responseString = duplicateResponseString;
311             }
312             if(fromAPI){
313                 return utils.getResultForApi(responseString);
314             }else{
315                 utils.setResponseData(response, psServiceDatas, responseString);
316             }
317         }catch (Exception e){
318             utils.setErrorResponseData(response, e);
319         }
320         return null;
321     }
322
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);
327     }
328
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);
333     }
334
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);
339     }
340
341     @RequestMapping(value={"/ps_dictionary/save_psType"}, method={RequestMethod.POST})
342     public ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
343         DictionaryUtils utils = getDictionaryUtilsInstance();
344         try {
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;
350             if(fromAPI){
351                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeType.class);
352             }else{
353                 onapData = mapper.readValue(root.get("psTypeDictionaryData").toString(), PolicyScopeType.class);
354             }
355
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;
365                 }
366             }
367             String responseString = null;
368             if(!duplicateflag){
369                 if(onapData.getId() == 0){
370                     commonClassDao.save(onapData);
371                 }else{
372                     commonClassDao.update(onapData);
373                 }
374                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
375             }else{
376                 responseString = duplicateResponseString;
377             }
378             if(fromAPI){
379                 return utils.getResultForApi(responseString);
380             }else{
381                 utils.setResponseData(response, psTypeDatas, responseString);
382             }
383         }catch (Exception e){
384             utils.setErrorResponseData(response, e);
385         }
386         return null;
387     }
388
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);
393     }
394
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);
399     }
400
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);
405     }
406
407     @RequestMapping(value={"/ps_dictionary/save_psResource"}, method={RequestMethod.POST})
408     public ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
409         DictionaryUtils utils = getDictionaryUtilsInstance();
410         try {
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;
416             if (fromAPI) {
417                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), PolicyScopeResource.class);
418             } else {
419                 onapData = mapper.readValue(root.get("psResourceDictionaryData").toString(), PolicyScopeResource.class);
420             }
421
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;
431                 }
432             }
433             String responseString = null;
434             if(!duplicateflag){
435                 if(onapData.getId() == 0){
436                     commonClassDao.save(onapData);
437                 }else{
438                     commonClassDao.update(onapData);
439                 }
440                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
441             }else{
442                 responseString = duplicateResponseString;
443             }
444             if(fromAPI){
445                 return utils.getResultForApi(responseString);
446             }else{
447                 utils.setResponseData(response, psResourceDatas, responseString);
448             }
449         }catch (Exception e){
450             utils.setErrorResponseData(response, e);
451         }
452         return null;
453     }
454
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);
459     }
460 }
461
462 class GroupPolicyScope{
463     String resource;
464     String type;
465     String service;
466     String closedloop;
467     public String getResource() {
468         return resource;
469     }
470     public void setResource(String resource) {
471         this.resource = resource;
472     }
473     public String getType() {
474         return type;
475     }
476     public void setType(String type) {
477         this.type = type;
478     }
479     public String getService() {
480         return service;
481     }
482     public void setService(String service) {
483         this.service = service;
484     }
485     public String getClosedloop() {
486         return closedloop;
487     }
488     public void setClosedloop(String closedloop) {
489         this.closedloop = closedloop;
490     }
491
492 }