2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.pdp.persistence.concepts;
26 import java.io.Serializable;
27 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;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
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;
55 * Class to represent a PDP in the database.
57 * @author Liam Fallon (liam.fallon@est.tech)
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
65 private static final long serialVersionUID = -357224425637789775L;
68 private PfReferenceKey key;
71 private PdpState pdpState;
74 private PdpHealthStatus healthy;
77 private String message;
80 * The Default Constructor creates a {@link JpaPdp} object with a null key.
83 this(new PfReferenceKey());
87 * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
91 public JpaPdp(@NonNull final PfReferenceKey key) {
92 this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
96 * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
99 * @param pdpState the state of the PDP
100 * @param healthy the health state of the PDP
102 public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
103 @NonNull PdpHealthStatus healthy) {
105 this.pdpState = pdpState;
106 this.healthy = healthy;
112 * @param copyConcept the concept to copy from
114 public JpaPdp(@NonNull final JpaPdp copyConcept) {
119 * Authorative constructor.
121 * @param authorativeConcept the authorative concept to copy from
123 public JpaPdp(@NonNull final Pdp authorativeConcept) {
124 this.fromAuthorative(authorativeConcept);
128 public Pdp toAuthorative() {
131 pdp.setInstanceId(key.getLocalName());
132 pdp.setPdpState(pdpState);
133 pdp.setHealthy(healthy);
134 pdp.setMessage(message);
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());
146 this.setPdpState(pdp.getPdpState());
147 this.setHealthy(pdp.getHealthy());
148 this.setMessage(pdp.getMessage());
152 public List<PfKey> getKeys() {
153 return getKey().getKeys();
157 public void clean() {
160 if (message != null) {
161 message = message.trim();
166 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
167 PfValidationResult result = resultIn;
169 if (key.isNullKey()) {
170 result.addValidationMessage(
171 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
174 result = key.validate(result);
176 if (key.getParentConceptKey().isNullKey()) {
177 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
178 "parent of key is a null key"));
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"));
186 if (pdpState == null) {
187 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
188 "PDP state may not be null"));
191 if (healthy == null) {
192 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
193 "PDP health status may not be null"));
196 if (StringUtils.isBlank(message)) {
197 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
198 "message may not be blank"));
205 public int compareTo(final PfConcept otherConcept) {
206 if (otherConcept == null) {
209 if (this == otherConcept) {
212 if (getClass() != otherConcept.getClass()) {
213 return this.hashCode() - otherConcept.hashCode();
216 final JpaPdp other = (JpaPdp) otherConcept;
217 if (!key.equals(other.key)) {
218 return key.compareTo(other.key);
221 int result = ObjectUtils.compare(pdpState, other.pdpState);
226 result = ObjectUtils.compare(healthy, other.healthy);
231 return ObjectUtils.compare(message, other.message);
235 public PfConcept copyTo(@NonNull final PfConcept target) {
236 Assertions.instanceOf(target, JpaPdp.class);
238 final JpaPdp copy = ((JpaPdp) target);
239 copy.setKey(new PfReferenceKey(key));
240 copy.setPdpState(pdpState);
241 copy.setHealthy(healthy);
242 copy.setMessage(message);