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.DeployState;
45 import org.onap.policy.clamp.models.acm.concepts.LockState;
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 DeployState deployState;
88 private LockState lockState;
91 private String description;
94 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
95 @JoinColumn(name = "instanceId", foreignKey = @ForeignKey(name = "ac_element_fk"))
96 private List<@NotNull @Valid JpaAutomationCompositionElement> elements;
99 * The Default Constructor creates a {@link JpaAutomationComposition} object with a null key.
101 public JpaAutomationComposition() {
102 this(UUID.randomUUID().toString(), new PfConceptKey(), UUID.randomUUID().toString(),
103 new ArrayList<>(), DeployState.UNDEPLOYED, LockState.LOCKED);
107 * The Key Constructor creates a {@link JpaAutomationComposition} object with all mandatory fields.
109 * @param instanceId The UUID of the automation composition instance
111 * @param compositionId the TOSCA compositionId of the automation composition definition
112 * @param elements the elements of the automation composition in participants
113 * @param deployState the Deploy State
114 * @param lockState the Lock State
116 public JpaAutomationComposition(@NonNull final String instanceId, @NonNull final PfConceptKey key,
117 @NonNull final String compositionId,
118 @NonNull final List<JpaAutomationCompositionElement> elements,
119 @NonNull final DeployState deployState, @NonNull final LockState lockState) {
120 this.instanceId = instanceId;
121 this.name = key.getName();
122 this.version = key.getVersion();
123 this.compositionId = compositionId;
124 this.deployState = deployState;
125 this.lockState = lockState;
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.deployState = copyConcept.deployState;
140 this.lockState = copyConcept.lockState;
141 this.description = copyConcept.description;
142 this.elements = PfUtils.mapList(copyConcept.elements, JpaAutomationCompositionElement::new);
146 * Authorative constructor.
148 * @param authorativeConcept the authorative concept to copy from
150 public JpaAutomationComposition(@NonNull final AutomationComposition authorativeConcept) {
151 this.fromAuthorative(authorativeConcept);
155 public AutomationComposition toAuthorative() {
156 var automationComposition = new AutomationComposition();
158 automationComposition.setInstanceId(UUID.fromString(instanceId));
159 automationComposition.setName(name);
160 automationComposition.setVersion(version);
161 automationComposition.setCompositionId(UUID.fromString(compositionId));
162 automationComposition.setDeployState(deployState);
163 automationComposition.setLockState(lockState);
164 automationComposition.setDescription(description);
165 automationComposition.setElements(new LinkedHashMap<>(this.elements.size()));
166 for (var element : this.elements) {
167 automationComposition.getElements().put(UUID.fromString(element.getElementId()), element.toAuthorative());
170 return automationComposition;
174 public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
175 this.instanceId = automationComposition.getInstanceId().toString();
176 this.name = automationComposition.getName();
177 this.version = automationComposition.getVersion();
178 this.compositionId = automationComposition.getCompositionId().toString();
179 this.deployState = automationComposition.getDeployState();
180 this.lockState = automationComposition.getLockState();
181 this.description = automationComposition.getDescription();
183 this.elements = new ArrayList<>(automationComposition.getElements().size());
184 for (var elementEntry : automationComposition.getElements().entrySet()) {
185 var jpaAutomationCompositionElement =
186 new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
187 jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
188 this.elements.add(jpaAutomationCompositionElement);
193 public int compareTo(final JpaAutomationComposition other) {
201 var result = ObjectUtils.compare(instanceId, other.instanceId);
206 result = ObjectUtils.compare(name, other.name);
211 result = ObjectUtils.compare(version, other.version);
216 result = ObjectUtils.compare(compositionId, other.compositionId);
221 result = ObjectUtils.compare(deployState, other.deployState);
226 result = ObjectUtils.compare(lockState, other.lockState);
231 result = ObjectUtils.compare(description, other.description);
235 return PfUtils.compareObjects(elements, other.elements);