2 * ============LICENSE_START=======================================================
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
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.persistence.concepts;
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;
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;
54 * Class to represent PDP-Policy deployment status in the database.
57 @Table(name = "PdpPolicyStatus", indexes = {@Index(name = "PdpPolicyStatus_PdpGroup", columnList = "pdpGroup")})
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPdpPolicyStatus extends PfConcept implements PfAuthorative<PdpPolicyStatus> {
63 private static final long serialVersionUID = -357224425637789775L;
66 * Parent key & version identifies the policy, while localName identifies the pdpId.
71 private PfReferenceKey key;
75 @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
76 private String pdpGroup;
80 @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
81 private String pdpType;
85 @VerifyKey(versionNotNull = true)
86 private PfConceptKey policyType;
88 @Column(columnDefinition = "TINYINT DEFAULT 1")
89 private boolean deploy;
97 * Constructs an empty object.
99 public JpaPdpPolicyStatus() {
100 key = new PfReferenceKey();
101 pdpGroup = PfKey.NULL_KEY_NAME;
102 pdpType = PfKey.NULL_KEY_NAME;
103 policyType = new PfConceptKey();
105 state = State.WAITING;
111 * @param source object from which to copy
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();
123 * Authorative constructor.
125 * @param source authorative object from which to copy
127 public JpaPdpPolicyStatus(PdpPolicyStatus source) {
128 fromAuthorative(source);
132 public int compareTo(PfConcept otherConcept) {
133 if (otherConcept == null) {
136 if (this == otherConcept) {
139 if (getClass() != otherConcept.getClass()) {
140 return getClass().getName().compareTo(otherConcept.getClass().getName());
143 final JpaPdpPolicyStatus other = (JpaPdpPolicyStatus) otherConcept;
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)
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());
164 return PdpPolicyStatus.builder()
166 .pdpId(key.getLocalName())
168 .policyType(policyTypeIdent)
177 public void fromAuthorative(PdpPolicyStatus source) {
178 final ToscaConceptIdentifier policyIdent = source.getPolicy();
179 final ToscaConceptIdentifier policyTypeIdent = source.getPolicyType();
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();
190 public List<PfKey> getKeys() {
191 return getKey().getKeys();
195 public void clean() {
198 pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
199 pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
204 public BeanValidationResult validate(@NonNull String fieldName) {
205 BeanValidationResult result = super.validate(fieldName);
207 if (PfKey.NULL_KEY_NAME.equals(key.getParentKeyName())) {
208 addResult(result, "policy name (parent key name of key)", key.getParentKeyName(), Validated.IS_NULL);
211 if (PfKey.NULL_KEY_VERSION.equals(key.getParentKeyVersion())) {
212 addResult(result, "policy version (parent key version of key)", key.getParentKeyVersion(),
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);
220 if (PfKey.NULL_KEY_NAME.equals(key.getLocalName())) {
221 addResult(result, "pdpId (local name of key)", key.getLocalName(), Validated.IS_NULL);