2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.common.parameters.annotations.NotNull;
46 import org.onap.policy.common.parameters.annotations.Valid;
47 import org.onap.policy.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.Validated;
51 import org.onap.policy.models.base.validation.annotations.VerifyKey;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55 * Class to represent a participant automation composition element in the database.
57 * @author Liam Fallon (liam.fallon@est.tech)
60 @Table(name = "AutomationCompositionElement")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaAutomationCompositionElement extends Validated
65 implements PfAuthorative<AutomationCompositionElement>, Comparable<JpaAutomationCompositionElement> {
69 private String elementId;
73 private String instanceId;
78 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
79 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
80 private PfConceptKey definition;
84 @AttributeOverride(name = "name", column = @Column(name = "participant_type_name"))
85 @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
86 private PfConceptKey participantType;
89 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
90 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
91 private PfConceptKey participantId;
96 private AutomationCompositionState state;
100 private AutomationCompositionOrderedState orderedState;
103 private String description;
108 @Convert(converter = StringToMapConverter.class)
109 private Map<String, Object> properties;
112 * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
114 public JpaAutomationCompositionElement() {
115 this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
119 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
121 * @param elementId The id of the automation composition instance Element
122 * @param instanceId The id of the automation composition instance
124 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
125 this(elementId, instanceId, new PfConceptKey(), new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
129 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
131 * @param elementId The id of the automation composition instance Element
132 * @param instanceId The id of the automation composition instance
133 * @param definition the TOSCA definition of the automation composition element
134 * @param participantType the TOSCA definition of the participant running the automation composition element
135 * @param state the state of the automation composition
137 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
138 @NonNull final PfConceptKey definition, @NonNull final PfConceptKey participantType,
139 @NonNull final AutomationCompositionState state) {
140 this.elementId = elementId;
141 this.instanceId = instanceId;
142 this.definition = definition;
143 this.participantType = participantType;
150 * @param copyConcept the concept to copy from
152 public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
153 this.elementId = copyConcept.elementId;
154 this.instanceId = copyConcept.instanceId;
155 this.definition = new PfConceptKey(copyConcept.definition);
156 this.participantType = new PfConceptKey(copyConcept.participantType);
157 this.participantId = new PfConceptKey(copyConcept.participantId);
158 this.state = copyConcept.state;
159 this.orderedState = copyConcept.orderedState;
160 this.description = copyConcept.description;
161 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
165 * Authorative constructor.
167 * @param authorativeConcept the authorative concept to copy from
169 public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
170 this.fromAuthorative(authorativeConcept);
174 public AutomationCompositionElement toAuthorative() {
175 var element = new AutomationCompositionElement();
177 element.setId(UUID.fromString(elementId));
178 element.setDefinition(new ToscaConceptIdentifier(definition));
179 element.setParticipantType(new ToscaConceptIdentifier(participantType));
180 element.setParticipantId(new ToscaConceptIdentifier(participantId));
181 element.setState(state);
182 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
183 element.setDescription(description);
184 element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
190 public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
191 this.definition = element.getDefinition().asConceptKey();
192 this.participantType = element.getParticipantType().asConceptKey();
193 this.participantId = element.getParticipantId().asConceptKey();
194 this.state = element.getState();
195 this.orderedState = element.getOrderedState();
196 this.description = element.getDescription();
197 properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
201 public int compareTo(final JpaAutomationCompositionElement other) {
209 var result = ObjectUtils.compare(elementId, other.elementId);
214 result = ObjectUtils.compare(instanceId, other.instanceId);
219 result = definition.compareTo(other.definition);
224 result = participantType.compareTo(other.participantType);
229 result = participantId.compareTo(other.participantId);
234 result = ObjectUtils.compare(state, other.state);
239 result = ObjectUtils.compare(orderedState, other.orderedState);
244 return ObjectUtils.compare(description, other.description);