diff options
Diffstat (limited to 'applications/optimization')
6 files changed, 587 insertions, 0 deletions
diff --git a/applications/optimization/pom.xml b/applications/optimization/pom.xml new file mode 100644 index 00000000..0723ec5e --- /dev/null +++ b/applications/optimization/pom.xml @@ -0,0 +1,43 @@ +<!-- + ============LICENSE_START======================================================= + ONAP Policy Engine - XACML PDP + ================================================================================ + 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. + ============LICENSE_END========================================================= + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.xacml-pdp.applications</groupId> + <artifactId>applications</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>optimization</artifactId> + + <name>${project.artifactId}</name> + <description>This modules contains applications that implement policy-types for XACML PDP.</description> + + <dependencies> + <dependency> + <groupId>org.onap.policy.xacml-pdp.applications</groupId> + <artifactId>common</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplication.java b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplication.java new file mode 100644 index 00000000..4a4a6046 --- /dev/null +++ b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplication.java @@ -0,0 +1,164 @@ +/*- + * ============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.xacml.pdp.application.optimization; + +import com.att.research.xacml.api.Request; +import com.att.research.xacml.api.Response; +import com.att.research.xacml.util.XACMLPolicyWriter; +import com.google.common.collect.Lists; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +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.XacmlPolicyUtils; +import org.onap.policy.pdp.xacml.application.common.std.StdMatchableTranslator; +import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OptimizationPdpApplication extends StdXacmlApplicationServiceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplication.class); + private static final String STRING_VERSION100 = "1.0.0"; + + private StdMatchableTranslator translator = new StdMatchableTranslator(); + private Map<String, String> supportedPolicyTypes = new HashMap<>(); + + /** + * Constructor. + */ + public OptimizationPdpApplication() { + this.supportedPolicyTypes.put("onap.policies.optimization.AffinityPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.DistancePolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.HpaPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.OptimizationPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.PciPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.QueryPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.SubscriberPolicy", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.Vim_fit", STRING_VERSION100); + this.supportedPolicyTypes.put("onap.policies.optimization.VnfPolicy", STRING_VERSION100); + } + + @Override + public String applicationName() { + return "Optimization Application"; + } + + @Override + public List<String> actionDecisionsSupported() { + return Arrays.asList("optimize"); + } + + @Override + public synchronized List<String> supportedPolicyTypes() { + return Lists.newArrayList(supportedPolicyTypes.keySet()); + } + + @Override + public boolean canSupportPolicyType(String policyType, String policyTypeVersion) { + // + // For the time being, restrict this if the version isn't known. + // Could be too difficult to support changing of versions dynamically. + // + if (! this.supportedPolicyTypes.containsKey(policyType)) { + return false; + } + // + // Must match version exactly + // + return this.supportedPolicyTypes.get(policyType).equals(policyTypeVersion); + } + + @Override + public synchronized void loadPolicies(Map<String, Object> toscaPolicies) { + try { + // + // Convert the policies first + // + List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies); + if (listPolicies.isEmpty()) { + throw new ToscaPolicyConversionException("Converted 0 policies"); + } + // + // Create a copy of the properties object + // + Properties newProperties = this.getProperties(); + // + // Iterate through the policies + // + for (PolicyType newPolicy : listPolicies) { + // + // Construct the filename + // + Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath()); + // + // Write the policy to disk + // Maybe check for an error + // + XACMLPolicyWriter.writePolicyFile(refPath, newPolicy); + // + // Add root policy to properties object + // + XacmlPolicyUtils.addRootPolicy(newProperties, refPath); + } + // + // Write the properties to disk + // + XacmlPolicyUtils.storeXacmlProperties(newProperties, + XacmlPolicyUtils.getPropertiesPath(this.getDataPath())); + // + // Reload the engine + // + this.createEngine(newProperties); + } catch (IOException | ToscaPolicyConversionException e) { + LOGGER.error("Failed to loadPolicies {}", e); + } + } + + @Override + public synchronized DecisionResponse makeDecision(DecisionRequest request) { + // + // Convert to a XacmlRequest + // + Request xacmlRequest = translator.convertRequest(request); + // + // Now get a decision + // + Response xacmlResponse = this.xacmlDecision(xacmlRequest); + // + // Convert to a DecisionResponse + // + return translator.convertResponse(xacmlResponse); + } + +} diff --git a/applications/optimization/src/main/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider b/applications/optimization/src/main/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider new file mode 100644 index 00000000..ffb7e802 --- /dev/null +++ b/applications/optimization/src/main/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider @@ -0,0 +1 @@ +org.onap.policy.xacml.pdp.application.optimization.OptimizationPdpApplication
\ No newline at end of file diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java new file mode 100644 index 00000000..efbf730c --- /dev/null +++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java @@ -0,0 +1,211 @@ +/*- + * ============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.xacml.pdp.application.optimization; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.ServiceLoader; + +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runners.MethodSorters; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.TextFileUtils; +import org.onap.policy.models.decisions.concepts.DecisionRequest; +import org.onap.policy.models.decisions.concepts.DecisionResponse; +import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider; +import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class OptimizationPdpApplicationTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTest.class); + private static Properties properties = new Properties(); + private static File propertiesFile; + private static XacmlApplicationServiceProvider service; + private static StandardCoder gson = new StandardCoder(); + private static DecisionRequest requestAffinity; + + @ClassRule + public static final TemporaryFolder policyFolder = new TemporaryFolder(); + + /** + * Copies the xacml.properties and policies files into + * temporary folder and loads the service provider saving + * instance of provider off for other tests to use. + */ + @BeforeClass + public static void setUp() throws Exception { + // + // Load Single Decision Request + // + requestAffinity = gson.decode( + TextFileUtils + .getTextFileAsString( + "../../main/src/test/resources/decisions/decision.optimization.affinity.input.json"), + DecisionRequest.class); + // + // Setup our temporary folder + // + XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename); + propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties", + properties, myCreator); + // + // Load service + // + ServiceLoader<XacmlApplicationServiceProvider> applicationLoader = + ServiceLoader.load(XacmlApplicationServiceProvider.class); + // + // Iterate through Xacml application services and find + // the optimization service. Save it for use throughout + // all the Junit tests. + // + StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator()); + Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator(); + while (iterator.hasNext()) { + XacmlApplicationServiceProvider application = iterator.next(); + // + // Is it our service? + // + if (application instanceof OptimizationPdpApplication) { + // + // Should be the first and only one + // + assertThat(service).isNull(); + service = application; + } + strDump.append(application.applicationName()); + strDump.append(" supports "); + strDump.append(application.supportedPolicyTypes()); + strDump.append(System.lineSeparator()); + } + LOGGER.debug("{}", strDump); + // + // Tell it to initialize based on the properties file + // we just built for it. + // + service.initialize(propertiesFile.toPath().getParent()); + } + + @Test + public void test1Basics() { + // + // Make sure there's an application name + // + assertThat(service.applicationName()).isNotEmpty(); + // + // Decisions + // + assertThat(service.actionDecisionsSupported().size()).isEqualTo(1); + assertThat(service.actionDecisionsSupported()).contains("optimize"); + // + // Ensure it has the supported policy types and + // can support the correct policy types. + // + assertThat(service.canSupportPolicyType("onap.policies.optimization.AffinityPolicy", "1.0.0")).isTrue(); + assertThat(service.canSupportPolicyType("onap.foobar", "1.0.0")).isFalse(); + } + + @Test + public void test2NoPolicies() { + // + // Ask for a decision + // + DecisionResponse response = service.makeDecision(requestAffinity); + LOGGER.info("Decision {}", response); + + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(0); + } + + @SuppressWarnings("unchecked") + @Test + public void test3AddOptimizationPolicies() throws CoderException, FileNotFoundException, IOException { + // + // Now load the optimization policies + // + try (InputStream is = new FileInputStream("src/test/resources/vCPE.policies.optimization.input.tosca.yaml")) { + // + // Have yaml parse it + // + Yaml yaml = new Yaml(); + Map<String, Object> toscaObject = yaml.load(is); + List<Object> policies = (List<Object>) toscaObject.get("policies"); + // + // Sanity check to ensure the policy type and version are supported + // + for (Object policyObject : policies) { + // + // Get the contents + // + Map<String, Object> policyContents = (Map<String, Object>) policyObject; + for (Entry<String, Object> entrySet : policyContents.entrySet()) { + LOGGER.info("Entry set {}", entrySet.getKey()); + Map<String, Object> policyDefinition = (Map<String, Object>) entrySet.getValue(); + // + // Find the type and make sure the engine supports it + // + assertThat(policyDefinition.containsKey("type")).isTrue(); + assertThat(service.canSupportPolicyType( + policyDefinition.get("type").toString(), + policyDefinition.get("version").toString())) + .isTrue(); + } + } + // + // Load the policies + // + service.loadPolicies(toscaObject); + // + // Ask for a decision + // + DecisionResponse response = service.makeDecision(requestAffinity); + LOGGER.info("Decision {}", response); + + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(1); + // + // Dump it out as Json + // + LOGGER.info(gson.encode(response)); + } + } + +} diff --git a/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml b/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml new file mode 100644 index 00000000..2fc7c140 --- /dev/null +++ b/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml @@ -0,0 +1,137 @@ +tosca_definitions_version: tosca_simple_yaml_1_0_0 +topology_template: +policies: + - + OSDF_CASABLANCA.Affinity_vCPE_1: + type: onap.policies.optimization.AffinityPolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.Affinity_vCPE_1 + policy-version: 1 + properties: + identity: affinity_vCPE + policyScope: [vCPE, US, INTERNATIONAL, ip, vGMuxInfra, vG] + affinityProperties: + qualifier: same + category: complex + policyType: zone + resources: [vGMuxInfra, vG] + - + OSDF_CASABLANCA.Capacity_vG_1: + type: onap.policies.optimization.Vim_fit + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.Capacity_vG_1 + policy-version: 1 + properties: + identity: capacity_vG + policyScope: [VCPE, US, INTERNATIONAL, ip, vG] + resources: [vG] + capacityProperty: + controller: multicloud + request: "{\"vCPU\": 10, \"Memory\": {\"quantity\": {\"get_param\": \"REQUIRED_MEM\"}, \"unit\": \"GB\"}, \"Storage\": {\"quantity\": {\"get_param\": \"REQUIRED_DISK\"}, \"unit\": \"GB\"}}" + policyType: vim_fit + applicableResources: any + - + OSDF_CASABLANCA.Distance_vG_1: + type: onap.policies.optimization.DistancePolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.Distance_vG_1 + policy-version: 1 + properties: + distanceProperties: + locationInfo: customer_loc + distance: + value: 1500 + operator: "<" + unit: km + identity: "distance-vG" + resources: [vG] + policyScope: [vCPE, US, INTERNATIONAL, ip, vG] + policyType: distance_to_location + applicableResources: any + - + OSDF_CASABLANCA.hpa_policy_vG_1: + type: onap.policies.optimization.HpaPolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.hpa_policy_vG_1 + policy-version: 1 + properties: + resources: [vG] + identity: "hpa-vG" + policyScope: [vCPE, US, INTERNATIONAL, ip, vG] + policyType: hpa + # NONE OF THE FLAVORFEATURES CAME OUT RIGHT + - + OSDF_CASABLANCA.queryPolicy_vCPE: + type: onap.policies.optimization.QueryPolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.queryPolicy_vCPE + policy-version: 1 + properties: + queryProperties: + - + attribute: locationId + attribute_location: customerLocation + value: "" + - + attribute: id + attribute_location: "vpnInfo.vpnId" + value: "" + - + attribute: upstreamBW + attribute_location: "vpnInfo.upstreamBW" + value: "" + - + attribute: customerLatitude + attribute_location: customerLatitude + value: 1.1 + - + attribute: customerLongitude + attribute_location: customerLongitude + value: 2.2 + serviceName: vCPE + policyScope: [vCPE, US, INTERNATIONAL, ip, vGMuxInfra, vG] + policyType: request_param_query + identity: vCPE_Query_Policy + + - + OSDF_CASABLANCA.SubscriberPolicy_v1: + type: onap.policies.optimization.SubscriberPolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.SubscriberPolicy_v1 + policy-version: 1 + properties: + identity: subscriber_vCPE + policyScope: [vCPE, subscriber_x, subscriber_y, subscriberPolicy] + properties: + subscriberName: [subscriber_x, subscriber_y] + subscriberRole: ["PVT Homing"] + provStatus: [CAPPED] + policyType: subscriberPolicy + serviceName: vCPE + - + OSDF_CASABLANCA.vnfPolicy_vG: + type: onap.policies.optimization.VnfPolicy + version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.vnfPolicy_vG + policy-version: 1 + properties: + identity: vnf_vG + policyScope: [vCPE, US, INTERNATIONAL, ip, vG] + policyType: vnfPolicy + resources: [vG] + applicableResources: any + vnfProperties: + - + inventoryProvider: aai + serviceType: "" + inventoryType: cloud + customerId: "" + orchestrationStatus: "" + equipmentRole: ""
\ No newline at end of file diff --git a/applications/optimization/src/test/resources/xacml.properties b/applications/optimization/src/test/resources/xacml.properties new file mode 100644 index 00000000..5ea247cf --- /dev/null +++ b/applications/optimization/src/test/resources/xacml.properties @@ -0,0 +1,31 @@ +# +# Properties that the embedded PDP engine uses to configure and load +# +# Standard API Factories +# +xacml.dataTypeFactory=com.att.research.xacml.std.StdDataTypeFactory +xacml.pdpEngineFactory=com.att.research.xacmlatt.pdp.ATTPDPEngineFactory +xacml.pepEngineFactory=com.att.research.xacml.std.pep.StdEngineFactory +xacml.pipFinderFactory=com.att.research.xacml.std.pip.StdPIPFinderFactory +xacml.traceEngineFactory=com.att.research.xacml.std.trace.LoggingTraceEngineFactory +# +# AT&T PDP Implementation Factories +# +xacml.att.evaluationContextFactory=com.att.research.xacmlatt.pdp.std.StdEvaluationContextFactory +xacml.att.combiningAlgorithmFactory=com.att.research.xacmlatt.pdp.std.StdCombiningAlgorithmFactory +xacml.att.functionDefinitionFactory=com.att.research.xacmlatt.pdp.std.StdFunctionDefinitionFactory +# +# ONAP PDP Implementation Factories +# +xacml.att.policyFinderFactory=org.onap.policy.pdp.xacml.application.common.OnapPolicyFinderFactory + +# +# Use a root combining algorithm +# +xacml.att.policyFinderFactory.combineRootPolicies=urn:com:att:xacml:3.0:policy-combining-algorithm:combined-permit-overrides + +# +# Policies to load +# +xacml.rootPolicies= +xacml.referencedPolicies=
\ No newline at end of file |