c89d21e91f9deaea73b7808b1978c078a6de6044
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / DecisionPolicyDictionaryController.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.ArrayList;
28 import java.util.Date;
29 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
33 import org.onap.policy.rest.dao.CommonClassDao;
34 import org.onap.policy.rest.jpa.DecisionSettings;
35 import org.onap.policy.rest.jpa.RainyDayTreatments;
36 import org.onap.policy.rest.jpa.UserInfo;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.MediaType;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.servlet.ModelAndView;
43
44 @Controller
45 public class DecisionPolicyDictionaryController {
46
47     private static CommonClassDao commonClassDao;
48     private static String xacmlId = "xacmlId";
49     private static String bbID = "bbid";
50     private static String operation = "operation";
51     private static String duplicateResponseString = "Duplicate";
52     private static String settingDatas = "settingsDictionaryDatas";
53     private static String rainDayDatas = "rainyDayDictionaryDatas";
54     private static String dictionaryFields = "dictionaryFields";
55
56     @Autowired
57     public DecisionPolicyDictionaryController(CommonClassDao commonClassDao) {
58         DecisionPolicyDictionaryController.commonClassDao = commonClassDao;
59     }
60
61     public DecisionPolicyDictionaryController() {
62         super();
63     }
64
65     private DictionaryUtils getDictionaryUtilsInstance() {
66         return DictionaryUtils.getDictionaryUtils();
67     }
68
69     @RequestMapping(value = {"/get_SettingsDictionaryDataByName"}, method = {RequestMethod.GET},
70             produces = MediaType.APPLICATION_JSON_VALUE)
71     public void getSettingsDictionaryByNameEntityData(HttpServletRequest request,
72             HttpServletResponse response) {
73         DictionaryUtils utils = getDictionaryUtilsInstance();
74         utils.getDataByEntity(response, settingDatas, xacmlId, DecisionSettings.class);
75     }
76
77
78     @RequestMapping(value = {"/get_SettingsDictionaryData"}, method = {RequestMethod.GET},
79             produces = MediaType.APPLICATION_JSON_VALUE)
80     public void getSettingsDictionaryEntityData(HttpServletResponse response) {
81         DictionaryUtils utils = getDictionaryUtilsInstance();
82         utils.getData(response, settingDatas, DecisionSettings.class);
83     }
84
85     @RequestMapping(value = {"/decision_dictionary/save_Settings"}, method = {RequestMethod.POST})
86     public ModelAndView saveSettingsDictionary(HttpServletRequest request,
87             HttpServletResponse response) throws IOException {
88         DictionaryUtils utils = getDictionaryUtilsInstance();
89         try {
90             boolean fromAPI = utils.isRequestFromAPI(request);
91             ObjectMapper mapper = new ObjectMapper();
92             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
93             JsonNode root = mapper.readTree(request.getReader());
94             DecisionSettings decisionSettings;
95             String userId = null;
96
97             if (fromAPI) {
98                 decisionSettings = mapper.readValue(root.get(dictionaryFields).toString(),
99                         DecisionSettings.class);
100                 userId = "API";
101             } else {
102                 decisionSettings = mapper.readValue(root.get("settingsDictionaryData").toString(),
103                         DecisionSettings.class);
104                 userId = root.get("userid").textValue();
105             }
106             UserInfo userInfo = utils.getUserInfo(userId);
107             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
108                     decisionSettings.getXacmlId(), xacmlId, DecisionSettings.class);
109             boolean duplicateflag = false;
110             if (!duplicateData.isEmpty()) {
111                 DecisionSettings data = (DecisionSettings) duplicateData.get(0);
112                 if (request.getParameter(operation) != null
113                         && "update".equals(request.getParameter(operation))) {
114                     decisionSettings.setId(data.getId());
115                 } else if ((request.getParameter(operation) != null
116                         && !"update".equals(request.getParameter(operation)))
117                         || (request.getParameter(operation) == null
118                                 && (data.getId() != decisionSettings.getId()))) {
119                     duplicateflag = true;
120                 }
121             }
122             if (decisionSettings.getDatatypeBean().getShortName() != null) {
123                 String datatype = decisionSettings.getDatatypeBean().getShortName();
124                 decisionSettings.setDatatypeBean(utils.getDataType(datatype));
125             }
126             String responseString = null;
127             if (!duplicateflag) {
128                 decisionSettings.setUserModifiedBy(userInfo);
129                 if (decisionSettings.getId() == 0) {
130                     decisionSettings.setUserCreatedBy(userInfo);
131                     commonClassDao.save(decisionSettings);
132                 } else {
133                     decisionSettings.setModifiedDate(new Date());
134                     commonClassDao.update(decisionSettings);
135                 }
136                 responseString =
137                         mapper.writeValueAsString(commonClassDao.getData(DecisionSettings.class));
138             } else {
139                 responseString = duplicateResponseString;
140             }
141             if (fromAPI) {
142                 return utils.getResultForApi(responseString);
143             } else {
144                 utils.setResponseData(response, settingDatas, responseString);
145             }
146         } catch (Exception e) {
147             utils.setErrorResponseData(response, e);
148         }
149         return null;
150     }
151
152     @RequestMapping(value = {"/settings_dictionary/remove_settings"}, method = {RequestMethod.POST})
153     public void removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response)
154             throws IOException {
155         DictionaryUtils utils = getDictionaryUtilsInstance();
156         utils.removeData(request, response, settingDatas, DecisionSettings.class);
157     }
158
159     @RequestMapping(value = {"/get_RainyDayDictionaryDataByName"}, method = {RequestMethod.GET},
160             produces = MediaType.APPLICATION_JSON_VALUE)
161     public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request,
162             HttpServletResponse response) {
163         DictionaryUtils utils = getDictionaryUtilsInstance();
164         utils.getDataByEntity(response, rainDayDatas, bbID, RainyDayTreatments.class);
165     }
166
167     @RequestMapping(value = {"/get_RainyDayDictionaryData"}, method = {RequestMethod.GET},
168             produces = MediaType.APPLICATION_JSON_VALUE)
169     public void getRainyDayDictionaryEntityData(HttpServletResponse response) {
170         DictionaryUtils utils = getDictionaryUtilsInstance();
171         utils.getData(response, rainDayDatas, RainyDayTreatments.class);
172     }
173
174     @RequestMapping(value = {"/decision_dictionary/save_RainyDay"}, method = {RequestMethod.POST})
175     public ModelAndView saveRainyDayDictionary(HttpServletRequest request,
176             HttpServletResponse response) throws IOException {
177         DictionaryUtils utils = getDictionaryUtilsInstance();
178         try {
179             boolean fromAPI = utils.isRequestFromAPI(request);
180             ObjectMapper mapper = new ObjectMapper();
181             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
182             JsonNode root = mapper.readTree(request.getReader());
183             RainyDayTreatments decisionRainyDay;
184             TreatmentValues treatmentsData = null;
185             if (fromAPI) {
186                 decisionRainyDay = mapper.readValue(root.get(dictionaryFields).toString(),
187                         RainyDayTreatments.class);
188                 treatmentsData = mapper.readValue(root.get(dictionaryFields).toString(),
189                         TreatmentValues.class);
190             } else {
191                 decisionRainyDay = mapper.readValue(root.get("rainyDayDictionaryData").toString(),
192                         RainyDayTreatments.class);
193                 treatmentsData = mapper.readValue(root.get("rainyDayDictionaryData").toString(),
194                         TreatmentValues.class);
195             }
196             decisionRainyDay.setTreatments(
197                     utils.appendKey(treatmentsData.getUserDataTypeValues(), "treatment", ","));
198
199             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(
200                     decisionRainyDay.getBbid() + ":" + decisionRainyDay.getWorkstep(),
201                     "bbid:workstep", RainyDayTreatments.class);
202             boolean duplicateflag = false;
203             if (!duplicateData.isEmpty()) {
204                 RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0);
205                 if (request.getParameter(operation) != null
206                         && "update".equals(request.getParameter(operation))) {
207                     decisionRainyDay.setId(data.getId());
208                 } else if ((request.getParameter(operation) != null
209                         && !"update".equals(request.getParameter(operation)))
210                         || (request.getParameter(operation) == null
211                                 && (data.getId() != decisionRainyDay.getId()))) {
212                     duplicateflag = true;
213                 }
214             }
215             String responseString = null;
216             if (!duplicateflag) {
217                 if (decisionRainyDay.getId() == 0) {
218                     commonClassDao.save(decisionRainyDay);
219                 } else {
220                     commonClassDao.update(decisionRainyDay);
221                 }
222                 responseString =
223                         mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class));
224             } else {
225                 responseString = duplicateResponseString;
226             }
227             if (fromAPI) {
228                 return utils.getResultForApi(responseString);
229             } else {
230                 utils.setResponseData(response, rainDayDatas, responseString);
231             }
232         } catch (Exception e) {
233             utils.setErrorResponseData(response, e);
234         }
235         return null;
236     }
237
238     @RequestMapping(value = {"/decision_dictionary/remove_rainyDay"}, method = {RequestMethod.POST})
239     public void removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response)
240             throws IOException {
241         DictionaryUtils utils = getDictionaryUtilsInstance();
242         utils.removeData(request, response, rainDayDatas, RainyDayTreatments.class);
243     }
244
245 }
246
247
248 class TreatmentValues {
249     private List<Object> userDataTypeValues = new ArrayList<>();
250
251     public List<Object> getUserDataTypeValues() {
252         return userDataTypeValues;
253     }
254
255     public void setUserDataTypeValues(List<Object> userDataTypeValues) {
256         this.userDataTypeValues = userDataTypeValues;
257     }
258 }