From 6e55b1d7c3e53fb49c5e94406ff5db4cb9990c87 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 19 Mar 2019 16:08:58 -0400 Subject: Use Policy Translator abstract class Instead of applications implementing the converter, use a protected abstract class that implements common code. Newer applications could re-use existing application, and shared code can be put into the base translator class. Fixed sonar issue and changed guard xml. Removed Converter and made translator into an implementation class that is an object in the Application Provider. Issue-ID: POLICY-1273 Change-Id: I9b48cbcdaf80d6d2ef19ef53e4b7cd4830e7e7b2 Signed-off-by: Pamela Dragosh --- .../pdp/application/guard/GuardPdpApplication.java | 162 ++------------------- .../guard/src/main/resources/RootGuardPolicy.xml | 9 +- 2 files changed, 11 insertions(+), 160 deletions(-) (limited to 'applications/guard/src') diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java index 2717c279..18385239 100644 --- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java +++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java @@ -24,34 +24,22 @@ package org.onap.policy.xacml.pdp.application.guard; import com.att.research.xacml.api.Request; import com.att.research.xacml.api.Response; -import com.att.research.xacml.api.pdp.PDPEngine; -import com.att.research.xacml.api.pdp.PDPException; -import com.att.research.xacml.util.XACMLPolicyWriter; import com.google.common.collect.Lists; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Path; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException; -import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConverter; -import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider; -import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils; +import org.onap.policy.pdp.xacml.application.common.std.StdMetadataTranslator; +import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; /** * This class implements the onap.policies.controlloop.Guard policy implementations. @@ -59,14 +47,12 @@ import org.yaml.snakeyaml.Yaml; * @author pameladragosh * */ -public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicationServiceProvider { +public class GuardPdpApplication extends StdXacmlApplicationServiceProvider { private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class); private static final String STRING_VERSION100 = "1.0.0"; private Map supportedPolicyTypes = new HashMap<>(); - private Path pathForData; - private Properties pdpProperties = null; - private PDPEngine pdpEngine = null; + private StdMetadataTranslator translator = new StdMetadataTranslator(); /** Constructor. * @@ -86,31 +72,6 @@ public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicati return Arrays.asList("guard"); } - @Override - public void initialize(Path pathForData) { - // - // Save our path - // - this.pathForData = pathForData; - LOGGER.debug("New Path is {}", this.pathForData.toAbsolutePath()); - // - // Look for and load the properties object - // - try { - pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData)); - LOGGER.debug("{}", pdpProperties); - } catch (IOException e) { - LOGGER.error("{}", e); - } - // - // Create an engine - // - PDPEngine newEngine = XacmlPolicyUtils.createEngine(pdpProperties); - if (newEngine != null) { - pdpEngine = newEngine; - } - } - @Override public List supportedPolicyTypes() { return Lists.newArrayList(supportedPolicyTypes.keySet()); @@ -137,10 +98,13 @@ public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicati // // Convert the policies first // - List listPolicies = this.convertPolicies(toscaPolicies); + List listPolicies = translator.scanAndConvertPolicies(toscaPolicies); if (listPolicies.isEmpty()) { throw new ToscaPolicyConversionException("Converted 0 policies"); } + // + // TODO update properties, save to disk, etc. + // } catch (ToscaPolicyConversionException e) { LOGGER.error("Failed to loadPolicies {}", e); } @@ -151,7 +115,7 @@ public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicati // // Convert to a XacmlRequest // - Request xacmlRequest = this.convertRequest(request); + Request xacmlRequest = translator.convertRequest(request); // // Now get a decision // @@ -159,113 +123,7 @@ public class GuardPdpApplication implements ToscaPolicyConverter, XacmlApplicati // // Convert to a DecisionResponse // - return this.convertResponse(xacmlResponse); - } - - @Override - public List convertPolicies(InputStream isToscaPolicy) throws ToscaPolicyConversionException { - // - // Have snakeyaml parse the object - // - Yaml yaml = new Yaml(); - Map toscaObject = yaml.load(isToscaPolicy); - // - // Return the policies - // - return scanAndConvertPolicies(toscaObject); - } - - @Override - public List convertPolicies(Map toscaObject) throws ToscaPolicyConversionException { - // - // Return the policies - // - return scanAndConvertPolicies(toscaObject); - } - - @Override - public Request convertRequest(DecisionRequest request) { - // TODO Auto-generated method stub - return null; - } - - @Override - public DecisionResponse convertResponse(Response response) { - // TODO Auto-generated method stub - return null; - } - - @SuppressWarnings("unchecked") - private List scanAndConvertPolicies(Map toscaObject) - throws ToscaPolicyConversionException { - // - // Our return object - // - List scannedPolicies = new ArrayList<>(); - // - // Iterate each of the Policies - // - List policies = (List) toscaObject.get("policies"); - for (Object policyObject : policies) { - // - // Get the contents - // - LOGGER.debug("Found policy {}", policyObject.getClass()); - Map policyContents = (Map) policyObject; - for (Entry entrySet : policyContents.entrySet()) { - LOGGER.debug("Entry set {}", entrySet); - // - // Convert this policy - // - PolicyType policy = this.convertPolicy(entrySet); - try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { - XACMLPolicyWriter.writePolicyFile(os, policy); - LOGGER.debug("{}", os); - } catch (IOException e) { - LOGGER.error("Failed to convert {}", e); - } - // - // Convert and add in the new policy - // - scannedPolicies.add(policy); - } - } - - return scannedPolicies; - } - - private PolicyType convertPolicy(Entry entrySet) throws ToscaPolicyConversionException { - - return null; - } - - /** - * Make a decision call. - * - * @param request Incoming request object - * @return Response object - */ - private synchronized Response xacmlDecision(Request request) { - // - // This is what we need to return - // - Response response = null; - // - // Track some timing - // - long timeStart = System.currentTimeMillis(); - try { - response = this.pdpEngine.decide(request); - } catch (PDPException e) { - LOGGER.error("Xacml PDP Engine failed {}", e); - } finally { - // - // Track the end of timing - // - long timeEnd = System.currentTimeMillis(); - LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart)); - } - return response; + return translator.convertResponse(xacmlResponse); } } diff --git a/applications/guard/src/main/resources/RootGuardPolicy.xml b/applications/guard/src/main/resources/RootGuardPolicy.xml index f9f47265..cc63792f 100644 --- a/applications/guard/src/main/resources/RootGuardPolicy.xml +++ b/applications/guard/src/main/resources/RootGuardPolicy.xml @@ -1,5 +1,5 @@ - + The root policy for supporting onap.Guard policies. @@ -18,11 +18,4 @@ onap.scaleout.tca urn:oasis:names:tc:xacml:2.0:conformance-test:IIE001:policyset1 --> - - Default is to allow a permit - returning 0 obligations - - - - - \ No newline at end of file -- cgit 1.2.3-korg