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.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.Participant;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
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.validation.annotations.VerifyKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 * Class to represent a participant in the database.
51 * @author Liam Fallon (liam.fallon@est.tech)
54 @Table(name = "Participant")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaParticipant extends PfConcept implements PfAuthorative<Participant>, Serializable {
59 private static final long serialVersionUID = -4697758484642403483L;
64 private PfConceptKey key;
70 @AttributeOverride(name = "name", column = @Column(name = "definition_name")),
71 @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
74 private PfConceptKey definition;
79 private ParticipantState participantState;
83 private ParticipantHealthStatus healthStatus;
86 private String description;
89 * The Default Constructor creates a {@link JpaParticipant} object with a null key.
91 public JpaParticipant() {
92 this(new PfConceptKey());
96 * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
100 public JpaParticipant(@NonNull final PfConceptKey key) {
101 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
105 * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
108 * @param definition the TOSCA definition of the participant
109 * @param participantState the state of the participant
110 * @param healthStatus the health state of the participant
112 public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
113 @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
115 this.definition = definition;
116 this.participantState = participantState;
117 this.healthStatus = healthStatus;
123 * @param copyConcept the concept to copy from
125 public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
127 this.key = new PfConceptKey(copyConcept.key);
128 this.definition = new PfConceptKey(copyConcept.definition);
129 this.participantState = copyConcept.participantState;
130 this.healthStatus = copyConcept.healthStatus;
131 this.description = copyConcept.description;
135 * Authorative constructor.
137 * @param authorativeConcept the authorative concept to copy from
139 public JpaParticipant(@NonNull final Participant authorativeConcept) {
140 this.fromAuthorative(authorativeConcept);
144 public Participant toAuthorative() {
145 Participant participant = new Participant();
147 participant.setName(key.getName());
148 participant.setVersion(key.getVersion());
149 participant.setDefinition(new ToscaConceptIdentifier(definition));
150 participant.setParticipantState(participantState);
151 participant.setHealthStatus(healthStatus);
152 participant.setDescription(description);
158 public void fromAuthorative(@NonNull final Participant participant) {
159 if (this.key == null || this.getKey().isNullKey()) {
160 this.setKey(new PfConceptKey(participant.getName(), participant.getVersion()));
163 this.definition = participant.getDefinition().asConceptKey();
164 this.setParticipantState(participant.getParticipantState());
165 this.setHealthStatus(participant.getHealthStatus());
166 this.setDescription(participant.getDescription());
170 public List<PfKey> getKeys() {
171 List<PfKey> keyList = getKey().getKeys();
173 keyList.add(definition);
179 public void clean() {
182 description = (description == null ? null : description.trim());
186 public int compareTo(final PfConcept otherConcept) {
187 if (otherConcept == null) {
190 if (this == otherConcept) {
193 if (getClass() != otherConcept.getClass()) {
194 return getClass().getName().compareTo(otherConcept.getClass().getName());
197 final JpaParticipant other = (JpaParticipant) otherConcept;
198 int result = key.compareTo(other.key);
203 result = definition.compareTo(other.definition);
208 result = ObjectUtils.compare(participantState, other.participantState);
213 result = ObjectUtils.compare(healthStatus, other.healthStatus);
218 return ObjectUtils.compare(description, other.description);