69b565266a7cca9298a2f0de18ebadd332ad4909
[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.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;
33 import lombok.Data;
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;
47
48 /**
49  * Class to represent a participant in the database.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 @Entity
54 @Table(name = "Participant")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = false)
58 public class JpaParticipant extends PfConcept implements PfAuthorative<Participant>, Serializable {
59     private static final long serialVersionUID = -4697758484642403483L;
60
61     @EmbeddedId
62     @VerifyKey
63     @NotNull
64     private PfConceptKey key;
65
66     // @formatter:off
67     @VerifyKey
68     @NotNull
69     @AttributeOverrides({
70             @AttributeOverride(name = "name",    column = @Column(name = "definition_name")),
71             @AttributeOverride(name = "version", column = @Column(name = "definition_version"))
72         }
73     )
74     private PfConceptKey definition;
75     // @formatter:on
76
77     @Column
78     @NotNull
79     private ParticipantState participantState;
80
81     @Column
82     @NotNull
83     private ParticipantHealthStatus healthStatus;
84
85     @Column
86     private String description;
87
88     /**
89      * The Default Constructor creates a {@link JpaParticipant} object with a null key.
90      */
91     public JpaParticipant() {
92         this(new PfConceptKey());
93     }
94
95     /**
96      * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
97      *
98      * @param key the key
99      */
100     public JpaParticipant(@NonNull final PfConceptKey key) {
101         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
102     }
103
104     /**
105      * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
106      *
107      * @param key the key
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
111      */
112     public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
113             @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
114         this.key = key;
115         this.definition = definition;
116         this.participantState = participantState;
117         this.healthStatus = healthStatus;
118     }
119
120     /**
121      * Copy constructor.
122      *
123      * @param copyConcept the concept to copy from
124      */
125     public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
126         super(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;
132     }
133
134     /**
135      * Authorative constructor.
136      *
137      * @param authorativeConcept the authorative concept to copy from
138      */
139     public JpaParticipant(@NonNull final Participant authorativeConcept) {
140         this.fromAuthorative(authorativeConcept);
141     }
142
143     @Override
144     public Participant toAuthorative() {
145         Participant participant = new Participant();
146
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);
153
154         return participant;
155     }
156
157     @Override
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()));
161         }
162
163         this.definition = participant.getDefinition().asConceptKey();
164         this.setParticipantState(participant.getParticipantState());
165         this.setHealthStatus(participant.getHealthStatus());
166         this.setDescription(participant.getDescription());
167     }
168
169     @Override
170     public List<PfKey> getKeys() {
171         List<PfKey> keyList = getKey().getKeys();
172
173         keyList.add(definition);
174
175         return keyList;
176     }
177
178     @Override
179     public void clean() {
180         key.clean();
181         definition.clean();
182         description = (description == null ? null : description.trim());
183     }
184
185     @Override
186     public int compareTo(final PfConcept otherConcept) {
187         if (otherConcept == null) {
188             return -1;
189         }
190         if (this == otherConcept) {
191             return 0;
192         }
193         if (getClass() != otherConcept.getClass()) {
194             return getClass().getName().compareTo(otherConcept.getClass().getName());
195         }
196
197         final JpaParticipant other = (JpaParticipant) otherConcept;
198         int result = key.compareTo(other.key);
199         if (result != 0) {
200             return result;
201         }
202
203         result = definition.compareTo(other.definition);
204         if (result != 0) {
205             return result;
206         }
207
208         result = ObjectUtils.compare(participantState, other.participantState);
209         if (result != 0) {
210             return result;
211         }
212
213         result = ObjectUtils.compare(healthStatus, other.healthStatus);
214         if (result != 0) {
215             return result;
216         }
217
218         return ObjectUtils.compare(description, other.description);
219     }
220 }