79576f6eb135a39ec53d775ece0265be097e5131
[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     @NotNull
89     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
90     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
91     private PfConceptKey participantId;
92     // @formatter:on
93
94     @Column
95     @NotNull
96     private AutomationCompositionState state;
97
98     @Column
99     @NotNull
100     private AutomationCompositionOrderedState orderedState;
101
102     @Column
103     private String description;
104
105     @Lob
106     @NotNull
107     @Valid
108     @Convert(converter = StringToMapConverter.class)
109     private Map<String, Object> properties;
110
111     /**
112      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
113      */
114     public JpaAutomationCompositionElement() {
115         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
116     }
117
118     /**
119      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
120      *
121      * @param elementId The id of the automation composition instance Element
122      * @param instanceId The id of the automation composition instance
123      */
124     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
125         this(elementId, instanceId, new PfConceptKey(), new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
126     }
127
128     /**
129      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
130      *
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
136      */
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;
144         this.state = state;
145     }
146
147     /**
148      * Copy constructor.
149      *
150      * @param copyConcept the concept to copy from
151      */
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);
162     }
163
164     /**
165      * Authorative constructor.
166      *
167      * @param authorativeConcept the authorative concept to copy from
168      */
169     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
170         this.fromAuthorative(authorativeConcept);
171     }
172
173     @Override
174     public AutomationCompositionElement toAuthorative() {
175         var element = new AutomationCompositionElement();
176
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()));
185
186         return element;
187     }
188
189     @Override
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());
198     }
199
200     @Override
201     public int compareTo(final JpaAutomationCompositionElement other) {
202         if (other == null) {
203             return -1;
204         }
205         if (this == other) {
206             return 0;
207         }
208
209         var result = ObjectUtils.compare(elementId, other.elementId);
210         if (result != 0) {
211             return result;
212         }
213
214         result = ObjectUtils.compare(instanceId, other.instanceId);
215         if (result != 0) {
216             return result;
217         }
218
219         result = definition.compareTo(other.definition);
220         if (result != 0) {
221             return result;
222         }
223
224         result = participantType.compareTo(other.participantType);
225         if (result != 0) {
226             return result;
227         }
228
229         result = participantId.compareTo(other.participantId);
230         if (result != 0) {
231             return result;
232         }
233
234         result = ObjectUtils.compare(state, other.state);
235         if (result != 0) {
236             return result;
237         }
238
239         result = ObjectUtils.compare(orderedState, other.orderedState);
240         if (result != 0) {
241             return result;
242         }
243
244         return ObjectUtils.compare(description, other.description);
245     }
246 }