2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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.onap.policy.controller;
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.api.pap.PDPPolicy;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
30 import java.io.PrintWriter;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.Iterator;
35 import java.util.List;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
42 import org.json.JSONObject;
43 import org.onap.policy.admin.RESTfulPAPEngine;
44 import org.onap.policy.common.logging.flexlogger.FlexLogger;
45 import org.onap.policy.common.logging.flexlogger.Logger;
46 import org.onap.policy.model.PDPGroupContainer;
47 import org.onap.policy.utils.UserUtils.Pair;
48 import org.onap.policy.xacml.api.XACMLErrorConstants;
49 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
50 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
51 import org.onap.policy.xacml.std.pap.StdPDP;
52 import org.onap.policy.xacml.std.pap.StdPDPGroup;
53 import org.onap.portalsdk.core.controller.RestrictedBaseController;
54 import org.onap.portalsdk.core.web.support.JsonMessage;
55 import org.onap.portalsdk.core.web.support.UserUtils;
56 import org.springframework.http.MediaType;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestMapping;
61 @RequestMapping({"/"})
62 public class PDPController extends RestrictedBaseController {
63 private static final Logger policyLogger = FlexLogger.getLogger(PDPController.class);
65 protected List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<OnapPDPGroup>());
66 private PDPGroupContainer container;
68 private static String SUPERADMIN = "super-admin";
69 private static String SUPEREDITOR = "super-editor";
70 private static String SUPERGUEST = "super-guest";
72 private Set<OnapPDPGroup> groupsData;
74 private boolean junit = false;
76 private PolicyController policyController;
78 public PolicyController getPolicyController() {
79 return policyController;
82 public void setPolicyController(PolicyController policyController) {
83 this.policyController = policyController;
89 * @param request HttpServletRequest
91 public synchronized void refreshGroups(HttpServletRequest request) {
92 synchronized (this.groups) {
95 PolicyController controller = getPolicyControllerInstance();
96 Set<PDPPolicy> filteredPolicies = new HashSet<>();
99 String userId = isJunit() ? "Test" : UserUtils.getUserSession(request).getOrgUserId();
100 List<Object> userRoles = controller.getRoles(userId);
101 Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
105 if (!junit && controller.getPapEngine() == null) {
106 setPapEngine(request);
108 if (roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)) {
110 this.groups.addAll(controller.getPapEngine().getOnapPDPGroups());
112 this.groups.addAll(this.getGroupsData());
115 if (!userRoles.isEmpty() && !scopes.isEmpty()) {
116 this.groups.addAll(controller.getPapEngine().getOnapPDPGroups());
117 List<OnapPDPGroup> tempGroups = new ArrayList<>();
118 if (!groups.isEmpty()) {
119 Iterator<OnapPDPGroup> pdpGroup = groups.iterator();
120 while (pdpGroup.hasNext()) {
121 OnapPDPGroup group = pdpGroup.next();
122 Set<PDPPolicy> policies = group.getPolicies();
123 for (PDPPolicy policy : policies) {
124 for (String scope : scopes) {
125 scope = scope.replace(File.separator, ".");
126 String policyName = policy.getId();
127 if (policyName.contains(".Config_")) {
128 policyName = policyName.substring(0, policyName.lastIndexOf(".Config_"));
129 } else if (policyName.contains(".Action_")) {
130 policyName = policyName.substring(0, policyName.lastIndexOf(".Action_"));
131 } else if (policyName.contains(".Decision_")) {
132 policyName = policyName.substring(0, policyName.lastIndexOf(".Decision_"));
134 if (policyName.startsWith(scope)) {
135 filteredPolicies.add(policy);
140 StdPDPGroup newGroup = (StdPDPGroup) group;
141 newGroup.setPolicies(filteredPolicies);
142 tempGroups.add(newGroup);
149 } catch (PAPException e) {
150 String message = "Unable to retrieve Groups from server: " + e;
151 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Pap Engine is Null" + message);
156 private void setPapEngine(HttpServletRequest request) {
159 // Set the URL for the RESTful PAP Engine
161 PolicyController.setPapEngine(new RESTfulPAPEngine(request.getRequestURL().toString()));
162 } catch (Exception e) {
163 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while loading PAP", e);
168 * getPDPGroupEntityData.
170 * @param request HttpServletRequest
171 * @param response HttpServletResponse
174 value = {"/get_PDPGroupData"},
175 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
176 produces = MediaType.APPLICATION_JSON_VALUE)
177 public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response) {
179 refreshGroups(request);
180 response.getWriter().write(new JSONObject(new JsonMessage(
181 new ObjectMapper().writeValueAsString(groups))).toString());
182 } catch (Exception e) {
184 XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while retrieving the PDP Group data" + e);
191 * @param request HttpServletRequest
192 * @param response HttpServletResponse
195 value = {"/pdp_Group/save_pdp_group"},
196 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
197 public void savePDPGroup(HttpServletRequest request, HttpServletResponse response) {
199 ObjectMapper mapper = new ObjectMapper();
200 PolicyController controller = getPolicyControllerInstance();
201 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
202 JsonNode root = mapper.readTree(request.getReader());
203 this.container = new PDPGroupContainer(controller.getPapEngine());
205 String userId = UserUtils.getUserSession(request).getOrgUserId();
207 "*******************Logging UserID for Save PDP Group Function*******************************");
208 policyLogger.info("UserId: " + userId + "PDP Group Data: " + root.get("pdpGroupData").toString());
210 "********************************************************************************************");
212 StdPDPGroup pdpGroupData = mapper
213 .readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
215 if (pdpGroupData.getId() == null) {
216 this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
218 this.container.updateGroup(pdpGroupData);
221 } catch (Exception e) {
222 String message = "Unable to create Group. Reason:\n" + e.getMessage();
223 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while creating the PDP Group"
227 response.setCharacterEncoding("UTF-8");
228 response.setContentType("application / json");
229 request.setCharacterEncoding("UTF-8");
231 refreshGroups(request);
232 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(groups))).toString());
233 } catch (Exception e) {
234 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while Saving the PDP Group" + e);
235 response.setCharacterEncoding("UTF-8");
236 PrintWriter out = null;
238 request.setCharacterEncoding("UTF-8");
239 out = response.getWriter();
240 out.write(e.getMessage());
241 } catch (Exception e1) {
243 .error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while Saving the PDP Group" + e1);
251 * @param request HttpServletRequest
252 * @param response HttpServletResponse
255 value = {"/pdp_Group/remove_pdp_group"},
256 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
257 public void removePDPGroup(HttpServletRequest request, HttpServletResponse response) {
259 ObjectMapper mapper = new ObjectMapper();
260 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
261 JsonNode root = mapper.readTree(request.getReader());
262 PolicyController controller = getPolicyControllerInstance();
263 this.container = new PDPGroupContainer(controller.getPapEngine());
265 String userId = UserUtils.getUserSession(request).getOrgUserId();
267 "*********************Logging UserID for Remove PDP Group Function*******************************");
268 policyLogger.info("UserId: " + userId + "PDP Group Data: " + root.get("pdpGroupData").toString());
270 "************************************************************************************************");
272 StdPDPGroup pdpGroupData = mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
273 if ("Default".equals(pdpGroupData.getName())) {
274 throw new UnsupportedOperationException("You can't remove the Default Group.");
276 this.container.removeGroup(pdpGroupData, null);
279 response.setCharacterEncoding("UTF-8");
280 response.setContentType("application / json");
281 request.setCharacterEncoding("UTF-8");
282 refreshGroups(request);
283 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(groups))).toString());
284 } catch (Exception e) {
285 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while Removing the PDP Group" + e);
288 response.setCharacterEncoding("UTF-8");
289 request.setCharacterEncoding("UTF-8");
290 out = response.getWriter();
291 out.write(e.getMessage());
292 } catch (Exception e1) {
293 policyLogger.error("Exception Occured" + e1);
301 * @param request HttpServletRequest
302 * @param response HttpServletResponse
305 value = {"/pdp_Group/save_pdpTogroup"},
306 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
307 public void savePDPToGroup(HttpServletRequest request, HttpServletResponse response) {
309 ObjectMapper mapper = new ObjectMapper();
310 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
311 JsonNode root = mapper.readTree(request.getReader());
312 PolicyController controller = getPolicyControllerInstance();
313 this.container = new PDPGroupContainer(controller.getPapEngine());
314 String update = root.get("update").toString();
315 PdpData pdpGroupData = mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
316 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
318 String userId = UserUtils.getUserSession(request).getOrgUserId();
320 "*************Logging UserID while Saving pdp in PDP Group***********************************");
321 policyLogger.info("UserId: " + userId + "PDP Group Data: " + root.get("pdpInGroup").toString()
322 + "Active Group Data: " + root.get("activePDP").toString());
324 "**********************************************************************************************");
328 if (update.contains("false")) {
329 this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(),
330 pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
332 this.container.updateGroup(activeGroupData);
334 } catch (Exception e) {
335 String message = "Unable to create Group. Reason:\n" + e.getMessage();
336 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE
337 + "Error Occured while Creating Pdp in PDP Group" + message + e);
340 response.setCharacterEncoding("UTF-8");
341 response.setContentType("application / json");
342 request.setCharacterEncoding("UTF-8");
343 refreshGroups(request);
344 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(groups))).toString());
345 } catch (Exception e) {
347 .error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while Creating Pdp in PDP Group" + e);
350 response.setCharacterEncoding("UTF-8");
351 request.setCharacterEncoding("UTF-8");
352 out = response.getWriter();
353 out.write(e.getMessage());
354 } catch (Exception e1) {
355 policyLogger.error("Exception Occured" + e1);
361 * removePDPFromGroup.
363 * @param request HttpServletRequest
364 * @param response HttpServletResponse
367 value = {"/pdp_Group/remove_pdpFromGroup"},
368 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
369 public void removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) {
371 ObjectMapper mapper = new ObjectMapper();
372 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
373 JsonNode root = mapper.readTree(request.getReader());
374 PolicyController controller = getPolicyControllerInstance();
375 this.container = new PDPGroupContainer(controller.getPapEngine());
377 String userId = UserUtils.getUserSession(request).getOrgUserId();
379 "********************Logging UserID while Removing pdp from PDP Group**************************");
380 policyLogger.info("UserId: " + userId + "Delete PDP Group Data: " + root.get("data").toString()
381 + "Active Group Data: " + root.get("activePDP").toString());
383 "************************************************************************************************");
385 StdPDP deletePdp = mapper.readValue(root.get("data").toString(), StdPDP.class);
386 StdPDPGroup activeGroupData = mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
387 this.container.removePDP(deletePdp, activeGroupData);
388 response.setCharacterEncoding("UTF-8");
389 response.setContentType("application / json");
390 request.setCharacterEncoding("UTF-8");
392 refreshGroups(request);
393 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(groups))).toString());
394 } catch (Exception e) {
396 XACMLErrorConstants.ERROR_DATA_ISSUE + "Error Occured while Removing Pdp from PDP Group" + e);
398 response.setCharacterEncoding("UTF-8");
399 request.setCharacterEncoding("UTF-8");
400 response.getWriter().write(e.getMessage());
401 } catch (Exception e1) {
402 policyLogger.error("Exception Occured" + e1);
407 private PolicyController getPolicyControllerInstance() {
408 return policyController != null ? getPolicyController() : new PolicyController();
411 public boolean isJunit() {
415 public void setJunit(boolean junit) {
419 public Set<OnapPDPGroup> getGroupsData() {
423 public void setGroupsData(Set<OnapPDPGroup> groupsData) {
424 this.groupsData = groupsData;