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.Iterator;
30 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
36 import org.json.JSONObject;
37 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
38 import org.openecomp.policy.common.logging.flexlogger.Logger;
39 import org.openecomp.policy.model.PDPGroupContainer;
40 import org.openecomp.policy.model.Roles;
41 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
42 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
43 import org.openecomp.policy.xacml.std.pap.StdPDP;
44 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
45 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
46 import org.openecomp.portalsdk.core.web.support.JsonMessage;
47 import org.openecomp.portalsdk.core.web.support.UserUtils;
48 import org.springframework.http.MediaType;
49 import org.springframework.stereotype.Controller;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.servlet.ModelAndView;
53 import com.att.research.xacml.api.pap.PAPException;
54 import com.att.research.xacml.api.pap.PDPPolicy;
55 import com.fasterxml.jackson.databind.DeserializationFeature;
56 import com.fasterxml.jackson.databind.JsonNode;
57 import com.fasterxml.jackson.databind.ObjectMapper;
60 @RequestMapping({"/"})
61 public class PDPController extends RestrictedBaseController {
62 private static final Logger logger = FlexLogger.getLogger(PDPController.class);
64 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
65 private PDPGroupContainer container;
67 private static String SUPERADMIN = "super-admin";
68 private static String SUPEREDITOR = "super-editor";
69 private static String SUPERGUEST = "super-guest";
71 public synchronized void refreshGroups(HttpServletRequest request) {
72 synchronized(this.groups) {
75 Set<PDPPolicy> filteredPolicies = new HashSet<>();
76 Set<String> scopes = null;
77 List<String> roles = null;
78 String userId = UserUtils.getUserSession(request).getOrgUserId();
79 List<Object> userRoles = PolicyController.getRoles(userId);
80 roles = new ArrayList<>();
81 scopes = new HashSet<>();
82 for(Object role: userRoles){
83 Roles userRole = (Roles) role;
84 roles.add(userRole.getRole());
85 if(userRole.getScope() != null){
86 if(userRole.getScope().contains(",")){
87 String[] multipleScopes = userRole.getScope().split(",");
88 for(int i =0; i < multipleScopes.length; i++){
89 scopes.add(multipleScopes[i]);
92 scopes.add(userRole.getScope());
96 if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST) ) {
97 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
99 if(!userRoles.isEmpty()){
100 if(!scopes.isEmpty()){
101 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
102 List<EcompPDPGroup> tempGroups = new ArrayList<EcompPDPGroup>();
103 if(!groups.isEmpty()){
104 Iterator<EcompPDPGroup> pdpGroup = groups.iterator();
105 while(pdpGroup.hasNext()){
106 EcompPDPGroup group = pdpGroup.next();
107 Set<PDPPolicy> policies = group.getPolicies();
108 for(PDPPolicy policy : policies){
109 for(String scope : scopes){
110 scope = scope.replace(File.separator, ".");
111 String policyName = policy.getId();
112 if(policyName.contains(".Config_")){
113 policyName = policyName.substring(0, policyName.lastIndexOf(".Config_"));
114 }else if(policyName.contains(".Action_")){
115 policyName = policyName.substring(0, policyName.lastIndexOf(".Action_"));
116 }else if(policyName.contains(".Decision_")){
117 policyName = policyName.substring(0, policyName.lastIndexOf(".Decision_"));
119 if(policyName.startsWith(scope)){
120 filteredPolicies.add(policy);
125 StdPDPGroup newGroup = (StdPDPGroup) group;
126 newGroup.setPolicies(filteredPolicies);
127 tempGroups.add(newGroup);
135 } catch (PAPException e) {
136 String message = "Unable to retrieve Groups from server: " + e;
137 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Pap Engine is Null" + message);
142 @RequestMapping(value={"/get_PDPGroupContainerData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
143 public void getPDPGroupContainerData(HttpServletRequest request, HttpServletResponse response){
145 ObjectMapper mapper = new ObjectMapper();
146 refreshGroups(request);
147 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
148 JSONObject j = new JSONObject(msg);
149 response.getWriter().write(j.toString());
152 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
156 @RequestMapping(value={"/get_PDPGroupData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
157 public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response){
159 ObjectMapper mapper = new ObjectMapper();
160 refreshGroups(request);
161 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
162 JSONObject j = new JSONObject(msg);
163 response.getWriter().write(j.toString());
166 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
170 @RequestMapping(value={"/pdp_Group/save_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
171 public ModelAndView savePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
173 ObjectMapper mapper = new ObjectMapper();
174 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
175 JsonNode root = mapper.readTree(request.getReader());
176 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
177 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
179 if(pdpGroupData.getId() == null){
180 this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
182 this.container.updateGroup(pdpGroupData);
185 } catch (Exception e) {
186 String message = "Unable to create Group. Reason:\n" + e.getMessage();
187 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while creating the PDP Group" + message);
191 response.setCharacterEncoding("UTF-8");
192 response.setContentType("application / json");
193 request.setCharacterEncoding("UTF-8");
195 PrintWriter out = response.getWriter();
196 refreshGroups(request);
197 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
198 JSONObject j = new JSONObject(msg);
199 out.write(j.toString());
204 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Saving the PDP Group" + e);
205 response.setCharacterEncoding("UTF-8");
206 request.setCharacterEncoding("UTF-8");
207 PrintWriter out = response.getWriter();
208 out.write(e.getMessage());
213 @RequestMapping(value={"/pdp_Group/remove_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
214 public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
216 ObjectMapper mapper = new ObjectMapper();
217 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
218 JsonNode root = mapper.readTree(request.getReader());
219 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
220 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
221 if(pdpGroupData.getName().equals("Default")) {
222 throw new UnsupportedOperationException("You can't remove the Default Group.");
224 this.container.removeGroup(pdpGroupData, null);
227 response.setCharacterEncoding("UTF-8");
228 response.setContentType("application / json");
229 request.setCharacterEncoding("UTF-8");
231 PrintWriter out = response.getWriter();
233 refreshGroups(request);
234 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
235 JSONObject j = new JSONObject(msg);
236 out.write(j.toString());
241 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing the PDP Group" + e);
242 response.setCharacterEncoding("UTF-8");
243 request.setCharacterEncoding("UTF-8");
244 PrintWriter out = response.getWriter();
245 out.write(e.getMessage());
250 @RequestMapping(value={"/pdp_Group/save_pdpTogroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
251 public ModelAndView savePDPToGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
253 ObjectMapper mapper = new ObjectMapper();
254 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
255 JsonNode root = mapper.readTree(request.getReader());
256 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
257 String update = root.get("update").toString();
258 PdpData pdpGroupData = (PdpData)mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
259 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
262 if(update.contains("false")){
263 this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
265 this.container.updateGroup(activeGroupData);
267 } catch (Exception e) {
268 String message = "Unable to create Group. Reason:\n" + e.getMessage();
269 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + message);
273 response.setCharacterEncoding("UTF-8");
274 response.setContentType("application / json");
275 request.setCharacterEncoding("UTF-8");
277 PrintWriter out = response.getWriter();
278 refreshGroups(request);
279 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
280 JSONObject j = new JSONObject(msg);
281 out.write(j.toString());
286 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + e);
287 response.setCharacterEncoding("UTF-8");
288 request.setCharacterEncoding("UTF-8");
289 PrintWriter out = response.getWriter();
290 out.write(e.getMessage());
295 @RequestMapping(value={"/pdp_Group/remove_pdpFromGroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
296 public ModelAndView removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
298 ObjectMapper mapper = new ObjectMapper();
299 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
300 JsonNode root = mapper.readTree(request.getReader());
301 this.container = new PDPGroupContainer(PolicyController.getPapEngine());
302 StdPDP deletePdp = mapper.readValue(root.get("data").toString(), StdPDP.class);
303 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
305 this.container.removePDP(deletePdp, activeGroupData);
306 response.setCharacterEncoding("UTF-8");
307 response.setContentType("application / json");
308 request.setCharacterEncoding("UTF-8");
310 PrintWriter out = response.getWriter();
311 refreshGroups(request);
312 String responseString = mapper.writeValueAsString(groups);
313 JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
314 out.write(j.toString());
319 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing Pdp from PDP Group" + e);
320 response.setCharacterEncoding("UTF-8");
321 request.setCharacterEncoding("UTF-8");
322 PrintWriter out = response.getWriter();
323 out.write(e.getMessage());
334 public String getId() {
337 public void setId(String id) {
340 public int getJmxPort() {
343 public void setJmxPort(int jmxPort) {
344 this.jmxPort = jmxPort;
346 public String getName() {
349 public void setName(String name) {
352 public String getDescription() {
355 public void setDescription(String description) {
356 this.description = description;