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.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;
41 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
42 import org.openecomp.policy.common.logging.flexlogger.Logger;
44 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
45 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
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;
55 @RequestMapping({"/"})
56 public class PDPController extends RestrictedBaseController {
57 private static final Logger logger = FlexLogger.getLogger(PDPController.class);
59 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
60 private PDPGroupContainer container;
62 public synchronized void refreshGroups() {
63 synchronized(this.groups) {
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);
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){
78 ObjectMapper mapper = new ObjectMapper();
80 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
81 JSONObject j = new JSONObject(msg);
82 response.getWriter().write(j.toString());
85 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
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){
92 ObjectMapper mapper = new ObjectMapper();
94 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
95 JSONObject j = new JSONObject(msg);
96 response.getWriter().write(j.toString());
99 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
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{
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);
112 if(pdpGroupData.getId() == null){
113 this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
115 this.container.updateGroup(pdpGroupData);
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);
124 response.setCharacterEncoding("UTF-8");
125 response.setContentType("application / json");
126 request.setCharacterEncoding("UTF-8");
128 PrintWriter out = response.getWriter();
130 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
131 JSONObject j = new JSONObject(msg);
132 out.write(j.toString());
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());
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 {
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.");
157 this.container.removeGroup(pdpGroupData, null);
160 response.setCharacterEncoding("UTF-8");
161 response.setContentType("application / json");
162 request.setCharacterEncoding("UTF-8");
164 PrintWriter out = response.getWriter();
167 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
168 JSONObject j = new JSONObject(msg);
169 out.write(j.toString());
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());
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{
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);
195 if(update.contains("false")){
196 this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
198 this.container.updateGroup(activeGroupData);
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);
206 response.setCharacterEncoding("UTF-8");
207 response.setContentType("application / json");
208 request.setCharacterEncoding("UTF-8");
210 PrintWriter out = response.getWriter();
212 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
213 JSONObject j = new JSONObject(msg);
214 out.write(j.toString());
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());
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 {
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);
238 this.container.removePDP(deletePdp, activeGroupData);
239 response.setCharacterEncoding("UTF-8");
240 response.setContentType("application / json");
241 request.setCharacterEncoding("UTF-8");
243 PrintWriter out = response.getWriter();
245 String responseString = mapper.writeValueAsString(groups);
246 JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
247 out.write(j.toString());
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());
267 public String getId() {
270 public void setId(String id) {
273 public int getJmxPort() {
276 public void setJmxPort(int jmxPort) {
277 this.jmxPort = jmxPort;
279 public String getName() {
282 public void setName(String name) {
285 public String getDescription() {
288 public void setDescription(String description) {
289 this.description = description;