diff options
Diffstat (limited to 'models/src/main/java')
7 files changed, 146 insertions, 71 deletions
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<Nod @Column private String participantId; - @Column + @VerifyKey @NotNull - private ToscaConceptIdentifier nodeTemplateId; + @AttributeOverride(name = "name", column = @Column(name = "nodeTemplate_name")) + @AttributeOverride(name = "version", column = @Column(name = "nodeTemplate_version")) + private PfConceptKey nodeTemplateId; @Column @NotNull @@ -86,7 +91,8 @@ public class JpaNodeTemplateState extends Validated implements PfAuthorative<Nod if (copyConcept.getParticipantId() != null) { this.participantId = copyConcept.getParticipantId().toString(); } - this.nodeTemplateId = copyConcept.getNodeTemplateId(); + this.nodeTemplateId = copyConcept.getNodeTemplateId().asConceptKey(); + this.state = copyConcept.getState(); } @Override @@ -96,7 +102,7 @@ public class JpaNodeTemplateState extends Validated implements PfAuthorative<Nod if (this.participantId != null) { nodeTemplateState.setParticipantId(UUID.fromString(this.participantId)); } - nodeTemplateState.setNodeTemplateId(this.nodeTemplateId); + nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier(this.nodeTemplateId)); nodeTemplateState.setState(this.state); return nodeTemplateState; } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProvider.java index 3757f6c7b..71f510a67 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcDefinitionProvider.java @@ -32,6 +32,7 @@ import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition; import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository; +import org.onap.policy.clamp.models.acm.utils.AcmUtils; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.data.domain.Example; @@ -62,6 +63,8 @@ public class AcDefinitionProvider { } serviceTemplate.getMetadata().put("compositionId", compositionId); acmDefinition.setServiceTemplate(serviceTemplate); + var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate); + acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED)); var jpaAcmDefinition = ProviderUtils.getJpaAndValidate(acmDefinition, JpaAutomationCompositionDefinition::new, "AutomationCompositionDefinition"); var result = acmDefinitionRepository.save(jpaAcmDefinition); @@ -80,6 +83,8 @@ public class AcDefinitionProvider { acmDefinition.setCompositionId(compositionId); acmDefinition.setState(AcTypeState.COMMISSIONED); acmDefinition.setServiceTemplate(serviceTemplate); + var acElements = AcmUtils.extractAcElementsFromServiceTemplate(serviceTemplate); + acmDefinition.setElementStateMap(AcmUtils.createElementStateMap(acElements, AcTypeState.COMMISSIONED)); updateAcDefinition(acmDefinition); } @@ -137,10 +142,9 @@ public class AcDefinitionProvider { * @return the automation composition definition */ @Transactional(readOnly = true) - public Optional<ToscaServiceTemplate> findAcDefinition(UUID compositionId) { + public Optional<AutomationCompositionDefinition> 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<ToscaConceptIdentifier, UUID> getSupportedElementMap() { + var list = participantRepository.findAll(); + Map<ToscaConceptIdentifier, UUID> 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<ParticipantDefinition> participantDefinitionUpdates) { - - var acDefinition = new AutomationCompositionElementDefinition(); - acDefinition.setAcElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - acDefinition.setAutomationCompositionElementToscaNodeTemplate(entryValue); - - List<AutomationCompositionElementDefinition> 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<ParticipantDefinition> prepareParticipantPriming( + List<Entry<String, ToscaNodeTemplate>> acElements, Map<ToscaConceptIdentifier, UUID> supportedElementMap) { + + Map<UUID, List<AutomationCompositionElementDefinition>> 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<ParticipantDefinition> prepareParticipantPriming( + Map<UUID, List<AutomationCompositionElementDefinition>> map) { + List<ParticipantDefinition> 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<AutomationCompositionElementDefinition> 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<Entry<String, ToscaNodeTemplate>> 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<String, NodeTemplateState> createElementStateMap( + List<Entry<String, ToscaNodeTemplate>> acElements, AcTypeState state) { + Map<String, NodeTemplateState> 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; } /** |