743bb7fd3276ae1d680a995c4fad734ef24eb73f
[policy/clamp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.models.acm.persistence.concepts;
24
25 import java.util.LinkedHashMap;
26 import java.util.Map;
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;
38 import lombok.Data;
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;
53
54 /**
55  * Class to represent a participant automation composition element in the database.
56  *
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Entity
60 @Table(name = "AutomationCompositionElement")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @Data
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaAutomationCompositionElement extends Validated
65         implements PfAuthorative<AutomationCompositionElement>, Comparable<JpaAutomationCompositionElement> {
66
67     @Id
68     @NotNull
69     private String elementId;
70
71     @Column
72     @NotNull
73     private String instanceId;
74
75     // @formatter:off
76     @VerifyKey
77     @NotNull
78     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
79     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
80     private PfConceptKey definition;
81
82     @NotNull
83     private String participantId;
84
85     @Column
86     @NotNull
87     private AutomationCompositionState state;
88
89     @Column
90     @NotNull
91     private AutomationCompositionOrderedState orderedState;
92
93     @Column
94     private String description;
95
96     @Lob
97     @NotNull
98     @Valid
99     @Convert(converter = StringToMapConverter.class)
100     private Map<String, Object> properties;
101
102     /**
103      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
104      */
105     public JpaAutomationCompositionElement() {
106         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
107     }
108
109     /**
110      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
111      *
112      * @param elementId The id of the automation composition instance Element
113      * @param instanceId The id of the automation composition instance
114      */
115     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
116         this(elementId, instanceId, new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
117     }
118
119     /**
120      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
121      *
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
126      */
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;
133         this.state = state;
134     }
135
136     /**
137      * Copy constructor.
138      *
139      * @param copyConcept the concept to copy from
140      */
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);
150     }
151
152     /**
153      * Authorative constructor.
154      *
155      * @param authorativeConcept the authorative concept to copy from
156      */
157     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
158         this.fromAuthorative(authorativeConcept);
159     }
160
161     @Override
162     public AutomationCompositionElement toAuthorative() {
163         var element = new AutomationCompositionElement();
164
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()));
172
173         return element;
174     }
175
176     @Override
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());
184     }
185
186     @Override
187     public int compareTo(final JpaAutomationCompositionElement other) {
188         if (other == null) {
189             return -1;
190         }
191         if (this == other) {
192             return 0;
193         }
194
195         var result = ObjectUtils.compare(elementId, other.elementId);
196         if (result != 0) {
197             return result;
198         }
199
200         result = ObjectUtils.compare(instanceId, other.instanceId);
201         if (result != 0) {
202             return result;
203         }
204
205         result = definition.compareTo(other.definition);
206         if (result != 0) {
207             return result;
208         }
209
210         result = participantId.compareTo(other.participantId);
211         if (result != 0) {
212             return result;
213         }
214
215         result = ObjectUtils.compare(state, other.state);
216         if (result != 0) {
217             return result;
218         }
219
220         result = ObjectUtils.compare(orderedState, other.orderedState);
221         if (result != 0) {
222             return result;
223         }
224
225         return ObjectUtils.compare(description, other.description);
226     }
227 }