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.io.Serializable;
24 import java.util.List;
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.Participant;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
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.validation.annotations.VerifyKey;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 * Class to represent a participant in the database.
50 * @author Liam Fallon (liam.fallon@est.tech)
53 @Table(name = "Participant")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @EqualsAndHashCode(callSuper = false)
57 public class JpaParticipant extends PfConcept implements PfAuthorative<Participant>, Serializable {
58 private static final long serialVersionUID = -4697758484642403483L;
63 private PfConceptKey key;
68 @AttributeOverride(name = "name", column = @Column(name = "definition_name"))
69 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
70 private PfConceptKey definition;
75 private ParticipantState participantState;
79 private ParticipantHealthStatus healthStatus;
82 private String description;
85 * The Default Constructor creates a {@link JpaParticipant} object with a null key.
87 public JpaParticipant() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
96 public JpaParticipant(@NonNull final PfConceptKey key) {
97 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
101 * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
104 * @param definition the TOSCA definition of the participant
105 * @param participantState the state of the participant
106 * @param healthStatus the health state of the participant
108 public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
109 @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
111 this.definition = definition;
112 this.participantState = participantState;
113 this.healthStatus = healthStatus;
119 * @param copyConcept the concept to copy from
121 public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
123 this.key = new PfConceptKey(copyConcept.key);
124 this.definition = new PfConceptKey(copyConcept.definition);
125 this.participantState = copyConcept.participantState;
126 this.healthStatus = copyConcept.healthStatus;
127 this.description = copyConcept.description;
131 * Authorative constructor.
133 * @param authorativeConcept the authorative concept to copy from
135 public JpaParticipant(@NonNull final Participant authorativeConcept) {
136 this.fromAuthorative(authorativeConcept);
140 public Participant toAuthorative() {
141 var participant = new Participant();
143 participant.setName(key.getName());
144 participant.setVersion(key.getVersion());
145 participant.setDefinition(new ToscaConceptIdentifier(definition));
146 participant.setParticipantState(participantState);
147 participant.setHealthStatus(healthStatus);
148 participant.setDescription(description);
154 public void fromAuthorative(@NonNull final Participant participant) {
155 if (this.key == null || this.getKey().isNullKey()) {
156 this.setKey(new PfConceptKey(participant.getName(), participant.getVersion()));
159 this.definition = participant.getDefinition().asConceptKey();
160 this.setParticipantState(participant.getParticipantState());
161 this.setHealthStatus(participant.getHealthStatus());
162 this.setDescription(participant.getDescription());
166 public List<PfKey> getKeys() {
167 List<PfKey> keyList = getKey().getKeys();
169 keyList.add(definition);
175 public void clean() {
178 description = (description == null ? null : description.trim());
182 public int compareTo(final PfConcept otherConcept) {
183 if (otherConcept == null) {
186 if (this == otherConcept) {
189 if (getClass() != otherConcept.getClass()) {
190 return getClass().getName().compareTo(otherConcept.getClass().getName());
193 final JpaParticipant other = (JpaParticipant) otherConcept;
194 int result = key.compareTo(other.key);
199 result = definition.compareTo(other.definition);
204 result = ObjectUtils.compare(participantState, other.participantState);
209 result = ObjectUtils.compare(healthStatus, other.healthStatus);
214 return ObjectUtils.compare(description, other.description);