2a2cde2aee6db8475808d8e4b8dd3b8c90ba53b0
[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.CascadeType;
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.ManyToMany;
37 import javax.persistence.Table;
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.controlloop.models.controlloop.concepts.ControlLoop;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
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 control loop in the database.
59  *
60  * @author Liam Fallon (liam.fallon@est.tech)
61  */
62 @Entity
63 @Table(name = "ControlLoop")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @Data
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaControlLoop extends PfConcept implements PfAuthorative<ControlLoop> {
68     private static final long serialVersionUID = -4725410933242154805L;
69
70     @EmbeddedId
71     @VerifyKey
72     @NotNull
73     private PfConceptKey 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     // @formatter:on
82
83     @Column
84     @NotNull
85     private ControlLoopState state;
86
87     @Column
88     @NotNull
89     private ControlLoopOrderedState orderedState;
90
91     @Column
92     private String description;
93
94     // @formatter:off
95     @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
96     @NotNull
97     private Map<@NotNull UUID, @NotNull @Valid JpaControlLoopElement> elements;
98
99     /**
100      * The Default Constructor creates a {@link JpaControlLoop} object with a null key.
101      */
102     public JpaControlLoop() {
103         this(new PfConceptKey());
104     }
105
106     /**
107      * The Key Constructor creates a {@link JpaControlLoop} object with the given concept key.
108      *
109      * @param key the key
110      */
111     public JpaControlLoop(@NonNull final PfConceptKey key) {
112         this(key, new PfConceptKey(), ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
113     }
114
115     /**
116      * The Key Constructor creates a {@link JpaControlLoop} object with all mandatory fields.
117      *
118      * @param key the key
119      * @param definition the TOSCA definition of the control loop
120      * @param state the state of the control loop
121      * @param elements the elements of the control looop in participants
122      */
123     public JpaControlLoop(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
124             @NonNull final ControlLoopState state, @NonNull final Map<UUID, JpaControlLoopElement> elements) {
125         this.key = key;
126         this.definition = definition;
127         this.state = state;
128         this.elements = elements;
129     }
130
131     /**
132      * Copy constructor.
133      *
134      * @param copyConcept the concept to copy from
135      */
136     public JpaControlLoop(@NonNull final JpaControlLoop copyConcept) {
137         super(copyConcept);
138         this.key = new PfConceptKey(copyConcept.key);
139         this.definition = new PfConceptKey(copyConcept.definition);
140         this.state = copyConcept.state;
141         this.orderedState = copyConcept.orderedState;
142         this.description = copyConcept.description;
143         this.elements = PfUtils.mapMap(copyConcept.elements, JpaControlLoopElement::new, new LinkedHashMap<>(0));
144     }
145
146     /**
147      * Authorative constructor.
148      *
149      * @param authorativeConcept the authorative concept to copy from
150      */
151     public JpaControlLoop(@NonNull final ControlLoop authorativeConcept) {
152         this.fromAuthorative(authorativeConcept);
153     }
154
155     @Override
156     public ControlLoop toAuthorative() {
157         var controlLoop = new ControlLoop();
158
159         controlLoop.setName(getKey().getName());
160         controlLoop.setVersion(getKey().getVersion());
161         controlLoop.setDefinition(new ToscaConceptIdentifier(definition));
162         controlLoop.setState(state);
163         controlLoop.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
164         controlLoop.setDescription(description);
165         controlLoop.setElements(PfUtils.mapMap(elements, JpaControlLoopElement::toAuthorative, new LinkedHashMap<>(0)));
166
167         return controlLoop;
168     }
169
170     @Override
171     public void fromAuthorative(@NonNull final ControlLoop controlLoop) {
172         if (this.key == null || this.getKey().isNullKey()) {
173             this.setKey(new PfConceptKey(controlLoop.getName(), controlLoop.getVersion()));
174         }
175
176         this.definition = controlLoop.getDefinition().asConceptKey();
177         this.state = controlLoop.getState();
178         this.orderedState = controlLoop.getOrderedState();
179         this.description = controlLoop.getDescription();
180
181         this.elements = new LinkedHashMap<>(controlLoop.getElements().size());
182         for (Entry<UUID, ControlLoopElement> elementEntry : controlLoop.getElements().entrySet()) {
183             var jpaControlLoopElement = new JpaControlLoopElement();
184             jpaControlLoopElement.setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
185             jpaControlLoopElement.fromAuthorative(elementEntry.getValue());
186             this.elements.put(elementEntry.getKey(), jpaControlLoopElement);
187         }
188     }
189
190     @Override
191     public List<PfKey> getKeys() {
192         List<PfKey> keyList = getKey().getKeys();
193
194         keyList.add(definition);
195
196         for (JpaControlLoopElement element : elements.values()) {
197             keyList.addAll(element.getKeys());
198         }
199
200         return keyList;
201     }
202
203     @Override
204     public void clean() {
205         key.clean();
206         definition.clean();
207         description = (description == null ? null : description.trim());
208
209         for (JpaControlLoopElement element : elements.values()) {
210             element.clean();
211         }
212     }
213
214     @Override
215     public int compareTo(final PfConcept otherConcept) {
216         if (otherConcept == null) {
217             return -1;
218         }
219         if (this == otherConcept) {
220             return 0;
221         }
222         if (getClass() != otherConcept.getClass()) {
223             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
224         }
225
226         final JpaControlLoop other = (JpaControlLoop) otherConcept;
227         int result = key.compareTo(other.key);
228         if (result != 0) {
229             return result;
230         }
231
232         result = definition.compareTo(other.definition);
233         if (result != 0) {
234             return result;
235         }
236
237         result = ObjectUtils.compare(state, other.state);
238         if (result != 0) {
239             return result;
240         }
241
242         result = ObjectUtils.compare(orderedState, other.orderedState);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(description, other.description);
248         if (result != 0) {
249             return result;
250         }
251
252         return PfUtils.compareObjects(elements, other.elements);
253     }
254 }