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