summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java16
-rw-r--r--main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java200
2 files changed, 213 insertions, 3 deletions
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 0e4bef25..a9d58b9e 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
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -179,7 +179,6 @@ public class XacmlPdpApplicationManager {
* @throws XacmlApplicationException if loadPolicy fails
*/
public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
-
for (XacmlApplicationServiceProvider application : applicationLoader) {
//
// There should be only one application per policytype. We can
@@ -187,6 +186,9 @@ public class XacmlPdpApplicationManager {
// just use the first one found.
//
if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
+ //
+ // Try to load the policy
+ //
application.loadPolicy(policy);
mapLoadedPolicies.put(policy, application);
if (LOGGER.isInfoEnabled()) {
@@ -196,6 +198,13 @@ public class XacmlPdpApplicationManager {
return;
}
}
+ //
+ // Ideally we shouldn't ever get here if we
+ // are ensuring we are reporting a set of Policy Types and the
+ // pap honors that. The loadPolicy for each application should be
+ // the own throwing exceptions if there are any errors in the policy type.
+ //
+ throw new XacmlApplicationException("Application not found for policy type" + policy.getTypeIdentifier());
}
/**
@@ -245,7 +254,8 @@ public class XacmlPdpApplicationManager {
//
Files.createDirectory(path);
} catch (IOException e) {
- LOGGER.error("Failed to create application directory {}", path.toAbsolutePath().toString(), e);
+ throw new XacmlApplicationException("Failed to create application directory " + path.toAbsolutePath(),
+ e);
}
}
//
diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java
new file mode 100644
index 00000000..243c9eab
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java
@@ -0,0 +1,200 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdpx.main.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+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.util.Map;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.decisions.concepts.DecisionRequest;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
+import org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication;
+import org.onap.policy.xacml.pdp.application.nativ.NativePdpApplication;
+import org.onap.policy.xacml.pdp.application.optimization.OptimizationPdpApplication;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XacmlPdpApplicationManagerTest {
+ private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManagerTest.class);
+ private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
+ private static final RestServerParameters params = new RestServerParameters();
+ private static Path appsDirectory;
+ private static ToscaServiceTemplate completedJtst;
+
+ @ClassRule
+ public static final TemporaryFolder appsFolder = new TemporaryFolder();
+
+ /**
+ * setupTestEnvironment.
+ *
+ * @throws Exception Exception if anything is missing
+ */
+ @BeforeClass
+ public static void setupTestEnvironment() throws Exception {
+ //
+ // No need to do more than this
+ //
+ params.setName("policyApiParameters");
+ //
+ // Load an example policy
+ //
+ String policyYaml = ResourceUtils
+ .getResourceAsString("../applications/monitoring/src/test/resources/vDNS.policy.input.yaml");
+ //
+ // Serialize it into a class
+ //
+ ToscaServiceTemplate serviceTemplate;
+ try {
+ serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
+ } catch (CoderException e) {
+ throw new XacmlApplicationException("Failed to decode policy from resource file", e);
+ }
+ //
+ // Make sure all the fields are setup properly
+ //
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ jtst.fromAuthorative(serviceTemplate);
+ completedJtst = jtst.toAuthorative();
+ //
+ // We need at least 1 policies
+ //
+ assertThat(completedJtst).isNotNull();
+ assertThat(completedJtst.getToscaTopologyTemplate().getPolicies().size()).isGreaterThan(0);
+ //
+ // Copy test directory over of the application directories
+ //
+ Path src = Paths.get("src/test/resources/apps");
+ File apps = appsFolder.newFolder("apps");
+ Files.walk(src).forEach(source -> {
+ copy(source, apps.toPath().resolve(src.relativize(source)));
+ });
+ appsDirectory = apps.toPath();
+ }
+
+ @Test
+ public void testXacmlPdpApplicationManagerBadPath() throws Exception {
+ //
+ // Make up a non existent directory to initialize from
+ //
+ Path nonExistentPath = Paths.get(appsFolder.getRoot().getAbsolutePath(), "nonexistent");
+ //
+ // Create our app manager
+ //
+ XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(nonExistentPath, params);
+ //
+ // Still creates the manager, but the apps were not able to initialize
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.findNativeApplication()).isNull();
+ //
+ // Now create the directory
+ //
+ Files.createDirectory(nonExistentPath);
+ manager = new XacmlPdpApplicationManager(nonExistentPath, params);
+ //
+ // Now it should have initialized the apps
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.findNativeApplication()).isNull();
+ }
+
+ @Test
+ public void testXacmlPdpApplicationManagerSimple() {
+ XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(appsDirectory, params);
+ //
+ // Test the basics from the startup
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ assertThat(manager.getPolicyTypeCount()).isEqualTo(19);
+ assertThat(manager.getToscaPolicies()).isEmpty();
+ assertThat(manager.getToscaPolicyIdentifiers()).isEmpty();
+ assertThat(manager.getToscaPolicyTypeIdents()).hasSize(19);
+
+ assertThat(manager.findNativeApplication()).isInstanceOf(NativePdpApplication.class);
+
+ DecisionRequest request = new DecisionRequest();
+ request.setAction("optimize");
+ assertThat(manager.findApplication(request)).isInstanceOf(OptimizationPdpApplication.class);
+ request.setAction("guard");
+ assertThat(manager.findApplication(request)).isInstanceOf(GuardPdpApplication.class);
+ //
+ // Try to unload a policy that isn't loaded
+ //
+ ToscaPolicy policy = null;
+ for (Map<String, ToscaPolicy> map : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+ policy = map.get("onap.scaleout.tca");
+ }
+ assertThat(policy).isNotNull();
+ //
+ // Without this being set, it throws NonNull Exception
+ //
+ policy.setTypeVersion("1.0.0");
+ //
+ // Try loading and unloading
+ //
+ final ToscaPolicy policyFinal = policy;
+ assertThatCode(() -> {
+ manager.removeUndeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ manager.loadDeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(1);
+ manager.removeUndeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ }).doesNotThrowAnyException();
+ //
+ // try loading something unsupported
+ //
+ assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> {
+ ToscaPolicy unsupportedPolicy = new ToscaPolicy();
+ unsupportedPolicy.setType("I.am.not.supported");
+ unsupportedPolicy.setTypeVersion("5.5.5");
+ manager.loadDeployedPolicy(unsupportedPolicy);
+ });
+ }
+
+ private static void copy(Path source, Path dest) {
+ try {
+ Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ LOGGER.error("Failed to copy {} to {}", source, dest);
+ }
+ }
+
+}