2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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.common.parameters.annotations.NotBlank;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
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;
51 * Class to represent a PDP in the database.
53 * @author Liam Fallon (liam.fallon@est.tech)
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
61 private static final long serialVersionUID = -357224425637789775L;
66 private PfReferenceKey key;
70 private PdpState pdpState;
74 private PdpHealthStatus healthy;
78 private String message;
81 * The Default Constructor creates a {@link JpaPdp} object with a null key.
84 this(new PfReferenceKey());
88 * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
92 public JpaPdp(@NonNull final PfReferenceKey key) {
93 this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
97 * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
100 * @param pdpState the state of the PDP
101 * @param healthy the health state of the PDP
103 public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
104 @NonNull PdpHealthStatus healthy) {
106 this.pdpState = pdpState;
107 this.healthy = healthy;
113 * @param copyConcept the concept to copy from
115 public JpaPdp(@NonNull final JpaPdp copyConcept) {
117 this.key = new PfReferenceKey(copyConcept.key);
118 this.pdpState = copyConcept.pdpState;
119 this.healthy = copyConcept.healthy;
120 this.message = copyConcept.message;
124 * Authorative constructor.
126 * @param authorativeConcept the authorative concept to copy from
128 public JpaPdp(@NonNull final Pdp authorativeConcept) {
129 this.fromAuthorative(authorativeConcept);
133 public Pdp toAuthorative() {
136 pdp.setInstanceId(key.getLocalName());
137 pdp.setPdpState(pdpState);
138 pdp.setHealthy(healthy);
139 pdp.setMessage(message);
145 public void fromAuthorative(@NonNull final Pdp pdp) {
146 if (this.key == null || this.getKey().isNullKey()) {
147 this.setKey(new PfReferenceKey());
148 getKey().setLocalName(pdp.getInstanceId());
151 this.setPdpState(pdp.getPdpState());
152 this.setHealthy(pdp.getHealthy());
153 this.setMessage(pdp.getMessage());
157 public List<PfKey> getKeys() {
158 return getKey().getKeys();
162 public void clean() {
165 if (message != null) {
166 message = message.trim();
171 public BeanValidationResult validate(@NonNull String fieldName) {
172 BeanValidationResult result = super.validate(fieldName);
174 validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
176 if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
177 addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
184 public int compareTo(final PfConcept otherConcept) {
185 if (otherConcept == null) {
188 if (this == otherConcept) {
191 if (getClass() != otherConcept.getClass()) {
192 return getClass().getName().compareTo(otherConcept.getClass().getName());
195 final JpaPdp other = (JpaPdp) otherConcept;
196 if (!key.equals(other.key)) {
197 return key.compareTo(other.key);
200 int result = ObjectUtils.compare(pdpState, other.pdpState);
205 result = ObjectUtils.compare(healthy, other.healthy);
210 return ObjectUtils.compare(message, other.message);