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