33ec649f49d73e7c1c02677092da3233bb3d6e96
[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.common.parameters.annotations.NotNull;
47 import org.onap.policy.common.parameters.annotations.Valid;
48 import org.onap.policy.models.base.PfAuthorative;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.base.Validated;
52
53 /**
54  * Class to represent a automation composition in the database.
55  *
56  * @author Liam Fallon (liam.fallon@est.tech)
57  */
58 @Entity
59 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaAutomationComposition extends Validated
64         implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
65
66     @Id
67     @NotNull
68     private String instanceId;
69
70     @NotNull
71     @Column
72     private String name;
73
74     @NotNull
75     @Column
76     private String version;
77
78     @Column
79     @NotNull
80     private String compositionId;
81
82     @Column
83     @NotNull
84     private DeployState deployState;
85
86     @Column
87     @NotNull
88     private LockState lockState;
89
90     @Column
91     private String description;
92
93     @NotNull
94     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
95     @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
96     private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
97
98     /**
99      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
100      */
101     public JpaAutomationComposition() {
102         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
103                 new ArrayList<>(), DeployState.UNDEPLOYED, LockState.LOCKED);
104     }
105
106     /**
107      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
108      *
109      * @param instanceId The UUID of the automation composition instance
110      * @param key the key
111      * @param compositionId the TOSCA compositionId of the automation composition definition
112      * @param elements the elements of the automation composition in participants
113      * @param deployState the Deploy State
114      * @param lockState the Lock State
115      */
116     public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
117             @NonNull final String compositionId,
118             @NonNull final List<JpaAutomationCompositionElement> elements,
119             @NonNull final DeployState deployState, @NonNull final LockState lockState) {
120         this.instanceId = instanceId;
121         this.name = key.getName();
122         this.version = key.getVersion();
123         this.compositionId = compositionId;
124         this.deployState = deployState;
125         this.lockState = lockState;
126         this.elements = elements;
127     }
128
129     /**
130      * Copy constructor.
131      *
132      * @param copyConcept the concept to copy from
133      */
134     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
135         this.instanceId = copyConcept.instanceId;
136         this.name = copyConcept.name;
137         this.version = copyConcept.version;
138         this.compositionId = copyConcept.compositionId;
139         this.deployState = copyConcept.deployState;
140         this.lockState = copyConcept.lockState;
141         this.description = copyConcept.description;
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         automationComposition.setDeployState(deployState);
163         automationComposition.setLockState(lockState);
164         automationComposition.setDescription(description);
165         automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
166         for (var element : this.elements) {
167             automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
168         }
169
170         return automationComposition;
171     }
172
173     @Override
174     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
175         this.instanceId = automationComposition.getInstanceId().toString();
176         this.name = automationComposition.getName();
177         this.version = automationComposition.getVersion();
178         this.compositionId = automationComposition.getCompositionId().toString();
179         this.deployState = automationComposition.getDeployState();
180         this.lockState = automationComposition.getLockState();
181         this.description = automationComposition.getDescription();
182
183         this.elements = new ArrayList<>(automationComposition.getElements().size());
184         for (var elementEntry : automationComposition.getElements().entrySet()) {
185             var jpaAutomationCompositionElement =
186                     new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
187             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
188             this.elements.add(jpaAutomationCompositionElement);
189         }
190     }
191
192     @Override
193     public int compareTo(final JpaAutomationComposition other) {
194         if (other == null) {
195             return -1;
196         }
197         if (this == other) {
198             return 0;
199         }
200
201         var result = ObjectUtils.compare(instanceId, other.instanceId);
202         if (result != 0) {
203             return result;
204         }
205
206         result = ObjectUtils.compare(name, other.name);
207         if (result != 0) {
208             return result;
209         }
210
211         result = ObjectUtils.compare(version, other.version);
212         if (result != 0) {
213             return result;
214         }
215
216         result = ObjectUtils.compare(compositionId, other.compositionId);
217         if (result != 0) {
218             return result;
219         }
220
221         result = ObjectUtils.compare(deployState, other.deployState);
222         if (result != 0) {
223             return result;
224         }
225
226         result = ObjectUtils.compare(lockState, other.lockState);
227         if (result != 0) {
228             return result;
229         }
230
231         result = ObjectUtils.compare(description, other.description);
232         if (result != 0) {
233             return result;
234         }
235         return PfUtils.compareObjects(elements, other.elements);
236     }
237 }