Fix null not accepted on PDP message
[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 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.List;
28
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
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39
40 import org.apache.commons.lang3.ObjectUtils;
41 import org.apache.commons.lang3.StringUtils;
42 import org.onap.policy.common.utils.validation.Assertions;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.PfValidationMessage;
48 import org.onap.policy.models.base.PfValidationResult;
49 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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     private static final long serialVersionUID = -357224425637789775L;
66
67     @EmbeddedId
68     private PfReferenceKey key;
69
70     @Column
71     private PdpState pdpState;
72
73     @Column
74     private PdpHealthStatus healthy;
75
76     @Column
77     private String message;
78
79     /**
80      * The Default Constructor creates a {@link JpaPdp} object with a null key.
81      */
82     public JpaPdp() {
83         this(new PfReferenceKey());
84     }
85
86     /**
87      * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
88      *
89      * @param key the key
90      */
91     public JpaPdp(@NonNull final PfReferenceKey key) {
92         this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
93     }
94
95     /**
96      * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
97      *
98      * @param key the key
99      * @param pdpState the state of the PDP
100      * @param healthy the health state of the PDP
101      */
102     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
103             @NonNull PdpHealthStatus healthy) {
104         this.key = key;
105         this.pdpState = pdpState;
106         this.healthy = healthy;
107     }
108
109     /**
110      * Copy constructor.
111      *
112      * @param copyConcept the concept to copy from
113      */
114     public JpaPdp(@NonNull final JpaPdp copyConcept) {
115         super(copyConcept);
116     }
117
118     /**
119      * Authorative constructor.
120      *
121      * @param authorativeConcept the authorative concept to copy from
122      */
123     public JpaPdp(@NonNull final Pdp authorativeConcept) {
124         this.fromAuthorative(authorativeConcept);
125     }
126
127     @Override
128     public Pdp toAuthorative() {
129         Pdp pdp = new Pdp();
130
131         pdp.setInstanceId(key.getLocalName());
132         pdp.setPdpState(pdpState);
133         pdp.setHealthy(healthy);
134         pdp.setMessage(message);
135
136         return pdp;
137     }
138
139     @Override
140     public void fromAuthorative(@NonNull final Pdp pdp) {
141         if (this.key == null || this.getKey().isNullKey()) {
142             this.setKey(new PfReferenceKey());
143             getKey().setLocalName(pdp.getInstanceId());
144         }
145
146         this.setPdpState(pdp.getPdpState());
147         this.setHealthy(pdp.getHealthy());
148         this.setMessage(pdp.getMessage());
149     }
150
151     @Override
152     public List<PfKey> getKeys() {
153         return getKey().getKeys();
154     }
155
156     @Override
157     public void clean() {
158         key.clean();
159
160         if (message != null) {
161             message = message.trim();
162         }
163     }
164
165     @Override
166     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
167         PfValidationResult result = resultIn;
168
169         if (key.isNullKey()) {
170             result.addValidationMessage(
171                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
172         }
173
174         result = key.validate(result);
175
176         if (key.getParentConceptKey().isNullKey()) {
177             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
178                     "parent of key is a null key"));
179         }
180
181         if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
182             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
183                     "local name of parent of key is null"));
184         }
185
186         if (pdpState == null) {
187             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
188                     "PDP state may not be null"));
189         }
190
191         if (healthy == null) {
192             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
193                     "PDP health status may not be null"));
194         }
195
196         if (message != null && StringUtils.isBlank(message)) {
197             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
198                     "message may not be blank"));
199         }
200
201         return result;
202     }
203
204     @Override
205     public int compareTo(final PfConcept otherConcept) {
206         if (otherConcept == null) {
207             return -1;
208         }
209         if (this == otherConcept) {
210             return 0;
211         }
212         if (getClass() != otherConcept.getClass()) {
213             return this.hashCode() - otherConcept.hashCode();
214         }
215
216         final JpaPdp other = (JpaPdp) otherConcept;
217         if (!key.equals(other.key)) {
218             return key.compareTo(other.key);
219         }
220
221         int result = ObjectUtils.compare(pdpState, other.pdpState);
222         if (result != 0) {
223             return result;
224         }
225
226         result = ObjectUtils.compare(healthy, other.healthy);
227         if (result != 0) {
228             return result;
229         }
230
231         return ObjectUtils.compare(message, other.message);
232     }
233
234     @Override
235     public PfConcept copyTo(@NonNull final PfConcept target) {
236         Assertions.instanceOf(target, JpaPdp.class);
237
238         final JpaPdp copy = ((JpaPdp) target);
239         copy.setKey(new PfReferenceKey(key));
240         copy.setPdpState(pdpState);
241         copy.setHealthy(healthy);
242         copy.setMessage(message);
243
244         return copy;
245     }
246 }