diff options
Diffstat (limited to 'tosca-controlloop/runtime/src/test')
3 files changed, 433 insertions, 2 deletions
diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java new file mode 100644 index 000000000..97599cd64 --- /dev/null +++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProviderTest.java @@ -0,0 +1,211 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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.clamp.controlloop.runtime.commissioning; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.YamlJsonTranslator; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +public class CommissioningProviderTest { + private static final String TOSCA_SERVICE_TEMPLATE_YAML = + "src/test/resources/servicetemplates/pmsh_multiple_cl_tosca.yaml"; + private static final String TEMPLATE_IS_NULL = ".*serviceTemplate is marked non-null but is null"; + private static final Coder CODER = new StandardCoder(); + private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); + private static int dbNum = 0; + private static final Object lockit = new Object(); + private PolicyModelsProviderParameters databaseProviderParameters; + + private static String getParameterGroupAsString() { + dbNum++; + return ResourceUtils.getResourceAsString("src/test/resources/parameters/TestParameters.json") + .replace("jdbc:h2:mem:testdb", "jdbc:h2:mem:commissioningdb" + dbNum); + } + + /** + * Sets up db provider parameters before each test. + * + * @throws CoderException . + */ + @Before + public void setupDbProviderParameters() throws CoderException { + synchronized (lockit) { + databaseProviderParameters = CODER.decode(getParameterGroupAsString(), ClRuntimeParameterGroup.class) + .getDatabaseProviderParameters(); + } + } + + /** + * Test the fetching of control loop definitions (ToscaServiceTemplates). + * + * @throws Exception . + */ + @Test + public void testGetControlLoopDefinitions() throws Exception { + List<ToscaNodeTemplate> listOfTemplates; + + try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), + ToscaServiceTemplate.class); + + + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).isEmpty(); + + provider.createControlLoopDefinitions(serviceTemplate); + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).hasSize(2); + + //Test Filtering + listOfTemplates = provider.getControlLoopDefinitions("org.onap.domain.pmsh.PMSHControlLoopDefinition", + "1.2.3"); + assertThat(listOfTemplates).hasSize(1); + for (ToscaNodeTemplate template : listOfTemplates) { + //Other CL elements contain PMSD instead of PMSH in their name + assertFalse(template.getName().contains("PMSD")); + } + + //Test Wrong Name + listOfTemplates = provider.getControlLoopDefinitions("WrongControlLoopName", "0.0.0"); + assertThat(listOfTemplates).isEmpty(); + } + } + + /** + * Test the creation of control loop definitions (ToscaServiceTemplates). + * + * @throws Exception . + */ + @Test + public void testCreateControlLoopDefinitions() throws Exception { + List<ToscaNodeTemplate> listOfTemplates; + + try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { + //Test Service template is null + assertThatThrownBy(() -> provider.createControlLoopDefinitions(null)).hasMessageMatching(TEMPLATE_IS_NULL); + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).isEmpty(); + + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), + ToscaServiceTemplate.class); + + // Response should return the number of node templates present in the service template + List<ToscaConceptIdentifier> affectedDefinitions = + provider.createControlLoopDefinitions(serviceTemplate).getAffectedControlLoopDefinitions(); + assertThat(affectedDefinitions).hasSize(13); + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).hasSize(2); + } + } + + /** + * Test the deletion of control loop definitions (ToscaServiceTemplate). + * + * @throws Exception . + */ + @Test + public void testDeleteControlLoopDefinitions() throws Exception { + List<ToscaNodeTemplate> listOfTemplates; + + try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), + ToscaServiceTemplate.class); + + provider.createControlLoopDefinitions(serviceTemplate); + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).hasSize(2); + + provider.deleteControlLoopDefinition(serviceTemplate.getName(), serviceTemplate.getVersion()); + listOfTemplates = provider.getControlLoopDefinitions(null, null); + assertThat(listOfTemplates).isEmpty(); + } + } + + /** + * Test the fetching of control loop element definitions. + * + * @throws Exception . + */ + @Test + public void testGetControlLoopElementDefinitions() throws Exception { + try (CommissioningProvider provider = new CommissioningProvider(databaseProviderParameters)) { + ToscaServiceTemplate serviceTemplate = yamlTranslator + .fromYaml(ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), + ToscaServiceTemplate.class); + + provider.createControlLoopDefinitions(serviceTemplate); + List<ToscaNodeTemplate> controlLoopDefinitionList = provider.getControlLoopDefinitions( + "org.onap.domain.pmsh.PMSHControlLoopDefinition", "1.2.3"); + + List<ToscaNodeTemplate> controlLoopElementNodeTemplates = + provider.getControlLoopElementDefinitions(controlLoopDefinitionList.get(0)); + + // 4 PMSH control loop elements definitions. + assertThat(controlLoopElementNodeTemplates).hasSize(4); + + List<ToscaNodeType> derivedTypes = getDerivedNodeTypes(serviceTemplate); + for (ToscaNodeTemplate template : controlLoopElementNodeTemplates) { + assertTrue(checkNodeType(template, derivedTypes)); + } + } + } + + private boolean checkNodeType(ToscaNodeTemplate template, List<ToscaNodeType> derivedNodeTypes) { + String controlLoopElementType = "org.onap.policy.clamp.controlloop.ControlLoopElement"; + for (ToscaNodeType derivedType : derivedNodeTypes) { + if (template.getType().equals(derivedType.getName()) || template.getType().equals(controlLoopElementType)) { + return true; + } + } + return false; + } + + private List<ToscaNodeType> getDerivedNodeTypes(ToscaServiceTemplate serviceTemplate) { + String type = "org.onap.policy.clamp.controlloop.ControlLoopElement"; + List<ToscaNodeType> nodeTypes = new ArrayList<>(); + for (ToscaNodeType nodeType : serviceTemplate.getNodeTypes().values()) { + if (nodeType.getDerivedFrom().equals(type)) { + nodeTypes.add(nodeType); + } + } + return nodeTypes; + } +} diff --git a/tosca-controlloop/runtime/src/test/resources/META-INF/persistence.xml b/tosca-controlloop/runtime/src/test/resources/META-INF/persistence.xml index ff532fe97..610bfe97e 100644 --- a/tosca-controlloop/runtime/src/test/resources/META-INF/persistence.xml +++ b/tosca-controlloop/runtime/src/test/resources/META-INF/persistence.xml @@ -48,7 +48,7 @@ <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoopElement</class> <properties> - <property name="eclipselink.ddl-generation" value="create-or-extend-tables" /> + <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.logging.level" value="INFO" /> </properties> @@ -56,4 +56,3 @@ </persistence-unit> </persistence> - diff --git a/tosca-controlloop/runtime/src/test/resources/servicetemplates/pmsh_multiple_cl_tosca.yaml b/tosca-controlloop/runtime/src/test/resources/servicetemplates/pmsh_multiple_cl_tosca.yaml new file mode 100644 index 000000000..099e2e945 --- /dev/null +++ b/tosca-controlloop/runtime/src/test/resources/servicetemplates/pmsh_multiple_cl_tosca.yaml @@ -0,0 +1,221 @@ +tosca_definitions_version: tosca_simple_yaml_1_3 +data_types: + onap.datatypes.ToscaConceptIdentifier: + derived_from: tosca.datatypes.Root + properties: + name: + type: string + required: true + version: + type: string + required: true +node_types: + org.onap.policy.clamp.controlloop.Participant: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + org.onap.policy.clamp.controlloop.ControlLoopElement: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + participant_id: + type: onap.datatypes.ToscaConceptIdentifier + requred: true + org.onap.policy.clamp.controlloop.ControlLoop: + version: 1.0.1 + derived_from: tosca.nodetypes.Root + properties: + provider: + type: string + requred: false + elements: + type: list + required: true + entry_schema: + type: onap.datatypes.ToscaConceptIdentifier + org.onap.policy.clamp.controlloop.DCAEMicroserviceControlLoopElement: + version: 1.0.1 + derived_from: org.onap.policy.clamp.controlloop.ControlLoopElement + properties: + dcae_blueprint_id: + type: onap.datatypes.ToscaConceptIdentifier + requred: true + org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement: + version: 1.0.1 + derived_from: org.onap.policy.clamp.controlloop.ControlLoopElement + properties: + policy_type_id: + type: onap.datatypes.ToscaConceptIdentifier + requred: true + org.onap.policy.clamp.controlloop.CDSControlLoopElement: + version: 1.0.1 + derived_from: org.onap.policy.clamp.controlloop.ControlLoopElement + properties: + cds_blueprint_id: + type: onap.datatypes.ToscaConceptIdentifier + requred: true +topology_template: + node_templates: + org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant: + version: 2.3.4 + type: org.onap.policy.clamp.controlloop.Participant + type_version: 1.0.1 + description: Participant for DCAE microservices + properties: + provider: ONAP + org.onap.policy.controlloop.PolicyControlLoopParticipant: + version: 2.2.1 + type: org.onap.policy.clamp.controlloop.Participant + type_version: 1.0.1 + description: Participant for DCAE microservices + properties: + provider: ONAP + org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant: + version: 2.2.1 + type: org.onap.policy.clamp.controlloop.Participant + type_version: 1.0.1 + description: Participant for DCAE microservices + properties: + provider: ONAP + org.onap.domain.pmsh.PMSH_DCAEMicroservice: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.DCAEMicroserviceControlLoopElement + type_version: 1.0.0 + description: Control loop element for the DCAE microservice for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant + version: 2.3.4 + dcae_blueprint_id: + name: org.onap.dcae.blueprints.PMSHBlueprint + version: 1.0.0 + org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement + type_version: 1.0.0 + description: Control loop element for the monitoring policy for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.policy.controlloop.PolicyControlLoopParticipant + version: 2.2.1 + policy_type_id: + name: onap.policies.monitoring.pm-subscription-handler + version: 1.0.0 + org.onap.domain.pmsh.PMSH_OperationalPolicyControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement + type_version: 1.0.0 + description: Control loop element for the operational policy for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.policy.controlloop.PolicyControlLoopParticipant + version: 2.2.1 + policy_type_id: + name: onap.policies.operational.pm-subscription-handler + version: 1.0.0 + org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.ControlLoopElement + type_version: 1.0.0 + description: Control loop element for CDS for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_Id: + name: org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant + version: 3.2.1 + cds_blueprint_id: + name: org.onap.ccsdk.cds.PMSHCdsBlueprint + version: 1.0.0 + org.onap.domain.pmsh.PMSHControlLoopDefinition: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.ControlLoop + type_version: 1.0.0 + description: Control loop for Performance Management Subscription Handling + properties: + provider: Ericsson + elements: + - name: org.onap.domain.pmsh.PMSH_DCAEMicroservice + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSH_OperationalPolicyControlLoopElement + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement + version: 1.2.3 + org.onap.domain.pmsh.PMSD_DCAEMicroservice: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.DCAEMicroserviceControlLoopElement + type_version: 1.0.0 + description: Control loop element for the DCAE microservice for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant + version: 2.3.4 + dcae_blueprint_id: + name: org.onap.dcae.blueprints.PMSDBlueprint + version: 1.0.0 + org.onap.domain.pmsh.PMSD_MonitoringPolicyControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement + type_version: 1.0.0 + description: Control loop element for the monitoring policy for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.policy.controlloop.PolicyControlLoopParticipant + version: 2.2.1 + policy_type_id: + name: onap.policies.monitoring.pm-subscription-handler + version: 1.0.0 + org.onap.domain.pmsh.PMSD_OperationalPolicyControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement + type_version: 1.0.0 + description: Control loop element for the operational policy for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_id: + name: org.onap.policy.controlloop.PolicyControlLoopParticipant + version: 2.2.1 + policy_type_id: + name: onap.policies.operational.pm-subscription-handler + version: 1.0.0 + org.onap.domain.pmsh.PMSD_CDS_ControlLoopElement: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.ControlLoopElement + type_version: 1.0.0 + description: Control loop element for CDS for Performance Management Subscription Handling + properties: + provider: Ericsson + participant_Id: + name: org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant + version: 3.2.1 + cds_blueprint_id: + name: org.onap.ccsdk.cds.PMSDCdsBlueprint + version: 1.0.0 + org.onap.domain.pmsh.PMSDControlLoopDefinition: + version: 1.2.3 + type: org.onap.policy.clamp.controlloop.ControlLoop + type_version: 1.0.0 + description: Control loop for Performance Management Subscription Handling + properties: + provider: Ericsson + elements: + - name: org.onap.domain.pmsh.PMSD_DCAEMicroservice + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSD_MonitoringPolicyControlLoopElement + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSD_OperationalPolicyControlLoopElement + version: 1.2.3 + - name: org.onap.domain.pmsh.PMSD_CDS_ControlLoopElement + version: 1.2.3 |