2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 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 jakarta.persistence.CascadeType;
24 import jakarta.persistence.Column;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.FetchType;
27 import jakarta.persistence.ForeignKey;
28 import jakarta.persistence.Id;
29 import jakarta.persistence.Index;
30 import jakarta.persistence.Inheritance;
31 import jakarta.persistence.InheritanceType;
32 import jakarta.persistence.JoinColumn;
33 import jakarta.persistence.OneToMany;
34 import jakarta.persistence.Table;
35 import java.sql.Timestamp;
36 import java.util.ArrayList;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.UUID;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.apache.commons.lang3.ObjectUtils;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
45 import org.onap.policy.clamp.models.acm.concepts.DeployState;
46 import org.onap.policy.clamp.models.acm.concepts.LockState;
47 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
48 import org.onap.policy.clamp.models.acm.concepts.SubState;
49 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
50 import org.onap.policy.common.parameters.annotations.NotNull;
51 import org.onap.policy.common.parameters.annotations.Valid;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfConceptKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.Validated;
58 * Class to represent a automation composition in the database.
60 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaAutomationComposition extends Validated
68 implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
72 private String instanceId;
80 private String version;
84 private String compositionId;
87 private String compositionTargetId;
91 private DeployState deployState;
95 private LockState lockState;
99 private SubState subState;
102 private StateChangeResult stateChangeResult;
106 private Timestamp lastMsg;
109 private Integer phase;
112 private String description;
115 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
116 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
117 private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
120 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
122 public JpaAutomationComposition() {
123 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
124 DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
128 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
130 * @param instanceId The UUID of the automation composition instance
132 * @param compositionId the TOSCA compositionId of the automation composition definition
133 * @param elements the elements of the automation composition in participants
134 * @param deployState the Deploy State
135 * @param lockState the Lock State
136 * @param subState the Sub State
138 public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
139 @NonNull final String compositionId, @NonNull final List<JpaAutomationCompositionElement> elements,
140 @NonNull final DeployState deployState, @NonNull final LockState lockState,
141 @NonNull final SubState subState) {
142 this.instanceId = instanceId;
143 this.name = key.getName();
144 this.version = key.getVersion();
145 this.compositionId = compositionId;
146 this.deployState = deployState;
147 this.lockState = lockState;
148 this.elements = elements;
149 this.subState = subState;
155 * @param copyConcept the concept to copy from
157 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
158 this.instanceId = copyConcept.instanceId;
159 this.name = copyConcept.name;
160 this.version = copyConcept.version;
161 this.compositionId = copyConcept.compositionId;
162 this.compositionTargetId = copyConcept.compositionTargetId;
163 this.deployState = copyConcept.deployState;
164 this.lockState = copyConcept.lockState;
165 this.lastMsg = copyConcept.lastMsg;
166 this.phase = copyConcept.phase;
167 this.subState = copyConcept.subState;
168 this.description = copyConcept.description;
169 this.stateChangeResult = copyConcept.stateChangeResult;
170 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
174 * Authorative constructor.
176 * @param authorativeConcept the authorative concept to copy from
178 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
179 this.fromAuthorative(authorativeConcept);
183 public AutomationComposition toAuthorative() {
184 var automationComposition = new AutomationComposition();
186 automationComposition.setInstanceId(UUID.fromString(instanceId));
187 automationComposition.setName(name);
188 automationComposition.setVersion(version);
189 automationComposition.setCompositionId(UUID.fromString(compositionId));
190 if (compositionTargetId != null) {
191 automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
193 automationComposition.setDeployState(deployState);
194 automationComposition.setLockState(lockState);
195 automationComposition.setLastMsg(lastMsg.toString());
196 automationComposition.setPhase(phase);
197 automationComposition.setSubState(subState);
198 automationComposition.setDescription(description);
199 automationComposition.setStateChangeResult(stateChangeResult);
200 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
201 for (var element : this.elements) {
202 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
205 return automationComposition;
209 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
210 this.instanceId = automationComposition.getInstanceId().toString();
211 this.name = automationComposition.getName();
212 this.version = automationComposition.getVersion();
213 this.compositionId = automationComposition.getCompositionId().toString();
214 if (automationComposition.getCompositionTargetId() != null) {
215 this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
217 this.deployState = automationComposition.getDeployState();
218 this.lockState = automationComposition.getLockState();
219 this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
220 this.phase = automationComposition.getPhase();
221 this.subState = automationComposition.getSubState();
222 this.description = automationComposition.getDescription();
223 this.stateChangeResult = automationComposition.getStateChangeResult();
224 this.elements = new ArrayList<>(automationComposition.getElements().size());
225 for (var elementEntry : automationComposition.getElements().entrySet()) {
226 var jpaAutomationCompositionElement =
227 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
228 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
229 this.elements.add(jpaAutomationCompositionElement);
234 public int compareTo(final JpaAutomationComposition other) {
242 var result = ObjectUtils.compare(instanceId, other.instanceId);
247 result = ObjectUtils.compare(name, other.name);
252 result = lastMsg.compareTo(other.lastMsg);
257 result = ObjectUtils.compare(phase, other.phase);
262 result = ObjectUtils.compare(version, other.version);
267 result = ObjectUtils.compare(compositionId, other.compositionId);
272 result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
277 result = ObjectUtils.compare(deployState, other.deployState);
282 result = ObjectUtils.compare(lockState, other.lockState);
287 result = ObjectUtils.compare(subState, other.subState);
292 result = ObjectUtils.compare(description, other.description);
297 result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
301 return PfUtils.compareObjects(elements, other.elements);