2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.controller;
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.openecomp.policy.dao.GroupEntityDao;
34 import org.openecomp.policy.dao.PDPEntityDao;
35 import org.openecomp.policy.model.PDPGroupContainer;
36 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
37 import org.openecomp.portalsdk.core.web.support.JsonMessage;
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.servlet.ModelAndView;
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
47 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
48 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
50 import com.att.research.xacml.api.pap.PAPException;
51 import com.att.research.xacml.api.pap.PDPGroup;
52 import org.openecomp.policy.xacml.std.pap.StdPDP;
53 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
54 import com.fasterxml.jackson.databind.DeserializationFeature;
55 import com.fasterxml.jackson.databind.JsonNode;
56 import com.fasterxml.jackson.databind.ObjectMapper;
59 @RequestMapping({"/"})
60 public class PDPController extends RestrictedBaseController {
61 private static final Logger logger = FlexLogger.getLogger(PDPController.class);
64 GroupEntityDao groupDAO;
69 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
70 private PDPGroupContainer container;
72 public synchronized void refreshGroups() {
73 synchronized(this.groups) {
76 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
77 } catch (PAPException e) {
78 String message = "Unable to retrieve Groups from server: " + e;
79 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Pap Engine is Null" + message);
85 @RequestMapping(value={"/get_PDPGroupContainerData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
86 public void getPDPGroupContainerData(HttpServletRequest request, HttpServletResponse response){
88 ObjectMapper mapper = new ObjectMapper();
90 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
91 JSONObject j = new JSONObject(msg);
92 response.getWriter().write(j.toString());
95 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
99 @RequestMapping(value={"/get_PDPGroupData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
100 public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response){
102 ObjectMapper mapper = new ObjectMapper();
104 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
105 JSONObject j = new JSONObject(msg);
106 response.getWriter().write(j.toString());
109 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
113 @RequestMapping(value={"/pdp_Group/save_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
114 public ModelAndView savePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
116 ObjectMapper mapper = new ObjectMapper();
117 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118 JsonNode root = mapper.readTree(request.getReader());
119 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
120 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
122 if(pdpGroupData.getId() == null){
123 this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
125 this.container.updateGroup(pdpGroupData);
128 } catch (Exception e) {
129 String message = "Unable to create Group. Reason:\n" + e.getMessage();
130 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while creating the PDP Group" + message);
134 response.setCharacterEncoding("UTF-8");
135 response.setContentType("application / json");
136 request.setCharacterEncoding("UTF-8");
138 PrintWriter out = response.getWriter();
140 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
141 JSONObject j = new JSONObject(msg);
142 out.write(j.toString());
147 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Saving the PDP Group" + e);
148 response.setCharacterEncoding("UTF-8");
149 request.setCharacterEncoding("UTF-8");
150 PrintWriter out = response.getWriter();
151 out.write(e.getMessage());
156 @RequestMapping(value={"/pdp_Group/remove_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
157 public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
159 ObjectMapper mapper = new ObjectMapper();
160 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
161 JsonNode root = mapper.readTree(request.getReader());
162 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
163 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
164 if(pdpGroupData.getName().equals("Default")) {
165 throw new UnsupportedOperationException("You can't remove the Default Group.");
167 this.container.removeGroup(pdpGroupData, null);
170 response.setCharacterEncoding("UTF-8");
171 response.setContentType("application / json");
172 request.setCharacterEncoding("UTF-8");
174 PrintWriter out = response.getWriter();
177 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
178 JSONObject j = new JSONObject(msg);
179 out.write(j.toString());
184 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing the PDP Group" + e);
185 response.setCharacterEncoding("UTF-8");
186 request.setCharacterEncoding("UTF-8");
187 PrintWriter out = response.getWriter();
188 out.write(e.getMessage());
193 @RequestMapping(value={"/pdp_Group/save_pdpTogroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
194 public ModelAndView savePDPToGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
196 ObjectMapper mapper = new ObjectMapper();
197 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
198 JsonNode root = mapper.readTree(request.getReader());
199 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
200 String update = root.get("update").toString();
201 PdpData pdpGroupData = (PdpData)mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
202 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
205 if(update.contains("false")){
206 this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
208 this.container.updateGroup(activeGroupData);
210 } catch (Exception e) {
211 String message = "Unable to create Group. Reason:\n" + e.getMessage();
212 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + message);
216 response.setCharacterEncoding("UTF-8");
217 response.setContentType("application / json");
218 request.setCharacterEncoding("UTF-8");
220 PrintWriter out = response.getWriter();
222 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
223 JSONObject j = new JSONObject(msg);
224 out.write(j.toString());
229 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + e);
230 response.setCharacterEncoding("UTF-8");
231 request.setCharacterEncoding("UTF-8");
232 PrintWriter out = response.getWriter();
233 out.write(e.getMessage());
238 @RequestMapping(value={"/pdp_Group/remove_pdpFromGroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
239 public ModelAndView removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
241 ObjectMapper mapper = new ObjectMapper();
242 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
243 JsonNode root = mapper.readTree(request.getReader());
244 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
245 StdPDP deletePdp = mapper.readValue(root.get("data").toString(), StdPDP.class);
246 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
248 this.container.removePDP(deletePdp, activeGroupData);
249 response.setCharacterEncoding("UTF-8");
250 response.setContentType("application / json");
251 request.setCharacterEncoding("UTF-8");
253 PrintWriter out = response.getWriter();
255 String responseString = mapper.writeValueAsString(groups);
256 JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
257 out.write(j.toString());
262 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing Pdp from PDP Group" + e);
263 response.setCharacterEncoding("UTF-8");
264 request.setCharacterEncoding("UTF-8");
265 PrintWriter out = response.getWriter();
266 out.write(e.getMessage());
277 public String getId() {
280 public void setId(String id) {
283 public int getJmxPort() {
286 public void setJmxPort(int jmxPort) {
287 this.jmxPort = jmxPort;
289 public String getName() {
292 public void setName(String name) {
295 public String getDescription() {
298 public void setDescription(String description) {
299 this.description = description;