5e27fde53efb9e0850f4774bbfe9eac94dbb3f65
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2023 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.util.ArrayList;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.UUID;
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
44 import org.onap.policy.clamp.models.acm.concepts.DeployState;
45 import org.onap.policy.clamp.models.acm.concepts.LockState;
46 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.Validated;
53
54 /**
55  * Class to represent a automation composition in the database.
56  *
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Entity
60 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @Data
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaAutomationComposition extends Validated
65         implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
66
67     @Id
68     @NotNull
69     private String instanceId;
70
71     @NotNull
72     @Column
73     private String name;
74
75     @NotNull
76     @Column
77     private String version;
78
79     @Column
80     @NotNull
81     private String compositionId;
82
83     @Column
84     private String compositionTargetId;
85
86     @Column
87     private Boolean restarting;
88
89     @Column
90     @NotNull
91     private DeployState deployState;
92
93     @Column
94     @NotNull
95     private LockState lockState;
96
97     @Column
98     private StateChangeResult stateChangeResult;
99
100     @Column
101     private String description;
102
103     @NotNull
104     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
105     @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
106     private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
107
108     /**
109      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
110      */
111     public JpaAutomationComposition() {
112         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(), new ArrayList<>(),
113                 DeployState.UNDEPLOYED, LockState.NONE);
114     }
115
116     /**
117      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
118      *
119      * @param instanceId The UUID of the automation composition instance
120      * @param key the key
121      * @param compositionId the TOSCA compositionId of the automation composition definition
122      * @param elements the elements of the automation composition in participants
123      * @param deployState the Deploy State
124      * @param lockState the Lock State
125      */
126     public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
127             @NonNull final String compositionId, @NonNull final List<JpaAutomationCompositionElement> elements,
128             @NonNull final DeployState deployState, @NonNull final LockState lockState) {
129         this.instanceId = instanceId;
130         this.name = key.getName();
131         this.version = key.getVersion();
132         this.compositionId = compositionId;
133         this.deployState = deployState;
134         this.lockState = lockState;
135         this.elements = elements;
136     }
137
138     /**
139      * Copy constructor.
140      *
141      * @param copyConcept the concept to copy from
142      */
143     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
144         this.instanceId = copyConcept.instanceId;
145         this.name = copyConcept.name;
146         this.version = copyConcept.version;
147         this.compositionId = copyConcept.compositionId;
148         this.compositionTargetId = copyConcept.compositionTargetId;
149         this.restarting = copyConcept.restarting;
150         this.deployState = copyConcept.deployState;
151         this.lockState = copyConcept.lockState;
152         this.description = copyConcept.description;
153         this.stateChangeResult = copyConcept.stateChangeResult;
154         this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
155     }
156
157     /**
158      * Authorative constructor.
159      *
160      * @param authorativeConcept the authorative concept to copy from
161      */
162     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
163         this.fromAuthorative(authorativeConcept);
164     }
165
166     @Override
167     public AutomationComposition toAuthorative() {
168         var automationComposition = new AutomationComposition();
169
170         automationComposition.setInstanceId(UUID.fromString(instanceId));
171         automationComposition.setName(name);
172         automationComposition.setVersion(version);
173         automationComposition.setCompositionId(UUID.fromString(compositionId));
174         if (compositionTargetId != null) {
175             automationComposition.setCompositionTargetId(UUID.fromString(compositionTargetId));
176         }
177         automationComposition.setRestarting(restarting);
178         automationComposition.setDeployState(deployState);
179         automationComposition.setLockState(lockState);
180         automationComposition.setDescription(description);
181         automationComposition.setStateChangeResult(stateChangeResult);
182         automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
183         for (var element : this.elements) {
184             automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
185         }
186
187         return automationComposition;
188     }
189
190     @Override
191     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
192         this.instanceId = automationComposition.getInstanceId().toString();
193         this.name = automationComposition.getName();
194         this.version = automationComposition.getVersion();
195         this.compositionId = automationComposition.getCompositionId().toString();
196         if (automationComposition.getCompositionTargetId() != null) {
197             this.compositionTargetId = automationComposition.getCompositionTargetId().toString();
198         }
199         this.restarting = automationComposition.getRestarting();
200         this.deployState = automationComposition.getDeployState();
201         this.lockState = automationComposition.getLockState();
202         this.description = automationComposition.getDescription();
203         this.stateChangeResult = automationComposition.getStateChangeResult();
204         this.elements = new ArrayList<>(automationComposition.getElements().size());
205         for (var elementEntry : automationComposition.getElements().entrySet()) {
206             var jpaAutomationCompositionElement =
207                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
208             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
209             this.elements.add(jpaAutomationCompositionElement);
210         }
211     }
212
213     @Override
214     public int compareTo(final JpaAutomationComposition other) {
215         if (other == null) {
216             return -1;
217         }
218         if (this == other) {
219             return 0;
220         }
221
222         var result = ObjectUtils.compare(instanceId, other.instanceId);
223         if (result != 0) {
224             return result;
225         }
226
227         result = ObjectUtils.compare(name, other.name);
228         if (result != 0) {
229             return result;
230         }
231
232         result = ObjectUtils.compare(version, other.version);
233         if (result != 0) {
234             return result;
235         }
236
237         result = ObjectUtils.compare(compositionId, other.compositionId);
238         if (result != 0) {
239             return result;
240         }
241
242         result = ObjectUtils.compare(compositionTargetId, other.compositionTargetId);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(restarting, other.restarting);
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(description, other.description);
263         if (result != 0) {
264             return result;
265         }
266
267         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
268         if (result != 0) {
269             return result;
270         }
271         return PfUtils.compareObjects(elements, other.elements);
272     }
273 }