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