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.List;
24 import java.util.UUID;
25 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.apache.commons.lang3.ObjectUtils;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceKey;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 * Class to represent a participant control loop element in the database.
52 * @author Liam Fallon (liam.fallon@est.tech)
55 @Table(name = "ControlLoopElement")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
60 private static final long serialVersionUID = -1791732273187890213L;
65 private PfReferenceKey key;
71 @AttributeOverride(name = "name", column = @Column(name = "definition_name")),
72 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
75 private PfConceptKey definition;
80 @AttributeOverride(name = "name", column = @Column(name = "participant_name")),
81 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
84 private PfConceptKey participantId;
89 private ControlLoopState state;
93 private ControlLoopOrderedState orderedState;
96 private String description;
99 * The Default Constructor creates a {@link JpaControlLoopElement} object with a null key.
101 public JpaControlLoopElement() {
102 this(new PfReferenceKey());
106 * The Key Constructor creates a {@link JpaControlLoopElement} object with the given concept key.
110 public JpaControlLoopElement(@NonNull final PfReferenceKey key) {
111 this(key, new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED);
115 * The Key Constructor creates a {@link JpaControlLoopElement} object with all mandatory fields.
118 * @param definition the TOSCA definition of the control loop element
119 * @param participantId the TOSCA definition of the participant running the control loop element
120 * @param state the state of the control loop
122 public JpaControlLoopElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
123 @NonNull final PfConceptKey participantId, @NonNull final ControlLoopState state) {
125 this.definition = definition;
126 this.participantId = participantId;
133 * @param copyConcept the concept to copy from
135 public JpaControlLoopElement(@NonNull final JpaControlLoopElement copyConcept) {
137 this.key = new PfReferenceKey(copyConcept.key);
138 this.definition = new PfConceptKey(copyConcept.definition);
139 this.participantId = new PfConceptKey(copyConcept.participantId);
140 this.state = copyConcept.state;
141 this.orderedState = copyConcept.orderedState;
142 this.description = copyConcept.description;
146 * Authorative constructor.
148 * @param authorativeConcept the authorative concept to copy from
150 public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
151 this.fromAuthorative(authorativeConcept);
155 public ControlLoopElement toAuthorative() {
156 ControlLoopElement element = new ControlLoopElement();
158 element.setId(UUID.fromString(getKey().getLocalName()));
159 element.setDefinition(new ToscaConceptIdentifier(definition));
160 element.setParticipantId(new ToscaConceptIdentifier(participantId));
161 element.setState(state);
162 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
163 element.setDescription(description);
169 public void fromAuthorative(@NonNull final ControlLoopElement element) {
170 if (this.key == null || this.getKey().isNullKey()) {
171 this.setKey(new PfReferenceKey());
172 getKey().setLocalName(element.getId().toString());
175 this.definition = element.getDefinition().asConceptKey();
176 this.participantId = element.getParticipantId().asConceptKey();
177 this.state = element.getState();
178 this.orderedState = element.getOrderedState();
179 this.description = element.getDescription();
183 public List<PfKey> getKeys() {
184 List<PfKey> keyList = getKey().getKeys();
186 keyList.add(definition);
187 keyList.add(participantId);
193 public void clean() {
196 participantId.clean();
198 if (description != null) {
199 description = description.trim();
204 public int compareTo(final PfConcept otherConcept) {
205 if (otherConcept == null) {
208 if (this == otherConcept) {
211 if (getClass() != otherConcept.getClass()) {
212 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
215 final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
216 int result = key.compareTo(other.key);
221 result = definition.compareTo(other.definition);
226 result = participantId.compareTo(other.participantId);
231 result = ObjectUtils.compare(state, other.state);
236 result = ObjectUtils.compare(orderedState, other.orderedState);
241 return ObjectUtils.compare(description, other.description);