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;
26 import java.util.List;
28 import java.util.UUID;
29 import java.util.function.UnaryOperator;
30 import javax.persistence.AttributeOverride;
31 import javax.persistence.Column;
32 import javax.persistence.Convert;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.Lob;
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.AutomationCompositionElement;
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.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
54 import org.onap.policy.models.base.validation.annotations.VerifyKey;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
58 * Class to represent a participant automation composition element in the database.
60 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "AutomationCompositionElement")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaAutomationCompositionElement extends PfConcept implements PfAuthorative<AutomationCompositionElement> {
68 private static final long serialVersionUID = -1791732273187890213L;
73 private PfReferenceKey key;
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(new PfReferenceKey());
119 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
123 public JpaAutomationCompositionElement(@NonNull final PfReferenceKey key) {
124 this(key, new PfConceptKey(), new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
128 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
131 * @param definition the TOSCA definition of the automation composition element
132 * @param participantType the TOSCA definition of the participant running the automation composition element
133 * @param state the state of the automation composition
135 public JpaAutomationCompositionElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
136 @NonNull final PfConceptKey participantType, @NonNull final AutomationCompositionState state) {
138 this.definition = definition;
139 this.participantType = participantType;
146 * @param copyConcept the concept to copy from
148 public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
150 this.key = new PfReferenceKey(copyConcept.key);
151 this.definition = new PfConceptKey(copyConcept.definition);
152 this.participantType = new PfConceptKey(copyConcept.participantType);
153 this.participantId = new PfConceptKey(copyConcept.participantId);
154 this.state = copyConcept.state;
155 this.orderedState = copyConcept.orderedState;
156 this.description = copyConcept.description;
157 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
161 * Authorative constructor.
163 * @param authorativeConcept the authorative concept to copy from
165 public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
166 this.fromAuthorative(authorativeConcept);
170 public AutomationCompositionElement toAuthorative() {
171 var element = new AutomationCompositionElement();
173 element.setId(UUID.fromString(getKey().getLocalName()));
174 element.setDefinition(new ToscaConceptIdentifier(definition));
175 element.setParticipantType(new ToscaConceptIdentifier(participantType));
176 element.setParticipantId(new ToscaConceptIdentifier(participantId));
177 element.setState(state);
178 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
179 element.setDescription(description);
180 element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
186 public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
187 if (this.key == null || this.getKey().isNullKey()) {
188 this.setKey(new PfReferenceKey());
189 getKey().setLocalName(element.getId().toString());
192 this.definition = element.getDefinition().asConceptKey();
193 this.participantType = element.getParticipantType().asConceptKey();
194 this.participantId = element.getParticipantId().asConceptKey();
195 this.state = element.getState();
196 this.orderedState = element.getOrderedState();
197 this.description = element.getDescription();
198 properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
202 public List<PfKey> getKeys() {
203 List<PfKey> keyList = getKey().getKeys();
205 keyList.add(definition);
206 keyList.add(participantType);
207 keyList.add(participantId);
213 public void clean() {
216 participantType.clean();
217 participantId.clean();
219 if (description != null) {
220 description = description.trim();
225 public int compareTo(final PfConcept otherConcept) {
226 if (otherConcept == null) {
229 if (this == otherConcept) {
232 if (getClass() != otherConcept.getClass()) {
233 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
236 final JpaAutomationCompositionElement other = (JpaAutomationCompositionElement) otherConcept;
237 int result = key.compareTo(other.key);
242 result = definition.compareTo(other.definition);
247 result = participantType.compareTo(other.participantType);
252 result = participantId.compareTo(other.participantId);
257 result = ObjectUtils.compare(state, other.state);
262 result = ObjectUtils.compare(orderedState, other.orderedState);
267 return ObjectUtils.compare(description, other.description);