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