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;
74 @AttributeOverride(name = "name", column = @Column(name = "participant_type_name"))
75 @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
76 private PfConceptKey participantType;
80 private ParticipantState participantState;
84 private ParticipantHealthStatus healthStatus;
87 private String description;
90 * The Default Constructor creates a {@link JpaParticipant} object with a null key.
92 public JpaParticipant() {
93 this(new PfConceptKey());
97 * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
101 public JpaParticipant(@NonNull final PfConceptKey key) {
102 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
106 * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
109 * @param definition the TOSCA definition of the participant
110 * @param participantState the state of the participant
111 * @param healthStatus the health state of the participant
113 public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
114 @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
116 this.definition = definition;
117 this.participantState = participantState;
118 this.healthStatus = healthStatus;
124 * @param copyConcept the concept to copy from
126 public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
128 this.key = new PfConceptKey(copyConcept.key);
129 this.definition = new PfConceptKey(copyConcept.definition);
130 this.participantState = copyConcept.participantState;
131 this.healthStatus = copyConcept.healthStatus;
132 this.description = copyConcept.description;
133 this.participantType = copyConcept.participantType;
137 * Authorative constructor.
139 * @param authorativeConcept the authorative concept to copy from
141 public JpaParticipant(@NonNull final Participant authorativeConcept) {
142 this.fromAuthorative(authorativeConcept);
146 public Participant toAuthorative() {
147 var participant = new Participant();
149 participant.setName(key.getName());
150 participant.setVersion(key.getVersion());
151 participant.setDefinition(new ToscaConceptIdentifier(definition));
152 participant.setParticipantState(participantState);
153 participant.setHealthStatus(healthStatus);
154 participant.setDescription(description);
155 participant.setParticipantType(new ToscaConceptIdentifier(participantType));
161 public void fromAuthorative(@NonNull final Participant participant) {
162 if (this.key == null || this.getKey().isNullKey()) {
163 this.setKey(new PfConceptKey(participant.getName(), participant.getVersion()));
166 this.definition = participant.getDefinition().asConceptKey();
167 this.setParticipantState(participant.getParticipantState());
168 this.setHealthStatus(participant.getHealthStatus());
169 this.setDescription(participant.getDescription());
170 this.participantType = participant.getParticipantType().asConceptKey();
174 public List<PfKey> getKeys() {
175 List<PfKey> keyList = getKey().getKeys();
177 keyList.add(definition);
178 keyList.add(participantType);
184 public void clean() {
187 description = (description == null ? null : description.trim());
188 participantType.clean();
192 public int compareTo(final PfConcept otherConcept) {
193 if (otherConcept == null) {
196 if (this == otherConcept) {
199 if (getClass() != otherConcept.getClass()) {
200 return getClass().getName().compareTo(otherConcept.getClass().getName());
203 final JpaParticipant other = (JpaParticipant) otherConcept;
204 int result = key.compareTo(other.key);
209 result = definition.compareTo(other.definition);
214 result = ObjectUtils.compare(participantState, other.participantState);
219 result = ObjectUtils.compare(healthStatus, other.healthStatus);
224 result = ObjectUtils.compare(participantType, other.participantType);
229 return ObjectUtils.compare(description, other.description);