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