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.utils.TimestampHelper;
49 import org.onap.policy.common.parameters.annotations.NotNull;
50 import org.onap.policy.common.parameters.annotations.Valid;
51 import org.onap.policy.models.base.PfAuthorative;
52 import org.onap.policy.models.base.PfConceptKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.Validated;
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 Validated
67 implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
71 private String instanceId;
79 private String version;
83 private String compositionId;
86 private String compositionTargetId;
89 private Boolean restarting;
93 private DeployState deployState;
97 private LockState lockState;
100 private StateChangeResult stateChangeResult;
104 private Timestamp lastMsg;
107 private Integer phase;
110 private String description;
113 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
114 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
115 private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
118 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
120 public JpaAutomationComposition() {
121 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
122 DeployState.UNDEPLOYED, LockState.NONE);
126 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
128 * @param instanceId The UUID of the automation composition instance
130 * @param compositionId the TOSCA compositionId of the automation composition definition
131 * @param elements the elements of the automation composition in participants
132 * @param deployState the Deploy State
133 * @param lockState the Lock State
135 public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
136 @NonNull final String compositionId, @NonNull final List<JpaAutomationCompositionElement> elements,
137 @NonNull final DeployState deployState, @NonNull final LockState lockState) {
138 this.instanceId = instanceId;
139 this.name = key.getName();
140 this.version = key.getVersion();
141 this.compositionId = compositionId;
142 this.deployState = deployState;
143 this.lockState = lockState;
144 this.elements = elements;
150 * @param copyConcept the concept to copy from
152 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
153 this.instanceId = copyConcept.instanceId;
154 this.name = copyConcept.name;
155 this.version = copyConcept.version;
156 this.compositionId = copyConcept.compositionId;
157 this.compositionTargetId = copyConcept.compositionTargetId;
158 this.restarting = copyConcept.restarting;
159 this.deployState = copyConcept.deployState;
160 this.lockState = copyConcept.lockState;
161 this.lastMsg = copyConcept.lastMsg;
162 this.phase = copyConcept.phase;
163 this.description = copyConcept.description;
164 this.stateChangeResult = copyConcept.stateChangeResult;
165 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
169 * Authorative constructor.
171 * @param authorativeConcept the authorative concept to copy from
173 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
174 this.fromAuthorative(authorativeConcept);
178 public AutomationComposition toAuthorative() {
179 var automationComposition = new AutomationComposition();
181 automationComposition.setInstanceId(UUID.fromString(instanceId));
182 automationComposition.setName(name);
183 automationComposition.setVersion(version);
184 automationComposition.setCompositionId(UUID.fromString(compositionId));
185 if (compositionTargetId != null) {
186 automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
188 automationComposition.setRestarting(restarting);
189 automationComposition.setDeployState(deployState);
190 automationComposition.setLockState(lockState);
191 automationComposition.setLastMsg(lastMsg.toString());
192 automationComposition.setPhase(phase);
193 automationComposition.setDescription(description);
194 automationComposition.setStateChangeResult(stateChangeResult);
195 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
196 for (var element : this.elements) {
197 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
200 return automationComposition;
204 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
205 this.fromAuthorativeBase(automationComposition);
206 this.elements = new ArrayList<>(automationComposition.getElements().size());
207 for (var elementEntry : automationComposition.getElements().entrySet()) {
208 var jpaAutomationCompositionElement =
209 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
210 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
211 this.elements.add(jpaAutomationCompositionElement);
216 * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
218 * @param automationComposition the authorative concept
220 public void fromAuthorativeBase(@NonNull final AutomationComposition automationComposition) {
221 this.instanceId = automationComposition.getInstanceId().toString();
222 this.name = automationComposition.getName();
223 this.version = automationComposition.getVersion();
224 this.compositionId = automationComposition.getCompositionId().toString();
225 if (automationComposition.getCompositionTargetId() != null) {
226 this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
228 this.restarting = automationComposition.getRestarting();
229 this.deployState = automationComposition.getDeployState();
230 this.lockState = automationComposition.getLockState();
231 this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
232 this.phase = automationComposition.getPhase();
233 this.description = automationComposition.getDescription();
234 this.stateChangeResult = automationComposition.getStateChangeResult();
238 public int compareTo(final JpaAutomationComposition other) {
246 var result = ObjectUtils.compare(instanceId, other.instanceId);
251 result = ObjectUtils.compare(name, other.name);
256 result = lastMsg.compareTo(other.lastMsg);
261 result = ObjectUtils.compare(phase, other.phase);
266 result = ObjectUtils.compare(version, other.version);
271 result = ObjectUtils.compare(compositionId, other.compositionId);
276 result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
281 result = ObjectUtils.compare(restarting, other.restarting);
286 result = ObjectUtils.compare(deployState, other.deployState);
291 result = ObjectUtils.compare(lockState, other.lockState);
296 result = ObjectUtils.compare(description, other.description);
301 result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
305 return PfUtils.compareObjects(elements, other.elements);