7c584f3e62f40eb3f37bb65093c915e66d5e3846
[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 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 java.io.Serializable;
27 import java.util.Date;
28 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.builder.CompareToBuilder;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.common.parameters.annotations.NotBlank;
43 import org.onap.policy.common.parameters.annotations.NotNull;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.validation.annotations.VerifyKey;
49 import org.onap.policy.models.pdp.concepts.Pdp;
50 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
51 import org.onap.policy.models.pdp.enums.PdpState;
52
53 /**
54  * Class to represent a PDP in the database.
55  *
56  * @author Liam Fallon (liam.fallon@est.tech)
57  */
58 @Entity
59 @Table(name = "Pdp")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
64     private static final long serialVersionUID = -357224425637789775L;
65
66     @EmbeddedId
67     @VerifyKey
68     @NotNull
69     private PfReferenceKey key;
70
71     @Column
72     @NotNull
73     private PdpState pdpState;
74
75     @Column
76     @NotNull
77     private PdpHealthStatus healthy;
78
79     @Column
80     @NotBlank
81     private String message;
82
83     @Column(precision = 0)
84     @Temporal(TemporalType.TIMESTAMP)
85     private Date lastUpdate;
86
87     /**
88      * The Default Constructor creates a {@link JpaPdp} object with a null key.
89      */
90     public JpaPdp() {
91         this(new PfReferenceKey());
92     }
93
94     /**
95      * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
96      *
97      * @param key the key
98      */
99     public JpaPdp(@NonNull final PfReferenceKey key) {
100         this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
105      *
106      * @param key the key
107      * @param pdpState the state of the PDP
108      * @param healthy the health state of the PDP
109      */
110     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
111                     @NonNull PdpHealthStatus healthy) {
112         this.key = key;
113         this.pdpState = pdpState;
114         this.healthy = healthy;
115         this.lastUpdate = new Date();
116     }
117
118     /**
119      * Copy constructor.
120      *
121      * @param copyConcept the concept to copy from
122      */
123     public JpaPdp(@NonNull final JpaPdp copyConcept) {
124         super(copyConcept);
125         this.key = new PfReferenceKey(copyConcept.key);
126         this.pdpState = copyConcept.pdpState;
127         this.healthy = copyConcept.healthy;
128         this.message = copyConcept.message;
129         this.lastUpdate = copyConcept.lastUpdate;
130     }
131
132     /**
133      * Authorative constructor.
134      *
135      * @param authorativeConcept the authorative concept to copy from
136      */
137     public JpaPdp(@NonNull final Pdp authorativeConcept) {
138         this.fromAuthorative(authorativeConcept);
139     }
140
141     @Override
142     public Pdp toAuthorative() {
143         var pdp = new Pdp();
144
145         pdp.setInstanceId(key.getLocalName());
146         pdp.setPdpState(pdpState);
147         pdp.setHealthy(healthy);
148         pdp.setMessage(message);
149         pdp.setLastUpdate(lastUpdate.toInstant());
150
151         return pdp;
152     }
153
154     @Override
155     public void fromAuthorative(@NonNull final Pdp pdp) {
156         if (this.key == null || this.getKey().isNullKey()) {
157             this.setKey(new PfReferenceKey());
158             getKey().setLocalName(pdp.getInstanceId());
159         }
160
161         this.setPdpState(pdp.getPdpState());
162         this.setHealthy(pdp.getHealthy());
163         this.setMessage(pdp.getMessage());
164
165         if (pdp.getLastUpdate() == null) {
166             this.setLastUpdate(new Date());
167         } else {
168             this.setLastUpdate(Date.from(pdp.getLastUpdate()));
169         }
170     }
171
172     @Override
173     public List<PfKey> getKeys() {
174         return getKey().getKeys();
175     }
176
177     @Override
178     public void clean() {
179         key.clean();
180
181         if (message != null) {
182             message = message.trim();
183         }
184     }
185
186     @Override
187     public BeanValidationResult validate(@NonNull String fieldName) {
188         BeanValidationResult result = super.validate(fieldName);
189
190         validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
191
192         if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
193             addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
194         }
195
196         return result;
197     }
198
199     @Override
200     public int compareTo(final PfConcept otherConcept) {
201         if (otherConcept == null) {
202             return -1;
203         }
204         if (this == otherConcept) {
205             return 0;
206         }
207         if (getClass() != otherConcept.getClass()) {
208             return getClass().getName().compareTo(otherConcept.getClass().getName());
209         }
210
211         final JpaPdp other = (JpaPdp) otherConcept;
212
213         // @formatter:off
214         return new CompareToBuilder()
215                         .append(this.key, other.key)
216                         .append(this.pdpState, other.pdpState)
217                         .append(this.healthy, other.healthy)
218                         .append(this.message, other.message)
219                         .append(this.lastUpdate, other.lastUpdate)
220                         .toComparison();
221         // @formatter:on
222     }
223 }