From e90fadeec27e0249bb64da08d817fa1c475630fa Mon Sep 17 00:00:00 2001 From: Sirisha_Manchikanti Date: Wed, 27 Oct 2021 08:24:48 +0100 Subject: Move code to common, avoid duplication This commit moves duplicate code from runtime and participant to common Issue-ID: POLICY-3542 Signed-off-by: Sirisha_Manchikanti Change-Id: Ia7cea1d2c88031c72af77338e242f8981e536c21 --- .../comm/ControlLoopUpdatePublisher.java | 56 ++-------------------- .../comm/ParticipantUpdatePublisher.java | 53 ++------------------ 2 files changed, 8 insertions(+), 101 deletions(-) (limited to 'runtime-controlloop/src') 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 74d987240..06fbcd649 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 @@ -27,15 +27,14 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; import lombok.AllArgsConstructor; +import org.onap.policy.clamp.controlloop.common.utils.CommonUtils; 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.ParticipantUpdates; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -48,8 +47,6 @@ import org.springframework.stereotype.Component; public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher { private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class); - private static final String POLICY_TYPE_ID = "policy_type_id"; - private static final String POLICY_ID = "policy_id"; private final PolicyModelsProvider modelsProvider; /** @@ -83,59 +80,12 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher participantUpdates = new ArrayList<>(); for (ControlLoopElement element : controlLoop.getElements().values()) { - ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate - .getToscaTopologyTemplate().getNodeTemplates().get(element.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) - || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) { - // ControlLoopElement for policy framework, send policies and policyTypes to participants - if ((toscaServiceTemplate.getPolicyTypes() != null) - || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) { - ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate(); - toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes()); - - ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate(); - toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()); - toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate); - - toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes()); - - element.setToscaServiceTemplateFragment(toscaServiceTemplateFragment); - } - } - prepareParticipantUpdate(element, participantUpdates); + CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate); + CommonUtils.prepareParticipantUpdate(element, participantUpdates); } controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates); LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg); super.send(controlLoopUpdateMsg); } - - private void prepareParticipantUpdate(ControlLoopElement clElement, - List participantUpdates) { - if (participantUpdates.isEmpty()) { - participantUpdates.add(getControlLoopElementList(clElement)); - } else { - var participantExists = false; - for (ParticipantUpdates participantUpdate : participantUpdates) { - if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) { - participantUpdate.getControlLoopElementList().add(clElement); - participantExists = true; - } - } - if (!participantExists) { - participantUpdates.add(getControlLoopElementList(clElement)); - } - } - } - - private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) { - var participantUpdate = new ParticipantUpdates(); - List controlLoopElementList = new ArrayList<>(); - participantUpdate.setParticipantId(clElement.getParticipantId()); - controlLoopElementList.add(clElement); - participantUpdate.setControlLoopElementList(controlLoopElementList); - return participantUpdate; - } } 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 5d879dc56..c6788f12c 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 @@ -27,14 +27,13 @@ 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.common.utils.CommonUtils; 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.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; 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.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; @@ -99,10 +98,10 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher participantDefinitionUpdates, - Map 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 controlLoopElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates - .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } else { - var 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 controlLoopElementDefinitionList) { - var participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } - /** * Send ParticipantUpdate to Participant after that commissioning has been removed. */ -- cgit 1.2.3-korg