diff options
author | Liam Fallon <liam.fallon@est.tech> | 2021-09-10 20:15:37 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-09-10 20:15:37 +0000 |
commit | 859749a4143c104f6bd63392c8d2cb2839408029 (patch) | |
tree | 56a7794cf8f70cdcc6144c8001930c86b8f6d01b | |
parent | 09d8a86e2126ccc9a12456ddead12a5fe8f58697 (diff) | |
parent | 6b3c8bd96a66160714f768b185290ed765e23ec7 (diff) |
Merge "Verify participant Simulator and messages"
24 files changed, 451 insertions, 373 deletions
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 new file mode 100644 index 000000000..05f5a4bb8 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ParticipantUtils.java @@ -0,0 +1,81 @@ +/*- + * ============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 java.util.Map; +import javax.ws.rs.core.Response; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +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.models.base.PfModelRuntimeException; +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; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ParticipantUtils { + + private static final Coder CODER = new StandardCoder(); + private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; + + /** + * Finds participantType from a map of properties. + * + * @param properties Map of properties + * @return participantType + */ + public static ToscaConceptIdentifier findParticipantType(Map<String, Object> properties) { + var objParticipantType = properties.get("participantType"); + if (objParticipantType != null) { + try { + return CODER.decode(objParticipantType.toString(), ToscaConceptIdentifier.class); + } catch (CoderException e) { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); + } + } + return null; + } + + /** + * Checks If NodeTemplate Is ControlLoopElement. + * + * @param nodeTemplate the ToscaNodeTemplate + * @param toscaServiceTemplate the ToscaServiceTemplate + * @return true if NodeTemplate Is ControlLoopElement + */ + public 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); + } + } + } + return false; + } +} diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ProviderUtils.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ProviderUtils.java index ff5b132ab..3cce9c55c 100644 --- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ProviderUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ProviderUtils.java @@ -24,15 +24,15 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; import javax.ws.rs.core.Response; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfModelRuntimeException; +@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ProviderUtils { - private ProviderUtils() { - // Utility class has no instances - } protected static <A, J extends PfConcept & PfAuthorative<A>> List<J> getJpaAndValidate( List<A> authorativeConceptList, Supplier<J> jpaSupplier, String conceptDescription) { diff --git a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml index 635b98cb8..ade249e99 100644 --- a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml +++ b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml @@ -23,11 +23,11 @@ runtime: participantClUpdateIntervalSec: 1000 participantClStateChangeIntervalSec: 1000 participantParameters: - heartBeatMs: 120000 - maxMessageAgeMs: 600000 + heartBeatMs: 20000 + maxStatusWaitMs: 100000 updateParameters: maxRetryCount: 3 - maxWaitMs: 100000 + maxWaitMs: 20000 databaseProviderParameters: name: PolicyProviderParameterGroup implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl 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<String, ToscaNodeTemplate> nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); for (Map.Entry<String, ToscaNodeTemplate> 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> participantUpdates) { + List<ParticipantUpdates> 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<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> 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<String, ToscaNodeTemplate> 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<ParticipantDefinition> participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier( - entryKey, entryValue.getVersion())); + clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); List<ControlLoopElementDefinition> 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<ControlLoopElementDefinition> controlLoopElementDefinitionList) { + ToscaConceptIdentifier clParticipantType, + List<ControlLoopElementDefinition> 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<String> 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<String, ToscaNodeTemplate> nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); for (Map.Entry<String, ToscaNodeTemplate> 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> participantUpdates) { + List<ParticipantUpdates> 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<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> 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<String, ToscaNodeTemplate> 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<ParticipantDefinition> participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier( - entryKey, entryValue.getVersion())); + clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); List<ControlLoopElementDefinition> 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<ControlLoopElementDefinition> controlLoopElementDefinitionList) { + ToscaConceptIdentifier clParticipantType, + List<ControlLoopElementDefinition> 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); diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/ParticipantIntermediaryApi.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/ParticipantIntermediaryApi.java index 3859e0c39..891d67e2d 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/ParticipantIntermediaryApi.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/ParticipantIntermediaryApi.java @@ -48,16 +48,6 @@ public interface ParticipantIntermediaryApi { void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener); /** - * Send participant register message to controlloop runtime. - */ - void sendParticipantRegister(); - - /** - * Send participant deregister message to controlloop runtime. - */ - void sendParticipantDeregister(); - - /** * Get participants loops from the intermediary API. * * @param name the participant name, null for all diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java index 1f79d7179..aa1febadd 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java @@ -37,6 +37,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant 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.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.stereotype.Component; @@ -48,30 +49,26 @@ import org.springframework.stereotype.Component; public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi { // The handler for the participant intermediary - private ParticipantHandler participantHandler; + private final ParticipantHandler participantHandler; + + // The handler for the controlLoop intermediary + private final ControlLoopHandler controlLoopHandler; /** * Constructor. * * @param participantHandler ParticipantHandler + * @param controlLoopHandler ControlLoopHandler */ - public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler) { + public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler, + ControlLoopHandler controlLoopHandler) { this.participantHandler = participantHandler; + this.controlLoopHandler = controlLoopHandler; } @Override public void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener) { - participantHandler.getControlLoopHandler().registerControlLoopElementListener(controlLoopElementListener); - } - - @Override - public void sendParticipantRegister() { - participantHandler.sendParticipantRegister(); - } - - @Override - public void sendParticipantDeregister() { - participantHandler.sendParticipantDeregister(); + controlLoopHandler.registerControlLoopElementListener(controlLoopElementListener); } @Override @@ -91,13 +88,12 @@ public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryAp @Override public ControlLoops getControlLoops(String name, String version) { - return participantHandler.getControlLoopHandler().getControlLoops(); + return controlLoopHandler.getControlLoops(); } @Override public Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version) { - List<ControlLoop> controlLoops = - participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList(); + List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList(); for (ControlLoop controlLoop : controlLoops) { if (name.equals(controlLoop.getDefinition().getName())) { @@ -109,8 +105,7 @@ public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryAp @Override public ControlLoopElement getControlLoopElement(UUID id) { - List<ControlLoop> controlLoops = - participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList(); + List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList(); for (ControlLoop controlLoop : controlLoops) { ControlLoopElement clElement = controlLoop.getElements().get(id); @@ -125,12 +120,12 @@ public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryAp public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId, UUID id, ControlLoopOrderedState currentState, ControlLoopState newState, ParticipantMessageType messageType) { - return participantHandler.getControlLoopHandler().updateControlLoopElementState(controlLoopId, + return controlLoopHandler.updateControlLoopElementState(controlLoopId, id, currentState, newState); } @Override public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) { - participantHandler.getControlLoopHandler().updateControlLoopElementStatistics(id, elementStatistics); + controlLoopHandler.updateControlLoopElementStatistics(id, elementStatistics); } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java index 6e1b31cfe..8bdf91824 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; import lombok.Getter; -import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; @@ -45,7 +44,6 @@ import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.Parti import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters; -import org.onap.policy.common.utils.coder.CoderException; 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; @@ -97,33 +95,37 @@ public class ControlLoopHandler { * @param newState the ordered state * @return controlLoopElement the updated controlloop element */ - public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId, - UUID id, ControlLoopOrderedState orderedState, - ControlLoopState newState) { + public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId, UUID id, + ControlLoopOrderedState orderedState, ControlLoopState newState) { if (id == null) { LOGGER.warn("Cannot update Control loop element state, id is null"); + return null; } - ControlLoopElement clElement = elementsOnThisParticipant.get(id); for (var controlLoop : controlLoopMap.values()) { var element = controlLoop.getElements().get(id); if (element != null) { element.setState(newState); } + var checkOpt = controlLoop.getElements().values().stream() + .filter(clElement -> !newState.equals(clElement.getState())).findAny(); + if (checkOpt.isEmpty()) { + controlLoop.setState(newState); + controlLoop.setOrderedState(orderedState); + } } + var clElement = elementsOnThisParticipant.get(id); if (clElement != null) { - var controlLoopStateChangeAck = - new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK); + var controlLoopStateChangeAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK); controlLoopStateChangeAck.setParticipantId(participantId); controlLoopStateChangeAck.setParticipantType(participantType); controlLoopStateChangeAck.setControlLoopId(controlLoopId); clElement.setOrderedState(orderedState); clElement.setState(newState); - controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(), - new ControlLoopElementAck(newState, true, - "Control loop element {} state changed to {}\", id, newState)")); + controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(), new ControlLoopElementAck( + newState, true, "Control loop element {} state changed to {}\", id, newState)")); LOGGER.debug("Control loop element {} state changed to {}", id, newState); controlLoopStateChangeAck.setMessage("ControlLoopElement state changed to {} " + newState); controlLoopStateChangeAck.setResult(true); @@ -140,7 +142,7 @@ public class ControlLoopHandler { * @param elementStatistics control loop element Statistics */ public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) { - ControlLoopElement clElement = elementsOnThisParticipant.get(id); + var clElement = elementsOnThisParticipant.get(id); if (clElement != null) { elementStatistics.setParticipantId(participantId); elementStatistics.setId(id); @@ -165,7 +167,7 @@ public class ControlLoopHandler { controlLoopAck.setParticipantId(participantId); controlLoopAck.setParticipantType(participantType); controlLoopAck.setMessage("Control loop " + stateChangeMsg.getControlLoopId() - + " does not use this participant " + participantId); + + " does not use this participant " + participantId); controlLoopAck.setResult(false); controlLoopAck.setResponseTo(stateChangeMsg.getMessageId()); controlLoopAck.setControlLoopId(stateChangeMsg.getControlLoopId()); @@ -206,7 +208,7 @@ public class ControlLoopHandler { * @param updateMsg the update message */ public void handleControlLoopUpdate(ControlLoopUpdate updateMsg, - List<ControlLoopElementDefinition> clElementDefinitions) { + List<ControlLoopElementDefinition> clElementDefinitions) { if (!updateMsg.appliesTo(participantType, participantId)) { return; @@ -222,7 +224,7 @@ public class ControlLoopHandler { controlLoopUpdateAck.setParticipantType(participantType); controlLoopUpdateAck.setMessage("Control loop " + updateMsg.getControlLoopId() - + " already defined on participant " + participantId); + + " already defined on participant " + participantId); controlLoopUpdateAck.setResult(false); controlLoopUpdateAck.setResponseTo(updateMsg.getMessageId()); controlLoopUpdateAck.setControlLoopId(updateMsg.getControlLoopId()); @@ -235,22 +237,22 @@ public class ControlLoopHandler { return; } - List<ControlLoopElement> clElements = storeElementsOnThisParticipant(updateMsg.getParticipantUpdatesList()); + var clElements = storeElementsOnThisParticipant(updateMsg.getParticipantUpdatesList()); try { - for (ControlLoopElement element : clElements) { - ToscaNodeTemplate clElementNodeTemplate = getClElementNodeTemplate( - clElementDefinitions, element.getDefinition()); - for (ControlLoopElementListener clElementListener : listeners) { - clElementListener.controlLoopElementUpdate(updateMsg.getControlLoopId(), - element, clElementNodeTemplate); + for (var element : clElements) { + var clElementNodeTemplate = + getClElementNodeTemplate(clElementDefinitions, element.getDefinition()); + for (var clElementListener : listeners) { + clElementListener.controlLoopElementUpdate(updateMsg.getControlLoopId(), element, + clElementNodeTemplate); } } } catch (PfModelException e) { LOGGER.debug("Control loop element update failed {}", updateMsg.getControlLoopId()); } - Map<UUID, ControlLoopElement> clElementMap = prepareClElementMap(clElements); + var clElementMap = prepareClElementMap(clElements); controlLoop = new ControlLoop(); controlLoop.setDefinition(updateMsg.getControlLoopId()); controlLoop.setElements(clElementMap); @@ -258,8 +260,8 @@ public class ControlLoopHandler { } private ToscaNodeTemplate getClElementNodeTemplate(List<ControlLoopElementDefinition> clElementDefinitions, - ToscaConceptIdentifier clElementDefId) { - for (ControlLoopElementDefinition clElementDefinition : clElementDefinitions) { + ToscaConceptIdentifier clElementDefId) { + for (var clElementDefinition : clElementDefinitions) { if (clElementDefinition.getClElementDefinitionId().equals(clElementDefId)) { return clElementDefinition.getControlLoopElementToscaNodeTemplate(); } @@ -268,11 +270,9 @@ public class ControlLoopHandler { } private List<ControlLoopElement> storeElementsOnThisParticipant(List<ParticipantUpdates> participantUpdates) { - var clElementMap = - participantUpdates.stream() + var clElementMap = participantUpdates.stream() .flatMap(participantUpdate -> participantUpdate.getControlLoopElementList().stream()) - .filter(element -> participantType.equals(element.getParticipantType())) - .collect(Collectors.toList()); + .filter(element -> participantType.equals(element.getParticipantType())).collect(Collectors.toList()); for (var element : clElementMap) { elementsOnThisParticipant.put(element.getId(), element); @@ -282,7 +282,7 @@ public class ControlLoopHandler { private Map<UUID, ControlLoopElement> prepareClElementMap(List<ControlLoopElement> clElements) { Map<UUID, ControlLoopElement> clElementMap = new LinkedHashMap<>(); - for (ControlLoopElement element : clElements) { + for (var element : clElements) { clElementMap.put(element.getId(), element); } return clElementMap; @@ -295,19 +295,9 @@ public class ControlLoopHandler { * @param orderedState orderedState */ private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState, ControlLoopState.UNINITIALISED); - controlLoopMap.remove(controlLoop.getKey().asIdentifier()); - - for (ControlLoopElementListener clElementListener : listeners) { - try { - for (ControlLoopElement element : controlLoop.getElements().values()) { - clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), - element.getId(), element.getState(), orderedState); - } - } catch (PfModelException e) { - LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition()); - } - } + handleStateChange(controlLoop, orderedState); + controlLoopMap.remove(controlLoop.getDefinition()); + controlLoop.getElements().values().forEach(element -> elementsOnThisParticipant.remove(element.getId())); } /** @@ -317,7 +307,7 @@ public class ControlLoopHandler { * @param orderedState orderedState */ private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState, ControlLoopState.PASSIVE); + handleStateChange(controlLoop, orderedState); } /** @@ -327,7 +317,7 @@ public class ControlLoopHandler { * @param orderedState orderedState */ private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState, ControlLoopState.RUNNING); + handleStateChange(controlLoop, orderedState); } /** @@ -335,30 +325,30 @@ public class ControlLoopHandler { * * @param controlLoop participant status in memory * @param orderedState orderedState the new ordered state the participant should have - * @param newState new state of the control loop elements */ - private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState, - ControlLoopState newState) { + private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { if (orderedState.equals(controlLoop.getOrderedState())) { var controlLoopAck = new ControlLoopAck(ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); controlLoopAck.setParticipantId(participantId); controlLoopAck.setParticipantType(participantType); - controlLoopAck.setMessage("Control loop is already in state" + orderedState); + controlLoopAck.setMessage("Control loop is already in state " + orderedState); controlLoopAck.setResult(false); controlLoopAck.setControlLoopId(controlLoop.getDefinition()); publisher.sendControlLoopAck(controlLoopAck); return; } - if (!CollectionUtils.isEmpty(controlLoop.getElements().values())) { - controlLoop.getElements().values().forEach(element -> { - element.setState(newState); - element.setOrderedState(orderedState); - }); + for (var clElementListener : listeners) { + try { + for (var element : controlLoop.getElements().values()) { + clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), element.getId(), + element.getState(), orderedState); + } + } catch (PfModelException e) { + LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition()); + } } - - controlLoop.setOrderedState(orderedState); } /** diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java index 5458f7c34..1d445324b 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/IntermediaryActivator.java @@ -24,7 +24,6 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.handler; import java.io.Closeable; import java.io.IOException; import java.util.List; -import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters; import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; import org.onap.policy.common.endpoints.event.comm.TopicSink; @@ -48,7 +47,7 @@ public class IntermediaryActivator extends ServiceManagerContainer implements Cl private List<TopicSink> topicSinks; private List<TopicSource> topicSources; - ParticipantIntermediaryApi participantIntermediaryApi; + private ParticipantHandler participantHandler; private final MessageTypeDispatcher msgDispatcher; @@ -56,13 +55,14 @@ public class IntermediaryActivator extends ServiceManagerContainer implements Cl * Instantiate the activator for participant. * * @param parameters the ParticipantParameters + * @param participantHandler the ParticipantHandler * @param publishers list of Publishers * @param listeners list of Listeners */ public <T> IntermediaryActivator(final ParticipantParameters parameters, - ParticipantIntermediaryApi participantIntermediaryApi, List<Publisher> publishers, + ParticipantHandler participantHandler, List<Publisher> publishers, List<Listener<T>> listeners) { - this.participantIntermediaryApi = participantIntermediaryApi; + this.participantHandler = participantHandler; topicSinks = TopicEndpointManager.getManager() .addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks()); @@ -118,11 +118,11 @@ public class IntermediaryActivator extends ServiceManagerContainer implements Cl } private void sendParticipantRegister() { - participantIntermediaryApi.sendParticipantRegister(); + participantHandler.sendParticipantRegister(); } private void sendParticipantDeregister() { - participantIntermediaryApi.sendParticipantDeregister(); + participantHandler.sendParticipantDeregister(); } /** diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java index 19641b0d5..69f8febb8 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.UUID; +import java.util.stream.Collectors; import lombok.Getter; import lombok.Setter; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; @@ -66,13 +67,16 @@ import org.springframework.stereotype.Component; /** * This class is responsible for managing the state of a participant. */ -@Getter @Component public class ParticipantHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class); + @Getter private final ToscaConceptIdentifier participantType; + + @Getter private final ToscaConceptIdentifier participantId; + private final ControlLoopHandler controlLoopHandler; private final ParticipantStatistics participantStatistics; private final ParticipantMessagePublisher publisher; @@ -110,11 +114,6 @@ public class ParticipantHandler { * @param participantStatusReqMsg participant participantStatusReq message */ public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) { - var controlLoops = controlLoopHandler.getControlLoops(); - for (ControlLoopElementListener clElementListener : controlLoopHandler.getListeners()) { - updateClElementStatistics(controlLoops, clElementListener); - } - var participantStatus = makeHeartbeat(true); publisher.sendParticipantStatus(participantStatus); } @@ -331,6 +330,12 @@ public class ParticipantHandler { * Method to send heartbeat to controlloop runtime. */ public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) { + if (!responseToParticipantStatusReq) { + var controlLoops = controlLoopHandler.getControlLoops(); + for (var clElementListener : controlLoopHandler.getListeners()) { + updateClElementStatistics(controlLoops, clElementListener); + } + } this.participantStatistics.setState(state); this.participantStatistics.setHealthStatus(healthStatus); this.participantStatistics.setTimeStamp(Instant.now()); @@ -363,10 +368,15 @@ public class ParticipantHandler { clStatitistics.setControlLoopId(entry.getKey()); ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList(); clElementStatisticsList - .setClElementStatistics(entry.getValue().getControlLoopElementStatisticsList(entry.getValue())); + .setClElementStatistics(entry.getValue().getElements().values() + .stream() + .map(ControlLoopElement::getClElementStatistics) + .filter(Objects::nonNull) + .collect(Collectors.toList())); clStatitistics.setClElementStatisticsList(clElementStatisticsList); clInfo.setControlLoopStatistics(clStatitistics); clInfo.setState(entry.getValue().getState()); + controlLoopInfoList.add(clInfo); } return controlLoopInfoList; } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java index 47a99ca29..e3e34878f 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantParameters.java @@ -18,7 +18,6 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; -import java.util.concurrent.TimeUnit; import javax.validation.Valid; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; @@ -34,17 +33,11 @@ import org.springframework.validation.annotation.Validated; @Validated public class ParticipantParameters { - /** - * Default maximum message age, in milliseconds, that should be examined. Any message - * older than this is discarded. - */ - public static final long DEFAULT_MAX_AGE_MS = TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES); - - @Min(1) + @Min(100) private long heartBeatMs; - @Min(1) - private long maxMessageAgeMs = DEFAULT_MAX_AGE_MS; + @Min(100) + private long maxStatusWaitMs; @Valid @NotNull diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java index 8102fe90e..c0b0480de 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ParticipantUpdateParameters.java @@ -34,13 +34,13 @@ public class ParticipantUpdateParameters { /** * Maximum number of times to re-send a request to a PDP. */ - @Min(value = 0) + @Min(value = 1) private int maxRetryCount; /** * Maximum time to wait, in milliseconds, for a PDP response. */ - @Min(value = 0) + @Min(value = 100) private long maxWaitMs; } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java index 65149a733..d13d66c5d 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java @@ -61,8 +61,6 @@ public class SupervisionScanner { private final ParticipantStatusReqPublisher participantStatusReqPublisher; private final ParticipantUpdatePublisher participantUpdatePublisher; - private final long maxWaitMs; - /** * Constructor for instantiating SupervisionScanner. * @@ -89,8 +87,7 @@ public class SupervisionScanner { controlLoopCounter.setMaxRetryCount( clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount()); - controlLoopCounter - .setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs()); + controlLoopCounter.setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs()); participantUpdateCounter.setMaxRetryCount( clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount()); @@ -99,10 +96,7 @@ public class SupervisionScanner { participantStatusCounter.setMaxRetryCount( clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount()); - participantStatusCounter - .setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs()); - - maxWaitMs = clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs(); + participantStatusCounter.setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs()); } /** @@ -131,6 +125,7 @@ public class SupervisionScanner { } catch (PfModelException pfme) { LOGGER.warn("error reading control loops from database", pfme); } + if (counterCheck) { scanParticipantUpdate(); } @@ -145,7 +140,7 @@ public class SupervisionScanner { if (participantUpdateCounter.isFault(id)) { LOGGER.debug("report Participant Update fault"); - } else if (participantUpdateCounter.getDuration(id) > maxWaitMs) { + } else if (participantUpdateCounter.getDuration(id) > participantUpdateCounter.getMaxWaitMs()) { if (participantUpdateCounter.count(id)) { LOGGER.debug("retry message ParticipantUpdate"); @@ -166,7 +161,7 @@ public class SupervisionScanner { LOGGER.debug("report Participant fault"); return; } - if (participantStatusCounter.getDuration(id) > maxWaitMs) { + if (participantStatusCounter.getDuration(id) > participantStatusCounter.getMaxWaitMs()) { if (participantStatusCounter.count(id)) { LOGGER.debug("retry message ParticipantStatusReq"); participantStatusReqPublisher.send(id); @@ -243,17 +238,19 @@ public class SupervisionScanner { return; } - if (controlLoopCounter.count(id)) { - if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) { - LOGGER.debug("retry message ControlLoopUpdate"); - controlLoopUpdatePublisher.send(controlLoop); + if (controlLoopCounter.getDuration(id) > controlLoopCounter.getMaxWaitMs()) { + if (controlLoopCounter.count(id)) { + if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) { + LOGGER.debug("retry message ControlLoopUpdate"); + controlLoopUpdatePublisher.send(controlLoop); + } else { + LOGGER.debug("retry message ControlLoopStateChange"); + controlLoopStateChangePublisher.send(controlLoop); + } } else { - LOGGER.debug("retry message ControlLoopStateChange"); - controlLoopStateChangePublisher.send(controlLoop); + LOGGER.debug("report ControlLoop fault"); + controlLoopCounter.setFault(id); } - } else { - LOGGER.debug("report ControlLoop fault"); - controlLoopCounter.setFault(id); } } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java index d15a424b9..fe46297f1 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java @@ -25,14 +25,11 @@ package org.onap.policy.clamp.controlloop.runtime.supervision.comm; import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.Map; import lombok.AllArgsConstructor; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; -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.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -50,8 +47,7 @@ import org.springframework.stereotype.Component; public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> { private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class); - private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; - private static final Coder CODER = new StandardCoder(); + private final PolicyModelsProvider modelsProvider; /** @@ -67,28 +63,27 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par message.setParticipantType(participantType); message.setTimestamp(Instant.now()); - ToscaServiceTemplate toscaServiceTemplate; + ToscaServiceTemplate toscaServiceTemplate = null; try { - toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0); + var list = modelsProvider.getServiceTemplateList(null, null); + if (!list.isEmpty()) { + toscaServiceTemplate = list.get(0); + } } catch (PfModelException pfme) { LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme); return; } List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> 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); + if (toscaServiceTemplate != null) { + for (var 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); } - prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); } } @@ -138,20 +133,4 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); return participantDefinition; } - - 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; - } } diff --git a/runtime-controlloop/src/main/resources/application.yaml b/runtime-controlloop/src/main/resources/application.yaml index cddb3d0fb..96c340491 100644 --- a/runtime-controlloop/src/main/resources/application.yaml +++ b/runtime-controlloop/src/main/resources/application.yaml @@ -23,11 +23,11 @@ runtime: participantClUpdateIntervalSec: 1000 participantClStateChangeIntervalSec: 1000 participantParameters: - heartBeatMs: 120000 - maxMessageAgeMs: 600000 + heartBeatMs: 20000 + maxStatusWaitMs: 100000 updateParameters: maxRetryCount: 3 - maxWaitMs: 100000 + maxWaitMs: 20000 databaseProviderParameters: name: PolicyProviderParameterGroup implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java index 717858ebe..2ba321d36 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java @@ -130,7 +130,8 @@ class SupervisionScannerTest { when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop)); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanParticipant"); - clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().setMaxWaitMs(0); + clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().setMaxWaitMs(-1); + clRuntimeParameterGroup.getParticipantParameters().setMaxStatusWaitMs(-1); var participant = new Participant(); participant.setName("Participant0"); @@ -158,7 +159,6 @@ class SupervisionScannerTest { assertThat(participants.get(0).getHealthStatus()).isEqualTo(ParticipantHealthStatus.NOT_HEALTHY); supervisionScanner.run(true); - supervisionScanner.run(true); participants = participantProvider.getParticipants(null, null); assertThat(participants.get(0).getHealthStatus()).isEqualTo(ParticipantHealthStatus.OFF_LINE); } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java index 14de34d1c..bcc3a701c 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java @@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; @@ -53,9 +54,6 @@ import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicSink; -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.base.PfModelException; @@ -77,8 +75,6 @@ class SupervisionMessagesTest extends CommonRestController { private static PolicyModelsProvider modelsProvider; private static ParticipantProvider participantProvider; private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); - private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement"; - private static final Coder CODER = new StandardCoder(); /** * setup Db Provider Parameters. @@ -133,7 +129,7 @@ class SupervisionMessagesTest extends CommonRestController { // List<ToscaNodeTemplate> listOfTemplates = commissioningProvider.getControlLoopDefinitions(null, null); commissioningProvider.createControlLoopDefinitions(serviceTemplate); assertThatCode(() -> participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterMsg)) - .doesNotThrowAnyException(); + .doesNotThrowAnyException(); } } @@ -160,9 +156,10 @@ class SupervisionMessagesTest extends CommonRestController { synchronized (lockit) { ParticipantDeregisterListener participantDeregisterListener = - new ParticipantDeregisterListener(supervisionHandler); - assertThatCode(() -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, - participantDeregisterMsg)).doesNotThrowAnyException(); + new ParticipantDeregisterListener(supervisionHandler); + assertThatCode( + () -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg)) + .doesNotThrowAnyException(); } } @@ -191,59 +188,36 @@ class SupervisionMessagesTest extends CommonRestController { ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> 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<String, ToscaNodeTemplate> 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); } } participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); synchronized (lockit) { - ParticipantUpdatePublisher participantUpdatePublisher = - new ParticipantUpdatePublisher(modelsProvider); + ParticipantUpdatePublisher participantUpdatePublisher = new ParticipantUpdatePublisher(modelsProvider); participantUpdatePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); assertThatCode(() -> participantUpdatePublisher.send(participantUpdateMsg)).doesNotThrowAnyException(); } } - 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 void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier( - entryKey, entryValue.getVersion())); + clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); List<ControlLoopElementDefinition> 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) { @@ -253,15 +227,15 @@ class SupervisionMessagesTest extends CommonRestController { } } if (!participantExists) { - participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType, - controlLoopElementDefinitionList)); + participantDefinitionUpdates.add( + getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); } } } private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { + ToscaConceptIdentifier clParticipantType, + List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { ParticipantDefinition participantDefinition = new ParticipantDefinition(); participantDefinition.setParticipantType(clParticipantType); controlLoopElementDefinitionList.add(clDefinition); @@ -280,9 +254,9 @@ class SupervisionMessagesTest extends CommonRestController { synchronized (lockit) { ParticipantUpdateAckListener participantUpdateAckListener = - new ParticipantUpdateAckListener(supervisionHandler); + new ParticipantUpdateAckListener(supervisionHandler); assertThatCode(() -> participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg)) - .doesNotThrowAnyException(); + .doesNotThrowAnyException(); } } diff --git a/runtime-controlloop/src/test/resources/application_test.properties b/runtime-controlloop/src/test/resources/application_test.properties index 0074d9f95..61557879d 100644 --- a/runtime-controlloop/src/test/resources/application_test.properties +++ b/runtime-controlloop/src/test/resources/application_test.properties @@ -9,11 +9,10 @@ server.error.path=/error runtime.supervisionScannerIntervalSec=1000 runtime.participantClUpdateIntervalSec=1000 runtime.participantClStateChangeIntervalSec=1000 -runtime.participantParameters.heartBeatMs=120000 -runtime.participantParameters.updateParameters.maxRetryCount=1 -runtime.participantParameters.updateParameters.maxWaitMs=30000 -runtime.participantParameters.stateChangeParameters.maxRetryCount=1 -runtime.participantParameters.stateChangeParameters.maxWaitMs=30000 +runtime.participantParameters.heartBeatMs=20000 +runtime.participantParameters.maxStatusWaitMs=100000 +runtime.participantParameters.updateParameters.maxRetryCount=3 +runtime.participantParameters.updateParameters.maxWaitMs=20000 runtime.databaseProviderParameters.name=PolicyProviderParameterGroup runtime.databaseProviderParameters.implementation=org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl runtime.databaseProviderParameters.databaseDriver=org.h2.Driver diff --git a/runtime-controlloop/src/test/resources/parameters/TestParameters.json b/runtime-controlloop/src/test/resources/parameters/TestParameters.json index 99fc43d21..6d16f4853 100644 --- a/runtime-controlloop/src/test/resources/parameters/TestParameters.json +++ b/runtime-controlloop/src/test/resources/parameters/TestParameters.json @@ -5,13 +5,10 @@ "participantClStateChangeIntervalSec": 1000, "participantParameters": { "heartBeatMs": 120000, + "maxStatusWaitMs": 100000, "updateParameters": { "maxRetryCount": 1, "maxWaitMs": 30000 - }, - "stateChangeParameters": { - "maxRetryCount": 1, - "maxWaitMs": 30000 } }, "databaseProviderParameters": { |