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.Map.Entry;
27 import java.util.UUID;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.Index;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
45 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
46 import org.onap.policy.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Valid;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.validation.annotations.VerifyKey;
57 * Class to represent a automation composition in the database.
59 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> {
67 private static final long serialVersionUID = -4725410933242154805L;
72 private PfConceptKey key;
75 private String compositionId;
79 private AutomationCompositionState state;
83 private AutomationCompositionOrderedState orderedState;
86 private String description;
89 private Boolean primed;
91 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
93 private Map<@NotNull UUID, @NotNull @Valid JpaAutomationCompositionElement> elements;
97 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
99 public JpaAutomationComposition() {
100 this(new PfConceptKey());
104 * The Key Constructor creates a {@link JpaAutomationComposition} object with the given concept key.
108 public JpaAutomationComposition(@NonNull final PfConceptKey key) {
109 this(key, UUID.randomUUID().toString(), AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
113 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
116 * @param compositionId the TOSCA compositionId of the automation composition definition
117 * @param state the state of the automation composition
118 * @param elements the elements of the automation composition in participants
120 public JpaAutomationComposition(@NonNull final PfConceptKey key, @NonNull final String compositionId,
121 @NonNull final AutomationCompositionState state,
122 @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
124 this.compositionId = compositionId;
126 this.elements = elements;
132 * @param copyConcept the concept to copy from
134 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
136 this.key = new PfConceptKey(copyConcept.key);
137 this.compositionId = copyConcept.compositionId;
138 this.state = copyConcept.state;
139 this.orderedState = copyConcept.orderedState;
140 this.description = copyConcept.description;
142 PfUtils.mapMap(copyConcept.elements, JpaAutomationCompositionElement::new, new LinkedHashMap<>(0));
143 this.primed = copyConcept.primed;
147 * Authorative constructor.
149 * @param authorativeConcept the authorative concept to copy from
151 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
152 this.fromAuthorative(authorativeConcept);
156 public AutomationComposition toAuthorative() {
157 var automationComposition = new AutomationComposition();
159 automationComposition.setName(getKey().getName());
160 automationComposition.setVersion(getKey().getVersion());
161 automationComposition.setCompositionId(UUID.fromString(compositionId));
162 automationComposition.setState(state);
163 automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
164 automationComposition.setDescription(description);
165 automationComposition.setElements(
166 PfUtils.mapMap(elements, JpaAutomationCompositionElement::toAuthorative, new LinkedHashMap<>(0)));
167 automationComposition.setPrimed(primed);
169 return automationComposition;
173 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
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 (Entry<UUID, AutomationCompositionElement> 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 List<PfKey> keyList = getKey().getKeys();
198 for (JpaAutomationCompositionElement element : elements.values()) {
199 keyList.addAll(element.getKeys());
206 public void clean() {
208 description = (description == null ? null : description.trim());
210 for (JpaAutomationCompositionElement 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 JpaAutomationComposition other = (JpaAutomationComposition) otherConcept;
228 int result = key.compareTo(other.key);
233 result = ObjectUtils.compare(compositionId, other.compositionId);
238 result = ObjectUtils.compare(state, other.state);
243 result = ObjectUtils.compare(orderedState, other.orderedState);
248 result = ObjectUtils.compare(description, other.description);
253 result = ObjectUtils.compare(primed, other.primed);
257 return PfUtils.compareObjects(elements, other.elements);