diff options
Diffstat (limited to 'applications/common')
5 files changed, 116 insertions, 17 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationException.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationException.java new file mode 100644 index 00000000..e87da762 --- /dev/null +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationException.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.pdp.xacml.application.common; + +public class XacmlApplicationException extends Exception { + private static final long serialVersionUID = 7588170228926173716L; + + public XacmlApplicationException() { + super(); + } + + public XacmlApplicationException(String message) { + super(message); + } + + public XacmlApplicationException(Throwable cause) { + super(cause); + } + + public XacmlApplicationException(String message, Throwable cause) { + super(message, cause); + } + + public XacmlApplicationException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java index 2ddcd027..cf9b15cc 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java @@ -27,6 +27,7 @@ import java.util.Map; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** * This interface is how the XACML REST controller can communicate @@ -59,31 +60,30 @@ public interface XacmlApplicationServiceProvider { * * @param pathForData Local Path */ - void initialize(Path pathForData); + void initialize(Path pathForData) throws XacmlApplicationException; /** * Returns a list of supported Tosca Policy Types. * * @return List of Strings (eg. "onap.policy.foo.bar") */ - List<String> supportedPolicyTypes(); + List<ToscaPolicyTypeIdentifier> supportedPolicyTypes(); /** * Asks whether the application can support the incoming * Tosca Policy Type and version. * - * @param policyType String Tosca Policy Type - * @param policyTypeVersion String of the Tosca Policy Type version + * @param toscaPolicyId Identifier for policy type * @return true if supported */ - boolean canSupportPolicyType(String policyType, String policyTypeVersion); + boolean canSupportPolicyType(ToscaPolicyTypeIdentifier toscaPolicyId); /** * Load a Map representation of a Tosca Policy. * * @param toscaPolicies Map of Tosca Policy Objects */ - void loadPolicies(Map<String, Object> toscaPolicies); + void loadPolicies(Map<String, Object> toscaPolicies) throws XacmlApplicationException; /** * Makes a decision given the incoming request and returns a response. diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java index 30363b43..364b6519 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlPolicyUtils.java @@ -329,10 +329,6 @@ public class XacmlPolicyUtils { properties.load(is); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Loaded xacml properties {} {}", System.lineSeparator(), properties); - // - // It would be nice to sort this first - // - properties.list(System.out); for (Entry<Object, Object> entrySet : properties.entrySet()) { LOGGER.debug("{} -> {}", entrySet.getKey(), entrySet.getValue()); } @@ -349,7 +345,6 @@ public class XacmlPolicyUtils { public static void storeXacmlProperties(Properties properties, Path propertyPath) throws IOException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Storing xacml properties {} {} {}", properties, System.lineSeparator(), propertyPath); - properties.list(System.out); } try (OutputStream os = Files.newOutputStream(propertyPath)) { String strComments = "#"; @@ -367,6 +362,7 @@ public class XacmlPolicyUtils { return Paths.get(rootPath.toAbsolutePath().toString(), "xacml.properties"); } + @FunctionalInterface public interface FileCreator { public File createAFile(String filename) throws IOException; diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java index 826acbc3..19d8d829 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java @@ -42,6 +42,8 @@ import java.util.Properties; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; +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.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils; import org.slf4j.Logger; @@ -69,12 +71,28 @@ public class StdXacmlApplicationServiceProvider implements XacmlApplicationServi } @Override - public void initialize(Path pathForData) { + public void initialize(Path pathForData) throws XacmlApplicationException { // // Save our path // this.pathForData = pathForData; - LOGGER.debug("New Path is {}", this.pathForData.toAbsolutePath()); + LOGGER.info("New Path is {}", this.pathForData.toAbsolutePath()); + // + // Ensure properties exist + // + Path propertiesPath = XacmlPolicyUtils.getPropertiesPath(pathForData); + if (! propertiesPath.toFile().exists()) { + LOGGER.info("Copying src/main/resources/xacml.properties to path"); + // + // Properties do not exist, by default we will copy ours over + // from src/main/resources + // + try { + Files.copy(Paths.get("src/main/resources/xacml.properties"), propertiesPath); + } catch (IOException e) { + throw new XacmlApplicationException("Failed to copy xacml.propertis", e); + } + } // // Look for and load the properties object // @@ -82,7 +100,7 @@ public class StdXacmlApplicationServiceProvider implements XacmlApplicationServi pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData)); LOGGER.debug("{}", pdpProperties); } catch (IOException e) { - LOGGER.error("{}", e); + throw new XacmlApplicationException("Failed to load xacml.propertis", e); } // // Create an engine @@ -91,17 +109,17 @@ public class StdXacmlApplicationServiceProvider implements XacmlApplicationServi } @Override - public List<String> supportedPolicyTypes() { + public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() { return Collections.emptyList(); } @Override - public boolean canSupportPolicyType(String policyType, String policyTypeVersion) { + public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) { return false; } @Override - public void loadPolicies(Map<String, Object> toscaPolicies) { + public void loadPolicies(Map<String, Object> toscaPolicies) throws XacmlApplicationException { throw new UnsupportedOperationException("Please override and implement loadPolicies"); } diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationExceptionTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationExceptionTest.java new file mode 100644 index 00000000..be27b313 --- /dev/null +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationExceptionTest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.pdp.xacml.application.common; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.utils.test.ExceptionsTester; + +public class XacmlApplicationExceptionTest { + + @Test + public void test() { + assertEquals(5, new ExceptionsTester().test(XacmlApplicationException.class)); + } +} |