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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
23 import java.util.LinkedHashMap;
24 import java.util.List;
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;
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;
58 * Class to represent a control loop in the database.
60 * @author Liam Fallon (liam.fallon@est.tech)
63 @Table(name = "ControlLoop")
64 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaControlLoop extends PfConcept implements PfAuthorative<ControlLoop> {
68 private static final long serialVersionUID = -4725410933242154805L;
73 private PfConceptKey key;
78 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
79 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
80 private PfConceptKey definition;
85 private ControlLoopState state;
89 private ControlLoopOrderedState orderedState;
92 private String description;
95 private Boolean primed;
97 @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
99 private Map<@NotNull UUID, @NotNull @Valid JpaControlLoopElement> elements;
102 * The Default Constructor creates a {@link JpaControlLoop} object with a null key.
104 public JpaControlLoop() {
105 this(new PfConceptKey());
109 * The Key Constructor creates a {@link JpaControlLoop} object with the given concept key.
113 public JpaControlLoop(@NonNull final PfConceptKey key) {
114 this(key, new PfConceptKey(), ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
118 * The Key Constructor creates a {@link JpaControlLoop} object with all mandatory fields.
121 * @param definition the TOSCA definition of the control loop
122 * @param state the state of the control loop
123 * @param elements the elements of the control looop in participants
125 public JpaControlLoop(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
126 @NonNull final ControlLoopState state, @NonNull final Map<UUID, JpaControlLoopElement> elements) {
128 this.definition = definition;
130 this.elements = elements;
136 * @param copyConcept the concept to copy from
138 public JpaControlLoop(@NonNull final JpaControlLoop copyConcept) {
140 this.key = new PfConceptKey(copyConcept.key);
141 this.definition = new PfConceptKey(copyConcept.definition);
142 this.state = copyConcept.state;
143 this.orderedState = copyConcept.orderedState;
144 this.description = copyConcept.description;
145 this.elements = PfUtils.mapMap(copyConcept.elements, JpaControlLoopElement::new, new LinkedHashMap<>(0));
146 this.primed = copyConcept.primed;
150 * Authorative constructor.
152 * @param authorativeConcept the authorative concept to copy from
154 public JpaControlLoop(@NonNull final ControlLoop authorativeConcept) {
155 this.fromAuthorative(authorativeConcept);
159 public ControlLoop toAuthorative() {
160 var controlLoop = new ControlLoop();
162 controlLoop.setName(getKey().getName());
163 controlLoop.setVersion(getKey().getVersion());
164 controlLoop.setDefinition(new ToscaConceptIdentifier(definition));
165 controlLoop.setState(state);
166 controlLoop.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
167 controlLoop.setDescription(description);
168 controlLoop.setElements(PfUtils.mapMap(elements, JpaControlLoopElement::toAuthorative, new LinkedHashMap<>(0)));
169 controlLoop.setPrimed(primed);
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()));
180 this.definition = controlLoop.getDefinition().asConceptKey();
181 this.state = controlLoop.getState();
182 this.orderedState = controlLoop.getOrderedState();
183 this.description = controlLoop.getDescription();
184 this.primed = controlLoop.getPrimed();
186 this.elements = new LinkedHashMap<>(controlLoop.getElements().size());
187 for (Entry<UUID, ControlLoopElement> elementEntry : controlLoop.getElements().entrySet()) {
188 var jpaControlLoopElement = new JpaControlLoopElement();
189 jpaControlLoopElement.setKey(new PfReferenceKey(getKey(), elementEntry.getValue().getId().toString()));
190 jpaControlLoopElement.fromAuthorative(elementEntry.getValue());
191 this.elements.put(elementEntry.getKey(), jpaControlLoopElement);
196 public List<PfKey> getKeys() {
197 List<PfKey> keyList = getKey().getKeys();
199 keyList.add(definition);
201 for (JpaControlLoopElement element : elements.values()) {
202 keyList.addAll(element.getKeys());
209 public void clean() {
212 description = (description == null ? null : description.trim());
214 for (JpaControlLoopElement element : elements.values()) {
220 public int compareTo(final PfConcept otherConcept) {
221 if (otherConcept == null) {
224 if (this == otherConcept) {
227 if (getClass() != otherConcept.getClass()) {
228 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
231 final JpaControlLoop other = (JpaControlLoop) otherConcept;
232 int result = key.compareTo(other.key);
237 result = definition.compareTo(other.definition);
242 result = ObjectUtils.compare(state, other.state);
247 result = ObjectUtils.compare(orderedState, other.orderedState);
252 result = ObjectUtils.compare(description, other.description);
257 result = ObjectUtils.compare(primed, other.primed);
261 return PfUtils.compareObjects(elements, other.elements);