2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021,2023 Nordix Foundation.
6 * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.policy.models.pdp.persistence.concepts;
27 import java.io.Serializable;
28 import java.time.Instant;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32 import javax.persistence.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.Entity;
35 import javax.persistence.GeneratedValue;
36 import javax.persistence.GenerationType;
37 import javax.persistence.Id;
38 import javax.persistence.Index;
39 import javax.persistence.Inheritance;
40 import javax.persistence.InheritanceType;
41 import javax.persistence.Table;
42 import javax.persistence.TableGenerator;
43 import javax.persistence.Temporal;
44 import javax.persistence.TemporalType;
45 import lombok.AllArgsConstructor;
47 import lombok.EqualsAndHashCode;
48 import lombok.NonNull;
49 import org.apache.commons.lang3.builder.CompareToBuilder;
50 import org.onap.policy.common.parameters.BeanValidationResult;
51 import org.onap.policy.common.parameters.ValidationStatus;
52 import org.onap.policy.common.parameters.annotations.Pattern;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfConcept;
55 import org.onap.policy.models.base.PfConceptKey;
56 import org.onap.policy.models.base.PfKey;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.base.Validated;
59 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
60 import org.onap.policy.models.pdp.concepts.PdpStatistics;
63 * Class to represent a PDP statistics in the database.
68 name = "PdpStatistics",
72 columnList = "timeStamp,name,version",
77 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
80 @EqualsAndHashCode(callSuper = false)
81 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
82 private static final long serialVersionUID = -7312974966820980659L;
86 @GeneratedValue(strategy = GenerationType.TABLE, generator = "statisticsIdGen")
88 name = "statisticsIdGen",
89 table = "statistics_sequence",
90 pkColumnName = "SEQ_NAME",
91 valueColumnName = "SEQ_COUNT",
92 pkColumnValue = "SEQ_GEN")
93 private Long generatedId;
95 @Column(name = "name", length = 120)
96 @Pattern(regexp = PfKey.NAME_REGEXP)
99 @Column(name = "version", length = 20)
100 @Pattern(regexp = PfKey.VERSION_REGEXP)
101 private String version;
103 @Column(precision = 3)
104 @Temporal(TemporalType.TIMESTAMP)
105 private Date timeStamp;
107 @Column(length = 120)
108 private String pdpGroupName;
110 @Column(length = 120)
111 private String pdpSubGroupName;
114 private long policyDeployCount;
117 private long policyDeploySuccessCount;
120 private long policyDeployFailCount;
123 private long policyExecutedCount;
126 private long policyExecutedSuccessCount;
129 private long policyExecutedFailCount;
132 private long policyUndeployCount;
135 private long policyUndeploySuccessCount;
138 private long policyUndeployFailCount;
141 private List<PdpEngineWorkerStatistics> engineStats;
144 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
146 public JpaPdpStatistics() {
147 this.setName(PfKey.NULL_KEY_NAME);
148 this.setVersion(PfKey.NULL_KEY_VERSION);
154 * @param copyConcept the concept to copy from
156 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
158 this.name = copyConcept.name;
159 this.version = copyConcept.version;
160 this.generatedId = copyConcept.generatedId;
161 this.timeStamp = copyConcept.timeStamp;
162 this.pdpGroupName = copyConcept.pdpGroupName;
163 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
164 this.policyDeployCount = copyConcept.policyDeployCount;
165 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
166 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
167 this.policyUndeployCount = copyConcept.policyUndeployCount;
168 this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
169 this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
170 this.policyExecutedCount = copyConcept.policyExecutedCount;
171 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
172 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
173 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
177 * Authorative constructor.
179 * @param authorativeConcept the authorative concept to copy from
181 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
182 this.fromAuthorative(authorativeConcept);
186 public int compareTo(PfConcept otherConcept) {
187 if (otherConcept == null) {
190 if (this == otherConcept) {
193 if (getClass() != otherConcept.getClass()) {
194 return getClass().getName().compareTo(otherConcept.getClass().getName());
197 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
198 return new CompareToBuilder()
199 .append(this.name, other.name)
200 .append(this.version, other.version)
201 .append(this.generatedId, other.generatedId)
202 .append(this.timeStamp, other.timeStamp)
203 .append(this.pdpGroupName, other.pdpGroupName)
204 .append(this.pdpSubGroupName, other.pdpSubGroupName)
205 .append(this.policyDeployCount, other.policyDeployCount)
206 .append(this.policyDeployFailCount, other.policyDeployFailCount)
207 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
208 .append(this.policyUndeployCount, other.policyUndeployCount)
209 .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
210 .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
211 .append(this.policyExecutedCount, other.policyExecutedCount)
212 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
213 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
217 public PdpStatistics toAuthorative() {
218 var pdpStatistics = new PdpStatistics();
219 pdpStatistics.setPdpInstanceId(name);
220 pdpStatistics.setGeneratedId(generatedId);
221 pdpStatistics.setTimeStamp(timeStamp.toInstant());
222 pdpStatistics.setPdpGroupName(pdpGroupName);
223 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
224 pdpStatistics.setPolicyDeployCount(policyDeployCount);
225 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
226 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
227 pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
228 pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
229 pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
230 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
231 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
232 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
233 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
235 return pdpStatistics;
239 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
240 if (pdpStatistics.getGeneratedId() != null) {
241 this.setGeneratedId(pdpStatistics.getGeneratedId());
243 this.setName(pdpStatistics.getPdpInstanceId());
244 this.setVersion(PfKey.NULL_KEY_VERSION);
245 if (pdpStatistics.getTimeStamp() == null) {
246 this.setTimeStamp(Date.from(Instant.EPOCH));
248 this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
250 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
251 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
252 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
253 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
254 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
255 this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
256 this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
257 this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
258 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
259 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
260 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
262 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
266 public List<PfKey> getKeys() {
267 final List<PfKey> keyList = new ArrayList<>();
268 keyList.add(getKey());
273 public PfKey getKey() {
274 return new PfConceptKey(name, version);
278 public void clean() {
279 pdpGroupName = pdpGroupName.trim();
280 pdpSubGroupName = pdpSubGroupName.trim();
281 if (engineStats != null) {
282 for (PdpEngineWorkerStatistics engineStat : engineStats) {
289 public BeanValidationResult validate(@NonNull String fieldName) {
290 BeanValidationResult result = super.validate(fieldName);
291 if (PfKey.NULL_KEY_NAME.equals(name)) {
292 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);