diff options
Diffstat (limited to 'runtime-controlloop')
13 files changed, 286 insertions, 257 deletions
diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java index 0b7bc9a26..d2d57ed83 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java @@ -37,7 +37,6 @@ import org.apache.commons.collections4.MapUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; import org.onap.policy.models.base.PfModelException; @@ -108,23 +107,13 @@ public class CommissioningProvider { synchronized (lockit) { modelsProvider.createServiceTemplate(serviceTemplate); List<Participant> participantList = - participantProvider.getParticipants(null, - null); - - if (participantList != null) { - for (Participant participant: participantList) { - var participantType = new ToscaConceptIdentifier(); - participantType.setName(participant.getType()); - participantType.setVersion(participant.getTypeVersion()); - - var participantUpdate = new ParticipantUpdate(); - participantUpdate.setParticipantId(participant.getDefinition()); - participantUpdate.setParticipantType(participantType); - - this.supervisionHandler.handleSendCommissionMessage(participantUpdate); - } + participantProvider.getParticipants(null, null); + if (!participantList.isEmpty()) { + this.supervisionHandler.handleSendCommissionMessage( + getCommonOrInstancePropertiesFromNodeTypes(true, + serviceTemplate.getName(), + serviceTemplate.getVersion())); } - } var response = new CommissioningResponse(); @@ -159,21 +148,9 @@ public class CommissioningProvider { List<Participant> participantList = participantProvider.getParticipants(null, null); - - if (participantList != null) { - for (Participant participant : participantList) { - var participantType = new ToscaConceptIdentifier(); - participantType.setName(participant.getType()); - participantType.setVersion(participant.getTypeVersion()); - - var participantUpdate = new ParticipantUpdate(); - participantUpdate.setParticipantId(participant.getDefinition()); - participantUpdate.setParticipantType(participantType); - - this.supervisionHandler.handleSendDeCommissionMessage(participantUpdate); - } + if (!participantList.isEmpty()) { + this.supervisionHandler.handleSendDeCommissionMessage(); } - modelsProvider.deleteServiceTemplate(name, version); } @@ -342,7 +319,7 @@ public class CommissioningProvider { * @return the node types with common or instance properties * @throws PfModelException on errors getting node type properties */ - private Map<String, ToscaNodeType> getCommonOrInstancePropertiesFromNodeTypes(boolean common, String name, + public Map<String, ToscaNodeType> getCommonOrInstancePropertiesFromNodeTypes(boolean common, String name, String version) throws PfModelException { var serviceTemplates = new ToscaServiceTemplates(); serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version)); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java index 28558cc02..5ec6158bd 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.function.Function; import java.util.function.UnaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -44,7 +45,9 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop 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.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.GenericNameVersion; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimed; @@ -74,6 +77,7 @@ public class ControlLoopInstantiationProvider { private static final String CONTROL_LOOP_NODE_TYPE = "org.onap.policy.clamp.controlloop.ControlLoop"; private static final String CONTROL_LOOP_NODE_ELEMENT_TYPE = "ControlLoopElement"; private static final String PARTICIPANT_ID_PROPERTY_KEY = "participant_id"; + private static final String PARTICIPANT_TYPE_PROPERTY_KEY = "participantType"; private static final String CL_ELEMENT_NAME = "name"; private static final String CL_ELEMENT_VERSION = "version"; private static final String INSTANCE_TEXT = "_Instance"; @@ -83,6 +87,7 @@ public class ControlLoopInstantiationProvider { private final ControlLoopProvider controlLoopProvider; private final CommissioningProvider commissioningProvider; private final SupervisionHandler supervisionHandler; + private final ParticipantProvider participantProvider; private static final Object lockit = new Object(); @@ -94,20 +99,18 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on creation errors */ public InstancePropertiesResponse createInstanceProperties(ToscaServiceTemplate serviceTemplate) - throws PfModelException { + throws PfModelException { String instanceName = generateSequentialInstanceName(); ControlLoop controlLoop = new ControlLoop(); Map<UUID, ControlLoopElement> controlLoopElements = new HashMap<>(); - ToscaServiceTemplate toscaServiceTemplate = commissioningProvider - .getToscaServiceTemplate(null, null); + ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); - Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = toscaServiceTemplate - .getToscaTopologyTemplate().getNodeTemplates(); + Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); - Map<String, ToscaNodeTemplate> nodeTemplates = - deepCloneNodeTemplate(serviceTemplate); + Map<String, ToscaNodeTemplate> nodeTemplates = deepCloneNodeTemplate(serviceTemplate); nodeTemplates.forEach((key, template) -> { ToscaNodeTemplate newNodeTemplate = new ToscaNodeTemplate(); @@ -153,15 +156,14 @@ public class ControlLoopInstantiationProvider { ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(name, version); - toscaServiceTemplate.getToscaTopologyTemplate() - .getNodeTemplates().forEach((key, nodeTemplate) -> { - if (!nodeTemplate.getName().contains(instanceName)) { - filteredToscaNodeTemplateMap.put(key, nodeTemplate); - } - }); + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, nodeTemplate) -> { + if (!nodeTemplate.getName().contains(instanceName)) { + filteredToscaNodeTemplateMap.put(key, nodeTemplate); + } + }); - List<ToscaNodeTemplate> filteredToscaNodeTemplateList = - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().values().stream() + List<ToscaNodeTemplate> filteredToscaNodeTemplateList = toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().values().stream() .filter(nodeTemplate -> nodeTemplate.getName().contains(instanceName)).collect(Collectors.toList()); InstantiationResponse response = this.deleteControlLoop(name, version); @@ -182,8 +184,7 @@ public class ControlLoopInstantiationProvider { synchronized (lockit) { for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { - var checkControlLoop = controlLoopProvider - .getControlLoop(controlLoop.getKey().asIdentifier()); + var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier()); if (checkControlLoop != null) { throw new PfModelException(Response.Status.BAD_REQUEST, controlLoop.getKey().asIdentifier() + " already defined"); @@ -348,12 +349,20 @@ public class ControlLoopInstantiationProvider { } synchronized (lockit) { + var participants = participantProvider.getParticipants(null, null); + if (participants.isEmpty()) { + throw new ControlLoopException(Status.BAD_REQUEST, "No participants registered"); + } List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size()); for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) { var controlLoop = controlLoopProvider.getControlLoop(id); controlLoop.setCascadedOrderedState(command.getOrderedState()); controlLoops.add(controlLoop); } + BeanValidationResult validationResult = validateIssueControlLoops(controlLoops, participants); + if (!validationResult.isValid()) { + throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult()); + } controlLoopProvider.updateControlLoops(controlLoops); } @@ -364,6 +373,37 @@ public class ControlLoopInstantiationProvider { return response; } + private BeanValidationResult validateIssueControlLoops(List<ControlLoop> controlLoops, + List<Participant> participants) { + var result = new BeanValidationResult("ControlLoops", controlLoops); + + Map<ToscaConceptIdentifier, Participant> participantMap = participants.stream() + .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity())); + + for (ControlLoop controlLoop : controlLoops) { + + for (var element : controlLoop.getElements().values()) { + var subResult = new BeanValidationResult("entry " + element.getDefinition().getName(), element); + + Participant p = participantMap.get(element.getParticipantId()); + if (p == null) { + subResult.addResult(new ObjectValidationResult(CONTROL_LOOP_NODE_ELEMENT_TYPE, + element.getDefinition().getName(), ValidationStatus.INVALID, + "Participant with ID " + element.getParticipantId() + " is not registered")); + } else if (!p.getParticipantType().equals(element.getParticipantType())) { + subResult.addResult(new ObjectValidationResult(CONTROL_LOOP_NODE_ELEMENT_TYPE, + element.getDefinition().getName(), ValidationStatus.INVALID, + "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId() + + " is not registered")); + } + result.addResult(subResult); + } + + } + + return result; + } + /** * Gets a list of control loops with it's ordered state. * @@ -373,7 +413,7 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on errors getting control loops */ public ControlLoopOrderStateResponse getInstantiationOrderState(String name, String version) - throws PfModelException { + throws PfModelException { List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version); @@ -398,8 +438,7 @@ public class ControlLoopInstantiationProvider { * @return a list of Instantiation Command * @throws PfModelException on errors getting control loops */ - public ControlLoopPrimedResponse getControlLoopPriming(String name, String version) - throws PfModelException { + public ControlLoopPrimedResponse getControlLoopPriming(String name, String version) throws PfModelException { List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version); @@ -424,8 +463,8 @@ public class ControlLoopInstantiationProvider { * @return the result of the instance properties and instantiation operation * @throws PfModelException on creation errors */ - private InstancePropertiesResponse saveInstancePropertiesAndControlLoop( - ToscaServiceTemplate serviceTemplate, ControlLoops controlLoops) throws PfModelException { + private InstancePropertiesResponse saveInstancePropertiesAndControlLoop(ToscaServiceTemplate serviceTemplate, + ControlLoops controlLoops) throws PfModelException { var response = new InstancePropertiesResponse(); @@ -436,7 +475,7 @@ public class ControlLoopInstantiationProvider { var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier()); if (checkControlLoop != null) { throw new PfModelException(Response.Status.BAD_REQUEST, - controlLoop.getKey().asIdentifier() + " already defined"); + "Control loop with id " + controlLoop.getKey().asIdentifier() + " already defined"); } } @@ -447,19 +486,20 @@ public class ControlLoopInstantiationProvider { } List<ToscaConceptIdentifier> affectedControlLoops = controlLoops.getControlLoopList().stream() - .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()); + .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()); List<ToscaConceptIdentifier> toscaAffectedProperties = toscaSavedNodeTemplate.values().stream() - .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList()); + .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList()); response.setAffectedInstanceProperties(Stream.of(affectedControlLoops, toscaAffectedProperties) - .flatMap(Collection::stream).collect(Collectors.toList())); + .flatMap(Collection::stream).collect(Collectors.toList())); return response; } /** * Crates a new Control Loop instance. + * * @param instanceName Control Loop Instance name * @param controlLoop empty Control Loop * @param controlLoopElements new Control Loop Element map @@ -467,15 +507,14 @@ public class ControlLoopInstantiationProvider { * @param newNodeTemplate new Tosca Node Template */ private void crateNewControlLoopInstance(String instanceName, ControlLoop controlLoop, - Map<UUID, ControlLoopElement> controlLoopElements, - ToscaNodeTemplate template, - ToscaNodeTemplate newNodeTemplate) { + Map<UUID, ControlLoopElement> controlLoopElements, ToscaNodeTemplate template, + ToscaNodeTemplate newNodeTemplate) { if (template.getType().equals(CONTROL_LOOP_NODE_TYPE)) { controlLoop.setDefinition(getControlLoopDefinition(newNodeTemplate)); } if (template.getType().contains(CONTROL_LOOP_NODE_ELEMENT_TYPE)) { - ControlLoopElement controlLoopElement = getControlLoopElement(instanceName, newNodeTemplate); + ControlLoopElement controlLoopElement = getControlLoopElement(newNodeTemplate); controlLoopElements.put(controlLoopElement.getId(), controlLoopElement); } @@ -486,7 +525,6 @@ public class ControlLoopInstantiationProvider { controlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED); } - /** * Get's the instance property name of the control loop. * @@ -496,9 +534,8 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on errors getting control loops */ private String getInstancePropertyName(String name, String version) throws PfModelException { - List<String> toscaDefinitionsNames = - controlLoopProvider.getControlLoops(name, version).stream().map(ControlLoop::getDefinition) - .map(ToscaNameVersion::getName).collect(Collectors.toList()); + List<String> toscaDefinitionsNames = controlLoopProvider.getControlLoops(name, version).stream() + .map(ControlLoop::getDefinition).map(ToscaNameVersion::getName).collect(Collectors.toList()); return toscaDefinitionsNames.stream().reduce("", (s1, s2) -> { @@ -520,8 +557,7 @@ public class ControlLoopInstantiationProvider { private String generateSequentialInstanceName() { List<ToscaNodeTemplate> nodeTemplates = controlLoopProvider.getNodeTemplates(null, null); - int instanceNumber = - nodeTemplates.stream().map(ToscaNodeTemplate::getName) + int instanceNumber = nodeTemplates.stream().map(ToscaNodeTemplate::getName) .filter(name -> name.contains(INSTANCE_TEXT)).map(n -> { String[] defNameArr = n.split(INSTANCE_TEXT); @@ -548,27 +584,36 @@ public class ControlLoopInstantiationProvider { /** * Retrieves Control Loop Element. * - * @param instanceName instance name to be appended to participant name * @param template tosca node template * @return a control loop element */ @SuppressWarnings("unchecked") - private ControlLoopElement getControlLoopElement(String instanceName, ToscaNodeTemplate template) { + private ControlLoopElement getControlLoopElement(ToscaNodeTemplate template) { ControlLoopElement controlLoopElement = new ControlLoopElement(); ToscaConceptIdentifier definition = new ToscaConceptIdentifier(); definition.setName(template.getName()); definition.setVersion(template.getVersion()); controlLoopElement.setDefinition(definition); - LinkedTreeMap<String, Object> participantId = (LinkedTreeMap<String, Object>) template.getProperties() - .get(PARTICIPANT_ID_PROPERTY_KEY); + LinkedTreeMap<String, Object> participantId = + (LinkedTreeMap<String, Object>) template.getProperties().get(PARTICIPANT_ID_PROPERTY_KEY); - ToscaConceptIdentifier participantIdAndType = new ToscaConceptIdentifier(); - participantIdAndType.setName(participantId.get(CL_ELEMENT_NAME) + instanceName); - participantIdAndType.setVersion(String.valueOf(participantId.get(CL_ELEMENT_VERSION))); + if (participantId != null) { + ToscaConceptIdentifier participantIdProperty = new ToscaConceptIdentifier(); + participantIdProperty.setName(String.valueOf(participantId.get(CL_ELEMENT_NAME))); + participantIdProperty.setVersion(String.valueOf(participantId.get(CL_ELEMENT_VERSION))); + controlLoopElement.setParticipantId(participantIdProperty); + } + + LinkedTreeMap<String, Object> participantType = + (LinkedTreeMap<String, Object>) template.getProperties().get(PARTICIPANT_TYPE_PROPERTY_KEY); - controlLoopElement.setParticipantType(participantIdAndType); - controlLoopElement.setParticipantId(participantIdAndType); + if (participantType != null) { + ToscaConceptIdentifier participantTypeProperty = new ToscaConceptIdentifier(); + participantTypeProperty.setName(String.valueOf(participantType.get(CL_ELEMENT_NAME))); + participantTypeProperty.setVersion(participantType.get(CL_ELEMENT_VERSION).toString()); + controlLoopElement.setParticipantType(participantTypeProperty); + } return controlLoopElement; } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java index c4ca568f7..20292633b 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.controlloop.runtime.supervision; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -42,7 +43,6 @@ import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.Contr import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher; @@ -52,6 +52,7 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantReg import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -142,15 +143,11 @@ public class SupervisionHandler { * * @param participantRegisterMessage the ParticipantRegister message received from a participant */ - @MessageIntercept public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) { LOGGER.debug("Participant Register received {}", participantRegisterMessage); participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(), participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType()); - - participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(), - participantRegisterMessage.getParticipantType(), true); } /** @@ -209,25 +206,19 @@ public class SupervisionHandler { /** * Send commissioning update message to dmaap. * - * @param participantUpdateMessage the ParticipantUpdate message to send */ - public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) { - LOGGER.debug("Participant update message being sent {}", participantUpdateMessage); - - participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(), - participantUpdateMessage.getParticipantType(), true); + public void handleSendCommissionMessage(Map<String, ToscaNodeType> commonPropertiesMap) { + LOGGER.debug("Participant update message being sent {}"); + participantUpdatePublisher.send(commonPropertiesMap, true); } /** * Send decommissioning update message to dmaap. * - * @param participantUpdateMessage the ParticipantUpdate message to send */ - public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) { - LOGGER.debug("Participant update message being sent {}", participantUpdateMessage); - - participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(), - participantUpdateMessage.getParticipantType(), false); + public void handleSendDeCommissionMessage() { + LOGGER.debug("Participant update message being sent"); + participantUpdatePublisher.send(Collections.emptyMap(), false); } /** @@ -429,6 +420,7 @@ public class SupervisionHandler { participant.setName(participantStatusMessage.getParticipantId().getName()); participant.setVersion(participantStatusMessage.getParticipantId().getVersion()); participant.setDefinition(participantStatusMessage.getParticipantId()); + participant.setParticipantType(participantStatusMessage.getParticipantType()); participant.setParticipantState(participantStatusMessage.getState()); participant.setHealthStatus(participantStatusMessage.getHealthStatus()); 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 d13d66c5d..684711677 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 @@ -29,6 +29,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.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; @@ -37,7 +38,10 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpd import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusReqPublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -55,6 +59,7 @@ public class SupervisionScanner { new HandleCounter<>(); private final ControlLoopProvider controlLoopProvider; + private final PolicyModelsProvider modelsProvider; private final ControlLoopStateChangePublisher controlLoopStateChangePublisher; private final ControlLoopUpdatePublisher controlLoopUpdatePublisher; private final ParticipantProvider participantProvider; @@ -65,6 +70,7 @@ public class SupervisionScanner { * Constructor for instantiating SupervisionScanner. * * @param controlLoopProvider the provider to use to read control loops from the database + * @param modelsProvider the Policy Models Provider * @param controlLoopStateChangePublisher the ControlLoop StateChange Publisher * @param controlLoopUpdatePublisher the ControlLoopUpdate Publisher * @param participantProvider the Participant Provider @@ -72,13 +78,14 @@ public class SupervisionScanner { * @param participantUpdatePublisher the Participant Update Publisher * @param clRuntimeParameterGroup the parameters for the control loop runtime */ - public SupervisionScanner(final ControlLoopProvider controlLoopProvider, + public SupervisionScanner(final ControlLoopProvider controlLoopProvider, PolicyModelsProvider modelsProvider, final ControlLoopStateChangePublisher controlLoopStateChangePublisher, ControlLoopUpdatePublisher controlLoopUpdatePublisher, ParticipantProvider participantProvider, ParticipantStatusReqPublisher participantStatusReqPublisher, ParticipantUpdatePublisher participantUpdatePublisher, final ClRuntimeParameterGroup clRuntimeParameterGroup) { this.controlLoopProvider = controlLoopProvider; + this.modelsProvider = modelsProvider; this.controlLoopStateChangePublisher = controlLoopStateChangePublisher; this.controlLoopUpdatePublisher = controlLoopUpdatePublisher; this.participantProvider = participantProvider; @@ -119,8 +126,13 @@ public class SupervisionScanner { } try { - for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) { - scanControlLoop(controlLoop, counterCheck); + var list = modelsProvider.getServiceTemplateList(null, null); + if (list != null && !list.isEmpty()) { + ToscaServiceTemplate toscaServiceTemplate = list.get(0); + + for (ControlLoop controlLoop : controlLoopProvider.getControlLoops(null, null)) { + scanControlLoop(controlLoop, toscaServiceTemplate, counterCheck); + } } } catch (PfModelException pfme) { LOGGER.warn("error reading control loops from database", pfme); @@ -144,7 +156,7 @@ public class SupervisionScanner { if (participantUpdateCounter.count(id)) { LOGGER.debug("retry message ParticipantUpdate"); - participantUpdatePublisher.send(id.getLeft(), id.getRight(), true); + participantUpdatePublisher.send(null, true); } else { LOGGER.debug("report Participant Update fault"); participantUpdateCounter.setFault(id); @@ -190,7 +202,8 @@ public class SupervisionScanner { participantUpdateCounter.remove(id); } - private void scanControlLoop(final ControlLoop controlLoop, boolean counterCheck) throws PfModelException { + private void scanControlLoop(final ControlLoop controlLoop, ToscaServiceTemplate toscaServiceTemplate, + boolean counterCheck) throws PfModelException { LOGGER.debug("scanning control loop {} . . .", controlLoop.getKey().asIdentifier()); if (controlLoop.getState().equals(controlLoop.getOrderedState().asState())) { @@ -202,11 +215,16 @@ public class SupervisionScanner { } var completed = true; + var minSpNotCompleted = 1000; // min startPhase not completed for (ControlLoopElement element : controlLoop.getElements().values()) { if (!element.getState().equals(element.getOrderedState().asState())) { completed = false; - break; + ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates() + .get(element.getDefinition().getName()); + int startPhase = ParticipantUtils.findStartPhase(toscaNodeTemplate.getProperties()); + minSpNotCompleted = Math.min(minSpNotCompleted, startPhase); } + } if (completed) { @@ -222,7 +240,7 @@ public class SupervisionScanner { LOGGER.debug("control loop scan: transition from state {} to {} not completed", controlLoop.getState(), controlLoop.getOrderedState()); if (counterCheck) { - handleCounter(controlLoop); + handleCounter(controlLoop, minSpNotCompleted); } } } @@ -231,7 +249,7 @@ public class SupervisionScanner { controlLoopCounter.clear(controlLoop.getKey().asIdentifier()); } - private void handleCounter(ControlLoop controlLoop) { + private void handleCounter(ControlLoop controlLoop, int startPhase) { ToscaConceptIdentifier id = controlLoop.getKey().asIdentifier(); if (controlLoopCounter.isFault(id)) { LOGGER.debug("report ControlLoop fault"); @@ -242,7 +260,7 @@ public class SupervisionScanner { if (controlLoopCounter.count(id)) { if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) { LOGGER.debug("retry message ControlLoopUpdate"); - controlLoopUpdatePublisher.send(controlLoop); + controlLoopUpdatePublisher.send(controlLoop, startPhase); } else { LOGGER.debug("retry message ControlLoopStateChange"); controlLoopStateChangePublisher.send(controlLoop); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java index d68a643d2..74d987240 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java @@ -58,7 +58,18 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con * @param controlLoop the ControlLoop */ public void send(ControlLoop controlLoop) { + send(controlLoop, 0); + } + + /** + * Send ControlLoopUpdate to Participant. + * + * @param controlLoop the ControlLoop + * @param startPhase the Start Phase + */ + public void send(ControlLoop controlLoop, int startPhase) { var controlLoopUpdateMsg = new ControlLoopUpdate(); + controlLoopUpdateMsg.setStartPhase(startPhase); controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier()); controlLoopUpdateMsg.setMessageId(UUID.randomUUID()); controlLoopUpdateMsg.setTimestamp(Instant.now()); 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 fe46297f1..0f6cc7cbc 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,6 +25,7 @@ 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; @@ -34,6 +35,7 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,14 +55,9 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par /** * Send ParticipantUpdate to Participant. * - * @param participantId the participant Id - * @param participantType the participant Type */ - public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType, - boolean commissionFlag) { + public void send(Map<String, ToscaNodeType> commonPropertiesMap, boolean commissionFlag) { var message = new ParticipantUpdate(); - message.setParticipantId(participantId); - message.setParticipantType(participantType); message.setTimestamp(Instant.now()); ToscaServiceTemplate toscaServiceTemplate = null; @@ -82,7 +79,7 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par var clParticipantType = ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); + toscaInputEntry.getValue(), participantDefinitionUpdates, commonPropertiesMap); } } } @@ -99,11 +96,17 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par } private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { + ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates, + Map<String, ToscaNodeType> commonPropertiesMap) { var clDefinition = new ControlLoopElementDefinition(); clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); + ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType()); + if (nodeType != null) { + clDefinition.setCommonPropertiesMap(nodeType.getProperties()); + } + List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>(); if (participantDefinitionUpdates.isEmpty()) { diff --git a/runtime-controlloop/src/main/resources/application.yaml b/runtime-controlloop/src/main/resources/application.yaml index 96c340491..50c063fb3 100644 --- a/runtime-controlloop/src/main/resources/application.yaml +++ b/runtime-controlloop/src/main/resources/application.yaml @@ -26,7 +26,7 @@ runtime: heartBeatMs: 20000 maxStatusWaitMs: 100000 updateParameters: - maxRetryCount: 3 + maxRetryCount: 4 maxWaitMs: 20000 databaseProviderParameters: name: PolicyProviderParameterGroup diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java index 7f04056cf..24272e308 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -107,7 +108,7 @@ class ControlLoopInstantiationProviderTest { @BeforeAll public static void setUpBeforeClass() throws Exception { serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_TEMPLATE_YAML), - ToscaServiceTemplate.class); + ToscaServiceTemplate.class); } /** @@ -119,8 +120,7 @@ class ControlLoopInstantiationProviderTest { public static void setupDbProviderParameters() throws PfModelException { ClRuntimeParameterGroup controlLoopParameters = CommonTestData.geParameterGroup("instantproviderdb"); - modelsProvider = - CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); + modelsProvider = CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); @@ -138,8 +138,8 @@ class ControlLoopInstantiationProviderTest { var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, - controlLoopUpdatePublisher, controlLoopStateChangePublisher, - participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); + controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, + participantDeregisterAckPublisher, participantUpdatePublisher); } @BeforeEach @@ -160,13 +160,14 @@ class ControlLoopInstantiationProviderTest { @Test void testInstantiationCrud() throws Exception { + participantProvider.createParticipants(CommonTestData.createParticipants()); ControlLoops controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud"); ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); @@ -222,8 +223,8 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsDb = new ControlLoops(); controlLoopsDb.setControlLoopList(new ArrayList<>()); - var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, - commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) { ControlLoops controlLoopsFromDb = @@ -242,12 +243,12 @@ class ControlLoopInstantiationProviderTest { ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); assertThatThrownBy( - () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion())) - .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); + () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion())) + .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops), controlLoops); @@ -277,13 +278,13 @@ class ControlLoopInstantiationProviderTest { controlLoop.setState(state); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); instantiationProvider.updateControlLoops(controlLoops); assertThatThrownBy( - () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion())) - .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); + () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion())) + .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); } @Test @@ -295,8 +296,8 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); @@ -315,10 +316,11 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoops = InstantiationUtils .getControlLoopsFromResource(CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON, "ClElementNotFound"); - var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler, + participantProvider); // to validate control Loop, it needs to define ToscaServiceTemplate - // InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); + // InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); @@ -333,15 +335,16 @@ class ControlLoopInstantiationProviderTest { assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); - var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler, + participantProvider); assertThatThrownBy(() -> provider.createControlLoops(controlLoops)) .hasMessageMatching(CONTROLLOOP_DEFINITION_NOT_FOUND); } @Test void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException { - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); assertThatThrownBy(() -> instantiationProvider.issueControlLoopCommand(new InstantiationCommand())) .hasMessageMatching(ORDERED_STATE_INVALID); } @@ -353,8 +356,8 @@ class ControlLoopInstantiationProviderTest { InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1"); assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1), controlLoopsV1); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java index 42e4ddee2..5a15687c5 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java @@ -36,6 +36,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; @@ -43,6 +44,7 @@ import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstan import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController; +import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; @@ -94,6 +96,9 @@ class InstantiationControllerTest extends CommonRestController { @Autowired private ControlLoopInstantiationProvider instantiationProvider; + @Autowired + private ParticipantProvider participantProvider; + @LocalServerPort private int randomServerPort; @@ -326,10 +331,11 @@ class InstantiationControllerTest extends CommonRestController { @Test void testCommand() throws Exception { - var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command"); instantiationProvider.createControlLoops(controlLoops); + participantProvider.createParticipants(CommonTestData.createParticipants()); + InstantiationCommand command = InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command"); 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 2ba321d36..1bf445734 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 @@ -46,13 +46,16 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantSta import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; class SupervisionScannerTest { @Test void testScannerOrderedStateEqualsToState() throws PfModelException { var controlLoopProvider = mock(ControlLoopProvider.class); + var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantProvider = mock(ParticipantProvider.class); @@ -63,9 +66,9 @@ class SupervisionScannerTest { var controlLoop = new ControlLoop(); when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop)); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, controlLoopStateChangePublisher, - controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, - participantUpdatePublisher, clRuntimeParameterGroup); + var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, + participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.run(false); verify(controlLoopProvider, times(0)).updateControlLoop(any(ControlLoop.class)); @@ -87,9 +90,12 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, controlLoopStateChangePublisher, - controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, - participantUpdatePublisher, clRuntimeParameterGroup); + var modelsProvider = mock(PolicyModelsProvider.class); + when(modelsProvider.getServiceTemplateList(null, null)).thenReturn(List.of(new ToscaServiceTemplate())); + + var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, + participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.run(false); verify(controlLoopProvider, times(1)).updateControlLoop(any(ControlLoop.class)); @@ -107,15 +113,16 @@ class SupervisionScannerTest { participant.setVersion("1.0.0"); when(participantProvider.getParticipants(null, null)).thenReturn(List.of(participant)); + var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantStatusReqPublisher = mock(ParticipantStatusReqPublisher.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, controlLoopStateChangePublisher, - controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, - participantUpdatePublisher, clRuntimeParameterGroup); + var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, + participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.handleParticipantStatus(participant.getKey().asIdentifier()); supervisionScanner.run(true); @@ -139,17 +146,19 @@ class SupervisionScannerTest { participant.setHealthStatus(ParticipantHealthStatus.HEALTHY); participant.setParticipantState(ParticipantState.ACTIVE); participant.setDefinition(new ToscaConceptIdentifier("unknown", "0.0.0")); + participant.setParticipantType(new ToscaConceptIdentifier("ParticipantType1", "1.0.0")); var participantProvider = new ParticipantProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); participantProvider.updateParticipants(List.of(participant)); + var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantStatusReqPublisher = mock(ParticipantStatusReqPublisher.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, controlLoopStateChangePublisher, - controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, - participantUpdatePublisher, clRuntimeParameterGroup); + var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, + participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.handleParticipantStatus(participant.getKey().asIdentifier()); supervisionScanner.run(true); 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 bcc3a701c..70d115e47 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 @@ -24,29 +24,20 @@ package org.onap.policy.clamp.controlloop.runtime.supervision.comm; import static org.assertj.core.api.Assertions.assertThatCode; import java.time.Instant; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; -import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; 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; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; -import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; @@ -54,27 +45,18 @@ 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.YamlJsonTranslator; -import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; class SupervisionMessagesTest extends CommonRestController { - private static final String TOSCA_SERVICE_TEMPLATE_YAML = - "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; private static final Object lockit = new Object(); private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private static SupervisionHandler supervisionHandler; - private static CommissioningProvider commissioningProvider; private static ControlLoopProvider clProvider; private static PolicyModelsProvider modelsProvider; - private static ParticipantProvider participantProvider; - private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); /** * setup Db Provider Parameters. @@ -87,13 +69,11 @@ class SupervisionMessagesTest extends CommonRestController { modelsProvider = CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); - participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); var participantStatisticsProvider = new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); var clElementStatisticsProvider = new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); - commissioningProvider = new CommissioningProvider(modelsProvider, clProvider, null, participantProvider); var monitoringProvider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); var participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); @@ -114,26 +94,6 @@ class SupervisionMessagesTest extends CommonRestController { } @Test - void testReceiveParticipantRegister() throws Exception { - final ParticipantRegister participantRegisterMsg = new ParticipantRegister(); - participantRegisterMsg.setParticipantId(getParticipantId()); - participantRegisterMsg.setTimestamp(Instant.now()); - participantRegisterMsg.setParticipantType(getParticipantType()); - - synchronized (lockit) { - ParticipantRegisterListener participantRegisterListener = - new ParticipantRegisterListener(supervisionHandler); - ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( - ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); - - // List<ToscaNodeTemplate> listOfTemplates = commissioningProvider.getControlLoopDefinitions(null, null); - commissioningProvider.createControlLoopDefinitions(serviceTemplate); - assertThatCode(() -> participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterMsg)) - .doesNotThrowAnyException(); - } - } - - @Test void testSendParticipantRegisterAck() throws Exception { final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck(); participantRegisterAckMsg.setMessage("ParticipantRegisterAck message"); @@ -178,72 +138,6 @@ class SupervisionMessagesTest extends CommonRestController { } @Test - void testSendParticipantUpdate() throws Exception { - final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate(); - participantUpdateMsg.setParticipantId(getParticipantId()); - participantUpdateMsg.setTimestamp(Instant.now()); - participantUpdateMsg.setParticipantType(getParticipantType()); - participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); - participantUpdateMsg.setMessageId(UUID.randomUUID()); - - ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); - List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - 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); - } - } - - participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); - synchronized (lockit) { - ParticipantUpdatePublisher participantUpdatePublisher = new ParticipantUpdatePublisher(modelsProvider); - participantUpdatePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); - assertThatCode(() -> participantUpdatePublisher.send(participantUpdateMsg)).doesNotThrowAnyException(); - } - } - - private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { - - var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); - List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates - .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } else { - boolean participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) { - participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); - participantExists = true; - } - } - if (!participantExists) { - participantDefinitionUpdates.add( - getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } - } - } - - private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { - ParticipantDefinition participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } - - @Test void testReceiveParticipantUpdateAckMessage() throws Exception { final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck(); participantUpdateAckMsg.setMessage("ParticipantUpdateAck message"); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java index 300c662ce..13d30e401 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java @@ -20,8 +20,10 @@ package org.onap.policy.clamp.controlloop.runtime.util; +import java.util.List; import javax.ws.rs.core.Response.Status; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -32,6 +34,7 @@ import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; /** * Class to hold/create all parameters for test cases. @@ -81,4 +84,40 @@ public class CommonTestData { throw new PfModelRuntimeException(e); } } + + /** + * Create a List of Participants. + * + * @return a List of Participants + */ + public static List<Participant> createParticipants() { + var participant1 = createParticipant( + new ToscaConceptIdentifier("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "2.3.4"), + new ToscaConceptIdentifier("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "2.3.4")); + var participant2 = createParticipant( + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"), + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1")); + var participant3 = createParticipant( + new ToscaConceptIdentifier("org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "2.2.1"), + new ToscaConceptIdentifier("org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "2.2.1")); + return List.of(participant1, participant2, participant3); + } + + /** + * Create a new Participant. + * + * @param participantType the participant Type + * @param participantId the participant id + * @return a new Participant + */ + public static Participant createParticipant(ToscaConceptIdentifier participantType, + ToscaConceptIdentifier participantId) { + var participant = new Participant(); + participant.setDefinition(participantId); + participant.setParticipantType(participantType); + participant.setName(participantId.getName()); + participant.setVersion(participantId.getVersion()); + return participant; + } } diff --git a/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json b/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json index 025e2a1fb..b09e46fe7 100644 --- a/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json +++ b/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json @@ -17,6 +17,10 @@ "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, "participantType": { "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "version": "2.3.4" @@ -31,6 +35,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -45,6 +53,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -59,6 +71,10 @@ "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, "participantType": { "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "version": "2.2.1" @@ -86,6 +102,10 @@ "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, "participantType": { "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "version": "2.3.4" @@ -100,6 +120,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -114,6 +138,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -128,6 +156,10 @@ "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, "participantType": { "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "version": "2.2.1" |