From e034d785a227815f66138d1f83f49624c402aa97 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Mon, 8 Apr 2019 08:47:38 -0400 Subject: Test decision from main entry Tests a decision upon startup. This also updates the use of ToscaPolicyTypeIdentifier for all the applications. Very basic packaging for applications and their properties. Added cleaning of unused imports to remove sonar issues. Added production persistence.xml file to guard application. Not sure if we need a copy in the application/common resource directory. Issue-ID: POLICY-1440 Change-Id: If96eef5a9e0a7c6cc5461c0bdb6f0cd708cc41bb Signed-off-by: Pamela Dragosh --- .../policy/pdpx/main/comm/XacmlPdpMessage.java | 2 + .../listeners/XacmlPdpStateChangeListener.java | 1 - .../pdpx/main/rest/XacmlPdpApplicationManager.java | 82 +++++++++++++++------- .../org/onap/policy/pdpx/main/startstop/Main.java | 4 +- .../pdpx/main/startstop/XacmlPdpActivator.java | 2 +- .../onap/policy/pdpx/main/rest/TestDecision.java | 59 ++++++++++++++-- .../pdpx/main/startstop/TestXacmlPdpActivator.java | 3 +- 7 files changed, 116 insertions(+), 37 deletions(-) (limited to 'main') diff --git a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java index e05120d7..233bd7f7 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpMessage.java @@ -58,6 +58,8 @@ public class XacmlPdpMessage { status.setState(state); status.setSupportedPolicyTypes(XacmlPdpApplicationManager.getToscaPolicyTypeIdents()); + LOGGER.debug("formatStatusMessage state {} status{}", state, status); + return status; } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/comm/listeners/XacmlPdpStateChangeListener.java b/main/src/main/java/org/onap/policy/pdpx/main/comm/listeners/XacmlPdpStateChangeListener.java index 68bf06dd..f5b2fbfa 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/comm/listeners/XacmlPdpStateChangeListener.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/comm/listeners/XacmlPdpStateChangeListener.java @@ -30,7 +30,6 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.pdpx.main.comm.XacmlPdpHeartbeatPublisher; import org.onap.policy.pdpx.main.comm.XacmlPdpMessage; -import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java index 7d3292a6..a5e1d030 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java @@ -20,6 +20,8 @@ package org.onap.policy.pdpx.main.rest; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -29,6 +31,7 @@ import java.util.Map; import java.util.ServiceLoader; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException; import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,44 +63,43 @@ public class XacmlPdpApplicationManager { // Load service // applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class); - // - // Iterate through them the applications for actions and supported policy types + // Iterate through the applications for actions and supported policy types // for (XacmlApplicationServiceProvider application : applicationLoader) { - LOGGER.info("Application {} supports {}", application.applicationName(), application.supportedPolicyTypes()); - // - // Iterate through the actions and save in the providerActionMap + // We are not going to make this available unless the application can + // install correctly. // - int pathCount = 1; - for (String action : application.actionDecisionsSupported()) { + boolean applicationInitialized = false; + // + // Have it initialize at a path + // + try { + initializeApplicationPath(applicationPath, application); // - // Save the actions that it supports + // We are initialized // - providerActionMap.put(action, application); + applicationInitialized = true; + } catch (XacmlApplicationException e) { + LOGGER.error("Failed to initialize path for {}", application.applicationName(), e); + } + if (applicationInitialized) { // - // Create a unique path for the application to store its data - // May need to scan this name to remove unsafe characters etc. - // But for debugging purposes, its good to use the application name + // Iterate through the actions and save in the providerActionMap // - Path path = Paths.get(applicationPath.toAbsolutePath().toString(), - application.applicationName(), Integer.toString(pathCount++)); + for (String action : application.actionDecisionsSupported()) { + // + // Save the actions that it supports + // + providerActionMap.put(action, application); + } // - // Have the application initialize + // Add all the supported policy types // - application.initialize(path); - } - - // Get string list of supportedPolicyTypes - List supportedPolicyTypes = application.supportedPolicyTypes(); - - // Iterate through the supportedPolicyTypes to set the toscaPolicyTypeIdents - for (String name : supportedPolicyTypes) { - ToscaPolicyTypeIdentifier ident = new ToscaPolicyTypeIdentifier(name, "1.0.0"); - toscaPolicyTypeIdents.add(ident); + toscaPolicyTypeIdents.addAll(application.supportedPolicyTypes()); } } // @@ -129,4 +131,34 @@ public class XacmlPdpApplicationManager { return types; } + private static void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application) + throws XacmlApplicationException { + // + // Making an assumption that all application names are unique, and + // they can result in a valid directory being created. + // + Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName()); + LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path); + // + // Create that the directory if it does not exist. Ideally + // this is only for testing, but could be used for production + // Probably better to have the docker container and/or helm + // scripts setup the local directory. + // + if (! path.toFile().exists()) { + try { + // + // Try to create the directory + // + Files.createDirectory(path); + } catch (IOException e) { + LOGGER.error("Failed to create application directory", e); + } + } + // + // Have the application initialize + // + application.initialize(path); + } + } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pdpx/main/startstop/Main.java index b5915e11..62cdc34a 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/startstop/Main.java @@ -21,12 +21,10 @@ package org.onap.policy.pdpx.main.startstop; import java.io.FileInputStream; -import java.net.UnknownHostException; import java.util.Arrays; import java.util.Properties; -import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException; + import org.onap.policy.pdpx.main.PolicyXacmlPdpException; -import org.onap.policy.pdpx.main.comm.XacmlPdpPapRegistration; import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup; import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler; import org.slf4j.Logger; diff --git a/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java b/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java index 5beeed38..9695c7b9 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java @@ -22,6 +22,7 @@ package org.onap.policy.pdpx.main.startstop; import java.util.Arrays; import java.util.Properties; + import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient; @@ -33,7 +34,6 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpMessageType; import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.pdpx.main.PolicyXacmlPdpException; import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException; import org.onap.policy.pdpx.main.comm.XacmlPdpMessage; import org.onap.policy.pdpx.main.comm.XacmlPdpPapRegistration; diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java index c93ba6f7..b81336a5 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java @@ -23,12 +23,21 @@ package org.onap.policy.pdpx.main.rest; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.Collections; import java.util.HashMap; import java.util.Map; + import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -37,11 +46,14 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; + import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.onap.policy.common.gson.GsonMessageBodyHandler; @@ -50,6 +62,9 @@ import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; import org.onap.policy.models.errors.concepts.ErrorResponse; import org.onap.policy.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.parameters.RestServerBuilder; +import org.onap.policy.pdpx.main.parameters.RestServerParameters; +import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup; import org.onap.policy.pdpx.main.startstop.Main; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,14 +75,40 @@ public class TestDecision { private static Main main; + @ClassRule + public static final TemporaryFolder appsFolder = new TemporaryFolder(); + /** * BeforeClass setup environment. + * @throws IOException Cannot create temp apps folder */ @BeforeClass - public static void beforeClass() { + public static void beforeClass() throws IOException { System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); - main = startXacmlPdpService(); + // + // Copy test directory over of the application directories + // + Path src = Paths.get("../packages/policy-xacmlpdp-tarball/src/main/resources/apps"); + File apps = appsFolder.newFolder("apps"); + Files.walk(src).forEach(source -> { + copy(source, apps.toPath().resolve(src.relativize(source))); + }); + // + // Get the parameters file correct. + // + RestServerParameters rest = new RestServerParameters(new RestServerBuilder() + .setHost("0.0.0.0").setPort(6969).setUserName("healthcheck").setPassword("zb!XztG34")); + XacmlPdpParameterGroup params = new XacmlPdpParameterGroup("XacmlPdpGroup", rest, apps.getAbsolutePath()); + final Gson gson = new GsonBuilder().create(); + File fileParams = appsFolder.newFile("params.json"); + String jsonParams = gson.toJson(params); + LOGGER.info("Creating new params: {}", jsonParams); + Files.write(fileParams.toPath(), jsonParams.getBytes()); + // + // Start the service + // + main = startXacmlPdpService(fileParams); } @AfterClass @@ -113,11 +154,11 @@ public class TestDecision { DecisionResponse response = getDecision(request); LOGGER.info("Response {}", response); - //assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo"); + assertThat(response.getStatus()).isEqualTo("Permit"); } - private static Main startXacmlPdpService() { - final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p", + private static Main startXacmlPdpService(File params) { + final String[] XacmlPdpConfigParameters = {"-c", params.getAbsolutePath(), "-p", "parameters/topic.properties"}; return new Main(XacmlPdpConfigParameters); } @@ -167,5 +208,13 @@ public class TestDecision { .userName("healthcheck").password("zb!XztG34").managed(true).build()); } + private static void copy(Path source, Path dest) { + try { + LOGGER.info("Copying {} to {}", source, dest); + Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + LOGGER.error("Failed to copy {} to {}", source, dest); + } + } } \ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java index 51f737a0..5930dd5e 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java @@ -26,12 +26,11 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.net.UnknownHostException; import java.util.Properties; + import org.junit.After; import org.junit.BeforeClass; - import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException; import org.onap.policy.pdpx.main.PolicyXacmlPdpException; -- cgit 1.2.3-korg