4c3a6a3be8d69ac7f22a5001e7578d6d9a8cfe59
[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     @NotNull
85     private String participantId;
86
87     @Column
88     @NotNull
89     private DeployState deployState;
90
91     @Column
92     @NotNull
93     private LockState lockState;
94
95     @Column
96     private String description;
97
98     @Lob
99     @NotNull
100     @Valid
101     @Convert(converter = StringToMapConverter.class)
102     private Map<String, Object> properties;
103
104     /**
105      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
106      */
107     public JpaAutomationCompositionElement() {
108         this(UUID.randomUUID().toString(), UUID.randomUUID().toString());
109     }
110
111     /**
112      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
113      *
114      * @param elementId The id of the automation composition instance Element
115      * @param instanceId The id of the automation composition instance
116      */
117     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId) {
118         this(elementId, instanceId, new PfConceptKey(),
119             DeployState.UNDEPLOYED, LockState.LOCKED);
120     }
121
122     /**
123      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
124      *
125      * @param elementId The id of the automation composition instance Element
126      * @param instanceId The id of the automation composition instance
127      * @param definition the TOSCA definition of the automation composition element
128      * @param deployState the Deploy State of the automation composition
129      * @param lockState the Lock State of the automation composition
130      */
131     public JpaAutomationCompositionElement(@NonNull final String elementId, @NonNull final String instanceId,
132             @NonNull final PfConceptKey definition,
133             @NonNull final DeployState deployState, @NonNull final LockState lockState) {
134         this.elementId = elementId;
135         this.instanceId = instanceId;
136         this.definition = definition;
137         this.deployState = deployState;
138         this.lockState = lockState;
139     }
140
141     /**
142      * Copy constructor.
143      *
144      * @param copyConcept the concept to copy from
145      */
146     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
147         this.elementId = copyConcept.elementId;
148         this.instanceId = copyConcept.instanceId;
149         this.definition = new PfConceptKey(copyConcept.definition);
150         this.participantId = copyConcept.participantId;
151         this.description = copyConcept.description;
152         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
153         this.deployState = copyConcept.deployState;
154         this.lockState = copyConcept.lockState;
155     }
156
157     /**
158      * Authorative constructor.
159      *
160      * @param authorativeConcept the authorative concept to copy from
161      */
162     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
163         this.fromAuthorative(authorativeConcept);
164     }
165
166     @Override
167     public AutomationCompositionElement toAuthorative() {
168         var element = new AutomationCompositionElement();
169
170         element.setId(UUID.fromString(elementId));
171         element.setDefinition(new ToscaConceptIdentifier(definition));
172         element.setParticipantId(UUID.fromString(participantId));
173         element.setDescription(description);
174         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
175         element.setDeployState(deployState);
176         element.setLockState(lockState);
177
178         return element;
179     }
180
181     @Override
182     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
183         this.definition = element.getDefinition().asConceptKey();
184         this.participantId = element.getParticipantId().toString();
185         this.description = element.getDescription();
186         this.properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
187         this.deployState = element.getDeployState();
188         this.lockState = element.getLockState();
189     }
190
191     @Override
192     public int compareTo(final JpaAutomationCompositionElement other) {
193         if (other == null) {
194             return -1;
195         }
196         if (this == other) {
197             return 0;
198         }
199
200         var result = ObjectUtils.compare(elementId, other.elementId);
201         if (result != 0) {
202             return result;
203         }
204
205         result = ObjectUtils.compare(instanceId, other.instanceId);
206         if (result != 0) {
207             return result;
208         }
209
210         result = definition.compareTo(other.definition);
211         if (result != 0) {
212             return result;
213         }
214
215         result = participantId.compareTo(other.participantId);
216         if (result != 0) {
217             return result;
218         }
219
220         result = ObjectUtils.compare(deployState, other.deployState);
221         if (result != 0) {
222             return result;
223         }
224
225         result = ObjectUtils.compare(lockState, other.lockState);
226         if (result != 0) {
227             return result;
228         }
229
230         return ObjectUtils.compare(description, other.description);
231     }
232 }