b87bad4e08a7567123fae2013eec68fd1c6c59c8
[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.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import javax.persistence.CascadeType;
28 import javax.persistence.Column;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.Index;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.ManyToMany;
36 import javax.persistence.Table;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.common.parameters.annotations.Valid;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.validation.annotations.VerifyKey;
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 PfConcept implements PfAuthorative<AutomationComposition> {
65     private static final long serialVersionUID = -4725410933242154805L;
66
67     @Column
68     @NotNull
69     private String instanceId;
70
71     @EmbeddedId
72     @VerifyKey
73     @NotNull
74     private PfConceptKey key;
75
76     @Column
77     @NotNull
78     private String compositionId;
79
80     @Column
81     @NotNull
82     private AutomationCompositionState state;
83
84     @Column
85     @NotNull
86     private AutomationCompositionOrderedState orderedState;
87
88     @Column
89     private String description;
90
91     @Column
92     private Boolean primed;
93
94     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
95     @NotNull
96     private Map<@NotNull UUID, @NotNull @Valid JpaAutomationCompositionElement> elements;
97     // @formatter:on
98
99     /**
100      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
101      */
102     public JpaAutomationComposition() {
103         this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
104                 AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
105     }
106
107     /**
108      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
109      *
110      * @param instanceId The UUID of the automation composition instance
111      * @param key the key
112      * @param compositionId the TOSCA compositionId of the automation composition definition
113      * @param state the state of the automation composition
114      * @param elements the elements of the automation composition in participants
115      */
116     public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
117             @NonNull final String compositionId, @NonNull final AutomationCompositionState state,
118             @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
119         this.instanceId = instanceId;
120         this.key = key;
121         this.compositionId = compositionId;
122         this.state = state;
123         this.elements = elements;
124     }
125
126     /**
127      * Copy constructor.
128      *
129      * @param copyConcept the concept to copy from
130      */
131     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
132         super(copyConcept);
133         this.instanceId = copyConcept.instanceId;
134         this.key = new PfConceptKey(copyConcept.key);
135         this.compositionId = copyConcept.compositionId;
136         this.state = copyConcept.state;
137         this.orderedState = copyConcept.orderedState;
138         this.description = copyConcept.description;
139         this.elements =
140                 PfUtils.mapMap(copyConcept.elements, JpaAutomationCompositionElement::new, new LinkedHashMap<>(0));
141         this.primed = copyConcept.primed;
142     }
143
144     /**
145      * Authorative constructor.
146      *
147      * @param authorativeConcept the authorative concept to copy from
148      */
149     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
150         this.fromAuthorative(authorativeConcept);
151     }
152
153     @Override
154     public AutomationComposition toAuthorative() {
155         var automationComposition = new AutomationComposition();
156
157         automationComposition.setInstanceId(UUID.fromString(instanceId));
158         automationComposition.setName(getKey().getName());
159         automationComposition.setVersion(getKey().getVersion());
160         automationComposition.setCompositionId(UUID.fromString(compositionId));
161         automationComposition.setState(state);
162         automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
163         automationComposition.setDescription(description);
164         automationComposition.setElements(
165                 PfUtils.mapMap(elements, JpaAutomationCompositionElement::toAuthorative, new LinkedHashMap<>(0)));
166         automationComposition.setPrimed(primed);
167
168         return automationComposition;
169     }
170
171     @Override
172     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
173         this.instanceId = automationComposition.getInstanceId().toString();
174         if (this.key == null || this.getKey().isNullKey()) {
175             this.setKey(new PfConceptKey(automationComposition.getName(), automationComposition.getVersion()));
176         }
177
178         this.compositionId = automationComposition.getCompositionId().toString();
179         this.state = automationComposition.getState();
180         this.orderedState = automationComposition.getOrderedState();
181         this.description = automationComposition.getDescription();
182         this.primed = automationComposition.getPrimed();
183
184         this.elements = new LinkedHashMap<>(automationComposition.getElements().size());
185         for (var elementEntry : automationComposition.getElements().entrySet()) {
186             var jpaAutomationCompositionElement = new JpaAutomationCompositionElement();
187             jpaAutomationCompositionElement
188                     .setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
189             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
190             this.elements.put(elementEntry.getKey(), jpaAutomationCompositionElement);
191         }
192     }
193
194     @Override
195     public List<PfKey> getKeys() {
196         var keyList = getKey().getKeys();
197
198         for (var element : elements.values()) {
199             keyList.addAll(element.getKeys());
200         }
201
202         return keyList;
203     }
204
205     @Override
206     public void clean() {
207         key.clean();
208         description = (description == null ? null : description.trim());
209
210         for (var element : elements.values()) {
211             element.clean();
212         }
213     }
214
215     @Override
216     public int compareTo(final PfConcept otherConcept) {
217         if (otherConcept == null) {
218             return -1;
219         }
220         if (this == otherConcept) {
221             return 0;
222         }
223         if (getClass() != otherConcept.getClass()) {
224             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
225         }
226
227         final var other = (JpaAutomationComposition) otherConcept;
228         var result = ObjectUtils.compare(instanceId, other.instanceId);
229         if (result != 0) {
230             return result;
231         }
232
233         result = key.compareTo(other.key);
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(description, other.description);
254         if (result != 0) {
255             return result;
256         }
257
258         result = ObjectUtils.compare(primed, other.primed);
259         if (result != 0) {
260             return result;
261         }
262         return PfUtils.compareObjects(elements, other.elements);
263     }
264 }