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.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;
83 private String participantId;
87 private AutomationCompositionState state;
91 private AutomationCompositionOrderedState orderedState;
94 private String description;
99 @Convert(converter = StringToMapConverter.class)
100 private Map<String, Object> properties;
103 * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
105 public JpaAutomationCompositionElement() {
106 this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
110 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
112 * @param elementId The id of the automation composition instance Element
113 * @param instanceId The id of the automation composition instance
115 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
116 this(elementId, instanceId, new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
120 * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
122 * @param elementId The id of the automation composition instance Element
123 * @param instanceId The id of the automation composition instance
124 * @param definition the TOSCA definition of the automation composition element
125 * @param state the state of the automation composition
127 public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
128 @NonNull final PfConceptKey definition,
129 @NonNull final AutomationCompositionState state) {
130 this.elementId = elementId;
131 this.instanceId = instanceId;
132 this.definition = definition;
139 * @param copyConcept the concept to copy from
141 public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
142 this.elementId = copyConcept.elementId;
143 this.instanceId = copyConcept.instanceId;
144 this.definition = new PfConceptKey(copyConcept.definition);
145 this.participantId = copyConcept.participantId;
146 this.state = copyConcept.state;
147 this.orderedState = copyConcept.orderedState;
148 this.description = copyConcept.description;
149 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
153 * Authorative constructor.
155 * @param authorativeConcept the authorative concept to copy from
157 public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
158 this.fromAuthorative(authorativeConcept);
162 public AutomationCompositionElement toAuthorative() {
163 var element = new AutomationCompositionElement();
165 element.setId(UUID.fromString(elementId));
166 element.setDefinition(new ToscaConceptIdentifier(definition));
167 element.setParticipantId(UUID.fromString(participantId));
168 element.setState(state);
169 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
170 element.setDescription(description);
171 element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
177 public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
178 this.definition = element.getDefinition().asConceptKey();
179 this.participantId = element.getParticipantId().toString();
180 this.state = element.getState();
181 this.orderedState = element.getOrderedState();
182 this.description = element.getDescription();
183 properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
187 public int compareTo(final JpaAutomationCompositionElement other) {
195 var result = ObjectUtils.compare(elementId, other.elementId);
200 result = ObjectUtils.compare(instanceId, other.instanceId);
205 result = definition.compareTo(other.definition);
210 result = participantId.compareTo(other.participantId);
215 result = ObjectUtils.compare(state, other.state);
220 result = ObjectUtils.compare(orderedState, other.orderedState);
225 return ObjectUtils.compare(description, other.description);