3f54ac2bb75a28602186e41e4e9ce7c870187b62
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import javax.persistence.AttributeOverride;
27 import javax.persistence.AttributeOverrides;
28 import javax.persistence.CascadeType;
29 import javax.persistence.CollectionTable;
30 import javax.persistence.Column;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.JoinColumn;
37 import javax.persistence.OneToMany;
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.controlloop.models.controlloop.concepts.ControlLoop;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
46 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
47 import org.onap.policy.common.parameters.annotations.NotNull;
48 import org.onap.policy.common.parameters.annotations.Valid;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.validation.annotations.VerifyKey;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
57
58 /**
59  * Class to represent a control loop in the database.
60  *
61  * @author Liam Fallon (liam.fallon@est.tech)
62  */
63 @Entity
64 @Table(name = "ControlLoop")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @Data
67 @EqualsAndHashCode(callSuper = false)
68 public class JpaControlLoop extends PfConcept implements PfAuthorative<ControlLoop> {
69     private static final long serialVersionUID = -4725410933242154805L;
70
71     @EmbeddedId
72     @VerifyKey
73     @NotNull
74     private PfConceptKey key;
75
76     // @formatter:off
77     @VerifyKey
78     @NotNull
79     @AttributeOverrides({
80             @AttributeOverride(name = "name",    column = @Column(name = "definition_name")),
81             @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
82         }
83     )
84     private PfConceptKey definition;
85     // @formatter:on
86
87     @Column
88     @NotNull
89     private ControlLoopState state;
90
91     @Column
92     @NotNull
93     private ControlLoopOrderedState orderedState;
94
95     @Column
96     private String description;
97
98     // @formatter:off
99     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
100     @CollectionTable(joinColumns = {
101             @JoinColumn(name = "controlLoopParentKeyName",    referencedColumnName = "parentKeyName"),
102             @JoinColumn(name = "controlLoopParentKeyVersion", referencedColumnName = "parentKeyVersion"),
103             @JoinColumn(name = "controlLoopParentLocalName",  referencedColumnName = "parentLocalName"),
104             @JoinColumn(name = "controlLoopLocalUUID",        referencedColumnName = "localUUID")
105         })
106     // @formatter:on
107     @NotNull
108     private List<@NotNull @Valid JpaControlLoopElement> elements;
109
110     /**
111      * The Default Constructor creates a {@link JpaControlLoop} object with a null key.
112      */
113     public JpaControlLoop() {
114         this(new PfConceptKey());
115     }
116
117     /**
118      * The Key Constructor creates a {@link JpaControlLoop} object with the given concept key.
119      *
120      * @param key the key
121      */
122     public JpaControlLoop(@NonNull final PfConceptKey key) {
123         this(key, new PfConceptKey(), ControlLoopState.UNINITIALISED, new ArrayList<>());
124     }
125
126     /**
127      * The Key Constructor creates a {@link JpaControlLoop} object with all mandatory fields.
128      *
129      * @param key the key
130      * @param definition the TOSCA definition of the control loop
131      * @param state the state of the control loop
132      * @param elements the elements of the control looop in participants
133      */
134     public JpaControlLoop(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
135             @NonNull final ControlLoopState state, @NonNull final List<JpaControlLoopElement> elements) {
136         this.key = key;
137         this.definition = definition;
138         this.state = state;
139         this.elements = elements;
140     }
141
142     /**
143      * Copy constructor.
144      *
145      * @param copyConcept the concept to copy from
146      */
147     public JpaControlLoop(@NonNull final JpaControlLoop copyConcept) {
148         super(copyConcept);
149         this.key = new PfConceptKey(copyConcept.key);
150         this.definition = new PfConceptKey(copyConcept.definition);
151         this.state = copyConcept.state;
152         this.orderedState = copyConcept.orderedState;
153         this.description = copyConcept.description;
154         this.elements = PfUtils.mapList(copyConcept.elements, JpaControlLoopElement::new, new ArrayList<>(0));
155     }
156
157     /**
158      * Authorative constructor.
159      *
160      * @param authorativeConcept the authorative concept to copy from
161      */
162     public JpaControlLoop(@NonNull final ControlLoop authorativeConcept) {
163         this.fromAuthorative(authorativeConcept);
164     }
165
166     @Override
167     public ControlLoop toAuthorative() {
168         ControlLoop controlLoop = new ControlLoop();
169
170         controlLoop.setName(getKey().getName());
171         controlLoop.setVersion(getKey().getVersion());
172         controlLoop.setDefinition(new ToscaConceptIdentifier(definition));
173         controlLoop.setState(state);
174         controlLoop.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
175         controlLoop.setDescription(description);
176
177         controlLoop
178                 .setElements(elements.stream().map(JpaControlLoopElement::toAuthorative).collect(Collectors.toList()));
179
180         return controlLoop;
181     }
182
183     @Override
184     public void fromAuthorative(@NonNull final ControlLoop controlLoop) {
185         if (this.key == null || this.getKey().isNullKey()) {
186             this.setKey(new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
187         }
188
189         this.definition = controlLoop.getDefinition().asConceptKey();
190         this.state = controlLoop.getState();
191         this.orderedState = controlLoop.getOrderedState();
192         this.description = controlLoop.getDescription();
193
194         this.elements = new ArrayList<>(controlLoop.getElements().size());
195         for (ControlLoopElement element : controlLoop.getElements()) {
196             JpaControlLoopElement jpaControlLoopElement = new JpaControlLoopElement();
197             jpaControlLoopElement.setKey(new PfReferenceKey(getKey(), element.getId().toString()));
198             jpaControlLoopElement.fromAuthorative(element);
199             this.elements.add(jpaControlLoopElement);
200         }
201     }
202
203     @Override
204     public List<PfKey> getKeys() {
205         List<PfKey> keyList = getKey().getKeys();
206
207         keyList.add(definition);
208
209         for (JpaControlLoopElement element : elements) {
210             keyList.addAll(element.getKeys());
211         }
212
213         return keyList;
214     }
215
216     @Override
217     public void clean() {
218         key.clean();
219         definition.clean();
220         description = (description == null ? null : description.trim());
221
222         for (JpaControlLoopElement element : elements) {
223             element.clean();
224         }
225     }
226
227     @Override
228     public int compareTo(final PfConcept otherConcept) {
229         if (otherConcept == null) {
230             return -1;
231         }
232         if (this == otherConcept) {
233             return 0;
234         }
235         if (getClass() != otherConcept.getClass()) {
236             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
237         }
238
239         final JpaControlLoop other = (JpaControlLoop) otherConcept;
240         int result = key.compareTo(other.key);
241         if (result != 0) {
242             return result;
243         }
244
245         result = definition.compareTo(other.definition);
246         if (result != 0) {
247             return result;
248         }
249
250         result = ObjectUtils.compare(state, other.state);
251         if (result != 0) {
252             return result;
253         }
254
255         result = ObjectUtils.compare(orderedState, other.orderedState);
256         if (result != 0) {
257             return result;
258         }
259
260         result = ObjectUtils.compare(description, other.description);
261         if (result != 0) {
262             return result;
263         }
264
265         return PfUtils.compareObjects(elements, other.elements);
266     }
267 }