2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2026 OpenInfra Foundation Europe. All rights reserved.
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 jakarta.validation.Valid;
36 import jakarta.validation.constraints.NotNull;
37 import java.sql.Timestamp;
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.UUID;
43 import lombok.EqualsAndHashCode;
44 import lombok.NoArgsConstructor;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
48 import org.onap.policy.clamp.models.acm.concepts.DeployState;
49 import org.onap.policy.clamp.models.acm.concepts.LockState;
50 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
51 import org.onap.policy.clamp.models.acm.concepts.SubState;
52 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfUtils;
57 * Class to represent an 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)
67 public class JpaAutomationComposition
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;
116 private String revisionId;
119 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
120 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
121 private List<@NotNull @Valid JpaAutomationCompositionElement> elements = new ArrayList<>();
126 * @param copyConcept the concept to copy from
128 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
129 this.instanceId = copyConcept.instanceId;
130 this.name = copyConcept.name;
131 this.version = copyConcept.version;
132 this.compositionId = copyConcept.compositionId;
133 this.compositionTargetId = copyConcept.compositionTargetId;
134 this.deployState = copyConcept.deployState;
135 this.lockState = copyConcept.lockState;
136 this.lastMsg = copyConcept.lastMsg;
137 this.phase = copyConcept.phase;
138 this.subState = copyConcept.subState;
139 this.description = copyConcept.description;
140 this.stateChangeResult = copyConcept.stateChangeResult;
141 this.revisionId = copyConcept.revisionId;
142 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
146 * Authorative constructor.
148 * @param authorativeConcept the authorative concept to copy from
150 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
151 this.fromAuthorative(authorativeConcept);
155 public AutomationComposition toAuthorative() {
156 var automationComposition = new AutomationComposition();
158 automationComposition.setInstanceId(UUID.fromString(instanceId));
159 automationComposition.setName(name);
160 automationComposition.setVersion(version);
161 automationComposition.setCompositionId(UUID.fromString(compositionId));
162 if (compositionTargetId != null) {
163 automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
165 automationComposition.setDeployState(deployState);
166 automationComposition.setLockState(lockState);
167 automationComposition.setLastMsg(lastMsg.toString());
168 automationComposition.setPhase(phase);
169 automationComposition.setSubState(subState);
170 automationComposition.setDescription(description);
171 automationComposition.setStateChangeResult(stateChangeResult);
172 automationComposition.setRevisionId(UUID.fromString(this.revisionId));
173 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
174 for (var element : this.elements) {
175 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
178 return automationComposition;
182 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
183 this.instanceId = automationComposition.getInstanceId().toString();
184 this.name = automationComposition.getName();
185 this.version = automationComposition.getVersion();
186 this.compositionId = automationComposition.getCompositionId().toString();
187 if (automationComposition.getCompositionTargetId() != null) {
188 this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
190 this.deployState = automationComposition.getDeployState();
191 this.lockState = automationComposition.getLockState();
192 this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
193 this.phase = automationComposition.getPhase();
194 this.subState = automationComposition.getSubState();
195 this.description = automationComposition.getDescription();
196 this.stateChangeResult = automationComposition.getStateChangeResult();
197 this.revisionId = automationComposition.getRevisionId().toString();
198 this.elements = new ArrayList<>(automationComposition.getElements().size());
199 for (var elementEntry : automationComposition.getElements().entrySet()) {
200 var jpaAutomationCompositionElement =
201 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
202 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
203 this.elements.add(jpaAutomationCompositionElement);
208 public int compareTo(final JpaAutomationComposition other) {
216 var result = ObjectUtils.compare(instanceId, other.instanceId);
221 result = ObjectUtils.compare(name, other.name);
226 result = lastMsg.compareTo(other.lastMsg);
231 result = ObjectUtils.compare(phase, other.phase);
236 result = ObjectUtils.compare(version, other.version);
241 result = ObjectUtils.compare(compositionId, other.compositionId);
246 result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
251 result = ObjectUtils.compare(deployState, other.deployState);
256 result = ObjectUtils.compare(lockState, other.lockState);
261 result = ObjectUtils.compare(subState, other.subState);
266 result = ObjectUtils.compare(description, other.description);
271 result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
275 result = ObjectUtils.compare(revisionId, other.revisionId);
279 return PfUtils.compareObjects(elements, other.elements);