Resolved Fortify System Information Leak issues
[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 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.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.json.JSONObject;
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.JsonMessage;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
40 import org.onap.policy.rest.jpa.PolicyScopeClosedLoop;
41 import org.onap.policy.rest.jpa.PolicyScopeResource;
42 import org.onap.policy.rest.jpa.PolicyScopeService;
43 import org.onap.policy.rest.jpa.PolicyScopeType;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.onap.policy.utils.PolicyUtils;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.MediaType;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.servlet.ModelAndView;
51
52 import com.fasterxml.jackson.databind.DeserializationFeature;
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 @Controller
57 public class PolicyScopeDictionaryController {
58
59         private static final Logger LOGGER  = FlexLogger.getLogger(PolicyScopeDictionaryController.class);
60
61         private static CommonClassDao commonClassDao;
62         private static String successMapKey = "successMapKey";
63         private static String exceptionOccured = "Exception Occured";
64         private static String successMessage = "success";
65         private static String operation = "operation";
66         private static String getDictionary =  "getDictionary";
67         private static String errorMsg  = "error";
68         private static String dictionaryDBQuery = "dictionaryDBQuery";
69         private static String apiflag = "apiflag";
70         private static String groupPolicyScopeListData1 = "groupPolicyScopeListData1";
71         private static String policyScope= "PolicyScope";
72         private static String update = "update";
73         private static String duplicateResponseString = "Duplicate";
74         private static String successMsg = "Success";
75         private static String existsResponseString = "Exists";
76         private static String utf8 = "UTF-8";
77         private static String applicationJsonContentType = "application / json";
78         /*
79          * This is an empty constructor
80          */
81         public PolicyScopeDictionaryController(){}      
82
83         @Autowired
84         public PolicyScopeDictionaryController(CommonClassDao commonClassDao){
85                 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
86         }
87         
88         
89         public UserInfo getUserInfo(String loginId){
90                 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId); 
91         }
92
93         @RequestMapping(value={"/get_GroupPolicyScopeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
94         public void getGroupPolicyScopeEntityDataByName(HttpServletResponse response){
95                 try{
96                         Map<String, Object> model = new HashMap<>();
97                         ObjectMapper mapper = new ObjectMapper();
98                         model.put("groupPolicyScopeListDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(GroupPolicyScopeList.class, "name")));
99                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
100                         JSONObject j = new JSONObject(msg);
101                         response.getWriter().write(j.toString());
102                 }
103                 catch (Exception e){
104                         LOGGER.error(exceptionOccured+e);
105                 }
106         }
107
108         @RequestMapping(value={"/get_GroupPolicyScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
109         public void getGroupPolicyScopeEntityData(HttpServletResponse response){
110                 try{
111                         Map<String, Object> model = new HashMap<>();
112                         ObjectMapper mapper = new ObjectMapper();
113                         model.put("groupPolicyScopeListDatas", mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class)));
114                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
115                         JSONObject j = new JSONObject(msg);
116                         response.addHeader(successMapKey, successMessage); 
117                         response.addHeader(operation, getDictionary);
118                         response.getWriter().write(j.toString());
119                 }
120                 catch (Exception e){
121                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
122                         response.addHeader(errorMsg, dictionaryDBQuery);
123                         LOGGER.error(e);
124                 }
125         }
126
127         @RequestMapping(value={"/ps_dictionary/save_psGroupPolicyScope"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
128         public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
129                 try {
130                         boolean duplicateflag = false;
131                         boolean isFakeUpdate = false;
132                         boolean duplicateGroupFlag = false;
133                         boolean fromAPI = false;
134                         if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
135                                 fromAPI = true;
136                         }
137
138                         ObjectMapper mapper = new ObjectMapper();
139                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
140                         JsonNode root = mapper.readTree(request.getReader());
141                         GroupPolicyScopeList gpdata = null;
142                         GroupPolicyScope groupData = null;
143                         if (fromAPI) {
144                                 gpdata = (GroupPolicyScopeList)mapper.readValue(root.get("dictionaryFields").toString(), GroupPolicyScopeList.class);
145                                 try{
146                                         groupData = (GroupPolicyScope)mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
147                                 }catch(Exception e){
148                                         groupData = new GroupPolicyScope();
149                                         groupData.setResource(root.get("dictionaryFields").get("resource").toString().replace("\"", ""));
150                                         groupData.setClosedloop(root.get("dictionaryFields").get("closedloop").toString().replace("\"", ""));
151                                         groupData.setService(root.get("dictionaryFields").get("service").toString().replace("\"", ""));
152                                         groupData.setType(root.get("dictionaryFields").get("type").toString().replace("\"", ""));
153                                         LOGGER.error(e);
154                                 }
155
156                                 if(!gpdata.getGroupName().startsWith(policyScope)){
157                                         String name = "PolicyScope_" + gpdata.getGroupName();
158                                         gpdata.setGroupName(name);
159                                 }
160
161                                 //check if update operation or create, get id for data to be updated and update attributeData
162                                 if ((update).equals(request.getParameter(operation))) {
163
164                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
165                                         int id = 0;
166                                         for (int i =0; i< duplicateData.size(); i++){
167                                                 GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0);
168                                                 id = data.getId();
169                                         }                   
170
171                                         if(id==0){
172                                                 isFakeUpdate=true;
173                                                 gpdata.setId(1);
174                                         } else {
175                                                 gpdata.setId(id);
176                                         }
177
178                                 }
179                         } else {
180                                 gpdata = (GroupPolicyScopeList)mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
181
182                                 try{
183                                         groupData = (GroupPolicyScope)mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class);
184                                 }catch(Exception e){
185                                         LOGGER.error(e);
186                                         groupData = new GroupPolicyScope();
187                                         groupData.setResource(root.get(groupPolicyScopeListData1).get("resource").toString().replace("\"", ""));
188                                         groupData.setClosedloop(root.get(groupPolicyScopeListData1).get("closedloop").toString().replace("\"", ""));
189                                         groupData.setService(root.get(groupPolicyScopeListData1).get("service").toString().replace("\"", ""));
190                                         groupData.setType(root.get(groupPolicyScopeListData1).get("type").toString().replace("\"", ""));
191                                 }
192                                 if(!gpdata.getGroupName().startsWith(policyScope)){
193                                         String name = "PolicyScope_" + gpdata.getGroupName();
194                                         gpdata.setGroupName(name);
195                                 }
196
197                         }
198                         ArrayList<String> valueList = new ArrayList<>();
199                         String resourceValue = groupData.getResource();
200                         String typeValue = groupData.getType();
201                         String serviceValue = groupData.getService();
202                         String closedLoopValue = groupData.getClosedloop();
203                         valueList.add("resource=" + resourceValue);
204                         valueList.add("service=" + serviceValue);
205                         valueList.add("type=" + typeValue);
206                         valueList.add("closedLoopControlName="  + closedLoopValue);
207                         String list = StringUtils.replaceEach(valueList.toString(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
208                         gpdata.setGroupList(list);
209                         if(!gpdata.getGroupName().startsWith(policyScope)){
210                                 String name = "PolicyScope_" + gpdata.getGroupName();
211                                 gpdata.setGroupName(name);
212                         }
213                         if(gpdata.getId() == 0){
214                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
215                                 if(!duplicateData.isEmpty()){
216                                         duplicateflag = true;
217                                 }else{
218                                         duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupList(), "groupList", GroupPolicyScopeList.class);
219                                         if(!duplicateData.isEmpty()){
220                                                 duplicateGroupFlag = true;
221                                         }else{
222                                                 commonClassDao.save(gpdata);
223                                         }
224                                 }
225                         }else{
226                                 if(!isFakeUpdate) {
227                                         List<Object> duplicateGroupList =  commonClassDao.checkExistingGroupListforUpdate(gpdata.getGroupList(), gpdata.getGroupName());
228                                         if(!duplicateGroupList.isEmpty()) {
229                                                 duplicateGroupFlag = true;
230                                         } else {
231                                                 commonClassDao.update(gpdata); 
232                                         }
233                                 } 
234                         }
235                         String responseString = "";
236                         if(duplicateflag){
237                                 responseString = duplicateResponseString;
238                         }else if(duplicateGroupFlag){
239                                 responseString = "DuplicateGroup";
240                         }else{
241                                 responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
242                         }
243
244                         if (fromAPI) {
245                                 if (responseString!=null && !(duplicateResponseString).equals(responseString) && !("DuplicateGroup").equals(responseString)) {
246                                         if(isFakeUpdate){
247                                                 responseString = existsResponseString;
248                                         } else {
249                                                 responseString = successMsg;
250                                         }
251                                 }
252
253                                 ModelAndView result = new ModelAndView();
254                                 result.setViewName(responseString);
255                                 return result;
256                         } else {
257
258                                 response.setCharacterEncoding(utf8);
259                                 response.setContentType(applicationJsonContentType);
260                                 request.setCharacterEncoding(utf8);
261
262                                 PrintWriter out = response.getWriter();
263                                 JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
264                                 out.write(j.toString());
265                                 return null;
266                         }
267                 }catch (Exception e){
268                         LOGGER.error(e);
269                         response.setCharacterEncoding(utf8);
270                         request.setCharacterEncoding(utf8);
271                         PrintWriter out = response.getWriter();
272                         out.write(PolicyUtils.CATCH_EXCEPTION);
273                 }
274                 return null;
275         }
276
277         @RequestMapping(value={"/ps_dictionary/remove_GroupPolicyScope"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
278         public ModelAndView removePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
279                 try{
280                         ObjectMapper mapper = new ObjectMapper();
281                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
282                         JsonNode root = mapper.readTree(request.getReader());
283                         GroupPolicyScopeList onapData = (GroupPolicyScopeList)mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
284                         commonClassDao.delete(onapData);
285                         response.setCharacterEncoding(utf8);
286                         response.setContentType(applicationJsonContentType);
287                         request.setCharacterEncoding(utf8);
288
289                         PrintWriter out = response.getWriter();
290
291                         String responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
292                         JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
293                         out.write(j.toString());
294
295                         return null;
296                 }
297                 catch (Exception e){
298                         LOGGER.error(e);
299                         response.setCharacterEncoding(utf8);
300                         request.setCharacterEncoding(utf8);
301                         PrintWriter out = response.getWriter();
302                         out.write(PolicyUtils.CATCH_EXCEPTION);
303                 }
304                 return null;
305         }
306
307         @RequestMapping(value={"/get_PSClosedLoopDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
308         public void getPSClosedLoopEntityDataByName(HttpServletResponse response){
309                 try{
310                         Map<String, Object> model = new HashMap<>();
311                         ObjectMapper mapper = new ObjectMapper();
312                         model.put("psClosedLoopDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeClosedLoop.class, "name")));
313                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
314                         JSONObject j = new JSONObject(msg);
315                         response.getWriter().write(j.toString());
316                 }
317                 catch (Exception e){
318                         LOGGER.error(exceptionOccured+e);
319                 }
320         }
321
322         @RequestMapping(value={"/get_PSClosedLoopData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
323         public void getPSClosedLoopEntityData(HttpServletResponse response){
324                 try{
325                         Map<String, Object> model = new HashMap<>();
326                         ObjectMapper mapper = new ObjectMapper();
327                         model.put("psClosedLoopDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class)));
328                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
329                         JSONObject j = new JSONObject(msg);
330                         response.addHeader(successMapKey, successMessage); 
331                         response.addHeader(operation, getDictionary);
332                         response.getWriter().write(j.toString());
333                 }
334                 catch (Exception e){
335                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
336                         response.addHeader(errorMsg, dictionaryDBQuery);
337                         LOGGER.error(e);
338                 }
339         }
340
341         @RequestMapping(value={"/ps_dictionary/save_psClosedLoop"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
342         public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
343                 try {
344                         boolean duplicateflag = false;
345                         boolean isFakeUpdate = false;
346                         boolean fromAPI = false;
347                         if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
348                                 fromAPI = true;
349                         }
350                         ObjectMapper mapper = new ObjectMapper();
351                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
352                         JsonNode root = mapper.readTree(request.getReader());
353                         PolicyScopeClosedLoop onapData;
354                         if (fromAPI) {
355                                 onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeClosedLoop.class);
356
357                                 //check if update operation or create, get id for data to be updated and update attributeData
358                                 if ((update).equals(request.getParameter(operation))) {
359                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
360                                         int id = 0;
361                                         for (int i =0; i< duplicateData.size(); i++){
362                                                 PolicyScopeClosedLoop data = (PolicyScopeClosedLoop) duplicateData.get(0);
363                                                 id = data.getId();
364                                         }
365
366                                         if(id==0){
367                                                 isFakeUpdate=true;
368                                                 onapData.setId(1);
369                                         } else {
370                                                 onapData.setId(id);
371                                         }   
372
373                                 }
374                         } else {
375                                 onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("psClosedLoopDictionaryData").toString(), PolicyScopeClosedLoop.class);
376                         }
377                         if(onapData.getId() == 0){
378                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
379                                 if(!duplicateData.isEmpty()){
380                                         duplicateflag = true;
381                                 }else{
382                                         commonClassDao.save(onapData);
383                                 }
384                         }else{
385                                 if(!isFakeUpdate) {
386                                         commonClassDao.update(onapData); 
387                                 }
388                         } 
389                         String responseString = "";
390                         if(duplicateflag){
391                                 responseString = duplicateResponseString;
392                         }else{
393                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
394                         }
395
396                         if (fromAPI) {
397                                 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
398                                         if(isFakeUpdate){
399                                                 responseString = existsResponseString;
400                                         } else {
401                                                 responseString = successMsg;
402                                         }
403                                 }
404                                 ModelAndView result = new ModelAndView();
405                                 result.setViewName(responseString);
406                                 return result;
407                         } else {
408                                 response.setCharacterEncoding(utf8);
409                                 response.setContentType(applicationJsonContentType);
410                                 request.setCharacterEncoding(utf8);
411
412                                 PrintWriter out = response.getWriter();
413                                 JSONObject j = new JSONObject("{psClosedLoopDictionaryDatas: " + responseString + "}");
414
415                                 out.write(j.toString());
416
417                                 return null;
418                         }
419
420                 }catch (Exception e){
421                         response.setCharacterEncoding(utf8);
422                         request.setCharacterEncoding(utf8);
423                         PrintWriter out = response.getWriter();
424                         out.write(PolicyUtils.CATCH_EXCEPTION);
425                         LOGGER.error(e);
426                 }
427                 return null;
428         }
429
430         @RequestMapping(value={"/ps_dictionary/remove_PSClosedLoop"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
431         public ModelAndView removePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
432                 try{
433                         ObjectMapper mapper = new ObjectMapper();
434                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
435                         JsonNode root = mapper.readTree(request.getReader());
436                         PolicyScopeClosedLoop onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("data").toString(), PolicyScopeClosedLoop.class);
437                         commonClassDao.delete(onapData);
438                         response.setCharacterEncoding(utf8);
439                         response.setContentType(applicationJsonContentType);
440                         request.setCharacterEncoding(utf8);
441
442                         PrintWriter out = response.getWriter();
443
444                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
445                         JSONObject j = new JSONObject("{psClosedLoopDictionaryDatas: " + responseString + "}");
446                         out.write(j.toString());
447
448                         return null;
449                 }
450                 catch (Exception e){
451                         LOGGER.error(e);
452                         response.setCharacterEncoding(utf8);
453                         request.setCharacterEncoding(utf8);
454                         PrintWriter out = response.getWriter();
455                         out.write(PolicyUtils.CATCH_EXCEPTION);
456                 }
457                 return null;
458         }
459
460         @RequestMapping(value={"/get_PSServiceDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
461         public void getPSServiceEntityDataByName(HttpServletResponse response){
462                 try{
463                         Map<String, Object> model = new HashMap<>();
464                         ObjectMapper mapper = new ObjectMapper();
465                         model.put("psServiceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeService.class, "name")));
466                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
467                         JSONObject j = new JSONObject(msg);
468                         response.getWriter().write(j.toString());
469                 }
470                 catch (Exception e){
471                         LOGGER.error(exceptionOccured+e);
472                 }
473         }
474
475         @RequestMapping(value={"/get_PSServiceData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
476         public void getPSServiceEntityData(HttpServletResponse response){
477                 try{
478                         Map<String, Object> model = new HashMap<>();
479                         ObjectMapper mapper = new ObjectMapper();
480                         model.put("psServiceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class)));
481                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
482                         JSONObject j = new JSONObject(msg);
483                         response.addHeader(successMapKey, successMessage); 
484                         response.addHeader(operation, getDictionary);
485                         response.getWriter().write(j.toString());
486                 }
487                 catch (Exception e){
488                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
489                         response.addHeader(errorMsg, dictionaryDBQuery);
490                         LOGGER.error(e);
491                 }
492         }
493
494         @RequestMapping(value={"/ps_dictionary/save_psService"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
495         public ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
496                 try {
497                         boolean duplicateflag = false;
498                         boolean isFakeUpdate = false;
499                         boolean fromAPI = false;
500                         if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
501                                 fromAPI = true;
502                         }
503                         ObjectMapper mapper = new ObjectMapper();
504                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
505                         JsonNode root = mapper.readTree(request.getReader());
506                         PolicyScopeService onapData;
507                         if (fromAPI) {
508                                 onapData = (PolicyScopeService)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeService.class);
509
510                                 //check if update operation or create, get id for data to be updated and update attributeData
511                                 if ((update).equals(request.getParameter(operation))) {
512                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
513                                         int id = 0;
514                                         for (int i =0; i< duplicateData.size(); i++){
515                                                 PolicyScopeService data = (PolicyScopeService) duplicateData.get(0);
516                                                 id = data.getId();
517                                         }
518                                         if(id==0){
519                                                 isFakeUpdate=true;
520                                                 onapData.setId(1);
521                                         } else {
522                                                 onapData.setId(id);
523                                         }
524                                 }
525                         } else {
526                                 onapData = (PolicyScopeService)mapper.readValue(root.get("psServiceDictionaryData").toString(), PolicyScopeService.class);
527                         }
528                         if(onapData.getId() == 0){
529                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
530                                 if(!duplicateData.isEmpty()){
531                                         duplicateflag = true;
532                                 }else{
533                                         commonClassDao.save(onapData);
534                                 }
535                         }else{
536                                 if(!isFakeUpdate) {
537                                         commonClassDao.update(onapData); 
538                                 }
539                         } 
540
541                         String responseString = "";
542                         if(duplicateflag){
543                                 responseString = duplicateResponseString;
544                         }else{
545                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
546                         }   
547
548                         if (fromAPI) {
549                                 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
550                                         if(isFakeUpdate){
551                                                 responseString = existsResponseString;
552                                         } else {
553                                                 responseString = successMsg;
554                                         }
555                                 }
556                                 ModelAndView result = new ModelAndView();
557                                 result.setViewName(responseString);
558                                 return result;
559
560                         } else {
561                                 response.setCharacterEncoding(utf8);
562                                 response.setContentType(applicationJsonContentType);
563                                 request.setCharacterEncoding(utf8);
564
565                                 PrintWriter out = response.getWriter();
566                                 JSONObject j = new JSONObject("{psServiceDictionaryDatas: " + responseString + "}");
567                                 out.write(j.toString());
568                                 return null;
569                         }
570                 }catch (Exception e){
571                         response.setCharacterEncoding(utf8);
572                         request.setCharacterEncoding(utf8);
573                         PrintWriter out = response.getWriter();
574                         out.write(PolicyUtils.CATCH_EXCEPTION);
575                         LOGGER.error(e);
576                 }
577                 return null;
578         }
579
580         @RequestMapping(value={"/ps_dictionary/remove_PSService"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
581         public ModelAndView removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
582                 try{
583                         ObjectMapper mapper = new ObjectMapper();
584                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
585                         JsonNode root = mapper.readTree(request.getReader());
586                         PolicyScopeService onapData = (PolicyScopeService)mapper.readValue(root.get("data").toString(), PolicyScopeService.class);
587                         commonClassDao.delete(onapData);
588                         response.setCharacterEncoding(utf8);
589                         response.setContentType(applicationJsonContentType);
590                         request.setCharacterEncoding(utf8);
591
592                         PrintWriter out = response.getWriter();
593
594                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
595                         JSONObject j = new JSONObject("{psServiceDictionaryDatas: " + responseString + "}");
596                         out.write(j.toString());
597
598                         return null;
599                 }
600                 catch (Exception e){
601                         LOGGER.error(e);
602                         response.setCharacterEncoding(utf8);
603                         request.setCharacterEncoding(utf8);
604                         PrintWriter out = response.getWriter();
605                         out.write(PolicyUtils.CATCH_EXCEPTION);
606                 }
607                 return null;
608         }
609
610         @RequestMapping(value={"/get_PSTypeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
611         public void getPSTypeEntityDataByName(HttpServletResponse response){
612                 try{
613                         Map<String, Object> model = new HashMap<>();
614                         ObjectMapper mapper = new ObjectMapper();
615                         model.put("psTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeType.class, "name")));
616                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
617                         JSONObject j = new JSONObject(msg);
618                         response.getWriter().write(j.toString());
619                 }
620                 catch (Exception e){
621                         LOGGER.error(exceptionOccured+e);
622                 }
623         }
624
625         @RequestMapping(value={"/get_PSTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
626         public void getPSTypeEntityData(HttpServletResponse response){
627                 try{
628                         Map<String, Object> model = new HashMap<>();
629                         ObjectMapper mapper = new ObjectMapper();
630                         model.put("psTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class)));
631                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
632                         JSONObject j = new JSONObject(msg);
633                         response.addHeader(successMapKey, successMessage); 
634                         response.addHeader(operation, getDictionary);
635                         response.getWriter().write(j.toString());
636                 }
637                 catch (Exception e){
638                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
639                         response.addHeader(errorMsg, dictionaryDBQuery);
640                         LOGGER.error(e);
641                 }
642         }
643
644         @RequestMapping(value={"/ps_dictionary/save_psType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
645         public ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
646                 try {
647                         boolean duplicateflag = false;
648                         boolean isFakeUpdate = false;
649                         boolean fromAPI = false;
650                         if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
651                                 fromAPI = true;
652                         }
653                         ObjectMapper mapper = new ObjectMapper();
654                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
655                         JsonNode root = mapper.readTree(request.getReader());
656                         PolicyScopeType onapData;
657                         if (fromAPI) {
658                                 onapData = (PolicyScopeType)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeType.class);
659
660                                 //check if update operation or create, get id for data to be updated and update attributeData
661                                 if ((update).equals(request.getParameter(operation))) {
662                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
663                                         int id = 0;
664                                         for (int i =0; i< duplicateData.size(); i++){
665                                                 PolicyScopeType data = (PolicyScopeType) duplicateData.get(0);
666                                                 id = data.getId();
667                                         }
668
669                                         if(id==0){
670                                                 isFakeUpdate=true;
671                                                 onapData.setId(1);
672                                         } else {
673                                                 onapData.setId(id);
674                                         }   
675
676                                 }
677                         } else {
678                                 onapData = (PolicyScopeType)mapper.readValue(root.get("psTypeDictionaryData").toString(), PolicyScopeType.class);
679                         }
680                         if(onapData.getId() == 0){
681                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
682                                 if(!duplicateData.isEmpty()){
683                                         duplicateflag = true;
684                                 }else{
685                                         commonClassDao.save(onapData);
686                                 }
687                         }else{
688                                 if(!isFakeUpdate) {
689                                         commonClassDao.update(onapData); 
690                                 }
691                         } 
692                         String responseString = "";
693                         if(duplicateflag){
694                                 responseString = duplicateResponseString;
695                         }else{
696                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
697                         } 
698
699                         if (fromAPI) {
700                                 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
701                                         if(isFakeUpdate){
702                                                 responseString = existsResponseString;
703                                         } else {
704                                                 responseString = successMsg;
705                                         }
706
707                                 }
708                                 ModelAndView result = new ModelAndView();
709                                 result.setViewName(responseString);
710                                 return result;
711
712                         } else {
713
714                                 response.setCharacterEncoding(utf8);
715                                 response.setContentType(applicationJsonContentType);
716                                 request.setCharacterEncoding(utf8);
717
718                                 PrintWriter out = response.getWriter();
719                                 JSONObject j = new JSONObject("{psTypeDictionaryDatas: " + responseString + "}");
720
721                                 out.write(j.toString());
722
723                                 return null;
724                         }
725
726                 }catch (Exception e){
727                         response.setCharacterEncoding(utf8);
728                         request.setCharacterEncoding(utf8);
729                         PrintWriter out = response.getWriter();
730                         out.write(PolicyUtils.CATCH_EXCEPTION);
731                         LOGGER.error(e);
732                 }
733                 return null;
734         }
735
736         @RequestMapping(value={"/ps_dictionary/remove_PSType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
737         public ModelAndView removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
738                 try{
739                         ObjectMapper mapper = new ObjectMapper();
740                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
741                         JsonNode root = mapper.readTree(request.getReader());
742                         PolicyScopeType onapData = (PolicyScopeType)mapper.readValue(root.get("data").toString(), PolicyScopeType.class);
743                         commonClassDao.delete(onapData);
744                         response.setCharacterEncoding(utf8);
745                         response.setContentType(applicationJsonContentType);
746                         request.setCharacterEncoding(utf8);
747
748                         PrintWriter out = response.getWriter();
749
750                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
751                         JSONObject j = new JSONObject("{psTypeDictionaryDatas: " + responseString + "}");
752                         out.write(j.toString());
753
754                         return null;
755                 }
756                 catch (Exception e){
757                         LOGGER.error(e);
758                         response.setCharacterEncoding(utf8);
759                         request.setCharacterEncoding(utf8);
760                         PrintWriter out = response.getWriter();
761                         out.write(PolicyUtils.CATCH_EXCEPTION);
762                 }
763                 return null;
764         }
765
766         @RequestMapping(value={"/get_PSResourceDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
767         public void getPSResourceEntityDataByName(HttpServletResponse response){
768                 try{
769                         Map<String, Object> model = new HashMap<>();
770                         ObjectMapper mapper = new ObjectMapper();
771                         model.put("psResourceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeResource.class, "name")));
772                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
773                         JSONObject j = new JSONObject(msg);
774                         response.getWriter().write(j.toString());
775                 }
776                 catch (Exception e){
777                         LOGGER.error(exceptionOccured+e);
778                 }
779         }
780
781         @RequestMapping(value={"/get_PSResourceData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
782         public void getPSResourceEntityData(HttpServletResponse response){
783                 try{
784                         Map<String, Object> model = new HashMap<>();
785                         ObjectMapper mapper = new ObjectMapper();
786                         model.put("psResourceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class)));
787                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
788                         JSONObject j = new JSONObject(msg);
789                         response.addHeader("successMapKey", successMessage); 
790                         response.addHeader(operation, getDictionary);
791                         response.getWriter().write(j.toString());
792                 }
793                 catch (Exception e){
794                         LOGGER.error(e);
795                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
796                         response.addHeader(errorMsg, dictionaryDBQuery);
797                         LOGGER.error(e);
798                 }
799         }
800
801         @RequestMapping(value={"/ps_dictionary/save_psResource"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
802         public ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
803                 try {
804                         boolean duplicateflag = false;
805                         boolean isFakeUpdate = false;
806                         boolean fromAPI = false;
807                         if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
808                                 fromAPI = true;
809                         }
810                         ObjectMapper mapper = new ObjectMapper();
811                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
812                         JsonNode root = mapper.readTree(request.getReader());
813                         PolicyScopeResource onapData;
814                         if (fromAPI) {
815                                 onapData = (PolicyScopeResource)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeResource.class);
816
817                                 //check if update operation or create, get id for data to be updated and update attributeData
818                                 if ((update).equals(request.getParameter(operation))) {
819                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
820                                         PolicyScopeResource data = (PolicyScopeResource) duplicateData.get(0);
821                                         int id = data.getId();
822
823                                         if(id==0){
824                                                 isFakeUpdate=true;
825                                                 onapData.setId(1);
826                                         } else {
827                                                 onapData.setId(id);
828                                         }
829                                 }
830                         } else {
831                                 onapData = (PolicyScopeResource)mapper.readValue(root.get("psResourceDictionaryData").toString(), PolicyScopeResource.class);
832                         }
833                         if(onapData.getId() == 0){
834                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
835                                 if(!duplicateData.isEmpty()){
836                                         duplicateflag = true;
837                                 }else{
838                                         commonClassDao.save(onapData);
839                                 }
840                         }else{
841                                 if(!isFakeUpdate) {
842                                         commonClassDao.update(onapData); 
843                                 }
844                         } 
845                         String responseString = "";
846                         if(duplicateflag){
847                                 responseString = duplicateResponseString;
848                         }else{
849                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
850                         }   
851
852                         if (fromAPI) {
853                                 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
854                                         if(isFakeUpdate){
855                                                 responseString = existsResponseString;
856                                         } else {
857                                                 responseString = successMsg;
858                                         }
859
860                                 }
861                                 ModelAndView result = new ModelAndView();
862                                 result.setViewName(responseString);
863                                 return result;
864
865                         } else {
866
867                                 response.setCharacterEncoding(utf8);
868                                 response.setContentType(applicationJsonContentType);
869                                 request.setCharacterEncoding(utf8);
870
871                                 PrintWriter out = response.getWriter();
872                                 JSONObject j = new JSONObject("{psResourceDictionaryDatas: " + responseString + "}");
873
874                                 out.write(j.toString());
875
876                                 return null;
877                         }
878
879                 }catch (Exception e){
880                         LOGGER.error(e);
881                         response.setCharacterEncoding(utf8);
882                         request.setCharacterEncoding(utf8);
883                         PrintWriter out = response.getWriter();
884                         out.write(PolicyUtils.CATCH_EXCEPTION);
885                 }
886                 return null;
887         }
888
889         @RequestMapping(value={"/ps_dictionary/remove_PSResource"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
890         public ModelAndView removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
891                 try{
892                         ObjectMapper mapper = new ObjectMapper();
893                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
894                         JsonNode root = mapper.readTree(request.getReader());
895                         PolicyScopeResource onapData = (PolicyScopeResource)mapper.readValue(root.get("data").toString(), PolicyScopeResource.class);
896                         commonClassDao.delete(onapData);
897                         response.setCharacterEncoding(utf8);
898                         response.setContentType(applicationJsonContentType);
899                         request.setCharacterEncoding(utf8);
900
901                         PrintWriter out = response.getWriter();
902
903                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
904                         JSONObject j = new JSONObject("{psResourceDictionaryDatas: " + responseString + "}");
905                         out.write(j.toString());
906
907                         return null;
908                 }
909                 catch (Exception e){
910                         LOGGER.error(e);
911                         response.setCharacterEncoding(utf8);
912                         request.setCharacterEncoding(utf8);
913                         PrintWriter out = response.getWriter();
914                         out.write(PolicyUtils.CATCH_EXCEPTION);
915                 }
916                 return null;
917         }
918 }
919
920 class GroupPolicyScope{
921         String resource;
922         String type;
923         String service;
924         String closedloop;
925         public String getResource() {
926                 return resource;
927         }
928         public void setResource(String resource) {
929                 this.resource = resource;
930         }
931         public String getType() {
932                 return type;
933         }
934         public void setType(String type) {
935                 this.type = type;
936         }
937         public String getService() {
938                 return service;
939         }
940         public void setService(String service) {
941                 this.service = service;
942         }
943         public String getClosedloop() {
944                 return closedloop;
945         }
946         public void setClosedloop(String closedloop) {
947                 this.closedloop = closedloop;
948         }
949
950 }