8268e9066f55e7adfeb8640aafaa52b4a14b7ee7
[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.Map.Entry;
27 import java.util.UUID;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.Index;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
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.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.validation.annotations.VerifyKey;
55
56 /**
57  * Class to represent a automation composition in the database.
58  *
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61 @Entity
62 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> {
67     private static final long serialVersionUID = -4725410933242154805L;
68
69     @EmbeddedId
70     @VerifyKey
71     @NotNull
72     private PfConceptKey key;
73
74     @NotNull
75     private String compositionId;
76
77     @Column
78     @NotNull
79     private AutomationCompositionState state;
80
81     @Column
82     @NotNull
83     private AutomationCompositionOrderedState orderedState;
84
85     @Column
86     private String description;
87
88     @Column
89     private Boolean primed;
90
91     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
92     @NotNull
93     private Map<@NotNull UUID, @NotNull @Valid JpaAutomationCompositionElement> elements;
94     // @formatter:on
95
96     /**
97      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
98      */
99     public JpaAutomationComposition() {
100         this(new PfConceptKey());
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaAutomationComposition} object with the given concept key.
105      *
106      * @param key the key
107      */
108     public JpaAutomationComposition(@NonNull final PfConceptKey key) {
109         this(key, UUID.randomUUID().toString(), AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
110     }
111
112     /**
113      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
114      *
115      * @param key the key
116      * @param compositionId the TOSCA compositionId of the automation composition definition
117      * @param state the state of the automation composition
118      * @param elements the elements of the automation composition in participants
119      */
120     public JpaAutomationComposition(@NonNull final PfConceptKey key, @NonNull final String compositionId,
121             @NonNull final AutomationCompositionState state,
122             @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
123         this.key = key;
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         super(copyConcept);
136         this.key = new PfConceptKey(copyConcept.key);
137         this.compositionId = copyConcept.compositionId;
138         this.state = copyConcept.state;
139         this.orderedState = copyConcept.orderedState;
140         this.description = copyConcept.description;
141         this.elements =
142                 PfUtils.mapMap(copyConcept.elements, JpaAutomationCompositionElement::new, new LinkedHashMap<>(0));
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.setName(getKey().getName());
160         automationComposition.setVersion(getKey().getVersion());
161         automationComposition.setCompositionId(UUID.fromString(compositionId));
162         automationComposition.setState(state);
163         automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
164         automationComposition.setDescription(description);
165         automationComposition.setElements(
166                 PfUtils.mapMap(elements, JpaAutomationCompositionElement::toAuthorative, new LinkedHashMap<>(0)));
167         automationComposition.setPrimed(primed);
168
169         return automationComposition;
170     }
171
172     @Override
173     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
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 (Entry<UUID, AutomationCompositionElement> 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         List<PfKey> keyList = getKey().getKeys();
197
198         for (JpaAutomationCompositionElement 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 (JpaAutomationCompositionElement 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 JpaAutomationComposition other = (JpaAutomationComposition) otherConcept;
228         int result = key.compareTo(other.key);
229         if (result != 0) {
230             return result;
231         }
232
233         result = ObjectUtils.compare(compositionId, other.compositionId);
234         if (result != 0) {
235             return result;
236         }
237
238         result = ObjectUtils.compare(state, other.state);
239         if (result != 0) {
240             return result;
241         }
242
243         result = ObjectUtils.compare(orderedState, other.orderedState);
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(primed, other.primed);
254         if (result != 0) {
255             return result;
256         }
257         return PfUtils.compareObjects(elements, other.elements);
258     }
259 }