2 * ============LICENSE_START=======================================================
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
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;
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;
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;
48 * Class to represent a PDP in the database.
50 * @author Liam Fallon (liam.fallon@est.tech)
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @EqualsAndHashCode(callSuper = false)
57 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
58 private static final long serialVersionUID = -357224425637789775L;
61 private PfReferenceKey key;
64 private PdpState pdpState;
67 private PdpHealthStatus healthy;
70 private String message;
73 * The Default Constructor creates a {@link JpaPdp} object with a null key.
76 this(new PfReferenceKey());
80 * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
84 public JpaPdp(@NonNull final PfReferenceKey key) {
85 this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
89 * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
92 * @param pdpState the state of the PDP
93 * @param healthy the health state of the PDP
95 public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
96 @NonNull PdpHealthStatus healthy) {
98 this.pdpState = pdpState;
99 this.healthy = healthy;
105 * @param copyConcept the concept to copy from
107 public JpaPdp(@NonNull final JpaPdp copyConcept) {
109 this.key = new PfReferenceKey(copyConcept.key);
110 this.pdpState = copyConcept.pdpState;
111 this.healthy = copyConcept.healthy;
112 this.message = copyConcept.message;
116 * Authorative constructor.
118 * @param authorativeConcept the authorative concept to copy from
120 public JpaPdp(@NonNull final Pdp authorativeConcept) {
121 this.fromAuthorative(authorativeConcept);
125 public Pdp toAuthorative() {
128 pdp.setInstanceId(key.getLocalName());
129 pdp.setPdpState(pdpState);
130 pdp.setHealthy(healthy);
131 pdp.setMessage(message);
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());
143 this.setPdpState(pdp.getPdpState());
144 this.setHealthy(pdp.getHealthy());
145 this.setMessage(pdp.getMessage());
149 public List<PfKey> getKeys() {
150 return getKey().getKeys();
154 public void clean() {
157 if (message != null) {
158 message = message.trim();
163 public BeanValidationResult validate(@NonNull String fieldName) {
164 BeanValidationResult result = new BeanValidationResult(fieldName, this);
166 result.addResult(validateKeyNotNull("key", key));
167 result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
169 if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
170 addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
173 result.addResult(validateNotNull("pdpState", pdpState));
174 result.addResult(validateNotNull("healthy", healthy));
175 result.addResult(validateNotBlank("message", message, false));
181 public int compareTo(final PfConcept otherConcept) {
182 if (otherConcept == null) {
185 if (this == otherConcept) {
188 if (getClass() != otherConcept.getClass()) {
189 return getClass().getName().compareTo(otherConcept.getClass().getName());
192 final JpaPdp other = (JpaPdp) otherConcept;
193 if (!key.equals(other.key)) {
194 return key.compareTo(other.key);
197 int result = ObjectUtils.compare(pdpState, other.pdpState);
202 result = ObjectUtils.compare(healthy, other.healthy);
207 return ObjectUtils.compare(message, other.message);