From 7ac38590a5c6d80f2aa4d7697ce3735f4a1a8132 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Fri, 22 Oct 2021 13:52:24 +0100 Subject: Add support startPhase in all state transitions Issue-ID: POLICY-3785 Change-Id: I1b5821cc42cdbb63e3172596431dc561ced09c55 Signed-off-by: FrancescoFioraEst --- .../controlloop/concepts/ParticipantUtils.java | 24 +++++++ .../controlloop/concepts/ParticipantUtilsTest.java | 84 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtilsTest.java (limited to 'models') diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtils.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtils.java index 4c3dd4b06..c05311892 100644 --- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtils.java @@ -56,6 +56,30 @@ public final class ParticipantUtils { return null; } + /** + * Get the First StartPhase + * it depend of the state of the Control Loop + * and also from the all startPhase defined into the ToscaServiceTemplate. + * @param controlLoop the ControlLoop + * @param toscaServiceTemplate the ToscaServiceTemplate + * @return the First StartPhase + */ + public static int getFirstStartPhase(ControlLoop controlLoop, ToscaServiceTemplate toscaServiceTemplate) { + var minStartPhase = 1000; + var maxStartPhase = 0; + for (var element : controlLoop.getElements().values()) { + ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates() + .get(element.getDefinition().getName()); + int startPhase = ParticipantUtils.findStartPhase(toscaNodeTemplate.getProperties()); + minStartPhase = Math.min(minStartPhase, startPhase); + maxStartPhase = Math.max(maxStartPhase, startPhase); + } + + return ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState()) + || ControlLoopState.PASSIVE2RUNNING.equals(controlLoop.getState()) ? minStartPhase + : maxStartPhase; + } + /** * Finds startPhase from a map of properties. * diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtilsTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtilsTest.java new file mode 100644 index 000000000..3fb8b6d7f --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtilsTest.java @@ -0,0 +1,84 @@ +/*- + * ============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.models.controlloop.concepts; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import org.junit.jupiter.api.Test; +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.StandardYamlCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +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.ToscaServiceTemplate; + +class ParticipantUtilsTest { + + private static final Coder CODER = new StandardCoder(); + private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml"; + private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json"; + private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; + private static final String POLICY_CONTROL_LOOP_ELEMENT = + "org.onap.policy.clamp.controlloop.PolicyControlLoopElement"; + private static final String PARTICIPANT_CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.Participant"; + private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder(); + + @Test + void testFindParticipantType() throws CoderException { + var identifier = new ToscaConceptIdentifier("Identifier", "1.0.1"); + var result = ParticipantUtils.findParticipantType(Map.of("participantType", CODER.encode(identifier))); + assertThat(result).isEqualTo(identifier); + } + + @Test + void testFindStartPhase() { + var identifier = 13; + var result = ParticipantUtils.findStartPhase(Map.of("startPhase", identifier)); + assertThat(result).isEqualTo(identifier); + } + + @Test + void testGetFirstStartPhase() throws CoderException { + var serviceTemplate = YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML), + ToscaServiceTemplate.class); + var controlLoops = CODER.decode(ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON), ControlLoops.class); + var result = ParticipantUtils.getFirstStartPhase(controlLoops.getControlLoopList().get(0), serviceTemplate); + assertThat(result).isZero(); + } + + @Test + void testCheckIfNodeTemplateIsControlLoopElement() throws CoderException { + var serviceTemplate = YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML), + ToscaServiceTemplate.class); + var nodeTemplate = new ToscaNodeTemplate(); + nodeTemplate.setType(CONTROL_LOOP_ELEMENT); + assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isTrue(); + + nodeTemplate.setType(POLICY_CONTROL_LOOP_ELEMENT); + assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isTrue(); + + nodeTemplate.setType(PARTICIPANT_CONTROL_LOOP_ELEMENT); + assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isFalse(); + } +} -- cgit 1.2.3-korg