88fb14a90f7bb130e2524434effe840442c7d080
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.concepts;
22
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;
31 import lombok.Data;
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;
50
51 /**
52  * Class to represent PDP-Policy deployment status in the database.
53  */
54 @Entity
55 @Table(name = "PdpPolicyStatus", indexes = {@Index(name = "PdpPolicyStatus_PdpGroup", columnList = "pdpGroup")})
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @Data
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaPdpPolicyStatus extends PfConcept implements PfAuthorative<PdpPolicyStatus> {
60     private static final long serialVersionUID = -357224425637789775L;
61
62     /**
63      * Parent key & version identifies the policy, while localName identifies the pdpId.
64      */
65     @EmbeddedId
66     @NotNull
67     @Valid
68     private PfReferenceKey key;
69
70     @Column
71     @NotNull
72     @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
73     private String pdpGroup;
74
75     @Column
76     @NotNull
77     @Pattern(regexp = PfReferenceKey.LOCAL_NAME_REGEXP)
78     private String pdpType;
79
80     @Column
81     @NotNull
82     @VerifyKey(versionNotNull = true)
83     private PfConceptKey policyType;
84
85     @Column(columnDefinition = "TINYINT DEFAULT 1")
86     private boolean deploy;
87
88     @Column
89     @NotNull
90     private State state;
91
92
93     /**
94      * Constructs an empty object.
95      */
96     public JpaPdpPolicyStatus() {
97         key = new PfReferenceKey();
98         pdpGroup = PfKey.NULL_KEY_NAME;
99         pdpType = PfKey.NULL_KEY_NAME;
100         policyType = new PfConceptKey();
101         deploy = false;
102         state = State.WAITING;
103     }
104
105     /**
106      * Copy constructor.
107      *
108      * @param source object from which to copy
109      */
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();
117     }
118
119     /**
120      * Authorative constructor.
121      *
122      * @param source authorative object from which to copy
123      */
124     public JpaPdpPolicyStatus(PdpPolicyStatus source) {
125         fromAuthorative(source);
126     }
127
128     @Override
129     public int compareTo(PfConcept otherConcept) {
130         if (otherConcept == null) {
131             return -1;
132         }
133         if (this == otherConcept) {
134             return 0;
135         }
136         if (getClass() != otherConcept.getClass()) {
137             return getClass().getName().compareTo(otherConcept.getClass().getName());
138         }
139
140         final JpaPdpPolicyStatus other = (JpaPdpPolicyStatus) otherConcept;
141
142         // @formatter:off
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)
150                         .toComparison();
151         // @formatter:on
152     }
153
154     @Override
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());
159
160         // @formatter:off
161         return PdpPolicyStatus.builder()
162                         .pdpGroup(pdpGroup)
163                         .pdpId(key.getLocalName())
164                         .pdpType(pdpType)
165                         .policyType(policyTypeIdent)
166                         .policy(policyIdent)
167                         .deploy(deploy)
168                         .state(state)
169                         .build();
170         // @formatter:on
171     }
172
173     @Override
174     public void fromAuthorative(PdpPolicyStatus source) {
175         final ToscaConceptIdentifier policyIdent = source.getPolicy();
176         final ToscaConceptIdentifier policyTypeIdent = source.getPolicyType();
177
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();
184     }
185
186     @Override
187     public List<PfKey> getKeys() {
188         return getKey().getKeys();
189     }
190
191     @Override
192     public void clean() {
193         key.clean();
194
195         pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
196         pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
197         policyType.clean();
198     }
199
200     @Override
201     public BeanValidationResult validate(@NonNull String fieldName) {
202         BeanValidationResult result = super.validate(fieldName);
203
204         if (PfKey.NULL_KEY_NAME.equals(key.getParentKeyName())) {
205             addResult(result, "policy name (parent key name of key)", key.getParentKeyName(), Validated.IS_NULL);
206         }
207
208         if (PfKey.NULL_KEY_VERSION.equals(key.getParentKeyVersion())) {
209             addResult(result, "policy version (parent key version of key)", key.getParentKeyVersion(),
210                             Validated.IS_NULL);
211         }
212
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);
215         }
216
217         if (PfKey.NULL_KEY_NAME.equals(key.getLocalName())) {
218             addResult(result, "pdpId (local name of key)", key.getLocalName(), Validated.IS_NULL);
219         }
220
221         return result;
222     }
223 }