2255aed5b111cf5748723a30fe39d1ccebe00c50
[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     @NotNull
87     private DeployState deployState;
88
89     @Column
90     @NotNull
91     private LockState lockState;
92
93     @Column
94     @NotNull
95     private SubState subState;
96
97     @Column
98     private String operationalState;
99
100     @Column
101     private String useState;
102
103     @Column
104     private Integer stage;
105
106     @Column
107     private String description;
108
109     @Column
110     private String message;
111
112     @NotNull
113     @Valid
114     @Convert(converter = StringToMapConverter.class)
115     @Column(length = 100000)
116     private Map<String, Object> properties;
117
118     @NotNull
119     @Valid
120     @Convert(converter = StringToMapConverter.class)
121     @Column(length = 100000)
122     private Map<String, Object> outProperties;
123
124     /**
125      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
126      */
127     public JpaAutomationCompositionElement() {
128         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
129     }
130
131     /**
132      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
133      *
134      * @param elementId The id of the automation composition instance Element
135      * @param instanceId The id of the automation composition instance
136      */
137     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
138         this(elementId, instanceId, new PfConceptKey(),
139             DeployState.UNDEPLOYED, LockState.NONE, SubState.NONE);
140     }
141
142     /**
143      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
144      *
145      * @param elementId The id of the automation composition instance Element
146      * @param instanceId The id of the automation composition instance
147      * @param definition the TOSCA definition of the automation composition element
148      * @param deployState the Deploy State of the automation composition
149      * @param lockState the Lock State of the automation composition
150      * @param subState the Sub State of the automation composition
151      */
152     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
153             @NonNull final PfConceptKey definition,
154             @NonNull final DeployState deployState, @NonNull final LockState lockState,
155             @NonNull final SubState subState) {
156         this.elementId = elementId;
157         this.instanceId = instanceId;
158         this.definition = definition;
159         this.deployState = deployState;
160         this.lockState = lockState;
161         this.subState = subState;
162     }
163
164     /**
165      * Copy constructor.
166      *
167      * @param copyConcept the concept to copy from
168      */
169     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
170         this.elementId = copyConcept.elementId;
171         this.instanceId = copyConcept.instanceId;
172         this.definition = new PfConceptKey(copyConcept.definition);
173         this.participantId = copyConcept.participantId;
174         this.description = copyConcept.description;
175         this.properties = PfUtils.mapMap(copyConcept.properties, UnaryOperator.identity());
176         this.outProperties = PfUtils.mapMap(copyConcept.outProperties, UnaryOperator.identity());
177         this.deployState = copyConcept.deployState;
178         this.lockState = copyConcept.lockState;
179         this.subState = copyConcept.subState;
180         this.operationalState = copyConcept.operationalState;
181         this.useState = copyConcept.useState;
182         this.stage = copyConcept.stage;
183         this.message = copyConcept.message;
184     }
185
186     /**
187      * Authorative constructor.
188      *
189      * @param authorativeConcept the authorative concept to copy from
190      */
191     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
192         this.fromAuthorative(authorativeConcept);
193     }
194
195     @Override
196     public AutomationCompositionElement toAuthorative() {
197         var element = new AutomationCompositionElement();
198
199         element.setId(UUID.fromString(elementId));
200         element.setDefinition(new ToscaConceptIdentifier(definition));
201         element.setParticipantId(UUID.fromString(participantId));
202         element.setDescription(description);
203         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
204         element.setOutProperties(PfUtils.mapMap(outProperties, UnaryOperator.identity()));
205         element.setDeployState(deployState);
206         element.setLockState(lockState);
207         element.setSubState(subState);
208         element.setOperationalState(operationalState);
209         element.setUseState(useState);
210         element.setStage(stage);
211         element.setMessage(message);
212
213         return element;
214     }
215
216     @Override
217     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
218         this.definition = element.getDefinition().asConceptKey();
219         this.participantId = element.getParticipantId().toString();
220         this.description = element.getDescription();
221         this.properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
222         this.outProperties = PfUtils.mapMap(element.getOutProperties(), UnaryOperator.identity());
223         this.deployState = element.getDeployState();
224         this.lockState = element.getLockState();
225         this.subState = element.getSubState();
226         this.operationalState = element.getOperationalState();
227         this.useState = element.getUseState();
228         this.stage = element.getStage();
229         this.message = element.getMessage();
230     }
231
232     @Override
233     public int compareTo(final JpaAutomationCompositionElement other) {
234         if (other == null) {
235             return -1;
236         }
237         if (this == other) {
238             return 0;
239         }
240
241         var result = ObjectUtils.compare(elementId, other.elementId);
242         if (result != 0) {
243             return result;
244         }
245
246         result = ObjectUtils.compare(instanceId, other.instanceId);
247         if (result != 0) {
248             return result;
249         }
250
251         result = definition.compareTo(other.definition);
252         if (result != 0) {
253             return result;
254         }
255
256         result = participantId.compareTo(other.participantId);
257         if (result != 0) {
258             return result;
259         }
260
261         result = ObjectUtils.compare(deployState, other.deployState);
262         if (result != 0) {
263             return result;
264         }
265
266         result = ObjectUtils.compare(lockState, other.lockState);
267         if (result != 0) {
268             return result;
269         }
270
271         result = ObjectUtils.compare(subState, other.subState);
272         if (result != 0) {
273             return result;
274         }
275
276         result = ObjectUtils.compare(useState, other.useState);
277         if (result != 0) {
278             return result;
279         }
280
281         result = ObjectUtils.compare(stage, other.stage);
282         if (result != 0) {
283             return result;
284         }
285
286         result = ObjectUtils.compare(operationalState, other.operationalState);
287         if (result != 0) {
288             return result;
289         }
290
291         result = ObjectUtils.compare(message, other.message);
292         if (result != 0) {
293             return result;
294         }
295         return ObjectUtils.compare(description, other.description);
296     }
297 }