Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpPolicyStatus.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 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  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.persistence.concepts;
23
24 import jakarta.persistence.Column;
25 import jakarta.persistence.EmbeddedId;
26 import jakarta.persistence.Entity;
27 import jakarta.persistence.Index;
28 import jakarta.persistence.Inheritance;
29 import jakarta.persistence.InheritanceType;
30 import jakarta.persistence.Table;
31 import java.io.Serial;
32 import java.util.List;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.apache.commons.lang3.builder.CompareToBuilder;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Pattern;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.common.utils.validation.Assertions;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.Validated;
48 import org.onap.policy.models.base.validation.annotations.VerifyKey;
49 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
50 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52
53 /**
54  * Class to represent PDP-Policy deployment status in the database.
55  */
56 @Entity
57 @Table(name = "PdpPolicyStatus", indexes = {@Index(name = "PdpPolicyStatus_PdpGroup", columnList = "pdpGroup")})
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @Data
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPdpPolicyStatus extends PfConcept implements PfAuthorative<PdpPolicyStatus> {
62     @Serial
63     private static final long serialVersionUID = -357224425637789775L;
64
65     /**
66      * Parent key & version identifies the policy, while localName identifies the pdpId.
67      */
68     @EmbeddedId
69     @NotNull
70     @Valid
71     private PfReferenceKey key;
72
73     @Column
74     @NotNull
75     @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
76     private String pdpGroup;
77
78     @Column
79     @NotNull
80     @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
81     private String pdpType;
82
83     @Column
84     @NotNull
85     @VerifyKey(versionNotNull = true)
86     private PfConceptKey policyType;
87
88     @Column(columnDefinition = "TINYINT DEFAULT 1")
89     private boolean deploy;
90
91     @Column
92     @NotNull
93     private State state;
94
95
96     /**
97      * Constructs an empty object.
98      */
99     public JpaPdpPolicyStatus() {
100         key = new PfReferenceKey();
101         pdpGroup = PfKey.NULL_KEY_NAME;
102         pdpType = PfKey.NULL_KEY_NAME;
103         policyType = new PfConceptKey();
104         deploy = false;
105         state = State.WAITING;
106     }
107
108     /**
109      * Copy constructor.
110      *
111      * @param source object from which to copy
112      */
113     public JpaPdpPolicyStatus(JpaPdpPolicyStatus source) {
114         key = new PfReferenceKey(source.getKey());
115         pdpGroup = source.getPdpGroup();
116         pdpType = source.getPdpType();
117         policyType = new PfConceptKey(source.getPolicyType());
118         deploy = source.isDeploy();
119         state = source.getState();
120     }
121
122     /**
123      * Authorative constructor.
124      *
125      * @param source authorative object from which to copy
126      */
127     public JpaPdpPolicyStatus(PdpPolicyStatus source) {
128         fromAuthorative(source);
129     }
130
131     @Override
132     public int compareTo(PfConcept otherConcept) {
133         if (otherConcept == null) {
134             return -1;
135         }
136         if (this == otherConcept) {
137             return 0;
138         }
139         if (getClass() != otherConcept.getClass()) {
140             return getClass().getName().compareTo(otherConcept.getClass().getName());
141         }
142
143         final JpaPdpPolicyStatus other = (JpaPdpPolicyStatus) otherConcept;
144
145         // @formatter:off
146         return new CompareToBuilder()
147                         .append(key, other.key)
148                         .append(pdpGroup, other.pdpGroup)
149                         .append(pdpType, other.pdpType)
150                         .append(policyType, other.policyType)
151                         .append(deploy, other.deploy)
152                         .append(state, other.state)
153                         .toComparison();
154         // @formatter:on
155     }
156
157     @Override
158     public PdpPolicyStatus toAuthorative() {
159         PfConceptKey policyKey = key.getParentConceptKey();
160         var policyIdent = new ToscaConceptIdentifier(policyKey.getName(), policyKey.getVersion());
161         var policyTypeIdent = new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion());
162
163         // @formatter:off
164         return PdpPolicyStatus.builder()
165                         .pdpGroup(pdpGroup)
166                         .pdpId(key.getLocalName())
167                         .pdpType(pdpType)
168                         .policyType(policyTypeIdent)
169                         .policy(policyIdent)
170                         .deploy(deploy)
171                         .state(state)
172                         .build();
173         // @formatter:on
174     }
175
176     @Override
177     public void fromAuthorative(PdpPolicyStatus source) {
178         final ToscaConceptIdentifier policyIdent = source.getPolicy();
179         final ToscaConceptIdentifier policyTypeIdent = source.getPolicyType();
180
181         key = new PfReferenceKey(policyIdent.getName(), policyIdent.getVersion(), source.getPdpId());
182         pdpGroup = source.getPdpGroup();
183         pdpType = source.getPdpType();
184         policyType = new PfConceptKey(policyTypeIdent.getName(), policyTypeIdent.getVersion());
185         deploy = source.isDeploy();
186         state = source.getState();
187     }
188
189     @Override
190     public List<PfKey> getKeys() {
191         return getKey().getKeys();
192     }
193
194     @Override
195     public void clean() {
196         key.clean();
197
198         pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
199         pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
200         policyType.clean();
201     }
202
203     @Override
204     public BeanValidationResult validate(@NonNull String fieldName) {
205         BeanValidationResult result = super.validate(fieldName);
206
207         if (PfKey.NULL_KEY_NAME.equals(key.getParentKeyName())) {
208             addResult(result, "policy name (parent key name of key)", key.getParentKeyName(), Validated.IS_NULL);
209         }
210
211         if (PfKey.NULL_KEY_VERSION.equals(key.getParentKeyVersion())) {
212             addResult(result, "policy version (parent key version of key)", key.getParentKeyVersion(),
213                             Validated.IS_NULL);
214         }
215
216         if (!PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
217             addResult(result, "parent local name of key", key.getParentLocalName(), "must be " + PfKey.NULL_KEY_NAME);
218         }
219
220         if (PfKey.NULL_KEY_NAME.equals(key.getLocalName())) {
221             addResult(result, "pdpId (local name of key)", key.getLocalName(), Validated.IS_NULL);
222         }
223
224         return result;
225     }
226 }