2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.models.acm.persistence.concepts;
25 import java.util.LinkedHashMap;
27 import java.util.UUID;
28 import java.util.function.UnaryOperator;
29 import javax.persistence.AttributeOverride;
30 import javax.persistence.Column;
31 import javax.persistence.Convert;
32 import javax.persistence.Entity;
33 import javax.persistence.Id;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Lob;
37 import javax.persistence.Table;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
45 import org.onap.policy.clamp.models.acm.concepts.DeployState;
46 import org.onap.policy.clamp.models.acm.concepts.LockState;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.Validated;
53 import org.onap.policy.models.base.validation.annotations.VerifyKey;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
57 * Class to represent a participant automation composition element in the database.
59 * @author Liam Fallon (liam.fallon@est.tech)
62 @Table(name = "AutomationCompositionElement")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaAutomationCompositionElement extends Validated
67 implements PfAuthorative<AutomationCompositionElement>, Comparable<JpaAutomationCompositionElement> {
71 private String elementId;
75 private String instanceId;
80 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
81 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
82 private PfConceptKey definition;
85 private String participantId;
89 private AutomationCompositionState state;
93 private AutomationCompositionOrderedState orderedState;
97 private DeployState deployState;
101 private LockState lockState;
104 private String description;
109 @Convert(converter = StringToMapConverter.class)
110 private Map<String, Object> properties;
113 * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
115 public JpaAutomationCompositionElement() {
116 this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
120 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
122 * @param elementId The id of the automation composition instance Element
123 * @param instanceId The id of the automation composition instance
125 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
126 this(elementId, instanceId, new PfConceptKey(),
127 AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED);
131 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
133 * @param elementId The id of the automation composition instance Element
134 * @param instanceId The id of the automation composition instance
135 * @param definition the TOSCA definition of the automation composition element
136 * @param state the state of the automation composition
138 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
139 @NonNull final PfConceptKey definition,
140 @NonNull final AutomationCompositionState state,
141 @NonNull final DeployState deployState, @NonNull final LockState lockState) {
142 this.elementId = elementId;
143 this.instanceId = instanceId;
144 this.definition = definition;
146 this.deployState = deployState;
147 this.lockState = lockState;
153 * @param copyConcept the concept to copy from
155 public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
156 this.elementId = copyConcept.elementId;
157 this.instanceId = copyConcept.instanceId;
158 this.definition = new PfConceptKey(copyConcept.definition);
159 this.participantId = copyConcept.participantId;
160 this.state = copyConcept.state;
161 this.orderedState = copyConcept.orderedState;
162 this.description = copyConcept.description;
163 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
164 this.deployState = copyConcept.deployState;
165 this.lockState = copyConcept.lockState;
169 * Authorative constructor.
171 * @param authorativeConcept the authorative concept to copy from
173 public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
174 this.fromAuthorative(authorativeConcept);
178 public AutomationCompositionElement toAuthorative() {
179 var element = new AutomationCompositionElement();
181 element.setId(UUID.fromString(elementId));
182 element.setDefinition(new ToscaConceptIdentifier(definition));
183 element.setParticipantId(UUID.fromString(participantId));
184 element.setState(state);
185 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
186 element.setDescription(description);
187 element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
188 element.setDeployState(deployState);
189 element.setLockState(lockState);
195 public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
196 this.definition = element.getDefinition().asConceptKey();
197 this.participantId = element.getParticipantId().toString();
198 this.state = element.getState();
199 this.orderedState = element.getOrderedState();
200 this.description = element.getDescription();
201 properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
202 this.deployState = element.getDeployState();
203 this.lockState = element.getLockState();
207 public int compareTo(final JpaAutomationCompositionElement other) {
215 var result = ObjectUtils.compare(elementId, other.elementId);
220 result = ObjectUtils.compare(instanceId, other.instanceId);
225 result = definition.compareTo(other.definition);
230 result = participantId.compareTo(other.participantId);
235 result = ObjectUtils.compare(state, other.state);
240 result = ObjectUtils.compare(orderedState, other.orderedState);
245 result = ObjectUtils.compare(deployState, other.deployState);
250 result = ObjectUtils.compare(lockState, other.lockState);
255 return ObjectUtils.compare(description, other.description);