2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.persistence.concepts;
23 import java.util.List;
24 import javax.persistence.Column;
25 import javax.persistence.EmbeddedId;
26 import javax.persistence.Entity;
27 import javax.persistence.Index;
28 import javax.persistence.Inheritance;
29 import javax.persistence.InheritanceType;
30 import javax.persistence.Table;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34 import org.apache.commons.lang3.builder.CompareToBuilder;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.parameters.annotations.NotNull;
37 import org.onap.policy.common.parameters.annotations.Pattern;
38 import org.onap.policy.common.parameters.annotations.Valid;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.Validated;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
48 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 * Class to represent PDP-Policy deployment status in the database.
55 @Table(name = "PdpPolicyStatus", indexes = {@Index(name = "PdpPolicyStatus_PdpGroup", columnList = "pdpGroup")})
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaPdpPolicyStatus extends PfConcept implements PfAuthorative<PdpPolicyStatus> {
60 private static final long serialVersionUID = -357224425637789775L;
63 * Parent key & version identifies the policy, while localName identifies the pdpId.
68 private PfReferenceKey key;
72 @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
73 private String pdpGroup;
77 @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
78 private String pdpType;
82 @VerifyKey(versionNotNull = true)
83 private PfConceptKey policyType;
85 @Column(columnDefinition = "TINYINT DEFAULT 1")
86 private boolean deploy;
94 * Constructs an empty object.
96 public JpaPdpPolicyStatus() {
97 key = new PfReferenceKey();
98 pdpGroup = PfKey.NULL_KEY_NAME;
99 pdpType = PfKey.NULL_KEY_NAME;
100 policyType = new PfConceptKey();
102 state = State.WAITING;
108 * @param source object from which to copy
110 public JpaPdpPolicyStatus(JpaPdpPolicyStatus source) {
111 key = new PfReferenceKey(source.getKey());
112 pdpGroup = source.getPdpGroup();
113 pdpType = source.getPdpType();
114 policyType = new PfConceptKey(source.getPolicyType());
115 deploy = source.isDeploy();
116 state = source.getState();
120 * Authorative constructor.
122 * @param source authorative object from which to copy
124 public JpaPdpPolicyStatus(PdpPolicyStatus source) {
125 fromAuthorative(source);
129 public int compareTo(PfConcept otherConcept) {
130 if (otherConcept == null) {
133 if (this == otherConcept) {
136 if (getClass() != otherConcept.getClass()) {
137 return getClass().getName().compareTo(otherConcept.getClass().getName());
140 final JpaPdpPolicyStatus other = (JpaPdpPolicyStatus) otherConcept;
143 return new CompareToBuilder()
144 .append(key, other.key)
145 .append(pdpGroup, other.pdpGroup)
146 .append(pdpType, other.pdpType)
147 .append(policyType, other.policyType)
148 .append(deploy, other.deploy)
149 .append(state, other.state)
155 public PdpPolicyStatus toAuthorative() {
156 PfConceptKey policyKey = key.getParentConceptKey();
157 var policyIdent = new ToscaConceptIdentifier(policyKey.getName(), policyKey.getVersion());
158 var policyTypeIdent = new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion());
161 return PdpPolicyStatus.builder()
163 .pdpId(key.getLocalName())
165 .policyType(policyTypeIdent)
174 public void fromAuthorative(PdpPolicyStatus source) {
175 final ToscaConceptIdentifier policyIdent = source.getPolicy();
176 final ToscaConceptIdentifier policyTypeIdent = source.getPolicyType();
178 key = new PfReferenceKey(policyIdent.getName(), policyIdent.getVersion(), source.getPdpId());
179 pdpGroup = source.getPdpGroup();
180 pdpType = source.getPdpType();
181 policyType = new PfConceptKey(policyTypeIdent.getName(), policyTypeIdent.getVersion());
182 deploy = source.isDeploy();
183 state = source.getState();
187 public List<PfKey> getKeys() {
188 return getKey().getKeys();
192 public void clean() {
195 pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
196 pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
201 public BeanValidationResult validate(@NonNull String fieldName) {
202 BeanValidationResult result = super.validate(fieldName);
204 if (PfKey.NULL_KEY_NAME.equals(key.getParentKeyName())) {
205 addResult(result, "policy name (parent key name of key)", key.getParentKeyName(), Validated.IS_NULL);
208 if (PfKey.NULL_KEY_VERSION.equals(key.getParentKeyVersion())) {
209 addResult(result, "policy version (parent key version of key)", key.getParentKeyVersion(),
213 if (!PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
214 addResult(result, "parent local name of key", key.getParentLocalName(), "must be " + PfKey.NULL_KEY_NAME);
217 if (PfKey.NULL_KEY_NAME.equals(key.getLocalName())) {
218 addResult(result, "pdpId (local name of key)", key.getLocalName(), Validated.IS_NULL);