4d49683bffb73f372c78d03033a3c29d40678dc2
[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     @Column
74     @NotNull
75     private ParticipantState participantState;
76
77     @Column
78     @NotNull
79     private ParticipantHealthStatus healthStatus;
80
81     @Column
82     private String description;
83
84     /**
85      * The Default Constructor creates a {@link JpaParticipant} object with a null key.
86      */
87     public JpaParticipant() {
88         this(new PfConceptKey());
89     }
90
91     /**
92      * The Key Constructor creates a {@link JpaParticipant} object with the given concept key.
93      *
94      * @param key the key
95      */
96     public JpaParticipant(@NonNull final PfConceptKey key) {
97         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.UNKNOWN);
98     }
99
100     /**
101      * The Key Constructor creates a {@link JpaParticipant} object with all mandatory fields.
102      *
103      * @param key the key
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
107      */
108     public JpaParticipant(@NonNull final PfConceptKey key, @NonNull final PfConceptKey definition,
109             @NonNull final ParticipantState participantState, @NonNull ParticipantHealthStatus healthStatus) {
110         this.key = key;
111         this.definition = definition;
112         this.participantState = participantState;
113         this.healthStatus = healthStatus;
114     }
115
116     /**
117      * Copy constructor.
118      *
119      * @param copyConcept the concept to copy from
120      */
121     public JpaParticipant(@NonNull final JpaParticipant copyConcept) {
122         super(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;
128     }
129
130     /**
131      * Authorative constructor.
132      *
133      * @param authorativeConcept the authorative concept to copy from
134      */
135     public JpaParticipant(@NonNull final Participant authorativeConcept) {
136         this.fromAuthorative(authorativeConcept);
137     }
138
139     @Override
140     public Participant toAuthorative() {
141         var participant = new Participant();
142
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);
149
150         return participant;
151     }
152
153     @Override
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()));
157         }
158
159         this.definition = participant.getDefinition().asConceptKey();
160         this.setParticipantState(participant.getParticipantState());
161         this.setHealthStatus(participant.getHealthStatus());
162         this.setDescription(participant.getDescription());
163     }
164
165     @Override
166     public List<PfKey> getKeys() {
167         List<PfKey> keyList = getKey().getKeys();
168
169         keyList.add(definition);
170
171         return keyList;
172     }
173
174     @Override
175     public void clean() {
176         key.clean();
177         definition.clean();
178         description = (description == null ? null : description.trim());
179     }
180
181     @Override
182     public int compareTo(final PfConcept otherConcept) {
183         if (otherConcept == null) {
184             return -1;
185         }
186         if (this == otherConcept) {
187             return 0;
188         }
189         if (getClass() != otherConcept.getClass()) {
190             return getClass().getName().compareTo(otherConcept.getClass().getName());
191         }
192
193         final JpaParticipant other = (JpaParticipant) otherConcept;
194         int result = key.compareTo(other.key);
195         if (result != 0) {
196             return result;
197         }
198
199         result = definition.compareTo(other.definition);
200         if (result != 0) {
201             return result;
202         }
203
204         result = ObjectUtils.compare(participantState, other.participantState);
205         if (result != 0) {
206             return result;
207         }
208
209         result = ObjectUtils.compare(healthStatus, other.healthStatus);
210         if (result != 0) {
211             return result;
212         }
213
214         return ObjectUtils.compare(description, other.description);
215     }
216 }