4020243bb342d507d1a13c4ae369a47521272514
[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.clamp.models.acm.concepts.DeployState;
46 import org.onap.policy.clamp.models.acm.concepts.LockState;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.base.Validated;
53 import org.onap.policy.models.base.validation.annotations.VerifyKey;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
55
56 /**
57  * Class to represent a participant automation composition element in the database.
58  *
59  * @author Liam Fallon (liam.fallon@est.tech)
60  */
61 @Entity
62 @Table(name = "AutomationCompositionElement")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @EqualsAndHashCode(callSuper = false)
66 public class JpaAutomationCompositionElement extends Validated
67         implements PfAuthorative<AutomationCompositionElement>, Comparable<JpaAutomationCompositionElement> {
68
69     @Id
70     @NotNull
71     private String elementId;
72
73     @Column
74     @NotNull
75     private String instanceId;
76
77     // @formatter:off
78     @VerifyKey
79     @NotNull
80     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
81     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
82     private PfConceptKey definition;
83
84     @NotNull
85     private String participantId;
86
87     @Column
88     @NotNull
89     private AutomationCompositionState state;
90
91     @Column
92     @NotNull
93     private AutomationCompositionOrderedState orderedState;
94
95     @Column
96     @NotNull
97     private DeployState deployState;
98
99     @Column
100     @NotNull
101     private LockState lockState;
102
103     @Column
104     private String description;
105
106     @Lob
107     @NotNull
108     @Valid
109     @Convert(converter = StringToMapConverter.class)
110     private Map<String, Object> properties;
111
112     /**
113      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
114      */
115     public JpaAutomationCompositionElement() {
116         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
117     }
118
119     /**
120      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
121      *
122      * @param elementId The id of the automation composition instance Element
123      * @param instanceId The id of the automation composition instance
124      */
125     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
126         this(elementId, instanceId, new PfConceptKey(),
127             AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED);
128     }
129
130     /**
131      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
132      *
133      * @param elementId The id of the automation composition instance Element
134      * @param instanceId The id of the automation composition instance
135      * @param definition the TOSCA definition of the automation composition element
136      * @param state the state of the automation composition
137      */
138     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
139             @NonNull final PfConceptKey definition,
140             @NonNull final AutomationCompositionState state,
141                                            @NonNull final DeployState deployState, @NonNull final LockState lockState) {
142         this.elementId = elementId;
143         this.instanceId = instanceId;
144         this.definition = definition;
145         this.state = state;
146         this.deployState = deployState;
147         this.lockState = lockState;
148     }
149
150     /**
151      * Copy constructor.
152      *
153      * @param copyConcept the concept to copy from
154      */
155     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
156         this.elementId = copyConcept.elementId;
157         this.instanceId = copyConcept.instanceId;
158         this.definition = new PfConceptKey(copyConcept.definition);
159         this.participantId = copyConcept.participantId;
160         this.state = copyConcept.state;
161         this.orderedState = copyConcept.orderedState;
162         this.description = copyConcept.description;
163         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
164         this.deployState = copyConcept.deployState;
165         this.lockState = copyConcept.lockState;
166     }
167
168     /**
169      * Authorative constructor.
170      *
171      * @param authorativeConcept the authorative concept to copy from
172      */
173     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
174         this.fromAuthorative(authorativeConcept);
175     }
176
177     @Override
178     public AutomationCompositionElement toAuthorative() {
179         var element = new AutomationCompositionElement();
180
181         element.setId(UUID.fromString(elementId));
182         element.setDefinition(new ToscaConceptIdentifier(definition));
183         element.setParticipantId(UUID.fromString(participantId));
184         element.setState(state);
185         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
186         element.setDescription(description);
187         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
188         element.setDeployState(deployState);
189         element.setLockState(lockState);
190
191         return element;
192     }
193
194     @Override
195     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
196         this.definition = element.getDefinition().asConceptKey();
197         this.participantId = element.getParticipantId().toString();
198         this.state = element.getState();
199         this.orderedState = element.getOrderedState();
200         this.description = element.getDescription();
201         properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
202         this.deployState = element.getDeployState();
203         this.lockState = element.getLockState();
204     }
205
206     @Override
207     public int compareTo(final JpaAutomationCompositionElement other) {
208         if (other == null) {
209             return -1;
210         }
211         if (this == other) {
212             return 0;
213         }
214
215         var result = ObjectUtils.compare(elementId, other.elementId);
216         if (result != 0) {
217             return result;
218         }
219
220         result = ObjectUtils.compare(instanceId, other.instanceId);
221         if (result != 0) {
222             return result;
223         }
224
225         result = definition.compareTo(other.definition);
226         if (result != 0) {
227             return result;
228         }
229
230         result = participantId.compareTo(other.participantId);
231         if (result != 0) {
232             return result;
233         }
234
235         result = ObjectUtils.compare(state, other.state);
236         if (result != 0) {
237             return result;
238         }
239
240         result = ObjectUtils.compare(orderedState, other.orderedState);
241         if (result != 0) {
242             return result;
243         }
244
245         result = ObjectUtils.compare(deployState, other.deployState);
246         if (result != 0) {
247             return result;
248         }
249
250         result = ObjectUtils.compare(lockState, other.lockState);
251         if (result != 0) {
252             return result;
253         }
254
255         return ObjectUtils.compare(description, other.description);
256     }
257 }