2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 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 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.NoArgsConstructor;
43 import lombok.NonNull;
44 import org.apache.commons.lang3.ObjectUtils;
45 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
46 import org.onap.policy.clamp.models.acm.concepts.DeployState;
47 import org.onap.policy.clamp.models.acm.concepts.LockState;
48 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
49 import org.onap.policy.clamp.models.acm.concepts.SubState;
50 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
51 import org.onap.policy.common.parameters.annotations.NotNull;
52 import org.onap.policy.common.parameters.annotations.Valid;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.Validated;
58 * Class to represent an 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)
68 public class JpaAutomationComposition extends Validated
69 implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
73 private String instanceId;
81 private String version;
85 private String compositionId;
88 private String compositionTargetId;
92 private DeployState deployState;
96 private LockState lockState;
100 private SubState subState;
103 private StateChangeResult stateChangeResult;
107 private Timestamp lastMsg;
110 private Integer phase;
113 private String description;
117 private String revisionId;
120 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
121 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
122 private List<@NotNull @Valid JpaAutomationCompositionElement> elements = new ArrayList<>();
127 * @param copyConcept the concept to copy from
129 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
130 this.instanceId = copyConcept.instanceId;
131 this.name = copyConcept.name;
132 this.version = copyConcept.version;
133 this.compositionId = copyConcept.compositionId;
134 this.compositionTargetId = copyConcept.compositionTargetId;
135 this.deployState = copyConcept.deployState;
136 this.lockState = copyConcept.lockState;
137 this.lastMsg = copyConcept.lastMsg;
138 this.phase = copyConcept.phase;
139 this.subState = copyConcept.subState;
140 this.description = copyConcept.description;
141 this.stateChangeResult = copyConcept.stateChangeResult;
142 this.revisionId = copyConcept.revisionId;
143 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
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.setInstanceId(UUID.fromString(instanceId));
160 automationComposition.setName(name);
161 automationComposition.setVersion(version);
162 automationComposition.setCompositionId(UUID.fromString(compositionId));
163 if (compositionTargetId != null) {
164 automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
166 automationComposition.setDeployState(deployState);
167 automationComposition.setLockState(lockState);
168 automationComposition.setLastMsg(lastMsg.toString());
169 automationComposition.setPhase(phase);
170 automationComposition.setSubState(subState);
171 automationComposition.setDescription(description);
172 automationComposition.setStateChangeResult(stateChangeResult);
173 automationComposition.setRevisionId(UUID.fromString(this.revisionId));
174 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
175 for (var element : this.elements) {
176 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
179 return automationComposition;
183 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
184 this.instanceId = automationComposition.getInstanceId().toString();
185 this.name = automationComposition.getName();
186 this.version = automationComposition.getVersion();
187 this.compositionId = automationComposition.getCompositionId().toString();
188 if (automationComposition.getCompositionTargetId() != null) {
189 this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
191 this.deployState = automationComposition.getDeployState();
192 this.lockState = automationComposition.getLockState();
193 this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
194 this.phase = automationComposition.getPhase();
195 this.subState = automationComposition.getSubState();
196 this.description = automationComposition.getDescription();
197 this.stateChangeResult = automationComposition.getStateChangeResult();
198 this.revisionId = automationComposition.getRevisionId().toString();
199 this.elements = new ArrayList<>(automationComposition.getElements().size());
200 for (var elementEntry : automationComposition.getElements().entrySet()) {
201 var jpaAutomationCompositionElement =
202 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
203 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
204 this.elements.add(jpaAutomationCompositionElement);
209 public int compareTo(final JpaAutomationComposition other) {
217 var result = ObjectUtils.compare(instanceId, other.instanceId);
222 result = ObjectUtils.compare(name, other.name);
227 result = lastMsg.compareTo(other.lastMsg);
232 result = ObjectUtils.compare(phase, other.phase);
237 result = ObjectUtils.compare(version, other.version);
242 result = ObjectUtils.compare(compositionId, other.compositionId);
247 result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
252 result = ObjectUtils.compare(deployState, other.deployState);
257 result = ObjectUtils.compare(lockState, other.lockState);
262 result = ObjectUtils.compare(subState, other.subState);
267 result = ObjectUtils.compare(description, other.description);
272 result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
276 result = ObjectUtils.compare(revisionId, other.revisionId);
280 return PfUtils.compareObjects(elements, other.elements);