cac405abd83b00f6a2c30740f624014a4add0162
[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.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.UUID;
28 import javax.persistence.AttributeOverride;
29 import javax.persistence.AttributeOverrides;
30 import javax.persistence.CascadeType;
31 import javax.persistence.Column;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.ManyToMany;
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     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
100     @NotNull
101     private Map<@NotNull UUID, @NotNull @Valid JpaControlLoopElement> elements;
102
103     /**
104      * The Default Constructor creates a {@link JpaControlLoop} object with a null key.
105      */
106     public JpaControlLoop() {
107         this(new PfConceptKey());
108     }
109
110     /**
111      * The Key Constructor creates a {@link JpaControlLoop} object with the given concept key.
112      *
113      * @param key the key
114      */
115     public JpaControlLoop(@NonNull final PfConceptKey key) {
116         this(key, new PfConceptKey(), ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
117     }
118
119     /**
120      * The Key Constructor creates a {@link JpaControlLoop} object with all mandatory fields.
121      *
122      * @param key the key
123      * @param definition the TOSCA definition of the control loop
124      * @param state the state of the control loop
125      * @param elements the elements of the control looop in participants
126      */
127     public JpaControlLoop(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
128             @NonNull final ControlLoopState state, @NonNull final Map<UUID, JpaControlLoopElement> elements) {
129         this.key = key;
130         this.definition = definition;
131         this.state = state;
132         this.elements = elements;
133     }
134
135     /**
136      * Copy constructor.
137      *
138      * @param copyConcept the concept to copy from
139      */
140     public JpaControlLoop(@NonNull final JpaControlLoop copyConcept) {
141         super(copyConcept);
142         this.key = new PfConceptKey(copyConcept.key);
143         this.definition = new PfConceptKey(copyConcept.definition);
144         this.state = copyConcept.state;
145         this.orderedState = copyConcept.orderedState;
146         this.description = copyConcept.description;
147         this.elements = PfUtils.mapMap(copyConcept.elements, JpaControlLoopElement::new, new LinkedHashMap<>(0));
148     }
149
150     /**
151      * Authorative constructor.
152      *
153      * @param authorativeConcept the authorative concept to copy from
154      */
155     public JpaControlLoop(@NonNull final ControlLoop authorativeConcept) {
156         this.fromAuthorative(authorativeConcept);
157     }
158
159     @Override
160     public ControlLoop toAuthorative() {
161         ControlLoop controlLoop = new ControlLoop();
162
163         controlLoop.setName(getKey().getName());
164         controlLoop.setVersion(getKey().getVersion());
165         controlLoop.setDefinition(new ToscaConceptIdentifier(definition));
166         controlLoop.setState(state);
167         controlLoop.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
168         controlLoop.setDescription(description);
169         controlLoop.setElements(PfUtils.mapMap(elements, JpaControlLoopElement::toAuthorative, new LinkedHashMap<>(0)));
170
171         return controlLoop;
172     }
173
174     @Override
175     public void fromAuthorative(@NonNull final ControlLoop controlLoop) {
176         if (this.key == null || this.getKey().isNullKey()) {
177             this.setKey(new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
178         }
179
180         this.definition = controlLoop.getDefinition().asConceptKey();
181         this.state = controlLoop.getState();
182         this.orderedState = controlLoop.getOrderedState();
183         this.description = controlLoop.getDescription();
184
185         this.elements = new LinkedHashMap<>(controlLoop.getElements().size());
186         for (Entry<UUID, ControlLoopElement> elementEntry : controlLoop.getElements().entrySet()) {
187             JpaControlLoopElement jpaControlLoopElement = new JpaControlLoopElement();
188             jpaControlLoopElement.setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
189             jpaControlLoopElement.fromAuthorative(elementEntry.getValue());
190             this.elements.put(elementEntry.getKey(), jpaControlLoopElement);
191         }
192     }
193
194     @Override
195     public List<PfKey> getKeys() {
196         List<PfKey> keyList = getKey().getKeys();
197
198         keyList.add(definition);
199
200         for (JpaControlLoopElement element : elements.values()) {
201             keyList.addAll(element.getKeys());
202         }
203
204         return keyList;
205     }
206
207     @Override
208     public void clean() {
209         key.clean();
210         definition.clean();
211         description = (description == null ? null : description.trim());
212
213         for (JpaControlLoopElement element : elements.values()) {
214             element.clean();
215         }
216     }
217
218     @Override
219     public int compareTo(final PfConcept otherConcept) {
220         if (otherConcept == null) {
221             return -1;
222         }
223         if (this == otherConcept) {
224             return 0;
225         }
226         if (getClass() != otherConcept.getClass()) {
227             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
228         }
229
230         final JpaControlLoop other = (JpaControlLoop) otherConcept;
231         int result = key.compareTo(other.key);
232         if (result != 0) {
233             return result;
234         }
235
236         result = definition.compareTo(other.definition);
237         if (result != 0) {
238             return result;
239         }
240
241         result = ObjectUtils.compare(state, other.state);
242         if (result != 0) {
243             return result;
244         }
245
246         result = ObjectUtils.compare(orderedState, other.orderedState);
247         if (result != 0) {
248             return result;
249         }
250
251         result = ObjectUtils.compare(description, other.description);
252         if (result != 0) {
253             return result;
254         }
255
256         return PfUtils.compareObjects(elements, other.elements);
257     }
258 }