1f4768ea8b8ae9b403a61058ba1380624527d012
[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     private Boolean restarting;
91
92     @Column
93     @NotNull
94     private DeployState deployState;
95
96     @Column
97     @NotNull
98     private LockState lockState;
99
100     @Column
101     @NotNull
102     private SubState subState;
103
104     @Column
105     private StateChangeResult stateChangeResult;
106
107     @Column
108     @NotNull
109     private Timestamp lastMsg;
110
111     @Column
112     private Integer phase;
113
114     @Column
115     private String description;
116
117     @NotNull
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;
121
122     /**
123      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
124      */
125     public JpaAutomationComposition() {
126         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
127                 DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
128     }
129
130     /**
131      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
132      *
133      * @param instanceId The UUID of the automation composition instance
134      * @param key the key
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
140      */
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;
153     }
154
155     /**
156      * Copy constructor.
157      *
158      * @param copyConcept the concept to copy from
159      */
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);
175     }
176
177     /**
178      * Authorative constructor.
179      *
180      * @param authorativeConcept the authorative concept to copy from
181      */
182     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
183         this.fromAuthorative(authorativeConcept);
184     }
185
186     @Override
187     public AutomationComposition toAuthorative() {
188         var automationComposition = new AutomationComposition();
189
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));
196         }
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());
208         }
209
210         return automationComposition;
211     }
212
213     @Override
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);
222         }
223     }
224
225     /**
226      * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
227      *
228      * @param automationComposition the authorative concept
229      */
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();
237         }
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();
246     }
247
248     @Override
249     public int compareTo(final JpaAutomationComposition other) {
250         if (other == null) {
251             return -1;
252         }
253         if (this == other) {
254             return 0;
255         }
256
257         var result = ObjectUtils.compare(instanceId, other.instanceId);
258         if (result != 0) {
259             return result;
260         }
261
262         result = ObjectUtils.compare(name, other.name);
263         if (result != 0) {
264             return result;
265         }
266
267         result = lastMsg.compareTo(other.lastMsg);
268         if (result != 0) {
269             return result;
270         }
271
272         result = ObjectUtils.compare(phase, other.phase);
273         if (result != 0) {
274             return result;
275         }
276
277         result = ObjectUtils.compare(version, other.version);
278         if (result != 0) {
279             return result;
280         }
281
282         result = ObjectUtils.compare(compositionId, other.compositionId);
283         if (result != 0) {
284             return result;
285         }
286
287         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
288         if (result != 0) {
289             return result;
290         }
291
292         result = ObjectUtils.compare(restarting, other.restarting);
293         if (result != 0) {
294             return result;
295         }
296
297         result = ObjectUtils.compare(deployState, other.deployState);
298         if (result != 0) {
299             return result;
300         }
301
302         result = ObjectUtils.compare(lockState, other.lockState);
303         if (result != 0) {
304             return result;
305         }
306
307         result = ObjectUtils.compare(subState, other.subState);
308         if (result != 0) {
309             return result;
310         }
311
312         result = ObjectUtils.compare(description, other.description);
313         if (result != 0) {
314             return result;
315         }
316
317         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
318         if (result != 0) {
319             return result;
320         }
321         return PfUtils.compareObjects(elements, other.elements);
322     }
323 }