e5c44d0a208b5f925e78f8f73d4aae49eea872c1
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.AttributeOverride;
29 import javax.persistence.CascadeType;
30 import javax.persistence.Column;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
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 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56
57 /**
58  * Class to represent a automation composition in the database.
59  *
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62 @Entity
63 @Table(name = "AutomationComposition")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaAutomationComposition extends PfConcept implements PfAuthorative<AutomationComposition> {
68     private static final long serialVersionUID = -4725410933242154805L;
69
70     @EmbeddedId
71     @VerifyKey
72     @NotNull
73     private PfConceptKey key;
74
75     // @formatter:off
76     @VerifyKey
77     @NotNull
78     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
79     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
80     private PfConceptKey definition;
81     // @formatter:on
82
83     @Column
84     @NotNull
85     private AutomationCompositionState state;
86
87     @Column
88     @NotNull
89     private AutomationCompositionOrderedState orderedState;
90
91     @Column
92     private String description;
93
94     @Column
95     private Boolean primed;
96
97     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
98     @NotNull
99     private Map<@NotNull UUID, @NotNull @Valid JpaAutomationCompositionElement> elements;
100     // @formatter:on
101
102     /**
103      * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
104      */
105     public JpaAutomationComposition() {
106         this(new PfConceptKey());
107     }
108
109     /**
110      * The Key Constructor creates a {@link JpaAutomationComposition} object with the given concept key.
111      *
112      * @param key the key
113      */
114     public JpaAutomationComposition(@NonNull final PfConceptKey key) {
115         this(key, new PfConceptKey(), AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
116     }
117
118     /**
119      * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
120      *
121      * @param key the key
122      * @param definition the TOSCA definition of the automation composition
123      * @param state the state of the automation composition
124      * @param elements the elements of the automation composition in participants
125      */
126     public JpaAutomationComposition(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
127         @NonNull final AutomationCompositionState state,
128         @NonNull final Map<UUID, JpaAutomationCompositionElement> elements) {
129         this.key = key;
130         this.definition = definition;
131         this.state = state;
132         this.elements = elements;
133     }
134
135     /**
136      * Copy constructor.
137      *
138      * @param copyConcept the concept to copy from
139      */
140     public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
141         super(copyConcept);
142         this.key = new PfConceptKey(copyConcept.key);
143         this.definition = new PfConceptKey(copyConcept.definition);
144         this.state = copyConcept.state;
145         this.orderedState = copyConcept.orderedState;
146         this.description = copyConcept.description;
147         this.elements =
148             PfUtils.mapMap(copyConcept.elements, JpaAutomationCompositionElement::new, new LinkedHashMap<>(0));
149         this.primed = copyConcept.primed;
150     }
151
152     /**
153      * Authorative constructor.
154      *
155      * @param authorativeConcept the authorative concept to copy from
156      */
157     public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
158         this.fromAuthorative(authorativeConcept);
159     }
160
161     @Override
162     public AutomationComposition toAuthorative() {
163         var automationComposition = new AutomationComposition();
164
165         automationComposition.setName(getKey().getName());
166         automationComposition.setVersion(getKey().getVersion());
167         automationComposition.setDefinition(new ToscaConceptIdentifier(definition));
168         automationComposition.setState(state);
169         automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
170         automationComposition.setDescription(description);
171         automationComposition.setElements(
172             PfUtils.mapMap(elements, JpaAutomationCompositionElement::toAuthorative, new LinkedHashMap<>(0)));
173         automationComposition.setPrimed(primed);
174
175         return automationComposition;
176     }
177
178     @Override
179     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
180         if (this.key == null || this.getKey().isNullKey()) {
181             this.setKey(new PfConceptKey(automationComposition.getName(), automationComposition.getVersion()));
182         }
183
184         this.definition = automationComposition.getDefinition().asConceptKey();
185         this.state = automationComposition.getState();
186         this.orderedState = automationComposition.getOrderedState();
187         this.description = automationComposition.getDescription();
188         this.primed = automationComposition.getPrimed();
189
190         this.elements = new LinkedHashMap<>(automationComposition.getElements().size());
191         for (Entry<UUID, AutomationCompositionElement> elementEntry : automationComposition.getElements().entrySet()) {
192             var jpaAutomationCompositionElement = new JpaAutomationCompositionElement();
193             jpaAutomationCompositionElement
194                 .setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
195             jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
196             this.elements.put(elementEntry.getKey(), jpaAutomationCompositionElement);
197         }
198     }
199
200     @Override
201     public List<PfKey> getKeys() {
202         List<PfKey> keyList = getKey().getKeys();
203
204         keyList.add(definition);
205
206         for (JpaAutomationCompositionElement element : elements.values()) {
207             keyList.addAll(element.getKeys());
208         }
209
210         return keyList;
211     }
212
213     @Override
214     public void clean() {
215         key.clean();
216         definition.clean();
217         description = (description == null ? null : description.trim());
218
219         for (JpaAutomationCompositionElement element : elements.values()) {
220             element.clean();
221         }
222     }
223
224     @Override
225     public int compareTo(final PfConcept otherConcept) {
226         if (otherConcept == null) {
227             return -1;
228         }
229         if (this == otherConcept) {
230             return 0;
231         }
232         if (getClass() != otherConcept.getClass()) {
233             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
234         }
235
236         final JpaAutomationComposition other = (JpaAutomationComposition) otherConcept;
237         int result = key.compareTo(other.key);
238         if (result != 0) {
239             return result;
240         }
241
242         result = definition.compareTo(other.definition);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(state, other.state);
248         if (result != 0) {
249             return result;
250         }
251
252         result = ObjectUtils.compare(orderedState, other.orderedState);
253         if (result != 0) {
254             return result;
255         }
256
257         result = ObjectUtils.compare(description, other.description);
258         if (result != 0) {
259             return result;
260         }
261
262         result = ObjectUtils.compare(primed, other.primed);
263         if (result != 0) {
264             return result;
265         }
266         return PfUtils.compareObjects(elements, other.elements);
267     }
268 }