Replace copyTo methods with copy constructors
[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 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.apache.commons.lang3.StringUtils;
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.base.PfValidationMessage;
44 import org.onap.policy.models.base.PfValidationResult;
45 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
46 import org.onap.policy.models.pdp.concepts.Pdp;
47 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
48 import org.onap.policy.models.pdp.enums.PdpState;
49
50 /**
51  * Class to represent a PDP in the database.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 @Entity
56 @Table(name = "Pdp")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
61     private static final long serialVersionUID = -357224425637789775L;
62
63     @EmbeddedId
64     private PfReferenceKey key;
65
66     @Column
67     private PdpState pdpState;
68
69     @Column
70     private PdpHealthStatus healthy;
71
72     @Column
73     private String message;
74
75     /**
76      * The Default Constructor creates a {@link JpaPdp} object with a null key.
77      */
78     public JpaPdp() {
79         this(new PfReferenceKey());
80     }
81
82     /**
83      * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
84      *
85      * @param key the key
86      */
87     public JpaPdp(@NonNull final PfReferenceKey key) {
88         this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
89     }
90
91     /**
92      * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
93      *
94      * @param key the key
95      * @param pdpState the state of the PDP
96      * @param healthy the health state of the PDP
97      */
98     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
99             @NonNull PdpHealthStatus healthy) {
100         this.key = key;
101         this.pdpState = pdpState;
102         this.healthy = healthy;
103     }
104
105     /**
106      * Copy constructor.
107      *
108      * @param copyConcept the concept to copy from
109      */
110     public JpaPdp(@NonNull final JpaPdp copyConcept) {
111         super(copyConcept);
112         this.key = new PfReferenceKey(copyConcept.key);
113         this.pdpState = copyConcept.pdpState;
114         this.healthy = copyConcept.healthy;
115         this.message = copyConcept.message;
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 getClass().getName().compareTo(otherConcept.getClass().getName());
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 }