diff options
Diffstat (limited to 'models/src/main')
5 files changed, 71 insertions, 45 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java index c3245d27c..b19f54c3b 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AutomationComposition.java @@ -38,6 +38,9 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; @EqualsAndHashCode(callSuper = true) public class AutomationComposition extends ToscaEntity implements Comparable<AutomationComposition> { @NonNull + private UUID instanceId; + + @NonNull private UUID compositionId; @NonNull @@ -58,6 +61,7 @@ public class AutomationComposition extends ToscaEntity implements Comparable<Aut */ public AutomationComposition(final AutomationComposition otherAutomationComposition) { super(otherAutomationComposition); + this.instanceId = otherAutomationComposition.instanceId; this.compositionId = otherAutomationComposition.compositionId; this.state = otherAutomationComposition.state; this.orderedState = otherAutomationComposition.orderedState; diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java index 4ecb8ce32..534c909aa 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationResponse.java @@ -20,6 +20,7 @@ package org.onap.policy.clamp.models.acm.messages.rest.instantiation; +import java.util.UUID; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -33,5 +34,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @Setter @ToString(callSuper = true) public class InstantiationResponse extends SimpleResponse { + private UUID instanceId; ToscaConceptIdentifier affectedAutomationComposition; } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java index 8268e9066..b87bad4e0 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java @@ -23,7 +23,6 @@ package org.onap.policy.clamp.models.acm.persistence.concepts; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -40,7 +39,6 @@ import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; 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.AutomationCompositionOrderedState; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; import org.onap.policy.common.parameters.annotations.NotNull; @@ -66,11 +64,16 @@ import org.onap.policy.models.base.validation.annotations.VerifyKey; public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> { private static final long serialVersionUID = -4725410933242154805L; + @Column + @NotNull + private String instanceId; + @EmbeddedId @VerifyKey @NotNull private PfConceptKey key; + @Column @NotNull private String compositionId; @@ -97,29 +100,23 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key. */ public JpaAutomationComposition() { - this(new PfConceptKey()); - } - - /** - * The Key Constructor creates a {@link JpaAutomationComposition} object with the given concept key. - * - * @param key the key - */ - public JpaAutomationComposition(@NonNull final PfConceptKey key) { - this(key, UUID.randomUUID().toString(), AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>()); + this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), + AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>()); } /** * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields. * + * @param instanceId The UUID of the automation composition instance * @param key the key * @param compositionId the TOSCA compositionId of the automation composition definition * @param state the state of the automation composition * @param elements the elements of the automation composition in participants */ - public JpaAutomationComposition(@NonNull final PfConceptKey key, @NonNull final String compositionId, - @NonNull final AutomationCompositionState state, + public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key, + @NonNull final String compositionId, @NonNull final AutomationCompositionState state, @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) { + this.instanceId = instanceId; this.key = key; this.compositionId = compositionId; this.state = state; @@ -133,6 +130,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative */ public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) { super(copyConcept); + this.instanceId = copyConcept.instanceId; this.key = new PfConceptKey(copyConcept.key); this.compositionId = copyConcept.compositionId; this.state = copyConcept.state; @@ -156,6 +154,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative public AutomationComposition toAuthorative() { var automationComposition = new AutomationComposition(); + automationComposition.setInstanceId(UUID.fromString(instanceId)); automationComposition.setName(getKey().getName()); automationComposition.setVersion(getKey().getVersion()); automationComposition.setCompositionId(UUID.fromString(compositionId)); @@ -171,6 +170,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative @Override public void fromAuthorative(@NonNull final AutomationComposition automationComposition) { + this.instanceId = automationComposition.getInstanceId().toString(); if (this.key == null || this.getKey().isNullKey()) { this.setKey(new PfConceptKey(automationComposition.getName(), automationComposition.getVersion())); } @@ -182,7 +182,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative this.primed = automationComposition.getPrimed(); this.elements = new LinkedHashMap<>(automationComposition.getElements().size()); - for (Entry<UUID, AutomationCompositionElement> elementEntry : automationComposition.getElements().entrySet()) { + for (var elementEntry : automationComposition.getElements().entrySet()) { var jpaAutomationCompositionElement = new JpaAutomationCompositionElement(); jpaAutomationCompositionElement .setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString())); @@ -193,9 +193,9 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative @Override public List<PfKey> getKeys() { - List<PfKey> keyList = getKey().getKeys(); + var keyList = getKey().getKeys(); - for (JpaAutomationCompositionElement element : elements.values()) { + for (var element : elements.values()) { keyList.addAll(element.getKeys()); } @@ -207,7 +207,7 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative key.clean(); description = (description == null ? null : description.trim()); - for (JpaAutomationCompositionElement element : elements.values()) { + for (var element : elements.values()) { element.clean(); } } @@ -224,8 +224,13 @@ public class JpaAutomationComposition extends PfConcept implements PfAuthorative return this.getClass().getName().compareTo(otherConcept.getClass().getName()); } - final JpaAutomationComposition other = (JpaAutomationComposition) otherConcept; - int result = key.compareTo(other.key); + final var other = (JpaAutomationComposition) otherConcept; + var result = ObjectUtils.compare(instanceId, other.instanceId); + if (result != 0) { + return result; + } + + result = key.compareTo(other.key); if (result != 0) { return result; } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java index 5024785f8..3d2813eb2 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java @@ -52,29 +52,31 @@ public class AutomationCompositionProvider { /** * Get automation composition. * - * @param automationCompositionId the ID of the automation composition to get + * @param instanceId the ID of the automation composition to get * @return the automation composition found */ @Transactional(readOnly = true) - public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) { - try { - return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative(); - } catch (EntityNotFoundException e) { - throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e); + public AutomationComposition getAutomationComposition(final UUID instanceId) { + var result = automationCompositionRepository.findByInstanceId(instanceId.toString()); + if (result.isEmpty()) { + throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found"); } + return result.get().toAuthorative(); } /** - * Find automation composition by automationCompositionId. + * Get automation composition. * - * @param name the name of the automation composition to get, null to get all automation compositions - * @param version the version of the automation composition to get, null to get all automation compositions + * @param automationCompositionId the ID of the automation composition to get * @return the automation composition found */ @Transactional(readOnly = true) - public Optional<AutomationComposition> findAutomationComposition(@NonNull final String name, - @NonNull final String version) { - return findAutomationComposition(new PfConceptKey(name, version)); + public AutomationComposition getAutomationComposition(final ToscaConceptIdentifier automationCompositionId) { + try { + return automationCompositionRepository.getById(automationCompositionId.asConceptKey()).toAuthorative(); + } catch (EntityNotFoundException e) { + throw new PfModelRuntimeException(Status.NOT_FOUND, "AutomationComposition not found", e); + } } /** @@ -94,12 +96,27 @@ public class AutomationCompositionProvider { } /** - * Save automation composition. + * Create automation composition. + * + * @param automationComposition the automation composition to create + * @return the create automation composition + */ + public AutomationComposition createAutomationComposition(final AutomationComposition automationComposition) { + automationComposition.setInstanceId(UUID.randomUUID()); + var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition, + JpaAutomationComposition::new, "automation composition")); + + // Return the saved automation composition + return result.toAuthorative(); + } + + /** + * Update automation composition. * * @param automationComposition the automation composition to update * @return the updated automation composition */ - public AutomationComposition saveAutomationComposition(final AutomationComposition automationComposition) { + public AutomationComposition updateAutomationComposition(final AutomationComposition automationComposition) { var result = automationCompositionRepository.save(ProviderUtils.getJpaAndValidate(automationComposition, JpaAutomationComposition::new, "automation composition")); @@ -136,23 +153,18 @@ public class AutomationCompositionProvider { /** * Delete a automation composition. * - * @param name the name of the automation composition to delete - * @param version the version of the automation composition to delete + * @param instanceId the ID of the automation composition to get * @return the automation composition deleted */ - public AutomationComposition deleteAutomationComposition(@NonNull final String name, - @NonNull final String version) { - - var automationCompositionKey = new PfConceptKey(name, version); - var jpaDeleteAutomationComposition = automationCompositionRepository.findById(automationCompositionKey); - + public AutomationComposition deleteAutomationComposition(@NonNull final UUID instanceId) { + var jpaDeleteAutomationComposition = automationCompositionRepository.findByInstanceId(instanceId.toString()); if (jpaDeleteAutomationComposition.isEmpty()) { - String errorMessage = "delete of automation composition \"" + automationCompositionKey.getId() + var errorMessage = "delete of automation composition \"" + instanceId + "\" failed, automation composition does not exist"; throw new PfModelRuntimeException(Response.Status.NOT_FOUND, errorMessage); } - automationCompositionRepository.deleteById(automationCompositionKey); + automationCompositionRepository.deleteById(jpaDeleteAutomationComposition.get().getKey()); return jpaDeleteAutomationComposition.get().toAuthorative(); } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java index 273b99a63..aba752667 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/repository/AutomationCompositionRepository.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.models.acm.persistence.repository; import java.util.List; +import java.util.Optional; import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition; import org.onap.policy.models.base.PfConceptKey; import org.springframework.data.jpa.repository.JpaRepository; @@ -28,7 +29,9 @@ import org.springframework.stereotype.Repository; @Repository public interface AutomationCompositionRepository - extends JpaRepository<JpaAutomationComposition, PfConceptKey>, FilterRepository { + extends JpaRepository<JpaAutomationComposition, PfConceptKey>, FilterRepository { + + Optional<JpaAutomationComposition> findByInstanceId(String instanceId); List<JpaAutomationComposition> findByCompositionId(String compositionId); } |