94275e7c19012a2c29064adc0d6bf1a21520cecf
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / DictionaryController.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 import java.io.IOException;
27 import java.util.Date;
28 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.policy.rest.jpa.Attribute;
36 import org.onap.policy.rest.jpa.OnapName;
37 import org.onap.policy.rest.jpa.UserInfo;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RequestMethod;
43 import org.springframework.web.servlet.ModelAndView;
44
45 @Controller
46 public class DictionaryController {
47
48     private static final Log LOGGER = LogFactory.getLog(DictionaryController.class);
49
50     private static CommonClassDao commonClassDao;
51     private static String xacmlId = "xacmlId";
52     private static String operation = "operation";
53     private static String dictionaryFields = "dictionaryFields";
54     private static String duplicateResponseString = "Duplicate";
55     private static String onapName = "onapName";
56     private static String attributeDatas = "attributeDictionaryDatas";
57     private static String onapNameDatas = "onapNameDictionaryDatas";
58
59     @Autowired
60     public DictionaryController(CommonClassDao commonClassDao) {
61         DictionaryController.commonClassDao = commonClassDao;
62     }
63
64     public DictionaryController() {
65         super();
66     }
67
68     private DictionaryUtils getDictionaryUtilsInstance() {
69         return DictionaryUtils.getDictionaryUtils();
70     }
71
72     @RequestMapping(value = {"/get_AttributeDatabyAttributeName"}, method = {RequestMethod.GET},
73             produces = MediaType.APPLICATION_JSON_VALUE)
74     public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response) {
75         DictionaryUtils utils = getDictionaryUtilsInstance();
76         utils.getDataByEntity(response, attributeDatas, xacmlId, Attribute.class);
77     }
78
79     // Attribute Dictionary
80     @RequestMapping(value = "/get_AttributeData", method = RequestMethod.GET,
81             produces = MediaType.APPLICATION_JSON_VALUE)
82     public void getAttributeDictionaryEntityData(HttpServletResponse response) {
83         DictionaryUtils utils = getDictionaryUtilsInstance();
84         utils.getData(response, attributeDatas, Attribute.class);
85     }
86
87     @RequestMapping(value = {"/attribute_dictionary/save_attribute"}, method = {RequestMethod.POST})
88     public ModelAndView saveAttributeDictionary(HttpServletRequest request,
89             HttpServletResponse response) throws IOException {
90         DictionaryUtils utils = getDictionaryUtilsInstance();
91         try {
92             boolean fromAPI = utils.isRequestFromAPI(request);
93             ObjectMapper mapper = new ObjectMapper();
94             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95             JsonNode root = mapper.readTree(request.getReader());
96             Attribute attributeData = null;
97             AttributeValues attributeValueData = null;
98             String userId = null;
99             if (fromAPI) {
100                 attributeData =
101                         mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
102                 attributeValueData = mapper.readValue(root.get(dictionaryFields).toString(),
103                         AttributeValues.class);
104                 userId = "API";
105             } else {
106                 attributeData = mapper.readValue(root.get("attributeDictionaryData").toString(),
107                         Attribute.class);
108                 attributeValueData = mapper.readValue(
109                         root.get("attributeDictionaryData").toString(), AttributeValues.class);
110                 userId = root.get("userid").textValue();
111             }
112             UserInfo userInfo = utils.getUserInfo(userId);
113             List<Object> duplicateData = commonClassDao
114                     .checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
115             boolean duplicateflag = false;
116             if (!duplicateData.isEmpty()) {
117                 Attribute data = (Attribute) duplicateData.get(0);
118                 if (request.getParameter(operation) != null
119                         && "update".equals(request.getParameter(operation))) {
120                     attributeData.setId(data.getId());
121                 } else if ((request.getParameter(operation) != null
122                         && !"update".equals(request.getParameter(operation)))
123                         || (request.getParameter(operation) == null
124                                 && (data.getId() != attributeData.getId()))) {
125                     duplicateflag = true;
126                 }
127             }
128             if (attributeValueData.getUserDataTypeValues() != null
129                     && !attributeValueData.getUserDataTypeValues().isEmpty()) {
130                 attributeData.setAttributeValue(utils.appendKey(
131                         attributeValueData.getUserDataTypeValues(), "attributeValues", ","));
132             }
133
134             if (attributeData.getDatatypeBean().getShortName() != null) {
135                 String datatype = attributeData.getDatatypeBean().getShortName();
136                 attributeData.setDatatypeBean(utils.getDataType(datatype));
137             }
138
139             String responseString = null;
140             if (!duplicateflag) {
141                 attributeData.setUserModifiedBy(userInfo);
142                 if (attributeData.getId() == 0) {
143                     attributeData.setCategoryBean(utils.getCategory());
144                     attributeData.setUserCreatedBy(userInfo);
145                     commonClassDao.save(attributeData);
146                 } else {
147                     attributeData.setModifiedDate(new Date());
148                     commonClassDao.update(attributeData);
149                 }
150                 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
151             } else {
152                 responseString = duplicateResponseString;
153             }
154             if (fromAPI) {
155                 return utils.getResultForApi(responseString);
156             } else {
157                 utils.setResponseData(response, attributeDatas, responseString);
158             }
159         } catch (Exception e) {
160             utils.setErrorResponseData(response, e);
161         }
162         return null;
163     }
164
165     @RequestMapping(value = {"/attribute_dictionary/remove_attribute"},
166             method = {RequestMethod.POST})
167     public void removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response)
168             throws IOException {
169         DictionaryUtils utils = getDictionaryUtilsInstance();
170         utils.removeData(request, response, attributeDatas, Attribute.class);
171     }
172
173     // OnapName Dictionary
174     @RequestMapping(value = {"/get_OnapNameDataByName"}, method = {RequestMethod.GET},
175             produces = MediaType.APPLICATION_JSON_VALUE)
176     public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response) {
177         LOGGER.info("get_OnapNameDataByName is called");
178         DictionaryUtils utils = getDictionaryUtilsInstance();
179         utils.getDataByEntity(response, onapNameDatas, onapName, OnapName.class);
180     }
181
182     @RequestMapping(value = {"/get_OnapNameData"}, method = {RequestMethod.GET},
183             produces = MediaType.APPLICATION_JSON_VALUE)
184     public void getOnapNameDictionaryEntityData(HttpServletResponse response) {
185         DictionaryUtils utils = getDictionaryUtilsInstance();
186         utils.getData(response, onapNameDatas, OnapName.class);
187     }
188
189     @RequestMapping(value = {"/onap_dictionary/save_onapName"}, method = {RequestMethod.POST})
190     public ModelAndView saveOnapDictionary(HttpServletRequest request, HttpServletResponse response)
191             throws IOException {
192         DictionaryUtils utils = getDictionaryUtilsInstance();
193         try {
194             boolean fromAPI = utils.isRequestFromAPI(request);
195             ObjectMapper mapper = new ObjectMapper();
196             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
197             JsonNode root = mapper.readTree(request.getReader());
198             OnapName onapData;
199             String userId = null;
200             if (fromAPI) {
201                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
202                 userId = "API";
203             } else {
204                 onapData = mapper.readValue(root.get("onapNameDictionaryData").toString(),
205                         OnapName.class);
206                 userId = root.get("userid").textValue();
207             }
208             UserInfo userInfo = utils.getUserInfo(userId);
209
210             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(onapData.getOnapName(),
211                     onapName, OnapName.class);
212             boolean duplicateflag = false;
213             if (!duplicateData.isEmpty()) {
214                 OnapName data = (OnapName) duplicateData.get(0);
215                 if (request.getParameter(operation) != null
216                         && "update".equals(request.getParameter(operation))) {
217                     onapData.setId(data.getId());
218                 } else if ((request.getParameter(operation) != null
219                         && !"update".equals(request.getParameter(operation)))
220                         || (request.getParameter(operation) == null
221                                 && (data.getId() != onapData.getId()))) {
222                     duplicateflag = true;
223                 }
224             }
225             String responseString = null;
226             if (!duplicateflag) {
227                 onapData.setUserModifiedBy(userInfo);
228                 if (onapData.getId() == 0) {
229                     onapData.setUserCreatedBy(userInfo);
230                     commonClassDao.save(onapData);
231                 } else {
232                     onapData.setModifiedDate(new Date());
233                     commonClassDao.update(onapData);
234                 }
235                 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
236             } else {
237                 responseString = duplicateResponseString;
238             }
239             if (fromAPI) {
240                 return utils.getResultForApi(responseString);
241             } else {
242                 utils.setResponseData(response, onapNameDatas, responseString);
243             }
244         } catch (Exception e) {
245             utils.setErrorResponseData(response, e);
246         }
247         return null;
248     }
249
250     @RequestMapping(value = {"/onap_dictionary/remove_onap"}, method = {RequestMethod.POST})
251     public void removeOnapDictionary(HttpServletRequest request, HttpServletResponse response)
252             throws IOException {
253         DictionaryUtils utils = getDictionaryUtilsInstance();
254         utils.removeData(request, response, onapNameDatas, OnapName.class);
255     }
256 }
257
258
259 class AttributeValues {
260     private List<Object> userDataTypeValues;
261
262     public List<Object> getUserDataTypeValues() {
263         return userDataTypeValues;
264     }
265
266     public void setUserDataTypeValues(List<Object> userDataTypeValues) {
267         this.userDataTypeValues = userDataTypeValues;
268     }
269 }