87a52fe387f892931fddd47b62571e71297d9d98
[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-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
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.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;
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     @VerifyKey
65     @NotNull
66     private PfReferenceKey key;
67
68     @Column
69     @NotNull
70     private PdpState pdpState;
71
72     @Column
73     @NotNull
74     private PdpHealthStatus healthy;
75
76     @Column
77     @NotBlank
78     private String message;
79
80     /**
81      * The Default Constructor creates a {@link JpaPdp} object with a null key.
82      */
83     public JpaPdp() {
84         this(new PfReferenceKey());
85     }
86
87     /**
88      * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
89      *
90      * @param key the key
91      */
92     public JpaPdp(@NonNull final PfReferenceKey key) {
93         this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
94     }
95
96     /**
97      * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
98      *
99      * @param key the key
100      * @param pdpState the state of the PDP
101      * @param healthy the health state of the PDP
102      */
103     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
104             @NonNull PdpHealthStatus healthy) {
105         this.key = key;
106         this.pdpState = pdpState;
107         this.healthy = healthy;
108     }
109
110     /**
111      * Copy constructor.
112      *
113      * @param copyConcept the concept to copy from
114      */
115     public JpaPdp(@NonNull final JpaPdp copyConcept) {
116         super(copyConcept);
117         this.key = new PfReferenceKey(copyConcept.key);
118         this.pdpState = copyConcept.pdpState;
119         this.healthy = copyConcept.healthy;
120         this.message = copyConcept.message;
121     }
122
123     /**
124      * Authorative constructor.
125      *
126      * @param authorativeConcept the authorative concept to copy from
127      */
128     public JpaPdp(@NonNull final Pdp authorativeConcept) {
129         this.fromAuthorative(authorativeConcept);
130     }
131
132     @Override
133     public Pdp toAuthorative() {
134         Pdp pdp = new Pdp();
135
136         pdp.setInstanceId(key.getLocalName());
137         pdp.setPdpState(pdpState);
138         pdp.setHealthy(healthy);
139         pdp.setMessage(message);
140
141         return pdp;
142     }
143
144     @Override
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());
149         }
150
151         this.setPdpState(pdp.getPdpState());
152         this.setHealthy(pdp.getHealthy());
153         this.setMessage(pdp.getMessage());
154     }
155
156     @Override
157     public List<PfKey> getKeys() {
158         return getKey().getKeys();
159     }
160
161     @Override
162     public void clean() {
163         key.clean();
164
165         if (message != null) {
166             message = message.trim();
167         }
168     }
169
170     @Override
171     public BeanValidationResult validate(@NonNull String fieldName) {
172         BeanValidationResult result = super.validate(fieldName);
173
174         validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
175
176         if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
177             addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
178         }
179
180         return result;
181     }
182
183     @Override
184     public int compareTo(final PfConcept otherConcept) {
185         if (otherConcept == null) {
186             return -1;
187         }
188         if (this == otherConcept) {
189             return 0;
190         }
191         if (getClass() != otherConcept.getClass()) {
192             return getClass().getName().compareTo(otherConcept.getClass().getName());
193         }
194
195         final JpaPdp other = (JpaPdp) otherConcept;
196         if (!key.equals(other.key)) {
197             return key.compareTo(other.key);
198         }
199
200         int result = ObjectUtils.compare(pdpState, other.pdpState);
201         if (result != 0) {
202             return result;
203         }
204
205         result = ObjectUtils.compare(healthy, other.healthy);
206         if (result != 0) {
207             return result;
208         }
209
210         return ObjectUtils.compare(message, other.message);
211     }
212 }