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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
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;
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;
54 * Class to represent a automation composition in the database.
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaAutomationComposition extends Validated
64 implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
68 private String instanceId;
76 private String version;
80 private String compositionId;
84 private AutomationCompositionState state;
88 private AutomationCompositionOrderedState orderedState;
91 private String description;
93 @Column(columnDefinition = "TINYINT DEFAULT 1")
94 private Boolean primed;
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;
102 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
104 public JpaAutomationComposition() {
105 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
106 AutomationCompositionState.UNINITIALISED, new ArrayList<>());
110 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
112 * @param instanceId The UUID of the automation composition instance
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
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;
126 this.elements = elements;
132 * @param copyConcept the concept to copy from
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;
147 * Authorative constructor.
149 * @param authorativeConcept the authorative concept to copy from
151 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
152 this.fromAuthorative(authorativeConcept);
156 public AutomationComposition toAuthorative() {
157 var automationComposition = new AutomationComposition();
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());
172 return automationComposition;
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();
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);
196 public int compareTo(final JpaAutomationComposition other) {
204 var result = ObjectUtils.compare(instanceId, other.instanceId);
209 result = ObjectUtils.compare(name, other.name);
214 result = ObjectUtils.compare(version, other.version);
219 result = ObjectUtils.compare(compositionId, other.compositionId);
224 result = ObjectUtils.compare(state, other.state);
229 result = ObjectUtils.compare(orderedState, other.orderedState);
234 result = ObjectUtils.compare(description, other.description);
239 result = ObjectUtils.compare(primed, other.primed);
243 return PfUtils.compareObjects(elements, other.elements);