c8c26a8dec9369c7c120d9479aa5b7437d74f033
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
22
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;
32 import lombok.Data;
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;
46
47 /**
48  * Class to represent a participant in the database.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 @Entity
53 @Table(name = "Participant")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = false)
57 public class JpaParticipant extends PfConcept implements PfAuthorative<Participant>, Serializable {
58     private static final long serialVersionUID = -4697758484642403483L;
59
60     @EmbeddedId
61     @VerifyKey
62     @NotNull
63     private PfConceptKey key;
64
65     // @formatter:off
66     @VerifyKey
67     @NotNull
68     @AttributeOverride(name = "name",    column = @Column(name = "definition_name"))
69     @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
70     private PfConceptKey definition;
71     // @formatter:on
72
73     @NotNull
74     @AttributeOverride(name = "name",    column = @Column(name = "participant_type_name"))
75     @AttributeOverride(name = "version", column = @Column(name = "participant_type_version"))
76     private PfConceptKey participantType;
77
78     @Column
79     @NotNull
80     private ParticipantState participantState;
81
82     @Column
83     @NotNull
84     private ParticipantHealthStatus healthStatus;
85
86     @Column
87     private String description;
88
89     /**
90      * The Default Constructor creates a {@link JpaParticipant} object with a null key.
91      */
92     public JpaParticipant() {
93         this(new PfConceptKey());
94     }
95
96     /**
97      * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
98      *
99      * @param key the key
100      */
101     public JpaParticipant(@NonNull final PfConceptKey key) {
102         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
107      *
108      * @param key the key
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
112      */
113     public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
114             @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
115         this.key = key;
116         this.definition = definition;
117         this.participantState = participantState;
118         this.healthStatus = healthStatus;
119     }
120
121     /**
122      * Copy constructor.
123      *
124      * @param copyConcept the concept to copy from
125      */
126     public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
127         super(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;
134     }
135
136     /**
137      * Authorative constructor.
138      *
139      * @param authorativeConcept the authorative concept to copy from
140      */
141     public JpaParticipant(@NonNull final Participant authorativeConcept) {
142         this.fromAuthorative(authorativeConcept);
143     }
144
145     @Override
146     public Participant toAuthorative() {
147         var participant = new Participant();
148
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));
156
157         return participant;
158     }
159
160     @Override
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()));
164         }
165
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();
171     }
172
173     @Override
174     public List<PfKey> getKeys() {
175         List<PfKey> keyList = getKey().getKeys();
176
177         keyList.add(definition);
178         keyList.add(participantType);
179
180         return keyList;
181     }
182
183     @Override
184     public void clean() {
185         key.clean();
186         definition.clean();
187         description = (description == null ? null : description.trim());
188         participantType.clean();
189     }
190
191     @Override
192     public int compareTo(final PfConcept otherConcept) {
193         if (otherConcept == null) {
194             return -1;
195         }
196         if (this == otherConcept) {
197             return 0;
198         }
199         if (getClass() != otherConcept.getClass()) {
200             return getClass().getName().compareTo(otherConcept.getClass().getName());
201         }
202
203         final JpaParticipant other = (JpaParticipant) otherConcept;
204         int result = key.compareTo(other.key);
205         if (result != 0) {
206             return result;
207         }
208
209         result = definition.compareTo(other.definition);
210         if (result != 0) {
211             return result;
212         }
213
214         result = ObjectUtils.compare(participantState, other.participantState);
215         if (result != 0) {
216             return result;
217         }
218
219         result = ObjectUtils.compare(healthStatus, other.healthStatus);
220         if (result != 0) {
221             return result;
222         }
223
224         result = ObjectUtils.compare(participantType, other.participantType);
225         if (result != 0) {
226             return result;
227         }
228
229         return ObjectUtils.compare(description, other.description);
230     }
231 }