2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
25 import java.util.List;
26 import java.util.UUID;
27 import javax.persistence.AttributeOverride;
28 import javax.persistence.Column;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.ObjectUtils;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 * Class to represent a participant control loop element in the database.
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ControlLoopElement")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaControlLoopElement extends PfConcept implements PfAuthorative<ControlLoopElement> {
61 private static final long serialVersionUID = -1791732273187890213L;
66 private PfReferenceKey key;
71 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
72 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
73 private PfConceptKey definition;
77 @AttributeOverride(name = "name", column = @Column(name = "participant_type_name"))
78 @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
79 private PfConceptKey participantType;
82 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
83 @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 participantType 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 participantType, @NonNull final ControlLoopState state) {
125 this.definition = definition;
126 this.participantType = participantType;
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.participantType = new PfConceptKey(copyConcept.participantType);
140 this.participantId = new PfConceptKey(copyConcept.participantId);
141 this.state = copyConcept.state;
142 this.orderedState = copyConcept.orderedState;
143 this.description = copyConcept.description;
147 * Authorative constructor.
149 * @param authorativeConcept the authorative concept to copy from
151 public JpaControlLoopElement(@NonNull final ControlLoopElement authorativeConcept) {
152 this.fromAuthorative(authorativeConcept);
156 public ControlLoopElement toAuthorative() {
157 var element = new ControlLoopElement();
159 element.setId(UUID.fromString(getKey().getLocalName()));
160 element.setDefinition(new ToscaConceptIdentifier(definition));
161 element.setParticipantType(new ToscaConceptIdentifier(participantType));
162 element.setParticipantId(new ToscaConceptIdentifier(participantId));
163 element.setState(state);
164 element.setOrderedState(orderedState != null ? orderedState : state.asOrderedState());
165 element.setDescription(description);
171 public void fromAuthorative(@NonNull final ControlLoopElement element) {
172 if (this.key == null || this.getKey().isNullKey()) {
173 this.setKey(new PfReferenceKey());
174 getKey().setLocalName(element.getId().toString());
177 this.definition = element.getDefinition().asConceptKey();
178 this.participantType = element.getParticipantType().asConceptKey();
179 this.participantId = element.getParticipantId().asConceptKey();
180 this.state = element.getState();
181 this.orderedState = element.getOrderedState();
182 this.description = element.getDescription();
186 public List<PfKey> getKeys() {
187 List<PfKey> keyList = getKey().getKeys();
189 keyList.add(definition);
190 keyList.add(participantType);
191 keyList.add(participantId);
197 public void clean() {
200 participantType.clean();
201 participantId.clean();
203 if (description != null) {
204 description = description.trim();
209 public int compareTo(final PfConcept otherConcept) {
210 if (otherConcept == null) {
213 if (this == otherConcept) {
216 if (getClass() != otherConcept.getClass()) {
217 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
220 final JpaControlLoopElement other = (JpaControlLoopElement) otherConcept;
221 int result = key.compareTo(other.key);
226 result = definition.compareTo(other.definition);
231 result = participantType.compareTo(other.participantType);
236 result = participantId.compareTo(other.participantId);
241 result = ObjectUtils.compare(state, other.state);
246 result = ObjectUtils.compare(orderedState, other.orderedState);
251 return ObjectUtils.compare(description, other.description);