2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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;
90 private Boolean restarting;
94 private DeployState deployState;
98 private LockState lockState;
102 private SubState subState;
105 private StateChangeResult stateChangeResult;
109 private Timestamp lastMsg;
112 private Integer phase;
115 private String description;
118 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
119 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
120 private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
123 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
125 public JpaAutomationComposition() {
126 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
127 DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
131 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
133 * @param instanceId The UUID of the automation composition instance
135 * @param compositionId the TOSCA compositionId of the automation composition definition
136 * @param elements the elements of the automation composition in participants
137 * @param deployState the Deploy State
138 * @param lockState the Lock State
139 * @param subState the Sub State
141 public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
142 @NonNull final String compositionId, @NonNull final List<JpaAutomationCompositionElement> elements,
143 @NonNull final DeployState deployState, @NonNull final LockState lockState,
144 @NonNull final SubState subState) {
145 this.instanceId = instanceId;
146 this.name = key.getName();
147 this.version = key.getVersion();
148 this.compositionId = compositionId;
149 this.deployState = deployState;
150 this.lockState = lockState;
151 this.elements = elements;
152 this.subState = subState;
158 * @param copyConcept the concept to copy from
160 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
161 this.instanceId = copyConcept.instanceId;
162 this.name = copyConcept.name;
163 this.version = copyConcept.version;
164 this.compositionId = copyConcept.compositionId;
165 this.compositionTargetId = copyConcept.compositionTargetId;
166 this.restarting = copyConcept.restarting;
167 this.deployState = copyConcept.deployState;
168 this.lockState = copyConcept.lockState;
169 this.lastMsg = copyConcept.lastMsg;
170 this.phase = copyConcept.phase;
171 this.subState = copyConcept.subState;
172 this.description = copyConcept.description;
173 this.stateChangeResult = copyConcept.stateChangeResult;
174 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
178 * Authorative constructor.
180 * @param authorativeConcept the authorative concept to copy from
182 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
183 this.fromAuthorative(authorativeConcept);
187 public AutomationComposition toAuthorative() {
188 var automationComposition = new AutomationComposition();
190 automationComposition.setInstanceId(UUID.fromString(instanceId));
191 automationComposition.setName(name);
192 automationComposition.setVersion(version);
193 automationComposition.setCompositionId(UUID.fromString(compositionId));
194 if (compositionTargetId != null) {
195 automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
197 automationComposition.setRestarting(restarting);
198 automationComposition.setDeployState(deployState);
199 automationComposition.setLockState(lockState);
200 automationComposition.setLastMsg(lastMsg.toString());
201 automationComposition.setPhase(phase);
202 automationComposition.setSubState(subState);
203 automationComposition.setDescription(description);
204 automationComposition.setStateChangeResult(stateChangeResult);
205 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
206 for (var element : this.elements) {
207 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
210 return automationComposition;
214 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
215 this.fromAuthorativeBase(automationComposition);
216 this.elements = new ArrayList<>(automationComposition.getElements().size());
217 for (var elementEntry : automationComposition.getElements().entrySet()) {
218 var jpaAutomationCompositionElement =
219 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
220 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
221 this.elements.add(jpaAutomationCompositionElement);
226 * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
228 * @param automationComposition the authorative concept
230 public void fromAuthorativeBase(@NonNull final AutomationComposition automationComposition) {
231 this.instanceId = automationComposition.getInstanceId().toString();
232 this.name = automationComposition.getName();
233 this.version = automationComposition.getVersion();
234 this.compositionId = automationComposition.getCompositionId().toString();
235 if (automationComposition.getCompositionTargetId() != null) {
236 this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
238 this.restarting = automationComposition.getRestarting();
239 this.deployState = automationComposition.getDeployState();
240 this.lockState = automationComposition.getLockState();
241 this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
242 this.phase = automationComposition.getPhase();
243 this.subState = automationComposition.getSubState();
244 this.description = automationComposition.getDescription();
245 this.stateChangeResult = automationComposition.getStateChangeResult();
249 public int compareTo(final JpaAutomationComposition other) {
257 var result = ObjectUtils.compare(instanceId, other.instanceId);
262 result = ObjectUtils.compare(name, other.name);
267 result = lastMsg.compareTo(other.lastMsg);
272 result = ObjectUtils.compare(phase, other.phase);
277 result = ObjectUtils.compare(version, other.version);
282 result = ObjectUtils.compare(compositionId, other.compositionId);
287 result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
292 result = ObjectUtils.compare(restarting, other.restarting);
297 result = ObjectUtils.compare(deployState, other.deployState);
302 result = ObjectUtils.compare(lockState, other.lockState);
307 result = ObjectUtils.compare(subState, other.subState);
312 result = ObjectUtils.compare(description, other.description);
317 result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
321 return PfUtils.compareObjects(elements, other.elements);