Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / PDPController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.policy.controller;
22
23
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.json.JSONObject;
33 import org.openecomp.policy.model.PDPGroupContainer;
34 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
35 import org.openecomp.portalsdk.core.web.support.JsonMessage;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.servlet.ModelAndView;
40
41 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
42 import org.openecomp.policy.common.logging.flexlogger.Logger;
43
44 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
45 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
46
47 import com.att.research.xacml.api.pap.PAPException;
48 import org.openecomp.policy.xacml.std.pap.StdPDP;
49 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
50 import com.fasterxml.jackson.databind.DeserializationFeature;
51 import com.fasterxml.jackson.databind.JsonNode;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 @Controller
55 @RequestMapping({"/"})
56 public class PDPController extends RestrictedBaseController {
57         private static final  Logger logger = FlexLogger.getLogger(PDPController.class);
58         
59         protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
60         private PDPGroupContainer container;
61         
62         public synchronized void refreshGroups() {
63                 synchronized(this.groups) { 
64                         this.groups.clear();
65                         try {
66                                 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
67                         } catch (PAPException e) {
68                                 String message = "Unable to retrieve Groups from server: " + e;
69                                 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Pap Engine is Null" + message);
70                         }
71                 
72                 }
73         }
74         
75         @RequestMapping(value={"/get_PDPGroupContainerData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
76         public void getPDPGroupContainerData(HttpServletRequest request, HttpServletResponse response){
77                 try{
78                         ObjectMapper mapper = new ObjectMapper();
79                         refreshGroups();
80                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
81                         JSONObject j = new JSONObject(msg);
82                         response.getWriter().write(j.toString());
83                 }
84                 catch (Exception e){
85                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
86                 }
87         }
88         
89         @RequestMapping(value={"/get_PDPGroupData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
90         public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response){
91                 try{
92                         ObjectMapper mapper = new ObjectMapper();
93                         refreshGroups();
94                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
95                         JSONObject j = new JSONObject(msg);
96                         response.getWriter().write(j.toString());
97                 }
98                 catch (Exception e){
99                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
100                 }
101         }
102         
103         @RequestMapping(value={"/pdp_Group/save_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
104           public ModelAndView savePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
105             try {
106               ObjectMapper mapper = new ObjectMapper();
107               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
108               JsonNode root = mapper.readTree(request.getReader());
109               this.container = new PDPGroupContainer(PolicyController.getPapEngine());
110               StdPDPGroup pdpGroupData =  mapper.readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
111               try {
112                   if(pdpGroupData.getId() == null){
113                           this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
114                   }else{
115                           this.container.updateGroup(pdpGroupData);
116                   }
117                                 
118                         } catch (Exception e) {
119                                 String message = "Unable to create Group.  Reason:\n" + e.getMessage();
120                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while creating the PDP Group" + message);
121                         }
122                 
123             
124               response.setCharacterEncoding("UTF-8");
125               response.setContentType("application / json");
126               request.setCharacterEncoding("UTF-8");
127               
128               PrintWriter out = response.getWriter();
129               refreshGroups();
130               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
131                   JSONObject j = new JSONObject(msg);
132               out.write(j.toString());
133               
134               return null;
135             }
136             catch (Exception e){
137              logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Saving the PDP Group" + e);
138               response.setCharacterEncoding("UTF-8");
139               request.setCharacterEncoding("UTF-8");
140               PrintWriter out = response.getWriter();
141               out.write(e.getMessage());
142             }
143             return null;
144           }
145           
146           @RequestMapping(value={"/pdp_Group/remove_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
147           public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
148             try{
149               ObjectMapper mapper = new ObjectMapper();
150               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
151               JsonNode root = mapper.readTree(request.getReader());
152               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
153               StdPDPGroup pdpGroupData =  mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
154                 if(pdpGroupData.getName().equals("Default")) {
155                                 throw new UnsupportedOperationException("You can't remove the Default Group.");
156                         }else{
157                                 this.container.removeGroup(pdpGroupData, null);
158                         }
159           
160               response.setCharacterEncoding("UTF-8");
161               response.setContentType("application / json");
162               request.setCharacterEncoding("UTF-8");
163               
164               PrintWriter out = response.getWriter();
165               
166               refreshGroups();
167               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
168                   JSONObject j = new JSONObject(msg);
169               out.write(j.toString());
170               
171               return null;
172             }
173             catch (Exception e){
174               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing the PDP Group" + e);
175               response.setCharacterEncoding("UTF-8");
176               request.setCharacterEncoding("UTF-8");
177               PrintWriter out = response.getWriter();
178               out.write(e.getMessage());
179             }
180             return null;
181           }
182           
183           @RequestMapping(value={"/pdp_Group/save_pdpTogroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
184           public ModelAndView savePDPToGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
185             try {
186               ObjectMapper mapper = new ObjectMapper();
187               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
188               JsonNode root = mapper.readTree(request.getReader());
189               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
190               String update = root.get("update").toString();
191               PdpData pdpGroupData = (PdpData)mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
192               StdPDPGroup activeGroupData =  mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
193               try {
194                   
195                   if(update.contains("false")){
196                           this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
197                   }else{
198                           this.container.updateGroup(activeGroupData);
199                   }
200                         } catch (Exception e) {
201                                 String message = "Unable to create Group.  Reason:\n" + e.getMessage();
202                                  logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + message);
203                         }
204                 
205             
206               response.setCharacterEncoding("UTF-8");
207               response.setContentType("application / json");
208               request.setCharacterEncoding("UTF-8");
209               
210               PrintWriter out = response.getWriter();
211               refreshGroups();
212               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
213                   JSONObject j = new JSONObject(msg);
214               out.write(j.toString());
215               
216               return null;
217             }
218             catch (Exception e){
219               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + e);
220               response.setCharacterEncoding("UTF-8");
221               request.setCharacterEncoding("UTF-8");
222               PrintWriter out = response.getWriter();
223               out.write(e.getMessage());
224             }
225             return null;
226           }
227           
228           @RequestMapping(value={"/pdp_Group/remove_pdpFromGroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
229           public ModelAndView removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
230             try{
231               ObjectMapper mapper = new ObjectMapper();
232               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
233               JsonNode root = mapper.readTree(request.getReader());
234               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
235               StdPDP deletePdp =  mapper.readValue(root.get("data").toString(), StdPDP.class);
236               StdPDPGroup activeGroupData =  mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
237                 
238               this.container.removePDP(deletePdp, activeGroupData);
239               response.setCharacterEncoding("UTF-8");
240               response.setContentType("application / json");
241               request.setCharacterEncoding("UTF-8");
242               
243               PrintWriter out = response.getWriter();
244               refreshGroups();
245               String responseString = mapper.writeValueAsString(groups);
246               JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
247               out.write(j.toString());
248               
249               return null;
250             }
251             catch (Exception e){
252               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing Pdp from PDP Group" + e);
253               response.setCharacterEncoding("UTF-8");
254               request.setCharacterEncoding("UTF-8");
255               PrintWriter out = response.getWriter();
256               out.write(e.getMessage());
257             }
258             return null;
259           }
260 }
261
262 class PdpData{
263         String id;
264         int jmxPort;
265         String name;
266         String description;
267         public String getId() {
268                 return id;
269         }
270         public void setId(String id) {
271                 this.id = id;
272         }
273         public int getJmxPort() {
274                 return jmxPort;
275         }
276         public void setJmxPort(int jmxPort) {
277                 this.jmxPort = jmxPort;
278         }
279         public String getName() {
280                 return name;
281         }
282         public void setName(String name) {
283                 this.name = name;
284         }
285         public String getDescription() {
286                 return description;
287         }
288         public void setDescription(String description) {
289                 this.description = description;
290         }
291         
292 }