2  * ============LICENSE_START=======================================================
 
   3  * Copyright (C) 2021-2022 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.clamp.models.acm.persistence.concepts;
 
  23 import java.util.LinkedHashMap;
 
  24 import java.util.List;
 
  26 import java.util.UUID;
 
  27 import javax.persistence.CascadeType;
 
  28 import javax.persistence.Column;
 
  29 import javax.persistence.EmbeddedId;
 
  30 import javax.persistence.Entity;
 
  31 import javax.persistence.FetchType;
 
  32 import javax.persistence.Index;
 
  33 import javax.persistence.Inheritance;
 
  34 import javax.persistence.InheritanceType;
 
  35 import javax.persistence.ManyToMany;
 
  36 import javax.persistence.Table;
 
  38 import lombok.EqualsAndHashCode;
 
  39 import lombok.NonNull;
 
  40 import org.apache.commons.lang3.ObjectUtils;
 
  41 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 
  42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
 
  43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
 
  44 import org.onap.policy.common.parameters.annotations.NotNull;
 
  45 import org.onap.policy.common.parameters.annotations.Valid;
 
  46 import org.onap.policy.models.base.PfAuthorative;
 
  47 import org.onap.policy.models.base.PfConcept;
 
  48 import org.onap.policy.models.base.PfConceptKey;
 
  49 import org.onap.policy.models.base.PfKey;
 
  50 import org.onap.policy.models.base.PfReferenceKey;
 
  51 import org.onap.policy.models.base.PfUtils;
 
  52 import org.onap.policy.models.base.validation.annotations.VerifyKey;
 
  55  * Class to represent a automation composition in the database.
 
  57  * @author Liam Fallon (liam.fallon@est.tech)
 
  60 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
 
  61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 
  63 @EqualsAndHashCode(callSuper = false)
 
  64 public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> {
 
  65     private static final long serialVersionUID = -4725410933242154805L;
 
  69     private String instanceId;
 
  74     private PfConceptKey key;
 
  78     private String compositionId;
 
  82     private AutomationCompositionState state;
 
  86     private AutomationCompositionOrderedState orderedState;
 
  89     private String description;
 
  92     private Boolean primed;
 
  94     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 
  96     private Map<@NotNull UUID, @NotNull @Valid JpaAutomationCompositionElement> elements;
 
 100      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
 
 102     public JpaAutomationComposition() {
 
 103         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
 
 104                 AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
 
 108      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
 
 110      * @param instanceId The UUID of the automation composition instance
 
 112      * @param compositionId the TOSCA compositionId of the automation composition definition
 
 113      * @param state the state of the automation composition
 
 114      * @param elements the elements of the automation composition in participants
 
 116     public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
 
 117             @NonNull final String compositionId, @NonNull final AutomationCompositionState state,
 
 118             @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
 
 119         this.instanceId = instanceId;
 
 121         this.compositionId = compositionId;
 
 123         this.elements = elements;
 
 129      * @param copyConcept the concept to copy from
 
 131     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
 
 133         this.instanceId = copyConcept.instanceId;
 
 134         this.key = new PfConceptKey(copyConcept.key);
 
 135         this.compositionId = copyConcept.compositionId;
 
 136         this.state = copyConcept.state;
 
 137         this.orderedState = copyConcept.orderedState;
 
 138         this.description = copyConcept.description;
 
 140                 PfUtils.mapMap(copyConcept.elements, JpaAutomationCompositionElement::new, new LinkedHashMap<>(0));
 
 141         this.primed = copyConcept.primed;
 
 145      * Authorative constructor.
 
 147      * @param authorativeConcept the authorative concept to copy from
 
 149     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
 
 150         this.fromAuthorative(authorativeConcept);
 
 154     public AutomationComposition toAuthorative() {
 
 155         var automationComposition = new AutomationComposition();
 
 157         automationComposition.setInstanceId(UUID.fromString(instanceId));
 
 158         automationComposition.setName(getKey().getName());
 
 159         automationComposition.setVersion(getKey().getVersion());
 
 160         automationComposition.setCompositionId(UUID.fromString(compositionId));
 
 161         automationComposition.setState(state);
 
 162         automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
 
 163         automationComposition.setDescription(description);
 
 164         automationComposition.setElements(
 
 165                 PfUtils.mapMap(elements, JpaAutomationCompositionElement::toAuthorative, new LinkedHashMap<>(0)));
 
 166         automationComposition.setPrimed(primed);
 
 168         return automationComposition;
 
 172     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
 
 173         this.instanceId = automationComposition.getInstanceId().toString();
 
 174         if (this.key == null || this.getKey().isNullKey()) {
 
 175             this.setKey(new PfConceptKey(automationComposition.getName(), automationComposition.getVersion()));
 
 178         this.compositionId = automationComposition.getCompositionId().toString();
 
 179         this.state = automationComposition.getState();
 
 180         this.orderedState = automationComposition.getOrderedState();
 
 181         this.description = automationComposition.getDescription();
 
 182         this.primed = automationComposition.getPrimed();
 
 184         this.elements = new LinkedHashMap<>(automationComposition.getElements().size());
 
 185         for (var elementEntry : automationComposition.getElements().entrySet()) {
 
 186             var jpaAutomationCompositionElement = new JpaAutomationCompositionElement();
 
 187             jpaAutomationCompositionElement
 
 188                     .setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
 
 189             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
 
 190             this.elements.put(elementEntry.getKey(), jpaAutomationCompositionElement);
 
 195     public List<PfKey> getKeys() {
 
 196         var keyList = getKey().getKeys();
 
 198         for (var element : elements.values()) {
 
 199             keyList.addAll(element.getKeys());
 
 206     public void clean() {
 
 208         description = (description == null ? null : description.trim());
 
 210         for (var element : elements.values()) {
 
 216     public int compareTo(final PfConcept otherConcept) {
 
 217         if (otherConcept == null) {
 
 220         if (this == otherConcept) {
 
 223         if (getClass() != otherConcept.getClass()) {
 
 224             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
 
 227         final var other = (JpaAutomationComposition) otherConcept;
 
 228         var result = ObjectUtils.compare(instanceId, other.instanceId);
 
 233         result = key.compareTo(other.key);
 
 238         result = ObjectUtils.compare(compositionId, other.compositionId);
 
 243         result = ObjectUtils.compare(state, other.state);
 
 248         result = ObjectUtils.compare(orderedState, other.orderedState);
 
 253         result = ObjectUtils.compare(description, other.description);
 
 258         result = ObjectUtils.compare(primed, other.primed);
 
 262         return PfUtils.compareObjects(elements, other.elements);