Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / ClosedLoopDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
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 com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import java.io.IOException;
28 import java.util.Date;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.onap.policy.pap.xacml.rest.adapters.GridData;
35 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
36 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.ClosedLoopD2Services;
39 import org.onap.policy.rest.jpa.ClosedLoopSite;
40 import org.onap.policy.rest.jpa.OnapName;
41 import org.onap.policy.rest.jpa.PepOptions;
42 import org.onap.policy.rest.jpa.UserInfo;
43 import org.onap.policy.rest.jpa.VNFType;
44 import org.onap.policy.rest.jpa.VSCLAction;
45 import org.onap.policy.rest.jpa.VarbindDictionary;
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.bind.annotation.RequestMethod;
51 import org.springframework.web.servlet.ModelAndView;
52
53 @Controller
54 public class ClosedLoopDictionaryController {
55
56     private static CommonClassDao commonClassDao;
57     private static String vsclaction = "vsclaction";
58     private static String operation = "operation";
59     private static String vnftype = "vnftype";
60     private static String pepName = "pepName";
61     private static String varbindName = "varbindName";
62     private static String serviceName = "serviceName";
63     private static String siteName = "siteName";
64     private static String dictionaryFields = "dictionaryFields";
65     private static String duplicateResponseString = "Duplicate";
66     private static String userid = "userid";
67     private static String vsclActionDatas = "vsclActionDictionaryDatas";
68     private static String vnfTypeDatas = "vnfTypeDictionaryDatas";
69     private static String pepOptionDatas = "pepOptionsDictionaryDatas";
70     private static String varbindDatas = "varbindDictionaryDatas";
71     private static String closedLoopDatas = "closedLoopServiceDictionaryDatas";
72     private static String closedLoopSiteDatas = "closedLoopSiteDictionaryDatas";
73
74     @Autowired
75     public ClosedLoopDictionaryController(CommonClassDao commonClassDao) {
76         ClosedLoopDictionaryController.commonClassDao = commonClassDao;
77     }
78
79     public void setCommonClassDao(CommonClassDao commonClassDao) {
80         ClosedLoopDictionaryController.commonClassDao = commonClassDao;
81     }
82
83     public static void setCommonClassDao(CommonClassDaoImpl commonClassDaoImpl) {
84         commonClassDao = commonClassDaoImpl;
85     }
86
87     public ClosedLoopDictionaryController() {
88         super();
89     }
90
91     private DictionaryUtils getDictionaryUtilsInstance() {
92         return DictionaryUtils.getDictionaryUtils();
93     }
94
95     @RequestMapping(
96             value = {"/get_VSCLActionDataByName"},
97             method = {RequestMethod.GET},
98             produces = MediaType.APPLICATION_JSON_VALUE)
99     public void getVSCLActionDictionaryByNameEntityData(HttpServletResponse response) {
100         DictionaryUtils utils = getDictionaryUtilsInstance();
101         utils.getDataByEntity(response, vsclActionDatas, vsclaction, VSCLAction.class);
102     }
103
104     @RequestMapping(
105             value = {"/get_VSCLActionData"},
106             method = {RequestMethod.GET},
107             produces = MediaType.APPLICATION_JSON_VALUE)
108     public void getVSCLActionDictionaryEntityData(HttpServletResponse response) {
109         DictionaryUtils utils = getDictionaryUtilsInstance();
110         utils.getData(response, vsclActionDatas, VSCLAction.class);
111     }
112
113     @RequestMapping(
114             value = {"/get_VNFTypeDataByName"},
115             method = {RequestMethod.GET},
116             produces = MediaType.APPLICATION_JSON_VALUE)
117     public void getVNFTypeDictionaryByNameEntityData(HttpServletResponse response) {
118         DictionaryUtils utils = getDictionaryUtilsInstance();
119         utils.getDataByEntity(response, vnfTypeDatas, vnftype, VNFType.class);
120     }
121
122     @RequestMapping(
123             value = {"/get_VNFTypeData"},
124             method = {RequestMethod.GET},
125             produces = MediaType.APPLICATION_JSON_VALUE)
126     public void getVNFTypeDictionaryEntityData(HttpServletResponse response) {
127         DictionaryUtils utils = getDictionaryUtilsInstance();
128         utils.getData(response, vnfTypeDatas, VNFType.class);
129     }
130
131     @RequestMapping(
132             value = {"/get_PEPOptionsDataByName"},
133             method = {RequestMethod.GET},
134             produces = MediaType.APPLICATION_JSON_VALUE)
135     public void getPEPOptionsDictionaryByNameEntityData(HttpServletResponse response) {
136         DictionaryUtils utils = getDictionaryUtilsInstance();
137         utils.getDataByEntity(response, pepOptionDatas, pepName, PepOptions.class);
138     }
139
140     @RequestMapping(
141             value = {"/get_PEPOptionsData"},
142             method = {RequestMethod.GET},
143             produces = MediaType.APPLICATION_JSON_VALUE)
144     public void getPEPOptionsDictionaryEntityData(HttpServletResponse response) {
145         DictionaryUtils utils = getDictionaryUtilsInstance();
146         utils.getData(response, pepOptionDatas, PepOptions.class);
147     }
148
149     @RequestMapping(
150             value = {"/get_VarbindDictionaryDataByName"},
151             method = {RequestMethod.GET},
152             produces = MediaType.APPLICATION_JSON_VALUE)
153     public void getVarbindDictionaryByNameEntityData(HttpServletResponse response) {
154         DictionaryUtils utils = getDictionaryUtilsInstance();
155         utils.getDataByEntity(response, varbindDatas, varbindName, VarbindDictionary.class);
156     }
157
158     @RequestMapping(
159             value = {"/get_VarbindDictionaryData"},
160             method = {RequestMethod.GET},
161             produces = MediaType.APPLICATION_JSON_VALUE)
162     public void getVarbindDictionaryEntityData(HttpServletResponse response) {
163         DictionaryUtils utils = getDictionaryUtilsInstance();
164         utils.getData(response, varbindDatas, VarbindDictionary.class);
165     }
166
167     @RequestMapping(
168             value = {"/get_ClosedLoopServicesDataByName"},
169             method = {RequestMethod.GET},
170             produces = MediaType.APPLICATION_JSON_VALUE)
171     public void getClosedLoopServiceDictionaryByNameEntityData(HttpServletResponse response) {
172         DictionaryUtils utils = getDictionaryUtilsInstance();
173         utils.getDataByEntity(response, closedLoopDatas, serviceName, ClosedLoopD2Services.class);
174     }
175
176     @RequestMapping(
177             value = {"/get_ClosedLoopServicesData"},
178             method = {RequestMethod.GET},
179             produces = MediaType.APPLICATION_JSON_VALUE)
180     public void getClosedLoopServiceDictionaryEntityData(HttpServletResponse response) {
181         DictionaryUtils utils = getDictionaryUtilsInstance();
182         utils.getData(response, closedLoopDatas, ClosedLoopD2Services.class);
183     }
184
185     @RequestMapping(
186             value = {"/get_ClosedLoopSiteDataByName"},
187             method = {RequestMethod.GET},
188             produces = MediaType.APPLICATION_JSON_VALUE)
189     public void getClosedLoopSiteDictionaryByNameEntityData(HttpServletResponse response) {
190         DictionaryUtils utils = getDictionaryUtilsInstance();
191         utils.getDataByEntity(response, closedLoopSiteDatas, siteName, ClosedLoopSite.class);
192     }
193
194     @RequestMapping(
195             value = {"/get_ClosedLoopSiteData"},
196             method = {RequestMethod.GET},
197             produces = MediaType.APPLICATION_JSON_VALUE)
198     public void getClosedLoopSiteDictionaryEntityData(HttpServletResponse response) {
199         DictionaryUtils utils = getDictionaryUtilsInstance();
200         utils.getData(response, closedLoopSiteDatas, ClosedLoopSite.class);
201     }
202
203     @RequestMapping(value = {"/cl_dictionary/save_vsclAction"}, method = {RequestMethod.POST})
204     public ModelAndView saveVSCLAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
205         DictionaryUtils utils = getDictionaryUtilsInstance();
206         try {
207             boolean fromAPI = utils.isRequestFromAPI(request);
208             ObjectMapper mapper = new ObjectMapper();
209             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
210             JsonNode root = mapper.readTree(request.getReader());
211             VSCLAction vSCLAction;
212             String userId = null;
213             if (fromAPI) {
214                 vSCLAction = mapper.readValue(root.get(dictionaryFields).toString(), VSCLAction.class);
215                 userId = "API";
216             } else {
217                 vSCLAction = mapper.readValue(root.get("vsclActionDictionaryData").toString(), VSCLAction.class);
218                 userId = root.get(userid).textValue();
219             }
220             UserInfo userInfo = utils.getUserInfo(userId);
221
222             List<Object> duplicateData =
223                     commonClassDao.checkDuplicateEntry(vSCLAction.getVsclaction(), vsclaction, VSCLAction.class);
224             boolean duplicateflag = false;
225             if (!duplicateData.isEmpty()) {
226                 VSCLAction data = (VSCLAction) duplicateData.get(0);
227                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
228                     vSCLAction.setId(data.getId());
229                 } else if ((request.getParameter(operation) != null
230                         && !"update".equals(request.getParameter(operation)))
231                         || (request.getParameter(operation) == null && (data.getId() != vSCLAction.getId()))) {
232                     duplicateflag = true;
233                 }
234             }
235             String responseString = null;
236             if (!duplicateflag) {
237                 vSCLAction.setUserModifiedBy(userInfo);
238                 if (vSCLAction.getId() == 0) {
239                     vSCLAction.setUserCreatedBy(userInfo);
240                     commonClassDao.save(vSCLAction);
241                 } else {
242                     vSCLAction.setModifiedDate(new Date());
243                     commonClassDao.update(vSCLAction);
244                 }
245                 responseString = mapper.writeValueAsString(commonClassDao.getData(VSCLAction.class));
246             } else {
247                 responseString = duplicateResponseString;
248             }
249
250             if (fromAPI) {
251                 return utils.getResultForApi(responseString);
252             } else {
253                 utils.setResponseData(response, vsclActionDatas, responseString);
254             }
255         } catch (Exception e) {
256             utils.setErrorResponseData(response, e);
257         }
258         return null;
259     }
260
261     @RequestMapping(value = {"/cl_dictionary/remove_VsclAction"}, method = {RequestMethod.POST})
262     public void removeVSCLAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
263         DictionaryUtils utils = getDictionaryUtilsInstance();
264         utils.removeData(request, response, vsclActionDatas, OnapName.class);
265     }
266
267     @RequestMapping(value = {"/cl_dictionary/save_vnfType"}, method = {RequestMethod.POST})
268     public ModelAndView saveVnfType(HttpServletRequest request, HttpServletResponse response) throws IOException {
269         DictionaryUtils utils = getDictionaryUtilsInstance();
270         try {
271             boolean fromAPI = utils.isRequestFromAPI(request);
272             ObjectMapper mapper = new ObjectMapper();
273             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
274             JsonNode root = mapper.readTree(request.getReader());
275             VNFType vNFType;
276             String userId = null;
277             if (fromAPI) {
278                 vNFType = mapper.readValue(root.get(dictionaryFields).toString(), VNFType.class);
279                 userId = "API";
280             } else {
281                 vNFType = mapper.readValue(root.get("vnfTypeDictionaryData").toString(), VNFType.class);
282                 userId = root.get(userid).textValue();
283             }
284             UserInfo userInfo = utils.getUserInfo(userId);
285
286             List<Object> duplicateData =
287                     commonClassDao.checkDuplicateEntry(vNFType.getVnftype(), vnftype, VNFType.class);
288             boolean duplicateflag = false;
289             if (!duplicateData.isEmpty()) {
290                 VNFType data = (VNFType) duplicateData.get(0);
291                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
292                     vNFType.setId(data.getId());
293                 } else if ((request.getParameter(operation) != null
294                         && !"update".equals(request.getParameter(operation)))
295                         || (request.getParameter(operation) == null && (data.getId() != vNFType.getId()))) {
296                     duplicateflag = true;
297                 }
298             }
299             String responseString = null;
300             if (!duplicateflag) {
301                 vNFType.setUserModifiedBy(userInfo);
302                 if (vNFType.getId() == 0) {
303                     vNFType.setUserCreatedBy(userInfo);
304                     commonClassDao.save(vNFType);
305                 } else {
306                     vNFType.setModifiedDate(new Date());
307                     commonClassDao.update(vNFType);
308                 }
309                 responseString = mapper.writeValueAsString(commonClassDao.getData(VNFType.class));
310             } else {
311                 responseString = duplicateResponseString;
312             }
313
314             if (fromAPI) {
315                 return utils.getResultForApi(responseString);
316             } else {
317                 utils.setResponseData(response, vnfTypeDatas, responseString);
318             }
319         } catch (Exception e) {
320             utils.setErrorResponseData(response, e);
321         }
322         return null;
323     }
324
325     @RequestMapping(value = {"/cl_dictionary/remove_vnfType"}, method = {RequestMethod.POST})
326     public void removeVnfType(HttpServletRequest request, HttpServletResponse response) throws IOException {
327         DictionaryUtils utils = getDictionaryUtilsInstance();
328         utils.removeData(request, response, vnfTypeDatas, VNFType.class);
329     }
330
331     @RequestMapping(value = {"/cl_dictionary/save_pepOptions"}, method = {RequestMethod.POST})
332     public ModelAndView savePEPOptions(HttpServletRequest request, HttpServletResponse response) throws IOException {
333         DictionaryUtils utils = getDictionaryUtilsInstance();
334         try {
335             boolean fromAPI = utils.isRequestFromAPI(request);
336             ObjectMapper mapper = new ObjectMapper();
337             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
338             JsonNode root = mapper.readTree(request.getReader());
339             PepOptions pEPOptions;
340             GridData gridData;
341             String userId = null;
342             if (fromAPI) {
343                 pEPOptions = mapper.readValue(root.get(dictionaryFields).toString(), PepOptions.class);
344                 gridData = mapper.readValue(root.get(dictionaryFields).toString(), GridData.class);
345                 userId = "API";
346             } else {
347                 pEPOptions = mapper.readValue(root.get("pepOptionsDictionaryData").toString(), PepOptions.class);
348                 gridData = mapper.readValue(root.get("pepOptionsDictionaryData").toString(), GridData.class);
349                 userId = root.get(userid).textValue();
350             }
351             UserInfo userInfo = utils.getUserInfo(userId);
352
353             if (!gridData.getAttributes().isEmpty()) {
354                 pEPOptions.setActions(utils.appendKeyValue(gridData.getAttributes(), ":#@", "=#@"));
355             }
356
357             List<Object> duplicateData =
358                     commonClassDao.checkDuplicateEntry(pEPOptions.getPepName(), pepName, PepOptions.class);
359             boolean duplicateflag = false;
360             if (!duplicateData.isEmpty()) {
361                 PepOptions data = (PepOptions) duplicateData.get(0);
362                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
363                     pEPOptions.setId(data.getId());
364                 } else if ((request.getParameter(operation) != null
365                         && !"update".equals(request.getParameter(operation)))
366                         || (request.getParameter(operation) == null && (data.getId() != pEPOptions.getId()))) {
367                     duplicateflag = true;
368                 }
369             }
370             String responseString = null;
371             if (!duplicateflag) {
372                 pEPOptions.setUserModifiedBy(userInfo);
373                 if (pEPOptions.getId() == 0) {
374                     pEPOptions.setUserCreatedBy(userInfo);
375                     commonClassDao.save(pEPOptions);
376                 } else {
377                     pEPOptions.setModifiedDate(new Date());
378                     commonClassDao.update(pEPOptions);
379                 }
380                 responseString = mapper.writeValueAsString(commonClassDao.getData(PepOptions.class));
381             } else {
382                 responseString = duplicateResponseString;
383             }
384
385             if (fromAPI) {
386                 return utils.getResultForApi(responseString);
387             } else {
388                 utils.setResponseData(response, pepOptionDatas, responseString);
389             }
390         } catch (Exception e) {
391             utils.setErrorResponseData(response, e);
392         }
393         return null;
394     }
395
396     @RequestMapping(value = {"/cl_dictionary/remove_pepOptions"}, method = {RequestMethod.POST})
397     public void removePEPOptions(HttpServletRequest request, HttpServletResponse response) throws IOException {
398         DictionaryUtils utils = getDictionaryUtilsInstance();
399         utils.removeData(request, response, pepOptionDatas, VNFType.class);
400     }
401
402     @RequestMapping(value = {"/cl_dictionary/save_service"}, method = {RequestMethod.POST})
403     public ModelAndView saveServiceType(HttpServletRequest request, HttpServletResponse response) throws IOException {
404         DictionaryUtils utils = getDictionaryUtilsInstance();
405         try {
406             boolean fromAPI = utils.isRequestFromAPI(request);
407             ObjectMapper mapper = new ObjectMapper();
408             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
409             JsonNode root = mapper.readTree(request.getReader());
410             ClosedLoopD2Services serviceData;
411             String userId = null;
412             if (fromAPI) {
413                 serviceData = mapper.readValue(root.get(dictionaryFields).toString(), ClosedLoopD2Services.class);
414                 userId = "API";
415             } else {
416                 serviceData = mapper.readValue(root.get("closedLoopServiceDictionaryData").toString(),
417                         ClosedLoopD2Services.class);
418                 userId = root.get(userid).textValue();
419             }
420             UserInfo userInfo = utils.getUserInfo(userId);
421
422             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(serviceData.getServiceName(), serviceName,
423                     ClosedLoopD2Services.class);
424             boolean duplicateflag = false;
425             if (!duplicateData.isEmpty()) {
426                 ClosedLoopD2Services data = (ClosedLoopD2Services) duplicateData.get(0);
427                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
428                     serviceData.setId(data.getId());
429                 } else if ((request.getParameter(operation) != null
430                         && !"update".equals(request.getParameter(operation)))
431                         || (request.getParameter(operation) == null && (data.getId() != serviceData.getId()))) {
432                     duplicateflag = true;
433                 }
434             }
435             String responseString = null;
436             if (!duplicateflag) {
437                 serviceData.setUserModifiedBy(userInfo);
438                 if (serviceData.getId() == 0) {
439                     serviceData.setUserCreatedBy(userInfo);
440                     commonClassDao.save(serviceData);
441                 } else {
442                     serviceData.setModifiedDate(new Date());
443                     commonClassDao.update(serviceData);
444                 }
445                 responseString = mapper.writeValueAsString(commonClassDao.getData(ClosedLoopD2Services.class));
446             } else {
447                 responseString = duplicateResponseString;
448             }
449             if (fromAPI) {
450                 return utils.getResultForApi(responseString);
451             } else {
452                 utils.setResponseData(response, closedLoopDatas, responseString);
453             }
454         } catch (Exception e) {
455             utils.setErrorResponseData(response, e);
456         }
457         return null;
458     }
459
460     @RequestMapping(value = {"/cl_dictionary/remove_Service"}, method = {RequestMethod.POST})
461     public void removeServiceType(HttpServletRequest request, HttpServletResponse response) throws IOException {
462         DictionaryUtils utils = getDictionaryUtilsInstance();
463         utils.removeData(request, response, closedLoopDatas, VNFType.class);
464     }
465
466     @RequestMapping(value = {"/cl_dictionary/save_siteName"}, method = {RequestMethod.POST})
467     public ModelAndView saveSiteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
468         DictionaryUtils utils = getDictionaryUtilsInstance();
469         try {
470             boolean fromAPI = utils.isRequestFromAPI(request);
471             ObjectMapper mapper = new ObjectMapper();
472             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
473             JsonNode root = mapper.readTree(request.getReader());
474             ClosedLoopSite siteData;
475             String userId = null;
476             if (fromAPI) {
477                 siteData = mapper.readValue(root.get(dictionaryFields).toString(), ClosedLoopSite.class);
478                 userId = "API";
479             } else {
480                 siteData = mapper.readValue(root.get("closedLoopSiteDictionaryData").toString(), ClosedLoopSite.class);
481                 userId = root.get(userid).textValue();
482             }
483             UserInfo userInfo = utils.getUserInfo(userId);
484
485             List<Object> duplicateData =
486                     commonClassDao.checkDuplicateEntry(siteData.getSiteName(), siteName, ClosedLoopSite.class);
487             boolean duplicateflag = false;
488             if (!duplicateData.isEmpty()) {
489                 ClosedLoopSite data = (ClosedLoopSite) duplicateData.get(0);
490                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
491                     siteData.setId(data.getId());
492                 } else if ((request.getParameter(operation) != null
493                         && !"update".equals(request.getParameter(operation)))
494                         || (request.getParameter(operation) == null && (data.getId() != siteData.getId()))) {
495                     duplicateflag = true;
496                 }
497             }
498             String responseString = null;
499             if (!duplicateflag) {
500                 siteData.setUserModifiedBy(userInfo);
501                 if (siteData.getId() == 0) {
502                     siteData.setUserCreatedBy(userInfo);
503                     commonClassDao.save(siteData);
504                 } else {
505                     siteData.setModifiedDate(new Date());
506                     commonClassDao.update(siteData);
507                 }
508                 responseString = mapper.writeValueAsString(commonClassDao.getData(ClosedLoopSite.class));
509             } else {
510                 responseString = duplicateResponseString;
511             }
512             if (fromAPI) {
513                 return utils.getResultForApi(responseString);
514             } else {
515                 utils.setResponseData(response, closedLoopSiteDatas, responseString);
516             }
517         } catch (Exception e) {
518             utils.setErrorResponseData(response, e);
519         }
520         return null;
521     }
522
523     @RequestMapping(value = {"/cl_dictionary/remove_site"}, method = {RequestMethod.POST})
524     public void removeSiteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
525         DictionaryUtils utils = getDictionaryUtilsInstance();
526         utils.removeData(request, response, closedLoopSiteDatas, VNFType.class);
527     }
528
529     @RequestMapping(value = {"/cl_dictionary/save_varbind"}, method = {RequestMethod.POST})
530     public ModelAndView saveVarbind(HttpServletRequest request, HttpServletResponse response) throws IOException {
531         DictionaryUtils utils = getDictionaryUtilsInstance();
532         try {
533             boolean fromAPI = utils.isRequestFromAPI(request);
534             ObjectMapper mapper = new ObjectMapper();
535             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
536             JsonNode root = mapper.readTree(request.getReader());
537             VarbindDictionary varbindDictionary;
538             String userId = null;
539             if (fromAPI) {
540                 varbindDictionary = mapper.readValue(root.get(dictionaryFields).toString(), VarbindDictionary.class);
541                 userId = "API";
542             } else {
543                 varbindDictionary =
544                         mapper.readValue(root.get("varbindDictionaryData").toString(), VarbindDictionary.class);
545                 userId = root.get(userid).textValue();
546             }
547             UserInfo userInfo = utils.getUserInfo(userId);
548
549             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(varbindDictionary.getVarbindName(),
550                     varbindName, VarbindDictionary.class);
551             boolean duplicateflag = false;
552             if (!duplicateData.isEmpty()) {
553                 VarbindDictionary data = (VarbindDictionary) duplicateData.get(0);
554                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
555                     varbindDictionary.setId(data.getId());
556                 } else if ((request.getParameter(operation) != null
557                         && !"update".equals(request.getParameter(operation)))
558                         || (request.getParameter(operation) == null && (data.getId() != varbindDictionary.getId()))) {
559                     duplicateflag = true;
560                 }
561             }
562
563             String responseString = null;
564             if (!duplicateflag) {
565                 varbindDictionary.setUserModifiedBy(userInfo);
566                 if (varbindDictionary.getId() == 0) {
567                     varbindDictionary.setUserCreatedBy(userInfo);
568                     commonClassDao.save(varbindDictionary);
569                 } else {
570                     varbindDictionary.setModifiedDate(new Date());
571                     commonClassDao.update(varbindDictionary);
572                 }
573                 responseString = mapper.writeValueAsString(commonClassDao.getData(VarbindDictionary.class));
574             } else {
575                 responseString = duplicateResponseString;
576             }
577             if (fromAPI) {
578                 return utils.getResultForApi(responseString);
579             } else {
580                 utils.setResponseData(response, varbindDatas, responseString);
581             }
582         } catch (Exception e) {
583             utils.setErrorResponseData(response, e);
584         }
585         return null;
586     }
587
588     @RequestMapping(value = {"/cl_dictionary/remove_varbindDict"}, method = {RequestMethod.POST})
589     public void removeVarbind(HttpServletRequest request, HttpServletResponse response) throws IOException {
590         DictionaryUtils utils = getDictionaryUtilsInstance();
591         utils.removeData(request, response, varbindDatas, VNFType.class);
592     }
593 }