Merge "JUnit additions for XACML,REST,POLICY-SDK"
[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-2018 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 java.io.IOException;
24 import java.util.Date;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
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.Attribute;
35 import org.onap.policy.rest.jpa.OnapName;
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 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
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.dictionaryUtils != null ? DictionaryUtils.getDictionaryUtils() : new DictionaryUtils();
73         }
74         
75         @RequestMapping(value={"/get_AttributeDatabyAttributeName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
76         public void getAttributeDictionaryEntityDatabyAttributeName(HttpServletResponse response){
77                 DictionaryUtils utils = getDictionaryUtilsInstance();
78                 utils.getDataByEntity(response, attributeDatas, xacmlId, Attribute.class);
79         }
80         
81         //Attribute Dictionary
82         @RequestMapping(value="/get_AttributeData", method= RequestMethod.GET , produces=MediaType.APPLICATION_JSON_VALUE)
83         public void getAttributeDictionaryEntityData(HttpServletResponse response){
84                 DictionaryUtils utils = getDictionaryUtilsInstance();
85                 utils.getData(response, attributeDatas, Attribute.class);
86         }
87         
88         @RequestMapping(value={"/attribute_dictionary/save_attribute"}, method={RequestMethod.POST})
89         public ModelAndView saveAttributeDictionary(HttpServletRequest request, 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 = mapper.readValue(root.get(dictionaryFields).toString(), Attribute.class);
101                                 attributeValueData = mapper.readValue(root.get(dictionaryFields).toString(), AttributeValues.class);
102                                 userId = "API";
103                         }else{
104                                 attributeData = mapper.readValue(root.get("attributeDictionaryData").toString(), Attribute.class);
105                                 attributeValueData = mapper.readValue(root.get("attributeDictionaryData").toString(), AttributeValues.class);
106                                 userId = root.get("userid").textValue();
107                         }
108                         UserInfo userInfo = utils.getUserInfo(userId);
109                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(attributeData.getXacmlId(), xacmlId, Attribute.class);
110                         boolean duplicateflag = false;
111                         if(!duplicateData.isEmpty()){
112                                 Attribute data = (Attribute) duplicateData.get(0);
113                                 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
114                                         attributeData.setId(data.getId());
115                                 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || 
116                                                 (request.getParameter(operation) == null && (data.getId() != attributeData.getId()))){
117                                         duplicateflag = true;
118                                 }
119                         }
120                         if(attributeValueData.getUserDataTypeValues() != null && !attributeValueData.getUserDataTypeValues().isEmpty()){
121                                 attributeData.setAttributeValue(utils.appendKey(attributeValueData.getUserDataTypeValues(), "attributeValues", ","));
122                         }
123                         
124                         if(attributeData.getDatatypeBean().getShortName() != null){
125                                 String datatype = attributeData.getDatatypeBean().getShortName();       
126                                 attributeData.setDatatypeBean(utils.getDataType(datatype));
127                         }
128
129                         String responseString = null;
130                         if(!duplicateflag){
131                                 attributeData.setUserModifiedBy(userInfo);
132                                 if(attributeData.getId() == 0){
133                                         attributeData.setCategoryBean(utils.getCategory());
134                                         attributeData.setUserCreatedBy(userInfo);
135                                         commonClassDao.save(attributeData);
136                                 }else{
137                                         attributeData.setModifiedDate(new Date());
138                                         commonClassDao.update(attributeData); 
139                                 } 
140                                 responseString = mapper.writeValueAsString(commonClassDao.getData(Attribute.class));
141                         }else{
142                                 responseString = duplicateResponseString;
143                         }
144                         if(fromAPI){
145                                 return utils.getResultForApi(responseString);
146                         }else{
147                                 utils.setResponseData(response, attributeDatas, responseString);
148                         }
149                 }catch (Exception e){
150                         utils.setErrorResponseData(response, e);
151                 }
152                 return null;
153         }
154
155         @RequestMapping(value={"/attribute_dictionary/remove_attribute"}, method={RequestMethod.POST})
156         public void removeAttributeDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException {
157                 DictionaryUtils utils = getDictionaryUtilsInstance();
158                 utils.removeData(request, response, attributeDatas, Attribute.class);
159         }
160         
161         //OnapName Dictionary
162         @RequestMapping(value={"/get_OnapNameDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
163         public void getOnapNameDictionaryByNameEntityData(HttpServletResponse response){
164                 LOGGER.info("get_OnapNameDataByName is called");
165                 DictionaryUtils utils = getDictionaryUtilsInstance();
166                 utils.getDataByEntity(response, onapNameDatas, onapName, OnapName.class);
167         }
168         
169         @RequestMapping(value={"/get_OnapNameData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
170         public void getOnapNameDictionaryEntityData(HttpServletResponse response){
171                 DictionaryUtils utils = getDictionaryUtilsInstance();
172                 utils.getData(response, onapNameDatas, OnapName.class);
173         }
174
175         @RequestMapping(value={"/onap_dictionary/save_onapName"}, method={RequestMethod.POST})
176         public ModelAndView saveOnapDictionary(HttpServletRequest request, 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                         OnapName onapData;
184                         String userId = null;
185                         if(fromAPI){
186                                 onapData = mapper.readValue(root.get(dictionaryFields).toString(), OnapName.class);
187                                 userId = "API";
188                         }else{
189                                 onapData = mapper.readValue(root.get("onapNameDictionaryData").toString(), OnapName.class);
190                                 userId = root.get("userid").textValue();
191                         }
192                         UserInfo userInfo = utils.getUserInfo(userId);
193                         
194                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getOnapName(), onapName, OnapName.class);
195                         boolean duplicateflag = false;
196                         if(!duplicateData.isEmpty()){
197                                 OnapName data = (OnapName) duplicateData.get(0);
198                                 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
199                                         onapData.setId(data.getId());
200                                 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || 
201                                                 (request.getParameter(operation) == null && (data.getId() != onapData.getId()))){
202                                         duplicateflag = true;
203                                 }
204                         }
205                         String responseString = null;
206                         if(!duplicateflag){
207                                 onapData.setUserModifiedBy(userInfo);
208                                 if(onapData.getId() == 0){
209                                         onapData.setUserCreatedBy(userInfo);
210                                         commonClassDao.save(onapData);
211                                 }else{
212                                         onapData.setModifiedDate(new Date());
213                                         commonClassDao.update(onapData); 
214                                 } 
215                                 responseString = mapper.writeValueAsString(commonClassDao.getData(OnapName.class));
216                         }else{
217                                 responseString = duplicateResponseString;
218                         }
219                         if(fromAPI){
220                                 return utils.getResultForApi(responseString);
221                         }else{
222                                 utils.setResponseData(response, onapNameDatas, responseString);
223                         }
224                 }catch (Exception e){
225                         utils.setErrorResponseData(response, e);
226                 }
227                 return null;
228         }
229
230         @RequestMapping(value={"/onap_dictionary/remove_onap"}, method={RequestMethod.POST})
231         public void removeOnapDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
232                 DictionaryUtils utils = getDictionaryUtilsInstance();
233                 utils.removeData(request, response, onapNameDatas, OnapName.class);
234         }
235 }
236
237 class AttributeValues{
238         private List<Object> userDataTypeValues;
239
240         public List<Object> getUserDataTypeValues() {
241                 return userDataTypeValues;
242         }
243
244         public void setUserDataTypeValues(List<Object> userDataTypeValues) {
245                 this.userDataTypeValues = userDataTypeValues;
246         }
247 }