Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / ActionPolicyDictionaryController.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.util.DictionaryUtils;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.ActionPolicyDict;
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 ActionPolicyDictionaryController {
47
48     private static CommonClassDao commonClassDao;
49     private static String operation = "operation";
50     private static String attributeName = "attributeName";
51     private static String actionDatas = "actionPolicyDictionaryDatas";
52
53     @Autowired
54     public ActionPolicyDictionaryController(CommonClassDao commonClassDao) {
55         ActionPolicyDictionaryController.commonClassDao = commonClassDao;
56     }
57
58     public void setCommonClassDao(CommonClassDao commonClassDao) {
59         ActionPolicyDictionaryController.commonClassDao = commonClassDao;
60     }
61
62     public ActionPolicyDictionaryController() {
63         super();
64     }
65
66     private DictionaryUtils getDictionaryUtilsInstance() {
67         return DictionaryUtils.getDictionaryUtils();
68     }
69
70     @RequestMapping(
71             value = {"/get_ActionPolicyDictDataByName"},
72             method = {RequestMethod.GET},
73             produces = MediaType.APPLICATION_JSON_VALUE)
74     public void getActionEntitybyName(HttpServletResponse response) {
75         DictionaryUtils utils = getDictionaryUtilsInstance();
76         utils.getDataByEntity(response, actionDatas, attributeName, ActionPolicyDict.class);
77     }
78
79     @RequestMapping(
80             value = {"/get_ActionPolicyDictData"},
81             method = {RequestMethod.GET},
82             produces = MediaType.APPLICATION_JSON_VALUE)
83     public void getActionPolicyDictionaryEntityData(HttpServletResponse response) {
84         DictionaryUtils utils = getDictionaryUtilsInstance();
85         utils.getData(response, actionDatas, ActionPolicyDict.class);
86     }
87
88     @RequestMapping(value = {"/action_dictionary/save_ActionDict"}, method = {RequestMethod.POST})
89     public ModelAndView saveActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response)
90             throws IOException {
91         DictionaryUtils utils = getDictionaryUtilsInstance();
92         try {
93             boolean fromAPI = utils.isRequestFromAPI(request);
94             ObjectMapper mapper = new ObjectMapper();
95             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
96             JsonNode root = mapper.readTree(request.getReader());
97             ActionPolicyDict actionPolicyDict = null;
98             ActionAdapter adapter = null;
99             String userId = null;
100             if (fromAPI) {
101                 actionPolicyDict = mapper.readValue(root.get("dictionaryFields").toString(), ActionPolicyDict.class);
102                 adapter = mapper.readValue(root.get("dictionaryFields").toString(), ActionAdapter.class);
103                 userId = "API";
104             } else {
105                 actionPolicyDict =
106                         mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionPolicyDict.class);
107                 adapter = mapper.readValue(root.get("actionPolicyDictionaryData").toString(), ActionAdapter.class);
108                 userId = root.get("userid").textValue();
109             }
110             UserInfo userInfo = utils.getUserInfo(userId);
111
112             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(actionPolicyDict.getAttributeName(),
113                     attributeName, ActionPolicyDict.class);
114             boolean duplicateflag = false;
115             if (!duplicateData.isEmpty()) {
116                 ActionPolicyDict data = (ActionPolicyDict) duplicateData.get(0);
117                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
118                     actionPolicyDict.setId(data.getId());
119                 } else if ((request.getParameter(operation) != null
120                         && !"update".equals(request.getParameter(operation)))
121                         || (request.getParameter(operation) == null && (data.getId() != actionPolicyDict.getId()))) {
122                     duplicateflag = true;
123                 }
124             }
125             actionPolicyDict.setHeader(utils.appendKeyValue(adapter.getHeaders(), ":", "="));
126
127             String responseString = null;
128             if (!duplicateflag) {
129                 actionPolicyDict.setUserModifiedBy(userInfo);
130                 if (actionPolicyDict.getId() == 0) {
131                     actionPolicyDict.setUserCreatedBy(userInfo);
132                     commonClassDao.save(actionPolicyDict);
133                 } else {
134                     actionPolicyDict.setModifiedDate(new Date());
135                     commonClassDao.update(actionPolicyDict);
136                 }
137                 responseString = mapper.writeValueAsString(commonClassDao.getData(ActionPolicyDict.class));
138             } else {
139                 responseString = "Duplicate";
140             }
141             if (fromAPI) {
142                 return utils.getResultForApi(responseString);
143             } else {
144                 utils.setResponseData(response, actionDatas, responseString);
145             }
146         } catch (Exception e) {
147             utils.setErrorResponseData(response, e);
148         }
149         return null;
150     }
151
152     @RequestMapping(value = {"/action_dictionary/remove_actionPolicyDict"}, method = {RequestMethod.POST})
153     public void removeActionPolicyDictionary(HttpServletRequest request, HttpServletResponse response)
154             throws IOException {
155         DictionaryUtils utils = getDictionaryUtilsInstance();
156         utils.removeData(request, response, actionDatas, ActionPolicyDict.class);
157     }
158 }
159
160
161 class ActionAdapter {
162     private List<Object> headers;
163
164     public List<Object> getHeaders() {
165         return headers;
166     }
167
168     public void setHeaders(List<Object> headers) {
169         this.headers = headers;
170     }
171 }