* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package org.onap.policy.controller;
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.api.pap.PDPPolicy;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.io.PrintWriter;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
-
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
+
import javax.script.SimpleBindings;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.PolicyEntity;
import org.onap.policy.rest.jpa.PolicyVersion;
-import org.onap.policy.rest.util.PDPPolicyContainer;
+import org.onap.policy.rest.util.PdpPolicyContainer;
import org.onap.policy.utils.PolicyUtils;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.api.pap.OnapPDPGroup;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
-import com.att.research.xacml.api.pap.PAPException;
-import com.att.research.xacml.api.pap.PDPPolicy;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
@Controller
-@RequestMapping({ "/" })
+@RequestMapping({"/"})
public class AutoPushController extends RestrictedBaseController {
private static final Logger logger = FlexLogger.getLogger(AutoPushController.class);
- private static final String UTF8 = "UTF-8";
@Autowired
CommonClassDao commonClassDao;
private PDPGroupContainer container;
- private PDPPolicyContainer policyContainer;
+ private PdpPolicyContainer policyContainer;
private PolicyController policyController;
protected List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<>());
this.policyController = policyController;
}
+ /**
+ * refreshGroups.
+ */
public synchronized void refreshGroups() {
synchronized (this.groups) {
this.groups.clear();
return policyController != null ? getPolicyController() : new PolicyController();
}
- @RequestMapping(value = {"/get_AutoPushPoliciesContainerData"}, method = {
- RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
+ private Set<String> addAllScopes(Roles userRole, Set<String> scopes) {
+ if (userRole.getScope() != null) {
+ scopes.addAll(Stream.of(userRole.getScope().split(",")).collect(Collectors.toSet()));
+ }
+ return scopes;
+ }
+
+ /**
+ * getPolicyGroupContainerData.
+ *
+ * @param request HttpServletRequest
+ * @param response HttpServletResponse
+ */
+ @RequestMapping(
+ value = {"/get_AutoPushPoliciesContainerData"},
+ method = {RequestMethod.GET},
+ produces = MediaType.APPLICATION_JSON_VALUE)
public void getPolicyGroupContainerData(HttpServletRequest request, HttpServletResponse response) {
try {
Set<String> scopes = new HashSet<>();
for (Object role : userRoles) {
Roles userRole = (Roles) role;
roles.add(userRole.getRole());
- scopes.addAll(Stream.of(userRole.getScope().split(","))
- .map(String::new)
- .collect(Collectors.toSet())
- );
+ addAllScopes(userRole, scopes);
}
+
if (roles.contains("super-admin") || roles.contains("super-editor") || roles.contains("super-guest")) {
data = commonClassDao.getData(PolicyVersion.class);
} else {
} else {
PolicyVersion emptyPolicyName = new PolicyVersion();
emptyPolicyName
- .setPolicyName("Please Contact Policy Super Admin, There are no scopes assigned to you");
+ .setPolicyName("Please Contact Policy Super Admin, There are no scopes assigned to you");
data.add(emptyPolicyName);
}
}
ObjectMapper mapper = new ObjectMapper();
model.put("policydatas", mapper.writeValueAsString(data));
JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
- JSONObject j = new JSONObject(msg);
- response.getWriter().write(j.toString());
+ response.getWriter().write(new JSONObject(msg).toString());
} catch (Exception e) {
logger.error("Exception Occurred" + e);
}
}
- @RequestMapping(value = { "/auto_Push/PushPolicyToPDP.htm" }, method = { RequestMethod.POST })
+ /**
+ * pushPolicyToPDPGroup.
+ *
+ * @param request HttpServletRequest
+ * @param response HttpServletResponse
+ * @return ModelAndView
+ * @throws IOException IOException
+ */
+ @RequestMapping(value = {"/auto_Push/PushPolicyToPDP.htm"}, method = {RequestMethod.POST})
public ModelAndView pushPolicyToPDPGroup(HttpServletRequest request, HttpServletResponse response)
throws IOException {
try {
- ArrayList<Object> selectedPDPS = new ArrayList<>();
+ response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ //
+ //
+ //
+ ArrayList<Object> selectedPdps = new ArrayList<>();
ArrayList<String> selectedPoliciesInUI = new ArrayList<>();
PolicyController controller = getPolicyControllerInstance();
this.groups.addAll(controller.getPapEngine().getOnapPDPGroups());
String userId = UserUtils.getUserSession(request).getOrgUserId();
logger.info(
- "****************************************Logging UserID while Pushing Policy to PDP Group*****************************************");
+ "**********************Logging UserID while Pushing Policy to PDP Group***********************");
logger.info("UserId: " + userId + "Push Policy Data: " + root.get("pushTabData").toString());
logger.info(
- "***********************************************************************************************************************************");
+ "**********************************************************************************************");
AutoPushTabAdapter adapter = mapper.readValue(root.get("pushTabData").toString(), AutoPushTabAdapter.class);
for (Object pdpGroupId : adapter.getPdpDatas()) {
- LinkedHashMap<?, ?> selectedPDP = (LinkedHashMap<?, ?>) pdpGroupId;
+ LinkedHashMap<?, ?> selectedPdp = (LinkedHashMap<?, ?>) pdpGroupId;
for (OnapPDPGroup pdpGroup : this.groups) {
- if (pdpGroup.getId().equals(selectedPDP.get("id"))) {
- selectedPDPS.add(pdpGroup);
+ if (pdpGroup.getId().equals(selectedPdp.get("id"))) {
+ selectedPdps.add(pdpGroup);
}
}
}
for (Object policyId : adapter.getPolicyDatas()) {
LinkedHashMap<?, ?> selected = (LinkedHashMap<?, ?>) policyId;
- String policyName = selected.get("policyName").toString() + "."
- + selected.get("activeVersion").toString() + ".xml";
+ String policyName =
+ selected.get("policyName").toString() + "." + selected.get("activeVersion").toString() + ".xml";
selectedPoliciesInUI.add(policyName);
}
- for (Object pdpDestinationGroupId : selectedPDPS) {
+ for (Object pdpDestinationGroupId : selectedPdps) {
Set<PDPPolicy> currentPoliciesInGroup = new HashSet<>();
Set<PDPPolicy> selectedPolicies = new HashSet<>();
for (String policyId : selectedPoliciesInUI) {
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(policyEntity.getPolicyData());
bw.close();
- URI selectedURI = temp.toURI();
try {
// Create the policy
- selectedPolicy = new StdPDPPolicy(name, true, id, selectedURI);
+ selectedPolicy = new StdPDPPolicy(name, true, id, temp.toURI());
} catch (IOException e) {
logger.error("Unable to create policy '" + name + "': " + e.getMessage(), e);
}
}
// copy policy to PAP
try {
- controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId);
+ controller.getPapEngine().copyPolicy(selectedPolicy, (StdPDPGroup) pdpDestinationGroupId, userId);
} catch (PAPException e) {
logger.error("Exception Occured" + e);
return null;
currentPoliciesInGroup.addAll(selectedPolicies);
updatedGroupObject.setPolicies(currentPoliciesInGroup);
- this.container.updateGroup(updatedGroupObject);
-
- response.setCharacterEncoding(UTF8);
- response.setContentType("application / json");
- request.setCharacterEncoding(UTF8);
-
- PrintWriter out = response.getWriter();
+ this.container.updateGroup(updatedGroupObject, userId);
+ response.setContentType(PolicyUtils.APPLICATION_JSON);
refreshGroups();
- JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
- JSONObject j = new JSONObject(msg);
- out.write(j.toString());
- //
- // Why is this here? This defeats the purpose of the loop??
- // Sonar says to remove it or make it conditional
- //
- return null;
+ response.getWriter().write(new JSONObject(
+ new JsonMessage(mapper.writeValueAsString(groups))).toString());
}
} catch (Exception e) {
- response.setCharacterEncoding(UTF8);
- request.setCharacterEncoding(UTF8);
- PrintWriter out = response.getWriter();
logger.error(e);
- out.write(PolicyUtils.CATCH_EXCEPTION);
+ response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);
}
return null;
}
+ /**
+ * removePDPGroup.
+ */
@SuppressWarnings("unchecked")
- @RequestMapping(value = { "/auto_Push/remove_GroupPolicies.htm" }, method = { RequestMethod.POST })
+ @RequestMapping(value = {"/auto_Push/remove_GroupPolicies.htm"}, method = {RequestMethod.POST})
public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
+ response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ //
+ //
+ //
PolicyController controller = getPolicyControllerInstance();
this.container = new PDPGroupContainer(controller.getPapEngine());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode root = mapper.readTree(request.getReader());
- StdPDPGroup group = mapper.readValue(root.get("activePdpGroup").toString(), StdPDPGroup.class);
- JsonNode removePolicyData = root.get("data");
+ final StdPDPGroup group = mapper.readValue(root.get("activePdpGroup").toString(), StdPDPGroup.class);
+ final JsonNode removePolicyData = root.get("data");
String userId = UserUtils.getUserSession(request).getOrgUserId();
logger.info(
- "****************************************Logging UserID while Removing Policy from PDP Group*****************************************");
+ "**********************Logging UserID while Removing Policy from PDP Group*********************");
logger.info("UserId: " + userId + "PDP Group Data: " + root.get("activePdpGroup").toString()
+ "Remove Policy Data: " + root.get("data"));
logger.info(
- "***********************************************************************************************************************************");
+ "**********************************************************************************************");
- policyContainer = new PDPPolicyContainer(group);
+ policyContainer = new PdpPolicyContainer(group);
if (removePolicyData.size() > 0) {
IntStream.range(0, removePolicyData.size()).mapToObj(i -> removePolicyData.get(i).toString())
- .forEach(polData -> this.policyContainer.removeItem(polData));
- Set<PDPPolicy> changedPolicies = new HashSet<>(
- (Collection<PDPPolicy>) this.policyContainer.getItemIds());
+ .forEach(polData -> this.policyContainer.removeItem(polData));
+ Set<PDPPolicy> changedPolicies =
+ new HashSet<>((Collection<PDPPolicy>) this.policyContainer.getItemIds());
StdPDPGroup updatedGroupObject = new StdPDPGroup(group.getId(), group.isDefaultGroup(), group.getName(),
group.getDescription(), null);
updatedGroupObject.setPolicies(changedPolicies);
updatedGroupObject.setPipConfigs(group.getPipConfigs());
updatedGroupObject.setStatus(group.getStatus());
updatedGroupObject.setOperation("delete");
- this.container.updateGroup(updatedGroupObject);
+ this.container.updateGroup(updatedGroupObject, userId);
}
- response.setCharacterEncoding(UTF8);
- response.setContentType("application / json");
- request.setCharacterEncoding(UTF8);
+ response.setContentType(PolicyUtils.APPLICATION_JSON);
- PrintWriter out = response.getWriter();
refreshGroups();
- JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
- JSONObject j = new JSONObject(msg);
-
- out.write(j.toString());
-
- return null;
+ response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(groups))).toString());
} catch (Exception e) {
- response.setCharacterEncoding(UTF8);
- request.setCharacterEncoding(UTF8);
- PrintWriter out = response.getWriter();
logger.error(e);
- out.write(PolicyUtils.CATCH_EXCEPTION);
+ response.getWriter().write(PolicyUtils.CATCH_EXCEPTION);
}
return null;
}