Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdp.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019, 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.pdp.persistence.concepts;
25
26 import jakarta.persistence.Column;
27 import jakarta.persistence.EmbeddedId;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.Table;
32 import jakarta.persistence.Temporal;
33 import jakarta.persistence.TemporalType;
34 import java.io.Serial;
35 import java.io.Serializable;
36 import java.util.Date;
37 import java.util.List;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.builder.CompareToBuilder;
42 import org.onap.policy.common.parameters.BeanValidationResult;
43 import org.onap.policy.common.parameters.annotations.NotBlank;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.models.base.PfAuthorative;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfReferenceKey;
49 import org.onap.policy.models.base.validation.annotations.VerifyKey;
50 import org.onap.policy.models.pdp.concepts.Pdp;
51 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
52 import org.onap.policy.models.pdp.enums.PdpState;
53
54 /**
55  * Class to represent a PDP in the database.
56  *
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Entity
60 @Table(name = "Pdp")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @Data
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
65     @Serial
66     private static final long serialVersionUID = -357224425637789775L;
67
68     @EmbeddedId
69     @VerifyKey
70     @NotNull
71     private PfReferenceKey key;
72
73     @Column
74     @NotNull
75     private PdpState pdpState;
76
77     @Column
78     @NotNull
79     private PdpHealthStatus healthy;
80
81     @Column
82     @NotBlank
83     private String message;
84
85     @Column(precision = 0)
86     @Temporal(TemporalType.TIMESTAMP)
87     private Date lastUpdate;
88
89     /**
90      * The Default Constructor creates a {@link JpaPdp} object with a null key.
91      */
92     public JpaPdp() {
93         this(new PfReferenceKey());
94     }
95
96     /**
97      * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
98      *
99      * @param key the key
100      */
101     public JpaPdp(@NonNull final PfReferenceKey key) {
102         this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
107      *
108      * @param key the key
109      * @param pdpState the state of the PDP
110      * @param healthy the health state of the PDP
111      */
112     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
113                     @NonNull PdpHealthStatus healthy) {
114         this.key = key;
115         this.pdpState = pdpState;
116         this.healthy = healthy;
117         this.lastUpdate = new Date();
118     }
119
120     /**
121      * Copy constructor.
122      *
123      * @param copyConcept the concept to copy from
124      */
125     public JpaPdp(@NonNull final JpaPdp copyConcept) {
126         super(copyConcept);
127         this.key = new PfReferenceKey(copyConcept.key);
128         this.pdpState = copyConcept.pdpState;
129         this.healthy = copyConcept.healthy;
130         this.message = copyConcept.message;
131         this.lastUpdate = copyConcept.lastUpdate;
132     }
133
134     /**
135      * Authorative constructor.
136      *
137      * @param authorativeConcept the authorative concept to copy from
138      */
139     public JpaPdp(@NonNull final Pdp authorativeConcept) {
140         this.fromAuthorative(authorativeConcept);
141     }
142
143     @Override
144     public Pdp toAuthorative() {
145         var pdp = new Pdp();
146
147         pdp.setInstanceId(key.getLocalName());
148         pdp.setPdpState(pdpState);
149         pdp.setHealthy(healthy);
150         pdp.setMessage(message);
151         pdp.setLastUpdate(lastUpdate.toInstant());
152
153         return pdp;
154     }
155
156     @Override
157     public void fromAuthorative(@NonNull final Pdp pdp) {
158         if (this.key == null || this.getKey().isNullKey()) {
159             this.setKey(new PfReferenceKey());
160             getKey().setLocalName(pdp.getInstanceId());
161         }
162
163         this.setPdpState(pdp.getPdpState());
164         this.setHealthy(pdp.getHealthy());
165         this.setMessage(pdp.getMessage());
166
167         if (pdp.getLastUpdate() == null) {
168             this.setLastUpdate(new Date());
169         } else {
170             this.setLastUpdate(Date.from(pdp.getLastUpdate()));
171         }
172     }
173
174     @Override
175     public List<PfKey> getKeys() {
176         return getKey().getKeys();
177     }
178
179     @Override
180     public void clean() {
181         key.clean();
182
183         if (message != null) {
184             message = message.trim();
185         }
186     }
187
188     @Override
189     public BeanValidationResult validate(@NonNull String fieldName) {
190         BeanValidationResult result = super.validate(fieldName);
191
192         validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
193
194         if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
195             addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
196         }
197
198         return result;
199     }
200
201     @Override
202     public int compareTo(final PfConcept otherConcept) {
203         if (otherConcept == null) {
204             return -1;
205         }
206         if (this == otherConcept) {
207             return 0;
208         }
209         if (getClass() != otherConcept.getClass()) {
210             return getClass().getName().compareTo(otherConcept.getClass().getName());
211         }
212
213         final JpaPdp other = (JpaPdp) otherConcept;
214
215         // @formatter:off
216         return new CompareToBuilder()
217                         .append(this.key, other.key)
218                         .append(this.pdpState, other.pdpState)
219                         .append(this.healthy, other.healthy)
220                         .append(this.message, other.message)
221                         .append(this.lastUpdate, other.lastUpdate)
222                         .toComparison();
223         // @formatter:on
224     }
225 }