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