diff options
Diffstat (limited to 'applications/optimization/src/test')
3 files changed, 379 insertions, 0 deletions
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 |