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.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.Table;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 * Class to represent a participant control loop element in the database.
51 * @author Liam Fallon (liam.fallon@est.tech)
54 @Table(name = "ControlLoopElement")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
59 private static final long serialVersionUID = -1791732273187890213L;
64 private PfReferenceKey key;
69 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
70 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
71 private PfConceptKey definition;
75 @AttributeOverride(name = "name", column = @Column(name = "participant_type_name"))
76 @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
77 private PfConceptKey participantType;
80 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
81 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
82 private PfConceptKey participantId;
87 private ControlLoopState state;
91 private ControlLoopOrderedState orderedState;
94 private String description;
97 * The Default Constructor creates a {@link JpaControlLoopElement} object with a null key.
99 public JpaControlLoopElement() {
100 this(new PfReferenceKey());
104 * The Key Constructor creates a {@link JpaControlLoopElement} object with the given concept key.
108 public JpaControlLoopElement(@NonNull final PfReferenceKey key) {
109 this(key, new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED);
113 * The Key Constructor creates a {@link JpaControlLoopElement} object with all mandatory fields.
116 * @param definition the TOSCA definition of the control loop element
117 * @param participantType the TOSCA definition of the participant running the control loop element
118 * @param state the state of the control loop
120 public JpaControlLoopElement(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey definition,
121 @NonNull final PfConceptKey participantType, @NonNull final ControlLoopState state) {
123 this.definition = definition;
124 this.participantType = participantType;
131 * @param copyConcept the concept to copy from
133 public JpaControlLoopElement(@NonNull final JpaControlLoopElement copyConcept) {
135 this.key = new PfReferenceKey(copyConcept.key);
136 this.definition = new PfConceptKey(copyConcept.definition);
137 this.participantType = new PfConceptKey(copyConcept.participantType);
138 this.participantId = new PfConceptKey(copyConcept.participantId);
139 this.state = copyConcept.state;
140 this.orderedState = copyConcept.orderedState;
141 this.description = copyConcept.description;
145 * Authorative constructor.
147 * @param authorativeConcept the authorative concept to copy from
149 public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
150 this.fromAuthorative(authorativeConcept);
154 public ControlLoopElement toAuthorative() {
155 ControlLoopElement element = new ControlLoopElement();
157 element.setId(UUID.fromString(getKey().getLocalName()));
158 element.setDefinition(new ToscaConceptIdentifier(definition));
159 element.setParticipantType(new ToscaConceptIdentifier(participantType));
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.participantType = element.getParticipantType().asConceptKey();
177 this.participantId = element.getParticipantId().asConceptKey();
178 this.state = element.getState();
179 this.orderedState = element.getOrderedState();
180 this.description = element.getDescription();
184 public List<PfKey> getKeys() {
185 List<PfKey> keyList = getKey().getKeys();
187 keyList.add(definition);
188 keyList.add(participantType);
189 keyList.add(participantId);
195 public void clean() {
198 participantType.clean();
199 participantId.clean();
201 if (description != null) {
202 description = description.trim();
207 public int compareTo(final PfConcept otherConcept) {
208 if (otherConcept == null) {
211 if (this == otherConcept) {
214 if (getClass() != otherConcept.getClass()) {
215 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
218 final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
219 int result = key.compareTo(other.key);
224 result = definition.compareTo(other.definition);
229 result = participantType.compareTo(other.participantType);
234 result = participantId.compareTo(other.participantId);
239 result = ObjectUtils.compare(state, other.state);
244 result = ObjectUtils.compare(orderedState, other.orderedState);
249 return ObjectUtils.compare(description, other.description);