23fc0a93eb916b3aca5db511ab53ea6613e203d8
[policy/clamp.git] /
1 /*-
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
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     @VerifyKey
83     @NotNull
84     @AttributeOverride(name = "name",    column = @Column(name = "participant_type_name"))
85     @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
86     private PfConceptKey participantType;
87
88     @Column
89     @NotNull
90     private String participantId;
91
92     @Column
93     @NotNull
94     private AutomationCompositionState state;
95
96     @Column
97     @NotNull
98     private AutomationCompositionOrderedState orderedState;
99
100     @Column
101     private String description;
102
103     @Lob
104     @NotNull
105     @Valid
106     @Convert(converter = StringToMapConverter.class)
107     private Map<String, Object> properties;
108
109     /**
110      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
111      */
112     public JpaAutomationCompositionElement() {
113         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
114     }
115
116     /**
117      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
118      *
119      * @param elementId The id of the automation composition instance Element
120      * @param instanceId The id of the automation composition instance
121      */
122     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
123         this(elementId, instanceId, new PfConceptKey(), new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
124     }
125
126     /**
127      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
128      *
129      * @param elementId The id of the automation composition instance Element
130      * @param instanceId The id of the automation composition instance
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
134      */
135     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
136             @NonNull final PfConceptKey definition, @NonNull final PfConceptKey participantType,
137             @NonNull final AutomationCompositionState state) {
138         this.elementId = elementId;
139         this.instanceId = instanceId;
140         this.definition = definition;
141         this.participantType = participantType;
142         this.state = state;
143     }
144
145     /**
146      * Copy constructor.
147      *
148      * @param copyConcept the concept to copy from
149      */
150     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
151         this.elementId = copyConcept.elementId;
152         this.instanceId = copyConcept.instanceId;
153         this.definition = new PfConceptKey(copyConcept.definition);
154         this.participantType = new PfConceptKey(copyConcept.participantType);
155         this.participantId = copyConcept.participantId;
156         this.state = copyConcept.state;
157         this.orderedState = copyConcept.orderedState;
158         this.description = copyConcept.description;
159         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
160     }
161
162     /**
163      * Authorative constructor.
164      *
165      * @param authorativeConcept the authorative concept to copy from
166      */
167     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
168         this.fromAuthorative(authorativeConcept);
169     }
170
171     @Override
172     public AutomationCompositionElement toAuthorative() {
173         var element = new AutomationCompositionElement();
174
175         element.setId(UUID.fromString(elementId));
176         element.setDefinition(new ToscaConceptIdentifier(definition));
177         element.setParticipantType(new ToscaConceptIdentifier(participantType));
178         element.setParticipantId(UUID.fromString(participantId));
179         element.setState(state);
180         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
181         element.setDescription(description);
182         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
183
184         return element;
185     }
186
187     @Override
188     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
189         this.definition = element.getDefinition().asConceptKey();
190         this.participantType = element.getParticipantType().asConceptKey();
191         this.participantId = element.getParticipantId().toString();
192         this.state = element.getState();
193         this.orderedState = element.getOrderedState();
194         this.description = element.getDescription();
195         properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
196     }
197
198     @Override
199     public int compareTo(final JpaAutomationCompositionElement other) {
200         if (other == null) {
201             return -1;
202         }
203         if (this == other) {
204             return 0;
205         }
206
207         var result = ObjectUtils.compare(elementId, other.elementId);
208         if (result != 0) {
209             return result;
210         }
211
212         result = ObjectUtils.compare(instanceId, other.instanceId);
213         if (result != 0) {
214             return result;
215         }
216
217         result = definition.compareTo(other.definition);
218         if (result != 0) {
219             return result;
220         }
221
222         result = participantType.compareTo(other.participantType);
223         if (result != 0) {
224             return result;
225         }
226
227         result = participantId.compareTo(other.participantId);
228         if (result != 0) {
229             return result;
230         }
231
232         result = ObjectUtils.compare(state, other.state);
233         if (result != 0) {
234             return result;
235         }
236
237         result = ObjectUtils.compare(orderedState, other.orderedState);
238         if (result != 0) {
239             return result;
240         }
241
242         return ObjectUtils.compare(description, other.description);
243     }
244 }