15f5479e19dd5cfdb585029681ebae52ed96a749
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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 jakarta.persistence.AttributeOverride;
26 import jakarta.persistence.Column;
27 import jakarta.persistence.Convert;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Id;
30 import jakarta.persistence.Inheritance;
31 import jakarta.persistence.InheritanceType;
32 import jakarta.persistence.Table;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.UUID;
36 import java.util.function.UnaryOperator;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.SubState;
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     @VerifyKey
76     @NotNull
77     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
78     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
79     private PfConceptKey definition;
80
81     @Column
82     @NotNull
83     private String participantId;
84
85     @Column
86     private Boolean restarting;
87
88     @Column
89     @NotNull
90     private DeployState deployState;
91
92     @Column
93     @NotNull
94     private LockState lockState;
95
96     @Column
97     @NotNull
98     private SubState subState;
99
100     @Column
101     private String operationalState;
102
103     @Column
104     private String useState;
105
106     @Column
107     private Integer stage;
108
109     @Column
110     private String description;
111
112     @Column
113     private String message;
114
115     @NotNull
116     @Valid
117     @Convert(converter = StringToMapConverter.class)
118     @Column(length = 100000)
119     private Map<String, Object> properties;
120
121     @NotNull
122     @Valid
123     @Convert(converter = StringToMapConverter.class)
124     @Column(length = 100000)
125     private Map<String, Object> outProperties;
126
127     /**
128      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
129      */
130     public JpaAutomationCompositionElement() {
131         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
132     }
133
134     /**
135      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
136      *
137      * @param elementId The id of the automation composition instance Element
138      * @param instanceId The id of the automation composition instance
139      */
140     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
141         this(elementId, instanceId, new PfConceptKey(),
142             DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
143     }
144
145     /**
146      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
147      *
148      * @param elementId The id of the automation composition instance Element
149      * @param instanceId The id of the automation composition instance
150      * @param definition the TOSCA definition of the automation composition element
151      * @param deployState the Deploy State of the automation composition
152      * @param lockState the Lock State of the automation composition
153      * @param subState the Sub State of the automation composition
154      */
155     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
156             @NonNull final PfConceptKey definition,
157             @NonNull final DeployState deployState, @NonNull final LockState lockState,
158             @NonNull final SubState subState) {
159         this.elementId = elementId;
160         this.instanceId = instanceId;
161         this.definition = definition;
162         this.deployState = deployState;
163         this.lockState = lockState;
164         this.subState = subState;
165     }
166
167     /**
168      * Copy constructor.
169      *
170      * @param copyConcept the concept to copy from
171      */
172     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
173         this.elementId = copyConcept.elementId;
174         this.instanceId = copyConcept.instanceId;
175         this.definition = new PfConceptKey(copyConcept.definition);
176         this.participantId = copyConcept.participantId;
177         this.description = copyConcept.description;
178         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
179         this.outProperties =
180                 (copyConcept.outProperties != null ? new LinkedHashMap<>(copyConcept.outProperties)
181                         : null);
182         this.restarting = copyConcept.restarting;
183         this.deployState = copyConcept.deployState;
184         this.lockState = copyConcept.lockState;
185         this.subState = copyConcept.subState;
186         this.operationalState = copyConcept.operationalState;
187         this.useState = copyConcept.useState;
188         this.stage = copyConcept.stage;
189         this.message = copyConcept.message;
190     }
191
192     /**
193      * Authorative constructor.
194      *
195      * @param authorativeConcept the authorative concept to copy from
196      */
197     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
198         this.fromAuthorative(authorativeConcept);
199     }
200
201     @Override
202     public AutomationCompositionElement toAuthorative() {
203         var element = new AutomationCompositionElement();
204
205         element.setId(UUID.fromString(elementId));
206         element.setDefinition(new ToscaConceptIdentifier(definition));
207         element.setParticipantId(UUID.fromString(participantId));
208         element.setDescription(description);
209         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
210         element.setOutProperties(PfUtils.mapMap(outProperties, UnaryOperator.identity()));
211         element.setRestarting(restarting);
212         element.setDeployState(deployState);
213         element.setLockState(lockState);
214         element.setSubState(subState);
215         element.setOperationalState(operationalState);
216         element.setUseState(useState);
217         element.setStage(stage);
218         element.setMessage(message);
219
220         return element;
221     }
222
223     @Override
224     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
225         this.definition = element.getDefinition().asConceptKey();
226         this.participantId = element.getParticipantId().toString();
227         this.description = element.getDescription();
228         this.properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
229         this.outProperties = PfUtils.mapMap(element.getOutProperties(), UnaryOperator.identity());
230         this.restarting = element.getRestarting();
231         this.deployState = element.getDeployState();
232         this.lockState = element.getLockState();
233         this.subState = element.getSubState();
234         this.operationalState = element.getOperationalState();
235         this.useState = element.getUseState();
236         this.stage = element.getStage();
237         this.message = element.getMessage();
238     }
239
240     @Override
241     public int compareTo(final JpaAutomationCompositionElement other) {
242         if (other == null) {
243             return -1;
244         }
245         if (this == other) {
246             return 0;
247         }
248
249         var result = ObjectUtils.compare(elementId, other.elementId);
250         if (result != 0) {
251             return result;
252         }
253
254         result = ObjectUtils.compare(instanceId, other.instanceId);
255         if (result != 0) {
256             return result;
257         }
258
259         result = definition.compareTo(other.definition);
260         if (result != 0) {
261             return result;
262         }
263
264         result = participantId.compareTo(other.participantId);
265         if (result != 0) {
266             return result;
267         }
268
269         result = ObjectUtils.compare(restarting, other.restarting);
270         if (result != 0) {
271             return result;
272         }
273
274         result = ObjectUtils.compare(deployState, other.deployState);
275         if (result != 0) {
276             return result;
277         }
278
279         result = ObjectUtils.compare(lockState, other.lockState);
280         if (result != 0) {
281             return result;
282         }
283
284         result = ObjectUtils.compare(subState, other.subState);
285         if (result != 0) {
286             return result;
287         }
288
289         result = ObjectUtils.compare(useState, other.useState);
290         if (result != 0) {
291             return result;
292         }
293
294         result = ObjectUtils.compare(stage, other.stage);
295         if (result != 0) {
296             return result;
297         }
298
299         result = ObjectUtils.compare(operationalState, other.operationalState);
300         if (result != 0) {
301             return result;
302         }
303
304         result = ObjectUtils.compare(message, other.message);
305         if (result != 0) {
306             return result;
307         }
308         return ObjectUtils.compare(description, other.description);
309     }
310 }