7f2a1e5aee9637d8df68f6204012e31dfd6e729c
[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.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 Boolean restarting;
85
86     @Column
87     @NotNull
88     private DeployState deployState;
89
90     @Column
91     @NotNull
92     private LockState lockState;
93
94     @Column
95     private StateChangeResult stateChangeResult;
96
97     @Column
98     private String description;
99
100     @NotNull
101     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
102     @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
103     private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
104
105     /**
106      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
107      */
108     public JpaAutomationComposition() {
109         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
110                 new ArrayList<>(), DeployState.UNDEPLOYED, LockState.NONE);
111     }
112
113     /**
114      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
115      *
116      * @param instanceId The UUID of the automation composition instance
117      * @param key the key
118      * @param compositionId the TOSCA compositionId of the automation composition definition
119      * @param elements the elements of the automation composition in participants
120      * @param deployState the Deploy State
121      * @param lockState the Lock State
122      */
123     public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
124             @NonNull final String compositionId,
125             @NonNull final List<JpaAutomationCompositionElement> elements,
126             @NonNull final DeployState deployState, @NonNull final LockState lockState) {
127         this.instanceId = instanceId;
128         this.name = key.getName();
129         this.version = key.getVersion();
130         this.compositionId = compositionId;
131         this.deployState = deployState;
132         this.lockState = lockState;
133         this.elements = elements;
134     }
135
136     /**
137      * Copy constructor.
138      *
139      * @param copyConcept the concept to copy from
140      */
141     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
142         this.instanceId = copyConcept.instanceId;
143         this.name = copyConcept.name;
144         this.version = copyConcept.version;
145         this.compositionId = copyConcept.compositionId;
146         this.restarting = copyConcept.restarting;
147         this.deployState = copyConcept.deployState;
148         this.lockState = copyConcept.lockState;
149         this.description = copyConcept.description;
150         this.stateChangeResult = copyConcept.stateChangeResult;
151         this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
152     }
153
154     /**
155      * Authorative constructor.
156      *
157      * @param authorativeConcept the authorative concept to copy from
158      */
159     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
160         this.fromAuthorative(authorativeConcept);
161     }
162
163     @Override
164     public AutomationComposition toAuthorative() {
165         var automationComposition = new AutomationComposition();
166
167         automationComposition.setInstanceId(UUID.fromString(instanceId));
168         automationComposition.setName(name);
169         automationComposition.setVersion(version);
170         automationComposition.setCompositionId(UUID.fromString(compositionId));
171         automationComposition.setRestarting(restarting);
172         automationComposition.setDeployState(deployState);
173         automationComposition.setLockState(lockState);
174         automationComposition.setDescription(description);
175         automationComposition.setStateChangeResult(stateChangeResult);
176         automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
177         for (var element : this.elements) {
178             automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
179         }
180
181         return automationComposition;
182     }
183
184     @Override
185     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
186         this.instanceId = automationComposition.getInstanceId().toString();
187         this.name = automationComposition.getName();
188         this.version = automationComposition.getVersion();
189         this.compositionId = automationComposition.getCompositionId().toString();
190         this.restarting = automationComposition.getRestarting();
191         this.deployState = automationComposition.getDeployState();
192         this.lockState = automationComposition.getLockState();
193         this.description = automationComposition.getDescription();
194         this.stateChangeResult = automationComposition.getStateChangeResult();
195         this.elements = new ArrayList<>(automationComposition.getElements().size());
196         for (var elementEntry : automationComposition.getElements().entrySet()) {
197             var jpaAutomationCompositionElement =
198                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
199             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
200             this.elements.add(jpaAutomationCompositionElement);
201         }
202     }
203
204     @Override
205     public int compareTo(final JpaAutomationComposition other) {
206         if (other == null) {
207             return -1;
208         }
209         if (this == other) {
210             return 0;
211         }
212
213         var result = ObjectUtils.compare(instanceId, other.instanceId);
214         if (result != 0) {
215             return result;
216         }
217
218         result = ObjectUtils.compare(name, other.name);
219         if (result != 0) {
220             return result;
221         }
222
223         result = ObjectUtils.compare(version, other.version);
224         if (result != 0) {
225             return result;
226         }
227
228         result = ObjectUtils.compare(compositionId, other.compositionId);
229         if (result != 0) {
230             return result;
231         }
232
233         result = ObjectUtils.compare(restarting, other.restarting);
234         if (result != 0) {
235             return result;
236         }
237
238         result = ObjectUtils.compare(deployState, other.deployState);
239         if (result != 0) {
240             return result;
241         }
242
243         result = ObjectUtils.compare(lockState, other.lockState);
244         if (result != 0) {
245             return result;
246         }
247
248         result = ObjectUtils.compare(description, other.description);
249         if (result != 0) {
250             return result;
251         }
252
253         result = ObjectUtils.compare(stateChangeResult, other.stateChangeResult);
254         if (result != 0) {
255             return result;
256         }
257         return PfUtils.compareObjects(elements, other.elements);
258     }
259 }