cb05eda6546f223ad2872bdfb89c3f3546385ff1
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 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.instanceId = automationComposition.getInstanceId().toString();
211         this.name = automationComposition.getName();
212         this.version = automationComposition.getVersion();
213         this.compositionId = automationComposition.getCompositionId().toString();
214         if (automationComposition.getCompositionTargetId() != null) {
215             this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
216         }
217         this.deployState = automationComposition.getDeployState();
218         this.lockState = automationComposition.getLockState();
219         this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
220         this.phase = automationComposition.getPhase();
221         this.subState = automationComposition.getSubState();
222         this.description = automationComposition.getDescription();
223         this.stateChangeResult = automationComposition.getStateChangeResult();
224         this.elements = new ArrayList<>(automationComposition.getElements().size());
225         for (var elementEntry : automationComposition.getElements().entrySet()) {
226             var jpaAutomationCompositionElement =
227                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
228             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
229             this.elements.add(jpaAutomationCompositionElement);
230         }
231     }
232
233     @Override
234     public int compareTo(final JpaAutomationComposition other) {
235         if (other == null) {
236             return -1;
237         }
238         if (this == other) {
239             return 0;
240         }
241
242         var result = ObjectUtils.compare(instanceId, other.instanceId);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(name, other.name);
248         if (result != 0) {
249             return result;
250         }
251
252         result = lastMsg.compareTo(other.lastMsg);
253         if (result != 0) {
254             return result;
255         }
256
257         result = ObjectUtils.compare(phase, other.phase);
258         if (result != 0) {
259             return result;
260         }
261
262         result = ObjectUtils.compare(version, other.version);
263         if (result != 0) {
264             return result;
265         }
266
267         result = ObjectUtils.compare(compositionId, other.compositionId);
268         if (result != 0) {
269             return result;
270         }
271
272         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
273         if (result != 0) {
274             return result;
275         }
276
277         result = ObjectUtils.compare(deployState, other.deployState);
278         if (result != 0) {
279             return result;
280         }
281
282         result = ObjectUtils.compare(lockState, other.lockState);
283         if (result != 0) {
284             return result;
285         }
286
287         result = ObjectUtils.compare(subState, other.subState);
288         if (result != 0) {
289             return result;
290         }
291
292         result = ObjectUtils.compare(description, other.description);
293         if (result != 0) {
294             return result;
295         }
296
297         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
298         if (result != 0) {
299             return result;
300         }
301         return PfUtils.compareObjects(elements, other.elements);
302     }
303 }