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