From 6b3c8bd96a66160714f768b185290ed765e23ec7 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Fri, 3 Sep 2021 17:22:30 +0100 Subject: Verify participant Simulator and messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing ControlLoopElementHandler in participant Simulator and fix some message issues. Issue-ID: POLICY-3628 Change-Id: I9f058c19aa18e070bac6ccaf98fdf7600d66d71e Signed-off-by: FrancescoFioraEst --- .../src/test/java/utils/ToscaUtils.java | 5 +- .../participant/kubernetes/utils/TestUtils.java | 5 +- .../policy/main/utils/TestListenerUtils.java | 108 ++++++++------------ .../simulator/config/ParticipantConfig.java | 16 +++ .../main/handler/ControlLoopElementHandler.java | 111 +++++++++++++++++++++ .../src/main/resources/config/application.yaml | 2 +- .../simulator/main/rest/TestListenerUtils.java | 100 +++++++------------ 7 files changed, 210 insertions(+), 137 deletions(-) create mode 100644 participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/handler/ControlLoopElementHandler.java (limited to 'participant/participant-impl') diff --git a/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java b/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java index cf71248c3..0bad5ed96 100644 --- a/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java +++ b/participant/participant-impl/participant-impl-http/src/test/java/utils/ToscaUtils.java @@ -20,6 +20,8 @@ package utils; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -27,7 +29,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** * Util class for Test scope. */ -public class ToscaUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ToscaUtils { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/HttpParticipantConfig.yaml"; diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java index b01dfa28e..af514f8aa 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/utils/TestUtils.java @@ -21,11 +21,14 @@ package org.onap.policy.clamp.controlloop.participant.kubernetes.utils; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -public class TestUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TestUtils { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/servicetemplates/KubernetesHelm.yaml"; diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java index fe7cb3c96..25da5a3e9 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java @@ -31,6 +31,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; @@ -38,6 +40,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; @@ -54,18 +57,16 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class TestListenerUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TestListenerUtils { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final Coder CODER = new StandardCoder(); static CommonTestData commonTestData = new CommonTestData(); private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); - private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; private static final String POLICY_TYPE_ID = "policy_type_id"; private static final String POLICY_ID = "policy_id"; - private TestListenerUtils() {} - /** * Method to create a controlLoop from a yaml file. * @@ -138,10 +139,8 @@ public class TestListenerUtils { */ public static ControlLoopUpdate createControlLoopUpdateMsg() { final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); - ToscaConceptIdentifier controlLoopId = - new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); - ToscaConceptIdentifier participantId = - new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); + ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); clUpdateMsg.setControlLoopId(controlLoopId); clUpdateMsg.setParticipantId(participantId); @@ -154,23 +153,18 @@ public class TestListenerUtils { Map nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); for (Map.Entry toscaInputEntry : nodeTemplatesMap.entrySet()) { - if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { ControlLoopElement clElement = new ControlLoopElement(); clElement.setId(UUID.randomUUID()); - ToscaConceptIdentifier clParticipantType; - try { - clParticipantType = CODER.decode( - toscaInputEntry.getValue().getProperties().get("participantType").toString(), - ToscaConceptIdentifier.class); - } catch (CoderException e) { - throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e); - } + var clParticipantType = + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); clElement.setParticipantId(clParticipantType); clElement.setParticipantType(clParticipantType); - clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(), - toscaInputEntry.getValue().getVersion())); + clElement.setDefinition( + new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion())); clElement.setState(ControlLoopState.UNINITIALISED); clElement.setDescription(toscaInputEntry.getValue().getDescription()); clElement.setOrderedState(ControlLoopOrderedState.PASSIVE); @@ -187,26 +181,10 @@ public class TestListenerUtils { return clUpdateMsg; } - private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate, - ToscaServiceTemplate toscaServiceTemplate) { - if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) { - return true; - } else { - var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); - if (nodeType != null) { - var derivedFrom = nodeType.getDerivedFrom(); - if (derivedFrom != null) { - return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false; - } - } - } - return false; - } - private static void populateToscaNodeTemplateFragment(ControlLoopElement clElement, ToscaServiceTemplate toscaServiceTemplate) { - ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate - .getToscaTopologyTemplate().getNodeTemplates().get(clElement.getDefinition().getName()); + ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates() + .get(clElement.getDefinition().getName()); // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null) @@ -229,7 +207,7 @@ public class TestListenerUtils { } private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement, - List participantUpdates) { + List participantUpdates) { if (participantUpdates.isEmpty()) { participantUpdates.add(getControlLoopElementList(clElement)); } else { @@ -263,8 +241,8 @@ public class TestListenerUtils { public static ParticipantUpdate createParticipantUpdateMsg() { final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate(); ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0"); - ToscaConceptIdentifier participantType = new ToscaConceptIdentifier( - "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); + ToscaConceptIdentifier participantType = + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); participantUpdateMsg.setParticipantId(participantId); participantUpdateMsg.setTimestamp(Instant.now()); @@ -277,19 +255,14 @@ public class TestListenerUtils { TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); List participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry toscaInputEntry : - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) { - if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) { - ToscaConceptIdentifier clParticipantType; - try { - clParticipantType = CODER.decode( - toscaInputEntry.getValue().getProperties().get("participantType").toString(), - ToscaConceptIdentifier.class); - } catch (CoderException e) { - throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e); - } + for (Map.Entry toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + var clParticipantType = + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); + toscaInputEntry.getValue(), participantDefinitionUpdates); } } @@ -298,17 +271,16 @@ public class TestListenerUtils { } private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List participantDefinitionUpdates) { var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier( - entryKey, entryValue.getVersion())); + clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); List controlLoopElementDefinitionList = new ArrayList<>(); if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType, - controlLoopElementDefinitionList)); + participantDefinitionUpdates + .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); } else { boolean participantExists = false; for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { @@ -318,15 +290,15 @@ public class TestListenerUtils { } } if (!participantExists) { - participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType, - controlLoopElementDefinitionList)); + participantDefinitionUpdates.add( + getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); } } } private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List controlLoopElementDefinitionList) { + ToscaConceptIdentifier clParticipantType, + List controlLoopElementDefinitionList) { ParticipantDefinition participantDefinition = new ParticipantDefinition(); participantDefinition.setParticipantType(clParticipantType); controlLoopElementDefinitionList.add(clDefinition); @@ -369,8 +341,7 @@ public class TestListenerUtils { return toscaServiceTemplate; } - private static void addPolicyTypesToToscaServiceTemplate( - ToscaServiceTemplate toscaServiceTemplate) { + private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) { Set policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes"); for (String policyTypeFilePath : policyTypeDirectoryContents) { @@ -413,10 +384,9 @@ public class TestListenerUtils { for (String policiesFilePath : policiesDirectoryContents) { String policiesString = ResourceUtils.getResourceAsString(policiesFilePath); - ToscaServiceTemplate foundPoliciesSt = - yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class); - toscaServiceTemplate.getToscaTopologyTemplate().setPolicies( - foundPoliciesSt.getToscaTopologyTemplate().getPolicies()); + ToscaServiceTemplate foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class); + toscaServiceTemplate.getToscaTopologyTemplate() + .setPolicies(foundPoliciesSt.getToscaTopologyTemplate().getPolicies()); } } @@ -427,8 +397,8 @@ public class TestListenerUtils { throw new FileNotFoundException(controlLoopFilePath); } - ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( - controlLoopString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = + yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class); return serviceTemplate; } catch (FileNotFoundException e) { LOGGER.error("cannot find YAML file", controlLoopFilePath); diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java index c890ec247..d28ddf9dc 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java @@ -20,7 +20,10 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.controlloop.participant.simulator.main.handler.ControlLoopElementHandler; import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RequestResponseLoggingFilter; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -42,4 +45,17 @@ public class ParticipantConfig { return registrationBean; } + + /** + * Register ControlLoopElementListener. + * + * @param intermediaryApi the ParticipantIntermediaryApi + * @param clElementHandler the ControlLoop Element Handler + */ + @Autowired + public void registerControlLoopElementListener(ParticipantIntermediaryApi intermediaryApi, + ControlLoopElementHandler clElementHandler) { + intermediaryApi.registerControlLoopElementListener(clElementHandler); + clElementHandler.setIntermediaryApi(intermediaryApi); + } } diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/handler/ControlLoopElementHandler.java new file mode 100644 index 000000000..fd46faf97 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/handler/ControlLoopElementHandler.java @@ -0,0 +1,111 @@ +/*- + * ============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.participant.simulator.main.handler; + +import java.time.Instant; +import java.util.UUID; +import lombok.Setter; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * This class handles implementation of controlLoopElement updates. + */ +@Component +public class ControlLoopElementHandler implements ControlLoopElementListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class); + + @Setter + private ParticipantIntermediaryApi intermediaryApi; + + /** + * Callback method to handle a control loop element state change. + * + * @param controlLoopElementId the ID of the control loop element + * @param currentState the current state of the control loop element + * @param newState the state to which the control loop element is changing to + * @throws PfModelException in case of an exception + */ + @Override + public void controlLoopElementStateChange(ToscaConceptIdentifier controlLoopId, + UUID controlLoopElementId, ControlLoopState currentState, + ControlLoopOrderedState newState) throws PfModelException { + switch (newState) { + case UNINITIALISED: + intermediaryApi.updateControlLoopElementState(controlLoopId, + controlLoopElementId, newState, ControlLoopState.UNINITIALISED, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + break; + case PASSIVE: + intermediaryApi.updateControlLoopElementState(controlLoopId, + controlLoopElementId, newState, ControlLoopState.PASSIVE, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + break; + case RUNNING: + intermediaryApi.updateControlLoopElementState(controlLoopId, + controlLoopElementId, newState, ControlLoopState.RUNNING, + ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); + break; + default: + LOGGER.debug("Unknown orderedstate {}", newState); + break; + } + } + + /** + * Callback method to handle an update on a control loop element. + * + * @param element the information on the control loop element + * @param clElementDefinition toscaNodeTemplate + * @throws PfModelException in case of an exception + */ + @Override + public void controlLoopElementUpdate(ToscaConceptIdentifier controlLoopId, ControlLoopElement element, + ToscaNodeTemplate clElementDefinition) + throws PfModelException { + intermediaryApi.updateControlLoopElementState(controlLoopId, element.getId(), element.getOrderedState(), + ControlLoopState.PASSIVE, ParticipantMessageType.CONTROL_LOOP_UPDATE); + } + + @Override + public void handleStatistics(UUID controlLoopElementId) throws PfModelException { + var clElement = intermediaryApi.getControlLoopElement(controlLoopElementId); + if (clElement != null) { + var clElementStatistics = new ClElementStatistics(); + clElementStatistics.setControlLoopState(clElement.getState()); + clElementStatistics.setTimeStamp(Instant.now()); + intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); + } + } + +} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml index c26749b4d..320b53805 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-simulator/src/main/resources/config/application.yaml @@ -13,7 +13,7 @@ server: participant: intermediaryParameters: - reportingTimeIntervalMs: 120000 + reportingTimeIntervalMs: 60000 description: Participant Description participantId: name: org.onap.PM_CDS_Blueprint diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java index 7439ccd5b..bd3316abb 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java @@ -31,6 +31,8 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; @@ -38,6 +40,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; @@ -53,15 +56,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class TestListenerUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TestListenerUtils { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final Coder CODER = new StandardCoder(); static CommonTestData commonTestData = new CommonTestData(); private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); - private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; - - private TestListenerUtils() {} /** * Method to create a controlLoop from a yaml file. @@ -131,10 +132,8 @@ public class TestListenerUtils { */ public static ControlLoopUpdate createControlLoopUpdateMsg() { final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); - ToscaConceptIdentifier controlLoopId = - new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); - ToscaConceptIdentifier participantId = - new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); + ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); clUpdateMsg.setControlLoopId(controlLoopId); clUpdateMsg.setParticipantId(participantId); @@ -146,23 +145,18 @@ public class TestListenerUtils { Map nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); for (Map.Entry toscaInputEntry : nodeTemplatesMap.entrySet()) { - if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { ControlLoopElement clElement = new ControlLoopElement(); clElement.setId(UUID.randomUUID()); - ToscaConceptIdentifier clParticipantType; - try { - clParticipantType = CODER.decode( - toscaInputEntry.getValue().getProperties().get("participantType").toString(), - ToscaConceptIdentifier.class); - } catch (CoderException e) { - throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e); - } + var clParticipantType = + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); clElement.setParticipantId(clParticipantType); clElement.setParticipantType(clParticipantType); - clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(), - toscaInputEntry.getValue().getVersion())); + clElement.setDefinition( + new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion())); clElement.setState(ControlLoopState.UNINITIALISED); clElement.setDescription(toscaInputEntry.getValue().getDescription()); clElement.setOrderedState(ControlLoopOrderedState.PASSIVE); @@ -178,24 +172,8 @@ public class TestListenerUtils { return clUpdateMsg; } - private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate, - ToscaServiceTemplate toscaServiceTemplate) { - if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) { - return true; - } else { - var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); - if (nodeType != null) { - var derivedFrom = nodeType.getDerivedFrom(); - if (derivedFrom != null) { - return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false; - } - } - } - return false; - } - private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement, - List participantUpdates) { + List participantUpdates) { if (participantUpdates.isEmpty()) { participantUpdates.add(getControlLoopElementList(clElement)); } else { @@ -229,8 +207,8 @@ public class TestListenerUtils { public static ParticipantUpdate createParticipantUpdateMsg() { final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate(); ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0"); - ToscaConceptIdentifier participantType = new ToscaConceptIdentifier( - "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); + ToscaConceptIdentifier participantType = + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); participantUpdateMsg.setParticipantId(participantId); participantUpdateMsg.setTimestamp(Instant.now()); @@ -242,19 +220,14 @@ public class TestListenerUtils { // Add policies to the toscaServiceTemplate List participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry toscaInputEntry : - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) { - if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) { - ToscaConceptIdentifier clParticipantType; - try { - clParticipantType = CODER.decode( - toscaInputEntry.getValue().getProperties().get("participantType").toString(), - ToscaConceptIdentifier.class); - } catch (CoderException e) { - throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e); - } + for (Map.Entry toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + var clParticipantType = + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); + toscaInputEntry.getValue(), participantDefinitionUpdates); } } @@ -263,17 +236,16 @@ public class TestListenerUtils { } private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List participantDefinitionUpdates) { var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier( - entryKey, entryValue.getVersion())); + clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); List controlLoopElementDefinitionList = new ArrayList<>(); if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType, - controlLoopElementDefinitionList)); + participantDefinitionUpdates + .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); } else { boolean participantExists = false; for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { @@ -283,15 +255,15 @@ public class TestListenerUtils { } } if (!participantExists) { - participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType, - controlLoopElementDefinitionList)); + participantDefinitionUpdates.add( + getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); } } } private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List controlLoopElementDefinitionList) { + ToscaConceptIdentifier clParticipantType, + List controlLoopElementDefinitionList) { ParticipantDefinition participantDefinition = new ParticipantDefinition(); participantDefinition.setParticipantType(clParticipantType); controlLoopElementDefinitionList.add(clDefinition); @@ -307,10 +279,8 @@ public class TestListenerUtils { * @return ControlLoopUpdate message * @throws CoderException exception while reading the file to object */ - public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) - throws CoderException { - ControlLoopUpdate controlLoopUpdateMsg = - CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); + public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException { + ControlLoopUpdate controlLoopUpdateMsg = CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); return controlLoopUpdateMsg; } @@ -340,8 +310,8 @@ public class TestListenerUtils { throw new FileNotFoundException(controlLoopFilePath); } - ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( - controlLoopString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = + yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class); return serviceTemplate; } catch (FileNotFoundException e) { LOGGER.error("cannot find YAML file", controlLoopFilePath); -- cgit 1.2.3-korg