5006b5a8bf0124073a583c65ddeffa8ae90cd0b3
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2026 OpenInfra Foundation Europe. All rights reserved.
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 jakarta.validation.Valid;
36 import jakarta.validation.constraints.NotNull;
37 import java.sql.Timestamp;
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.UUID;
42 import lombok.Data;
43 import lombok.EqualsAndHashCode;
44 import lombok.NoArgsConstructor;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
48 import org.onap.policy.clamp.models.acm.concepts.DeployState;
49 import org.onap.policy.clamp.models.acm.concepts.LockState;
50 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
51 import org.onap.policy.clamp.models.acm.concepts.SubState;
52 import org.onap.policy.clamp.models.acm.utils.TimestampHelper;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.Validated;
56
57 /**
58  * Class to represent an 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 @NoArgsConstructor
68 public class JpaAutomationComposition extends Validated
69         implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
70
71     @Id
72     @NotNull
73     private String instanceId;
74
75     @NotNull
76     @Column
77     private String name;
78
79     @NotNull
80     @Column
81     private String version;
82
83     @Column
84     @NotNull
85     private String compositionId;
86
87     @Column
88     private String compositionTargetId;
89
90     @Column
91     @NotNull
92     private DeployState deployState;
93
94     @Column
95     @NotNull
96     private LockState lockState;
97
98     @Column
99     @NotNull
100     private SubState subState;
101
102     @Column
103     private StateChangeResult stateChangeResult;
104
105     @Column
106     @NotNull
107     private Timestamp lastMsg;
108
109     @Column
110     private Integer phase;
111
112     @Column
113     private String description;
114
115     @Column
116     @NotNull
117     private String revisionId;
118
119     @NotNull
120     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
121     @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
122     private List<@NotNull @Valid JpaAutomationCompositionElement> elements = new ArrayList<>();
123
124     /**
125      * Copy constructor.
126      *
127      * @param copyConcept the concept to copy from
128      */
129     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
130         this.instanceId = copyConcept.instanceId;
131         this.name = copyConcept.name;
132         this.version = copyConcept.version;
133         this.compositionId = copyConcept.compositionId;
134         this.compositionTargetId = copyConcept.compositionTargetId;
135         this.deployState = copyConcept.deployState;
136         this.lockState = copyConcept.lockState;
137         this.lastMsg = copyConcept.lastMsg;
138         this.phase = copyConcept.phase;
139         this.subState = copyConcept.subState;
140         this.description = copyConcept.description;
141         this.stateChangeResult = copyConcept.stateChangeResult;
142         this.revisionId = copyConcept.revisionId;
143         this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
144     }
145
146     /**
147      * Authorative constructor.
148      *
149      * @param authorativeConcept the authorative concept to copy from
150      */
151     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
152         this.fromAuthorative(authorativeConcept);
153     }
154
155     @Override
156     public AutomationComposition toAuthorative() {
157         var automationComposition = new AutomationComposition();
158
159         automationComposition.setInstanceId(UUID.fromString(instanceId));
160         automationComposition.setName(name);
161         automationComposition.setVersion(version);
162         automationComposition.setCompositionId(UUID.fromString(compositionId));
163         if (compositionTargetId != null) {
164             automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
165         }
166         automationComposition.setDeployState(deployState);
167         automationComposition.setLockState(lockState);
168         automationComposition.setLastMsg(lastMsg.toString());
169         automationComposition.setPhase(phase);
170         automationComposition.setSubState(subState);
171         automationComposition.setDescription(description);
172         automationComposition.setStateChangeResult(stateChangeResult);
173         automationComposition.setRevisionId(UUID.fromString(this.revisionId));
174         automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
175         for (var element : this.elements) {
176             automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
177         }
178
179         return automationComposition;
180     }
181
182     @Override
183     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
184         this.instanceId = automationComposition.getInstanceId().toString();
185         this.name = automationComposition.getName();
186         this.version = automationComposition.getVersion();
187         this.compositionId = automationComposition.getCompositionId().toString();
188         if (automationComposition.getCompositionTargetId() != null) {
189             this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
190         }
191         this.deployState = automationComposition.getDeployState();
192         this.lockState = automationComposition.getLockState();
193         this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
194         this.phase = automationComposition.getPhase();
195         this.subState = automationComposition.getSubState();
196         this.description = automationComposition.getDescription();
197         this.stateChangeResult = automationComposition.getStateChangeResult();
198         this.revisionId = automationComposition.getRevisionId().toString();
199         this.elements = new ArrayList<>(automationComposition.getElements().size());
200         for (var elementEntry : automationComposition.getElements().entrySet()) {
201             var jpaAutomationCompositionElement =
202                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
203             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
204             this.elements.add(jpaAutomationCompositionElement);
205         }
206     }
207
208     @Override
209     public int compareTo(final JpaAutomationComposition other) {
210         if (other == null) {
211             return -1;
212         }
213         if (this == other) {
214             return 0;
215         }
216
217         var result = ObjectUtils.compare(instanceId, other.instanceId);
218         if (result != 0) {
219             return result;
220         }
221
222         result = ObjectUtils.compare(name, other.name);
223         if (result != 0) {
224             return result;
225         }
226
227         result = lastMsg.compareTo(other.lastMsg);
228         if (result != 0) {
229             return result;
230         }
231
232         result = ObjectUtils.compare(phase, other.phase);
233         if (result != 0) {
234             return result;
235         }
236
237         result = ObjectUtils.compare(version, other.version);
238         if (result != 0) {
239             return result;
240         }
241
242         result = ObjectUtils.compare(compositionId, other.compositionId);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
248         if (result != 0) {
249             return result;
250         }
251
252         result = ObjectUtils.compare(deployState, other.deployState);
253         if (result != 0) {
254             return result;
255         }
256
257         result = ObjectUtils.compare(lockState, other.lockState);
258         if (result != 0) {
259             return result;
260         }
261
262         result = ObjectUtils.compare(subState, other.subState);
263         if (result != 0) {
264             return result;
265         }
266
267         result = ObjectUtils.compare(description, other.description);
268         if (result != 0) {
269             return result;
270         }
271
272         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
273         if (result != 0) {
274             return result;
275         }
276         result = ObjectUtils.compare(revisionId, other.revisionId);
277         if (result != 0) {
278             return result;
279         }
280         return PfUtils.compareObjects(elements, other.elements);
281     }
282 }