d0dcd797f46c6b9bc12f0ca09547fdd2a364f4bd
[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.AutomationCompositionOrderedState;
45 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
46 import org.onap.policy.clamp.models.acm.concepts.DeployState;
47 import org.onap.policy.clamp.models.acm.concepts.LockState;
48 import org.onap.policy.common.parameters.annotations.NotNull;
49 import org.onap.policy.common.parameters.annotations.Valid;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfUtils;
53 import org.onap.policy.models.base.Validated;
54 import org.onap.policy.models.base.validation.annotations.VerifyKey;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56
57 /**
58  * Class to represent a participant automation composition element in the database.
59  *
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62 @Entity
63 @Table(name = "AutomationCompositionElement")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaAutomationCompositionElement extends Validated
68     implements PfAuthorative<AutomationCompositionElement>, Comparable<JpaAutomationCompositionElement> {
69
70     @Id
71     @NotNull
72     private String elementId;
73
74     @Column
75     @NotNull
76     private String instanceId;
77
78     // @formatter:off
79     @VerifyKey
80     @NotNull
81     @AttributeOverrides({
82         @AttributeOverride(name = "name",    column = @Column(name = "definition_name")),
83         @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
84     })
85     private PfConceptKey definition;
86
87     @NotNull
88     private String participantId;
89
90     @Column
91     @NotNull
92     private AutomationCompositionState state;
93
94     @Column
95     @NotNull
96     private AutomationCompositionOrderedState orderedState;
97
98     @Column
99     @NotNull
100     private DeployState deployState;
101
102     @Column
103     @NotNull
104     private LockState lockState;
105
106     @Column
107     private String description;
108
109     @Lob
110     @NotNull
111     @Valid
112     @Convert(converter = StringToMapConverter.class)
113     private Map<String, Object> properties;
114
115     /**
116      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
117      */
118     public JpaAutomationCompositionElement() {
119         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
120     }
121
122     /**
123      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
124      *
125      * @param elementId The id of the automation composition instance Element
126      * @param instanceId The id of the automation composition instance
127      */
128     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
129         this(elementId, instanceId, new PfConceptKey(),
130             AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED);
131     }
132
133     /**
134      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
135      *
136      * @param elementId The id of the automation composition instance Element
137      * @param instanceId The id of the automation composition instance
138      * @param definition the TOSCA definition of the automation composition element
139      * @param state the state of the automation composition
140      */
141     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
142             @NonNull final PfConceptKey definition,
143             @NonNull final AutomationCompositionState state,
144                                            @NonNull final DeployState deployState, @NonNull final LockState lockState) {
145         this.elementId = elementId;
146         this.instanceId = instanceId;
147         this.definition = definition;
148         this.state = state;
149         this.deployState = deployState;
150         this.lockState = lockState;
151     }
152
153     /**
154      * Copy constructor.
155      *
156      * @param copyConcept the concept to copy from
157      */
158     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
159         this.elementId = copyConcept.elementId;
160         this.instanceId = copyConcept.instanceId;
161         this.definition = new PfConceptKey(copyConcept.definition);
162         this.participantId = copyConcept.participantId;
163         this.state = copyConcept.state;
164         this.orderedState = copyConcept.orderedState;
165         this.description = copyConcept.description;
166         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
167         this.deployState = copyConcept.deployState;
168         this.lockState = copyConcept.lockState;
169     }
170
171     /**
172      * Authorative constructor.
173      *
174      * @param authorativeConcept the authorative concept to copy from
175      */
176     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
177         this.fromAuthorative(authorativeConcept);
178     }
179
180     @Override
181     public AutomationCompositionElement toAuthorative() {
182         var element = new AutomationCompositionElement();
183
184         element.setId(UUID.fromString(elementId));
185         element.setDefinition(new ToscaConceptIdentifier(definition));
186         element.setParticipantId(UUID.fromString(participantId));
187         element.setState(state);
188         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
189         element.setDescription(description);
190         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
191         element.setDeployState(deployState);
192         element.setLockState(lockState);
193
194         return element;
195     }
196
197     @Override
198     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
199         this.definition = element.getDefinition().asConceptKey();
200         this.participantId = element.getParticipantId().toString();
201         this.state = element.getState();
202         this.orderedState = element.getOrderedState();
203         this.description = element.getDescription();
204         properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
205         this.deployState = element.getDeployState();
206         this.lockState = element.getLockState();
207     }
208
209     @Override
210     public int compareTo(final JpaAutomationCompositionElement other) {
211         if (other == null) {
212             return -1;
213         }
214         if (this == other) {
215             return 0;
216         }
217
218         var result = ObjectUtils.compare(elementId, other.elementId);
219         if (result != 0) {
220             return result;
221         }
222
223         result = ObjectUtils.compare(instanceId, other.instanceId);
224         if (result != 0) {
225             return result;
226         }
227
228         result = definition.compareTo(other.definition);
229         if (result != 0) {
230             return result;
231         }
232
233         result = participantId.compareTo(other.participantId);
234         if (result != 0) {
235             return result;
236         }
237
238         result = ObjectUtils.compare(state, other.state);
239         if (result != 0) {
240             return result;
241         }
242
243         result = ObjectUtils.compare(orderedState, other.orderedState);
244         if (result != 0) {
245             return result;
246         }
247
248         result = ObjectUtils.compare(deployState, other.deployState);
249         if (result != 0) {
250             return result;
251         }
252
253         result = ObjectUtils.compare(lockState, other.lockState);
254         if (result != 0) {
255             return result;
256         }
257
258         return ObjectUtils.compare(description, other.description);
259     }
260 }