From 934f7bd443225a6945b0542fa5cb7c043deac426 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Thu, 26 Jan 2023 17:31:46 +0000 Subject: Refactor Prime and Deprime messages in ACM Issue-ID: POLICY-4502 Change-Id: Ib0ecc513285bf971a0c25cec528dcdeec5ad63a2 Signed-off-by: FrancescoFioraEst --- .../models/acm/concepts/ParticipantUtils.java | 25 ---- .../dmaap/participant/ParticipantAckMessage.java | 2 + .../JpaAutomationCompositionDefinition.java | 8 +- .../persistence/concepts/JpaNodeTemplateState.java | 14 ++- .../persistence/provider/AcDefinitionProvider.java | 10 +- .../persistence/provider/ParticipantProvider.java | 22 ++++ .../policy/clamp/models/acm/utils/AcmUtils.java | 136 +++++++++++++++------ 7 files changed, 146 insertions(+), 71 deletions(-) (limited to 'models/src/main/java') diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java index 015a96aad..54e6db1cc 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantUtils.java @@ -32,8 +32,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; public final class ParticipantUtils { private static final Coder CODER = new StandardCoder(); - private static final String AUTOMATION_COMPOSITION_ELEMENT = - "org.onap.policy.clamp.acm.AutomationCompositionElement"; /** * Get the First StartPhase. @@ -75,27 +73,4 @@ public final class ParticipantUtils { } return 0; } - - /** - * Checks if a NodeTemplate is an AutomationCompositionElement. - * - * @param nodeTemplate the ToscaNodeTemplate - * @param toscaServiceTemplate the ToscaServiceTemplate - * @return true if the NodeTemplate is an AutomationCompositionElement - */ - public static boolean checkIfNodeTemplateIsAutomationCompositionElement(ToscaNodeTemplate nodeTemplate, - ToscaServiceTemplate toscaServiceTemplate) { - if (nodeTemplate.getType().contains(AUTOMATION_COMPOSITION_ELEMENT)) { - return true; - } else { - var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); - if (nodeType != null) { - var derivedFrom = nodeType.getDerivedFrom(); - if (derivedFrom != null) { - return derivedFrom.contains(AUTOMATION_COMPOSITION_ELEMENT); - } - } - } - return false; - } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java index bdb2be861..e73f2e795 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/dmaap/participant/ParticipantAckMessage.java @@ -46,6 +46,8 @@ public class ParticipantAckMessage { private ParticipantMessageType messageType; + private UUID compositionId; + /** * Participant ID, or {@code null} for messages from participants. */ diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java index fcaa6e6db..11e3f583e 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationCompositionDefinition.java @@ -89,8 +89,9 @@ public class JpaAutomationCompositionDefinition extends Validated @Override public AutomationCompositionDefinition toAuthorative() { var acmDefinition = new AutomationCompositionDefinition(); - acmDefinition.setCompositionId(UUID.fromString(compositionId)); - acmDefinition.setServiceTemplate(serviceTemplate.toAuthorative()); + acmDefinition.setCompositionId(UUID.fromString(this.compositionId)); + acmDefinition.setState(this.state); + acmDefinition.setServiceTemplate(this.serviceTemplate.toAuthorative()); for (var element : this.elements) { var key = element.getNodeTemplateId().getName(); acmDefinition.getElementStateMap().put(key, element.toAuthorative()); @@ -105,11 +106,12 @@ public class JpaAutomationCompositionDefinition extends Validated this.serviceTemplate = new DocToscaServiceTemplate(copyConcept.getServiceTemplate()); setName(this.serviceTemplate.getName()); setVersion(this.serviceTemplate.getVersion()); - elements = new HashSet<>(copyConcept.getElementStateMap().size()); + this.elements = new HashSet<>(copyConcept.getElementStateMap().size()); for (var element : copyConcept.getElementStateMap().values()) { var nodeTemplateStateId = element.getNodeTemplateStateId().toString(); var jpaNodeTemplateState = new JpaNodeTemplateState(nodeTemplateStateId, this.compositionId); jpaNodeTemplateState.fromAuthorative(element); + this.elements.add(jpaNodeTemplateState); } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java index a6d13a638..088bf2196 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaNodeTemplateState.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.models.acm.persistence.concepts; import java.util.UUID; +import javax.persistence.AttributeOverride; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @@ -33,7 +34,9 @@ import org.onap.policy.clamp.models.acm.concepts.AcTypeState; import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.Validated; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @Entity @@ -54,9 +57,11 @@ public class JpaNodeTemplateState extends Validated implements PfAuthorative findAcDefinition(UUID compositionId) { + public Optional findAcDefinition(UUID compositionId) { var jpaGet = acmDefinitionRepository.findById(compositionId.toString()); - return jpaGet.stream().map(JpaAutomationCompositionDefinition::getServiceTemplate) - .map(DocToscaServiceTemplate::toAuthorative).findFirst(); + return jpaGet.stream().map(JpaAutomationCompositionDefinition::toAuthorative).findFirst(); } /** diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java index 7b8ebc038..8e45c770b 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/ParticipantProvider.java @@ -20,7 +20,9 @@ package org.onap.policy.clamp.models.acm.persistence.provider; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.UUID; import javax.ws.rs.core.Response.Status; @@ -30,6 +32,7 @@ import org.onap.policy.clamp.models.acm.concepts.Participant; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant; import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -127,4 +130,23 @@ public class ParticipantProvider { return jpaDeleteParticipantOpt.get().toAuthorative(); } + + + /** + * Get a map with SupportedElement as key and the participantId as value. + * + * @return a map + */ + public Map getSupportedElementMap() { + var list = participantRepository.findAll(); + Map map = new HashMap<>(); + for (var participant : list) { + for (var element : participant.getSupportedElements()) { + var supportedElement = new ToscaConceptIdentifier(element.getTypeName(), element.getTypeVersion()); + map.put(supportedElement, UUID.fromString(participant.getParticipantId())); + } + } + return map; + } + } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java index 8bd9039fd..9f73cb144 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/AcmUtils.java @@ -22,25 +22,31 @@ package org.onap.policy.clamp.models.acm.utils; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.function.Function; import java.util.function.UnaryOperator; import java.util.stream.Collectors; +import javax.ws.rs.core.Response; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; +import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState; import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition; import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.common.parameters.ObjectValidationResult; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.ValidationStatus; +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; @@ -53,6 +59,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class AcmUtils { + public static final String AUTOMATION_COMPOSITION_ELEMENT = + "org.onap.policy.clamp.acm.AutomationCompositionElement"; public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition"; public static final String ENTRY = "entry "; @@ -111,51 +119,107 @@ public final class AcmUtils { } /** - * Prepare ParticipantDefinitionUpdate to set in the message. + * Checks if a NodeTemplate is an AutomationCompositionElement. * - * @param acParticipantId participant id - * @param entryKey key for the entry - * @param entryValue value relates to toscaNodeTemplate - * @param participantDefinitionUpdates list of participantDefinitionUpdates + * @param nodeTemplate the ToscaNodeTemplate + * @param toscaServiceTemplate the ToscaServiceTemplate + * @return true if the NodeTemplate is an AutomationCompositionElement */ - public static void prepareParticipantDefinitionUpdate(UUID acParticipantId, String entryKey, - ToscaNodeTemplate entryValue, - List participantDefinitionUpdates) { - - var acDefinition = new AutomationCompositionElementDefinition(); - acDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - acDefinition.setAutomationCompositionElementToscaNodeTemplate(entryValue); - - List automationCompositionElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates.add(getParticipantDefinition(acDefinition, acParticipantId, - automationCompositionElementDefinitionList)); + public static boolean checkIfNodeTemplateIsAutomationCompositionElement(ToscaNodeTemplate nodeTemplate, + ToscaServiceTemplate toscaServiceTemplate) { + if (nodeTemplate.getType().contains(AUTOMATION_COMPOSITION_ELEMENT)) { + return true; } else { - var participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (acParticipantId != null || participantDefinitionUpdate.getParticipantId() != null) { - if (participantDefinitionUpdate.getParticipantId().equals(acParticipantId)) { - participantDefinitionUpdate.getAutomationCompositionElementDefinitionList().add(acDefinition); - participantExists = true; - } + var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType()); + if (nodeType != null) { + var derivedFrom = nodeType.getDerivedFrom(); + if (derivedFrom != null) { + return derivedFrom.contains(AUTOMATION_COMPOSITION_ELEMENT); } } - if (!participantExists) { - participantDefinitionUpdates.add(getParticipantDefinition(acDefinition, acParticipantId, - automationCompositionElementDefinitionList)); + } + return false; + } + + /** + * Prepare list of ParticipantDefinition for the Priming message. + * + * @param acElements the extracted AcElements from ServiceTemplate + * @param supportedElementMap supported Element Map + */ + public static List prepareParticipantPriming( + List> acElements, Map supportedElementMap) { + + Map> map = new HashMap<>(); + for (var elementEntry : acElements) { + var type = new ToscaConceptIdentifier(elementEntry.getValue().getType(), + elementEntry.getValue().getTypeVersion()); + var participantId = supportedElementMap.get(type); + if (participantId == null) { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, + "Element Type " + type + " not supported"); } + var acElementDefinition = new AutomationCompositionElementDefinition(); + acElementDefinition.setAcElementDefinitionId( + new ToscaConceptIdentifier(elementEntry.getKey(), elementEntry.getValue().getVersion())); + acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(elementEntry.getValue()); + map.putIfAbsent(participantId, new ArrayList<>()); + map.get(participantId).add(acElementDefinition); + } + return prepareParticipantPriming(map); + } + + /** + * Prepare ParticipantPriming. + * + * @param map of AutomationCompositionElementDefinition with participantId as key + * @return list of ParticipantDefinition + */ + public static List prepareParticipantPriming( + Map> map) { + List result = new ArrayList<>(); + for (var entry : map.entrySet()) { + var participantDefinition = new ParticipantDefinition(); + participantDefinition.setParticipantId(entry.getKey()); + participantDefinition.setAutomationCompositionElementDefinitionList(entry.getValue()); + result.add(participantDefinition); } + return result; } - private static ParticipantDefinition getParticipantDefinition(AutomationCompositionElementDefinition acDefinition, - UUID acParticipantId, - List automationCompositionElementDefinitionList) { - var participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantId(acParticipantId); - automationCompositionElementDefinitionList.add(acDefinition); - participantDefinition.setAutomationCompositionElementDefinitionList(automationCompositionElementDefinitionList); - return participantDefinition; + /** + * Extract AcElements from ServiceTemplate. + * + * @param serviceTemplate the ToscaServiceTemplate + * @return the list of Entry of AutomationCompositionElement + */ + public static List> extractAcElementsFromServiceTemplate( + ToscaServiceTemplate serviceTemplate) { + return serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet().stream().filter( + nodeTemplateEntry -> checkIfNodeTemplateIsAutomationCompositionElement(nodeTemplateEntry.getValue(), + serviceTemplate)) + .collect(Collectors.toList()); + } + + /** + * Create NodeTemplateState Map. + * + * @param acElements extracted AcElements from ServiceTemplate. + * @param state the AcTypeState + * @return the NodeTemplateState Map + */ + public static Map createElementStateMap( + List> acElements, AcTypeState state) { + Map result = new HashMap<>(acElements.size()); + for (var entry : acElements) { + var nodeTemplateState = new NodeTemplateState(); + nodeTemplateState.setNodeTemplateStateId(UUID.randomUUID()); + nodeTemplateState.setState(state); + nodeTemplateState + .setNodeTemplateId(new ToscaConceptIdentifier(entry.getKey(), entry.getValue().getVersion())); + result.put(entry.getKey(), nodeTemplateState); + } + return result; } /** -- cgit 1.2.3-korg