2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.clamp.models.acm.concepts.DeployState;
47 import org.onap.policy.clamp.models.acm.concepts.LockState;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.Validated;
56 * Class to represent a automation composition in the database.
58 * @author Liam Fallon (liam.fallon@est.tech)
61 @Table(name = "AutomationComposition", indexes = {@Index(name = "ac_compositionId", columnList = "compositionId")})
62 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @EqualsAndHashCode(callSuper = false)
65 public class JpaAutomationComposition extends Validated
66 implements PfAuthorative<AutomationComposition>, Comparable<JpaAutomationComposition> {
70 private String instanceId;
78 private String version;
82 private String compositionId;
86 private AutomationCompositionState state;
90 private AutomationCompositionOrderedState orderedState;
94 private DeployState deployState;
98 private LockState lockState;
101 private String description;
104 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
105 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
106 private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
109 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
111 public JpaAutomationComposition() {
112 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
113 AutomationCompositionState.UNINITIALISED, new ArrayList<>(), DeployState.UNDEPLOYED, LockState.LOCKED);
117 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
119 * @param instanceId The UUID of the automation composition instance
121 * @param compositionId the TOSCA compositionId of the automation composition definition
122 * @param state the state of the automation composition
123 * @param elements the elements of the automation composition in participants
125 public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
126 @NonNull final String compositionId, @NonNull final AutomationCompositionState state,
127 @NonNull final List<JpaAutomationCompositionElement> elements,
128 @NonNull final DeployState deployState, @NonNull final LockState lockState) {
129 this.instanceId = instanceId;
130 this.name = key.getName();
131 this.version = key.getVersion();
132 this.compositionId = compositionId;
134 this.deployState = deployState;
135 this.lockState = lockState;
136 this.elements = elements;
142 * @param copyConcept the concept to copy from
144 public JpaAutomationComposition(@NonNull final JpaAutomationComposition copyConcept) {
145 this.instanceId = copyConcept.instanceId;
146 this.name = copyConcept.name;
147 this.version = copyConcept.version;
148 this.compositionId = copyConcept.compositionId;
149 this.state = copyConcept.state;
150 this.orderedState = copyConcept.orderedState;
151 this.deployState = copyConcept.deployState;
152 this.lockState = copyConcept.lockState;
153 this.description = copyConcept.description;
154 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
158 * Authorative constructor.
160 * @param authorativeConcept the authorative concept to copy from
162 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
163 this.fromAuthorative(authorativeConcept);
167 public AutomationComposition toAuthorative() {
168 var automationComposition = new AutomationComposition();
170 automationComposition.setInstanceId(UUID.fromString(instanceId));
171 automationComposition.setName(name);
172 automationComposition.setVersion(version);
173 automationComposition.setCompositionId(UUID.fromString(compositionId));
174 automationComposition.setState(state);
175 automationComposition.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
176 automationComposition.setDeployState(deployState);
177 automationComposition.setLockState(lockState);
178 automationComposition.setDescription(description);
179 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
180 for (var element : this.elements) {
181 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
184 return automationComposition;
188 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
189 this.instanceId = automationComposition.getInstanceId().toString();
190 this.name = automationComposition.getName();
191 this.version = automationComposition.getVersion();
192 this.compositionId = automationComposition.getCompositionId().toString();
193 this.state = automationComposition.getState();
194 this.orderedState = automationComposition.getOrderedState();
195 this.deployState = automationComposition.getDeployState();
196 this.lockState = automationComposition.getLockState();
197 this.description = automationComposition.getDescription();
199 this.elements = new ArrayList<>(automationComposition.getElements().size());
200 for (var elementEntry : automationComposition.getElements().entrySet()) {
201 var jpaAutomationCompositionElement =
202 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
203 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
204 this.elements.add(jpaAutomationCompositionElement);
209 public int compareTo(final JpaAutomationComposition other) {
217 var result = ObjectUtils.compare(instanceId, other.instanceId);
222 result = ObjectUtils.compare(name, other.name);
227 result = ObjectUtils.compare(version, other.version);
232 result = ObjectUtils.compare(compositionId, other.compositionId);
237 result = ObjectUtils.compare(state, other.state);
242 result = ObjectUtils.compare(orderedState, other.orderedState);
247 result = ObjectUtils.compare(deployState, other.deployState);
252 result = ObjectUtils.compare(lockState, other.lockState);
257 result = ObjectUtils.compare(description, other.description);
261 return PfUtils.compareObjects(elements, other.elements);