d8e4237b7ba1c76d32206bea6d49025fe19995d5
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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.List;
27 import java.util.Map;
28 import java.util.UUID;
29 import java.util.function.UnaryOperator;
30 import javax.persistence.AttributeOverride;
31 import javax.persistence.Column;
32 import javax.persistence.Convert;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
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.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.PfConcept;
50 import org.onap.policy.models.base.PfConceptKey;
51 import org.onap.policy.models.base.PfKey;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfUtils;
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 PfConcept implements PfAuthorative<AutomationCompositionElement> {
68     private static final long serialVersionUID = -1791732273187890213L;
69
70     @EmbeddedId
71     @VerifyKey
72     @NotNull
73     private PfReferenceKey key;
74
75     // @formatter:off
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     @VerifyKey
83     @NotNull
84     @AttributeOverride(name = "name",    column = @Column(name = "participant_type_name"))
85     @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
86     private PfConceptKey participantType;
87
88     @NotNull
89     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
90     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
91     private PfConceptKey participantId;
92     // @formatter:on
93
94     @Column
95     @NotNull
96     private AutomationCompositionState state;
97
98     @Column
99     @NotNull
100     private AutomationCompositionOrderedState orderedState;
101
102     @Column
103     private String description;
104
105     @Lob
106     @NotNull
107     @Valid
108     @Convert(converter = StringToMapConverter.class)
109     private Map<String, Object> properties;
110
111     /**
112      * The Default Constructor creates a {@link JpaAutomationCompositionElement} object with a null key.
113      */
114     public JpaAutomationCompositionElement() {
115         this(new PfReferenceKey());
116     }
117
118     /**
119      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with the given concept key.
120      *
121      * @param key the key
122      */
123     public JpaAutomationCompositionElement(@NonNull final PfReferenceKey key) {
124         this(key, new PfConceptKey(), new PfConceptKey(), AutomationCompositionState.UNINITIALISED);
125     }
126
127     /**
128      * The Key Constructor creates a {@link JpaAutomationCompositionElement} object with all mandatory fields.
129      *
130      * @param key the key
131      * @param definition the TOSCA definition of the automation composition element
132      * @param participantType the TOSCA definition of the participant running the automation composition element
133      * @param state the state of the automation composition
134      */
135     public JpaAutomationCompositionElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
136         @NonNull final PfConceptKey participantType, @NonNull final AutomationCompositionState state) {
137         this.key = key;
138         this.definition = definition;
139         this.participantType = participantType;
140         this.state = state;
141     }
142
143     /**
144      * Copy constructor.
145      *
146      * @param copyConcept the concept to copy from
147      */
148     public JpaAutomationCompositionElement(@NonNull final JpaAutomationCompositionElement copyConcept) {
149         super(copyConcept);
150         this.key = new PfReferenceKey(copyConcept.key);
151         this.definition = new PfConceptKey(copyConcept.definition);
152         this.participantType = new PfConceptKey(copyConcept.participantType);
153         this.participantId = new PfConceptKey(copyConcept.participantId);
154         this.state = copyConcept.state;
155         this.orderedState = copyConcept.orderedState;
156         this.description = copyConcept.description;
157         this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
158     }
159
160     /**
161      * Authorative constructor.
162      *
163      * @param authorativeConcept the authorative concept to copy from
164      */
165     public JpaAutomationCompositionElement(@NonNull final AutomationCompositionElement authorativeConcept) {
166         this.fromAuthorative(authorativeConcept);
167     }
168
169     @Override
170     public AutomationCompositionElement toAuthorative() {
171         var element = new AutomationCompositionElement();
172
173         element.setId(UUID.fromString(getKey().getLocalName()));
174         element.setDefinition(new ToscaConceptIdentifier(definition));
175         element.setParticipantType(new ToscaConceptIdentifier(participantType));
176         element.setParticipantId(new ToscaConceptIdentifier(participantId));
177         element.setState(state);
178         element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
179         element.setDescription(description);
180         element.setProperties(PfUtils.mapMap(properties, UnaryOperator.identity()));
181
182         return element;
183     }
184
185     @Override
186     public void fromAuthorative(@NonNull final AutomationCompositionElement element) {
187         if (this.key == null || this.getKey().isNullKey()) {
188             this.setKey(new PfReferenceKey());
189             getKey().setLocalName(element.getId().toString());
190         }
191
192         this.definition = element.getDefinition().asConceptKey();
193         this.participantType = element.getParticipantType().asConceptKey();
194         this.participantId = element.getParticipantId().asConceptKey();
195         this.state = element.getState();
196         this.orderedState = element.getOrderedState();
197         this.description = element.getDescription();
198         properties = PfUtils.mapMap(element.getProperties(), UnaryOperator.identity());
199     }
200
201     @Override
202     public List<PfKey> getKeys() {
203         List<PfKey> keyList = getKey().getKeys();
204
205         keyList.add(definition);
206         keyList.add(participantType);
207         keyList.add(participantId);
208
209         return keyList;
210     }
211
212     @Override
213     public void clean() {
214         key.clean();
215         definition.clean();
216         participantType.clean();
217         participantId.clean();
218
219         if (description != null) {
220             description = description.trim();
221         }
222     }
223
224     @Override
225     public int compareTo(final PfConcept otherConcept) {
226         if (otherConcept == null) {
227             return -1;
228         }
229         if (this == otherConcept) {
230             return 0;
231         }
232         if (getClass() != otherConcept.getClass()) {
233             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
234         }
235
236         final JpaAutomationCompositionElement other = (JpaAutomationCompositionElement) otherConcept;
237         int result = key.compareTo(other.key);
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 = participantType.compareTo(other.participantType);
248         if (result != 0) {
249             return result;
250         }
251
252         result = participantId.compareTo(other.participantId);
253         if (result != 0) {
254             return result;
255         }
256
257         result = ObjectUtils.compare(state, other.state);
258         if (result != 0) {
259             return result;
260         }
261
262         result = ObjectUtils.compare(orderedState, other.orderedState);
263         if (result != 0) {
264             return result;
265         }
266
267         return ObjectUtils.compare(description, other.description);
268     }
269 }