fa6be7e30b4c00d591bbafa9e5d26ab047ee0d5f
[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
56 /**
57  * Class to represent an 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
66 @NoArgsConstructor
67 public class JpaAutomationComposition
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     @Column
115     @NotNull
116     private String revisionId;
117
118     @NotNull
119     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
120     @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
121     private List<@NotNull @Valid JpaAutomationCompositionElement> elements = new ArrayList<>();
122
123     /**
124      * Copy constructor.
125      *
126      * @param copyConcept the concept to copy from
127      */
128     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
129         this.instanceId = copyConcept.instanceId;
130         this.name = copyConcept.name;
131         this.version = copyConcept.version;
132         this.compositionId = copyConcept.compositionId;
133         this.compositionTargetId = copyConcept.compositionTargetId;
134         this.deployState = copyConcept.deployState;
135         this.lockState = copyConcept.lockState;
136         this.lastMsg = copyConcept.lastMsg;
137         this.phase = copyConcept.phase;
138         this.subState = copyConcept.subState;
139         this.description = copyConcept.description;
140         this.stateChangeResult = copyConcept.stateChangeResult;
141         this.revisionId = copyConcept.revisionId;
142         this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
143     }
144
145     /**
146      * Authorative constructor.
147      *
148      * @param authorativeConcept the authorative concept to copy from
149      */
150     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
151         this.fromAuthorative(authorativeConcept);
152     }
153
154     @Override
155     public AutomationComposition toAuthorative() {
156         var automationComposition = new AutomationComposition();
157
158         automationComposition.setInstanceId(UUID.fromString(instanceId));
159         automationComposition.setName(name);
160         automationComposition.setVersion(version);
161         automationComposition.setCompositionId(UUID.fromString(compositionId));
162         if (compositionTargetId != null) {
163             automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
164         }
165         automationComposition.setDeployState(deployState);
166         automationComposition.setLockState(lockState);
167         automationComposition.setLastMsg(lastMsg.toString());
168         automationComposition.setPhase(phase);
169         automationComposition.setSubState(subState);
170         automationComposition.setDescription(description);
171         automationComposition.setStateChangeResult(stateChangeResult);
172         automationComposition.setRevisionId(UUID.fromString(this.revisionId));
173         automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
174         for (var element : this.elements) {
175             automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
176         }
177
178         return automationComposition;
179     }
180
181     @Override
182     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
183         this.instanceId = automationComposition.getInstanceId().toString();
184         this.name = automationComposition.getName();
185         this.version = automationComposition.getVersion();
186         this.compositionId = automationComposition.getCompositionId().toString();
187         if (automationComposition.getCompositionTargetId() != null) {
188             this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
189         }
190         this.deployState = automationComposition.getDeployState();
191         this.lockState = automationComposition.getLockState();
192         this.lastMsg = TimestampHelper.toTimestamp(automationComposition.getLastMsg());
193         this.phase = automationComposition.getPhase();
194         this.subState = automationComposition.getSubState();
195         this.description = automationComposition.getDescription();
196         this.stateChangeResult = automationComposition.getStateChangeResult();
197         this.revisionId = automationComposition.getRevisionId().toString();
198         this.elements = new ArrayList<>(automationComposition.getElements().size());
199         for (var elementEntry : automationComposition.getElements().entrySet()) {
200             var jpaAutomationCompositionElement =
201                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
202             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
203             this.elements.add(jpaAutomationCompositionElement);
204         }
205     }
206
207     @Override
208     public int compareTo(final JpaAutomationComposition other) {
209         if (other == null) {
210             return -1;
211         }
212         if (this == other) {
213             return 0;
214         }
215
216         var result = ObjectUtils.compare(instanceId, other.instanceId);
217         if (result != 0) {
218             return result;
219         }
220
221         result = ObjectUtils.compare(name, other.name);
222         if (result != 0) {
223             return result;
224         }
225
226         result = lastMsg.compareTo(other.lastMsg);
227         if (result != 0) {
228             return result;
229         }
230
231         result = ObjectUtils.compare(phase, other.phase);
232         if (result != 0) {
233             return result;
234         }
235
236         result = ObjectUtils.compare(version, other.version);
237         if (result != 0) {
238             return result;
239         }
240
241         result = ObjectUtils.compare(compositionId, other.compositionId);
242         if (result != 0) {
243             return result;
244         }
245
246         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
247         if (result != 0) {
248             return result;
249         }
250
251         result = ObjectUtils.compare(deployState, other.deployState);
252         if (result != 0) {
253             return result;
254         }
255
256         result = ObjectUtils.compare(lockState, other.lockState);
257         if (result != 0) {
258             return result;
259         }
260
261         result = ObjectUtils.compare(subState, other.subState);
262         if (result != 0) {
263             return result;
264         }
265
266         result = ObjectUtils.compare(description, other.description);
267         if (result != 0) {
268             return result;
269         }
270
271         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
272         if (result != 0) {
273             return result;
274         }
275         result = ObjectUtils.compare(revisionId, other.revisionId);
276         if (result != 0) {
277             return result;
278         }
279         return PfUtils.compareObjects(elements, other.elements);
280     }
281 }