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