001f2e7f3e6602dda91041c26428e2b55127399e
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
22
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;
40 import lombok.Data;
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;
56
57 /**
58  * Class to represent a automation composition in the database.
59  *
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62 @Entity
63 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaAutomationComposition extends Validated
68         implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
69
70     @Id
71     @NotNull
72     private String instanceId;
73
74     @NotNull
75     @Column
76     private String name;
77
78     @NotNull
79     @Column
80     private String version;
81
82     @Column
83     @NotNull
84     private String compositionId;
85
86     @Column
87     private String compositionTargetId;
88
89     @Column
90     @NotNull
91     private DeployState deployState;
92
93     @Column
94     @NotNull
95     private LockState lockState;
96
97     @Column
98     @NotNull
99     private SubState subState;
100
101     @Column
102     private StateChangeResult stateChangeResult;
103
104     @Column
105     @NotNull
106     private Timestamp lastMsg;
107
108     @Column
109     private Integer phase;
110
111     @Column
112     private String description;
113
114     @NotNull
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;
118
119     /**
120      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
121      */
122     public JpaAutomationComposition() {
123         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
124                 DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
125     }
126
127     /**
128      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
129      *
130      * @param instanceId The UUID of the automation composition instance
131      * @param key the key
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
137      */
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;
150     }
151
152     /**
153      * Copy constructor.
154      *
155      * @param copyConcept the concept to copy from
156      */
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);
171     }
172
173     /**
174      * Authorative constructor.
175      *
176      * @param authorativeConcept the authorative concept to copy from
177      */
178     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
179         this.fromAuthorative(authorativeConcept);
180     }
181
182     @Override
183     public AutomationComposition toAuthorative() {
184         var automationComposition = new AutomationComposition();
185
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));
192         }
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());
203         }
204
205         return automationComposition;
206     }
207
208     @Override
209     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
210         this.fromAuthorativeBase(automationComposition);
211         this.elements = new ArrayList<>(automationComposition.getElements().size());
212         for (var elementEntry : automationComposition.getElements().entrySet()) {
213             var jpaAutomationCompositionElement =
214                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
215             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
216             this.elements.add(jpaAutomationCompositionElement);
217         }
218     }
219
220     /**
221      * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
222      *
223      * @param automationComposition the authorative concept
224      */
225     public void fromAuthorativeBase(@NonNull final AutomationComposition automationComposition) {
226         this.instanceId = automationComposition.getInstanceId().toString();
227         this.name = automationComposition.getName();
228         this.version = automationComposition.getVersion();
229         this.compositionId = automationComposition.getCompositionId().toString();
230         if (automationComposition.getCompositionTargetId() != null) {
231             this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
232         }
233         this.deployState = automationComposition.getDeployState();
234         this.lockState = automationComposition.getLockState();
235         this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
236         this.phase = automationComposition.getPhase();
237         this.subState = automationComposition.getSubState();
238         this.description = automationComposition.getDescription();
239         this.stateChangeResult = automationComposition.getStateChangeResult();
240     }
241
242     @Override
243     public int compareTo(final JpaAutomationComposition other) {
244         if (other == null) {
245             return -1;
246         }
247         if (this == other) {
248             return 0;
249         }
250
251         var result = ObjectUtils.compare(instanceId, other.instanceId);
252         if (result != 0) {
253             return result;
254         }
255
256         result = ObjectUtils.compare(name, other.name);
257         if (result != 0) {
258             return result;
259         }
260
261         result = lastMsg.compareTo(other.lastMsg);
262         if (result != 0) {
263             return result;
264         }
265
266         result = ObjectUtils.compare(phase, other.phase);
267         if (result != 0) {
268             return result;
269         }
270
271         result = ObjectUtils.compare(version, other.version);
272         if (result != 0) {
273             return result;
274         }
275
276         result = ObjectUtils.compare(compositionId, other.compositionId);
277         if (result != 0) {
278             return result;
279         }
280
281         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
282         if (result != 0) {
283             return result;
284         }
285
286         result = ObjectUtils.compare(deployState, other.deployState);
287         if (result != 0) {
288             return result;
289         }
290
291         result = ObjectUtils.compare(lockState, other.lockState);
292         if (result != 0) {
293             return result;
294         }
295
296         result = ObjectUtils.compare(subState, other.subState);
297         if (result != 0) {
298             return result;
299         }
300
301         result = ObjectUtils.compare(description, other.description);
302         if (result != 0) {
303             return result;
304         }
305
306         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
307         if (result != 0) {
308             return result;
309         }
310         return PfUtils.compareObjects(elements, other.elements);
311     }
312 }