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.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;
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;
59 * Class to represent a control loop in the database.
61 * @author Liam Fallon (liam.fallon@est.tech)
64 @Table(name = "ControlLoop")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
67 @EqualsAndHashCode(callSuper = false)
68 public class JpaControlLoop extends PfConcept implements PfAuthorative<ControlLoop> {
69 private static final long serialVersionUID = -4725410933242154805L;
74 private PfConceptKey key;
80 @AttributeOverride(name = "name", column = @Column(name = "definition_name")),
81 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
84 private PfConceptKey definition;
89 private ControlLoopState state;
93 private ControlLoopOrderedState orderedState;
96 private String description;
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")
108 private List<@NotNull @Valid JpaControlLoopElement> elements;
111 * The Default Constructor creates a {@link JpaControlLoop} object with a null key.
113 public JpaControlLoop() {
114 this(new PfConceptKey());
118 * The Key Constructor creates a {@link JpaControlLoop} object with the given concept key.
122 public JpaControlLoop(@NonNull final PfConceptKey key) {
123 this(key, new PfConceptKey(), ControlLoopState.UNINITIALISED, new ArrayList<>());
127 * The Key Constructor creates a {@link JpaControlLoop} object with all mandatory fields.
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
134 public JpaControlLoop(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
135 @NonNull final ControlLoopState state, @NonNull final List<JpaControlLoopElement> elements) {
137 this.definition = definition;
139 this.elements = elements;
145 * @param copyConcept the concept to copy from
147 public JpaControlLoop(@NonNull final JpaControlLoop 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));
158 * Authorative constructor.
160 * @param authorativeConcept the authorative concept to copy from
162 public JpaControlLoop(@NonNull final ControlLoop authorativeConcept) {
163 this.fromAuthorative(authorativeConcept);
167 public ControlLoop toAuthorative() {
168 ControlLoop controlLoop = new ControlLoop();
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);
178 .setElements(elements.stream().map(JpaControlLoopElement::toAuthorative).collect(Collectors.toList()));
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()));
189 this.definition = controlLoop.getDefinition().asConceptKey();
190 this.state = controlLoop.getState();
191 this.orderedState = controlLoop.getOrderedState();
192 this.description = controlLoop.getDescription();
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);
204 public List<PfKey> getKeys() {
205 List<PfKey> keyList = getKey().getKeys();
207 keyList.add(definition);
209 for (JpaControlLoopElement element : elements) {
210 keyList.addAll(element.getKeys());
217 public void clean() {
220 description = (description == null ? null : description.trim());
222 for (JpaControlLoopElement element : elements) {
228 public int compareTo(final PfConcept otherConcept) {
229 if (otherConcept == null) {
232 if (this == otherConcept) {
235 if (getClass() != otherConcept.getClass()) {
236 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
239 final JpaControlLoop other = (JpaControlLoop) otherConcept;
240 int result = key.compareTo(other.key);
245 result = definition.compareTo(other.definition);
250 result = ObjectUtils.compare(state, other.state);
255 result = ObjectUtils.compare(orderedState, other.orderedState);
260 result = ObjectUtils.compare(description, other.description);
265 return PfUtils.compareObjects(elements, other.elements);