355fcaff08da164d8e99239114e5ad4fee0a0c6a
[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.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;
55
56 /**
57  * Class to represent a automation composition in the database.
58  *
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61 @Entity
62 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaAutomationComposition extends Validated
67         implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
68
69     @Id
70     @NotNull
71     private String instanceId;
72
73     @NotNull
74     @Column
75     private String name;
76
77     @NotNull
78     @Column
79     private String version;
80
81     @Column
82     @NotNull
83     private String compositionId;
84
85     @Column
86     private String compositionTargetId;
87
88     @Column
89     private Boolean restarting;
90
91     @Column
92     @NotNull
93     private DeployState deployState;
94
95     @Column
96     @NotNull
97     private LockState lockState;
98
99     @Column
100     private StateChangeResult stateChangeResult;
101
102     @Column
103     @NotNull
104     private Timestamp lastMsg;
105
106     @Column
107     private Integer phase;
108
109     @Column
110     private String description;
111
112     @NotNull
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;
116
117     /**
118      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
119      */
120     public JpaAutomationComposition() {
121         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
122                 DeployState.UNDEPLOYED, LockState.NONE);
123     }
124
125     /**
126      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
127      *
128      * @param instanceId The UUID of the automation composition instance
129      * @param key the key
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
134      */
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;
145     }
146
147     /**
148      * Copy constructor.
149      *
150      * @param copyConcept the concept to copy from
151      */
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);
166     }
167
168     /**
169      * Authorative constructor.
170      *
171      * @param authorativeConcept the authorative concept to copy from
172      */
173     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
174         this.fromAuthorative(authorativeConcept);
175     }
176
177     @Override
178     public AutomationComposition toAuthorative() {
179         var automationComposition = new AutomationComposition();
180
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));
187         }
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());
198         }
199
200         return automationComposition;
201     }
202
203     @Override
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);
212         }
213     }
214
215     /**
216      * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
217      *
218      * @param automationComposition the authorative concept
219      */
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();
227         }
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();
235     }
236
237     @Override
238     public int compareTo(final JpaAutomationComposition other) {
239         if (other == null) {
240             return -1;
241         }
242         if (this == other) {
243             return 0;
244         }
245
246         var result = ObjectUtils.compare(instanceId, other.instanceId);
247         if (result != 0) {
248             return result;
249         }
250
251         result = ObjectUtils.compare(name, other.name);
252         if (result != 0) {
253             return result;
254         }
255
256         result = lastMsg.compareTo(other.lastMsg);
257         if (result != 0) {
258             return result;
259         }
260
261         result = ObjectUtils.compare(phase, other.phase);
262         if (result != 0) {
263             return result;
264         }
265
266         result = ObjectUtils.compare(version, other.version);
267         if (result != 0) {
268             return result;
269         }
270
271         result = ObjectUtils.compare(compositionId, other.compositionId);
272         if (result != 0) {
273             return result;
274         }
275
276         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
277         if (result != 0) {
278             return result;
279         }
280
281         result = ObjectUtils.compare(restarting, other.restarting);
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(description, other.description);
297         if (result != 0) {
298             return result;
299         }
300
301         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
302         if (result != 0) {
303             return result;
304         }
305         return PfUtils.compareObjects(elements, other.elements);
306     }
307 }