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;
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.json.JSONObject;
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
38 import org.openecomp.policy.model.PDPGroupContainer;
39 import org.openecomp.policy.model.Roles;
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
41 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
42 import org.openecomp.policy.xacml.std.pap.StdPDP;
43 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
44 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
45 import org.openecomp.portalsdk.core.web.support.JsonMessage;
46 import org.openecomp.portalsdk.core.web.support.UserUtils;
47 import org.springframework.http.MediaType;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.servlet.ModelAndView;
52 import com.att.research.xacml.api.pap.PAPException;
53 import com.att.research.xacml.api.pap.PDPPolicy;
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);
63 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
64 private PDPGroupContainer container;
66 private static String SUPERADMIN = "super-admin";
67 private static String SUPEREDITOR = "super-editor";
68 private static String SUPERGUEST = "super-guest";
70 public synchronized void refreshGroups(HttpServletRequest request) {
71 synchronized(this.groups) {
74 Set<PDPPolicy> filteredPolicies = new HashSet<PDPPolicy>();
75 Set<String> scopes = null;
76 List<String> roles = null;
77 String userId = UserUtils.getUserSession(request).getOrgUserId();
78 List<Object> userRoles = PolicyController.getRoles(userId);
79 roles = new ArrayList<String>();
80 scopes = new HashSet<String>();
81 for(Object role: userRoles){
82 Roles userRole = (Roles) role;
83 roles.add(userRole.getRole());
84 if(userRole.getScope() != null){
85 if(userRole.getScope().contains(",")){
86 String[] multipleScopes = userRole.getScope().split(",");
87 for(int i =0; i < multipleScopes.length; i++){
88 scopes.add(multipleScopes[i]);
91 scopes.add(userRole.getScope());
95 if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST) ) {
96 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
98 if(!userRoles.isEmpty()){
99 if(!scopes.isEmpty()){
100 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
101 if(!groups.isEmpty()){
102 for(EcompPDPGroup group : groups){
103 Set<PDPPolicy> policies = group.getPolicies();
104 for(PDPPolicy policy : policies){
105 for(String scope : scopes){
106 scope = scope.replace(File.separator, ".");
107 String policyName = policy.getId();
108 if(policyName.contains(".Config_")){
109 policyName = policyName.substring(0, policyName.lastIndexOf(".Config_"));
110 }else if(policyName.contains(".Action_")){
111 policyName = policyName.substring(0, policyName.lastIndexOf(".Action_"));
112 }else if(policyName.contains(".Decision_")){
113 policyName = policyName.substring(0, policyName.lastIndexOf(".Decision_"));
115 if(policyName.startsWith(scope)){
116 filteredPolicies.add(policy);
120 groups.remove(group);
121 StdPDPGroup newGroup = (StdPDPGroup) group;
122 newGroup.setPolicies(filteredPolicies);
123 groups.add(newGroup);
129 } catch (PAPException e) {
130 String message = "Unable to retrieve Groups from server: " + e;
131 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Pap Engine is Null" + message);
136 @RequestMapping(value={"/get_PDPGroupContainerData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
137 public void getPDPGroupContainerData(HttpServletRequest request, HttpServletResponse response){
139 ObjectMapper mapper = new ObjectMapper();
140 refreshGroups(request);
141 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
142 JSONObject j = new JSONObject(msg);
143 response.getWriter().write(j.toString());
146 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
150 @RequestMapping(value={"/get_PDPGroupData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
151 public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response){
153 ObjectMapper mapper = new ObjectMapper();
154 refreshGroups(request);
155 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
156 JSONObject j = new JSONObject(msg);
157 response.getWriter().write(j.toString());
160 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
164 @RequestMapping(value={"/pdp_Group/save_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
165 public ModelAndView savePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
167 ObjectMapper mapper = new ObjectMapper();
168 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169 JsonNode root = mapper.readTree(request.getReader());
170 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
171 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
173 if(pdpGroupData.getId() == null){
174 this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
176 this.container.updateGroup(pdpGroupData);
179 } catch (Exception e) {
180 String message = "Unable to create Group. Reason:\n" + e.getMessage();
181 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while creating the PDP Group" + message);
185 response.setCharacterEncoding("UTF-8");
186 response.setContentType("application / json");
187 request.setCharacterEncoding("UTF-8");
189 PrintWriter out = response.getWriter();
190 refreshGroups(request);
191 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
192 JSONObject j = new JSONObject(msg);
193 out.write(j.toString());
198 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Saving the PDP Group" + e);
199 response.setCharacterEncoding("UTF-8");
200 request.setCharacterEncoding("UTF-8");
201 PrintWriter out = response.getWriter();
202 out.write(e.getMessage());
207 @RequestMapping(value={"/pdp_Group/remove_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
208 public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
210 ObjectMapper mapper = new ObjectMapper();
211 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212 JsonNode root = mapper.readTree(request.getReader());
213 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
214 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
215 if(pdpGroupData.getName().equals("Default")) {
216 throw new UnsupportedOperationException("You can't remove the Default Group.");
218 this.container.removeGroup(pdpGroupData, null);
221 response.setCharacterEncoding("UTF-8");
222 response.setContentType("application / json");
223 request.setCharacterEncoding("UTF-8");
225 PrintWriter out = response.getWriter();
227 refreshGroups(request);
228 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
229 JSONObject j = new JSONObject(msg);
230 out.write(j.toString());
235 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing the PDP Group" + e);
236 response.setCharacterEncoding("UTF-8");
237 request.setCharacterEncoding("UTF-8");
238 PrintWriter out = response.getWriter();
239 out.write(e.getMessage());
244 @RequestMapping(value={"/pdp_Group/save_pdpTogroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
245 public ModelAndView savePDPToGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
247 ObjectMapper mapper = new ObjectMapper();
248 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
249 JsonNode root = mapper.readTree(request.getReader());
250 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
251 String update = root.get("update").toString();
252 PdpData pdpGroupData = (PdpData)mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
253 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
256 if(update.contains("false")){
257 this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
259 this.container.updateGroup(activeGroupData);
261 } catch (Exception e) {
262 String message = "Unable to create Group. Reason:\n" + e.getMessage();
263 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + message);
267 response.setCharacterEncoding("UTF-8");
268 response.setContentType("application / json");
269 request.setCharacterEncoding("UTF-8");
271 PrintWriter out = response.getWriter();
272 refreshGroups(request);
273 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
274 JSONObject j = new JSONObject(msg);
275 out.write(j.toString());
280 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + e);
281 response.setCharacterEncoding("UTF-8");
282 request.setCharacterEncoding("UTF-8");
283 PrintWriter out = response.getWriter();
284 out.write(e.getMessage());
289 @RequestMapping(value={"/pdp_Group/remove_pdpFromGroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
290 public ModelAndView removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
292 ObjectMapper mapper = new ObjectMapper();
293 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
294 JsonNode root = mapper.readTree(request.getReader());
295 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
296 StdPDP deletePdp = mapper.readValue(root.get("data").toString(), StdPDP.class);
297 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
299 this.container.removePDP(deletePdp, activeGroupData);
300 response.setCharacterEncoding("UTF-8");
301 response.setContentType("application / json");
302 request.setCharacterEncoding("UTF-8");
304 PrintWriter out = response.getWriter();
305 refreshGroups(request);
306 String responseString = mapper.writeValueAsString(groups);
307 JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
308 out.write(j.toString());
313 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing Pdp from PDP Group" + e);
314 response.setCharacterEncoding("UTF-8");
315 request.setCharacterEncoding("UTF-8");
316 PrintWriter out = response.getWriter();
317 out.write(e.getMessage());
328 public String getId() {
331 public void setId(String id) {
334 public int getJmxPort() {
337 public void setJmxPort(int jmxPort) {
338 this.jmxPort = jmxPort;
340 public String getName() {
343 public void setName(String name) {
346 public String getDescription() {
349 public void setDescription(String description) {
350 this.description = description;