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