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