2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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.Inheritance;
39 import javax.persistence.InheritanceType;
40 import javax.persistence.Table;
41 import javax.persistence.TableGenerator;
42 import javax.persistence.Temporal;
43 import javax.persistence.TemporalType;
44 import lombok.AllArgsConstructor;
46 import lombok.EqualsAndHashCode;
47 import lombok.NonNull;
48 import org.apache.commons.lang3.builder.CompareToBuilder;
49 import org.eclipse.persistence.annotations.Index;
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.
67 @Table(name = "PdpStatistics")
68 @Index(name = "IDXTSIDX1", columnNames = {
69 "timeStamp", "name", "version"
71 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
74 @EqualsAndHashCode(callSuper = false)
75 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
76 private static final long serialVersionUID = -7312974966820980659L;
80 @GeneratedValue(strategy = GenerationType.TABLE, generator = "statisticsIdGen")
82 name = "statisticsIdGen",
84 pkColumnName = "SEQ_NAME",
85 valueColumnName = "SEQ_COUNT",
86 pkColumnValue = "SEQ_GEN")
87 private Long generatedId;
89 @Column(name = "name", length = 120)
90 @Pattern(regexp = PfKey.NAME_REGEXP)
93 @Column(name = "version", length = 20)
94 @Pattern(regexp = PfKey.VERSION_REGEXP)
95 private String version;
97 @Column(precision = 3)
98 @Temporal(TemporalType.TIMESTAMP)
99 private Date timeStamp;
101 @Column(length = 120)
102 private String pdpGroupName;
104 @Column(length = 120)
105 private String pdpSubGroupName;
108 private long policyDeployCount;
111 private long policyDeploySuccessCount;
114 private long policyDeployFailCount;
117 private long policyExecutedCount;
120 private long policyExecutedSuccessCount;
123 private long policyExecutedFailCount;
126 private long policyUndeployCount;
129 private long policyUndeploySuccessCount;
132 private long policyUndeployFailCount;
135 private List<PdpEngineWorkerStatistics> engineStats;
138 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
140 public JpaPdpStatistics() {
141 this.setName(PfKey.NULL_KEY_NAME);
142 this.setVersion(PfKey.NULL_KEY_VERSION);
148 * @param copyConcept the concept to copy from
150 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
152 this.name = copyConcept.name;
153 this.version = copyConcept.version;
154 this.generatedId = copyConcept.generatedId;
155 this.timeStamp = copyConcept.timeStamp;
156 this.pdpGroupName = copyConcept.pdpGroupName;
157 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
158 this.policyDeployCount = copyConcept.policyDeployCount;
159 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
160 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
161 this.policyUndeployCount = copyConcept.policyUndeployCount;
162 this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
163 this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
164 this.policyExecutedCount = copyConcept.policyExecutedCount;
165 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
166 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
167 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
171 * Authorative constructor.
173 * @param authorativeConcept the authorative concept to copy from
175 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
176 this.fromAuthorative(authorativeConcept);
180 public int compareTo(PfConcept otherConcept) {
181 if (otherConcept == null) {
184 if (this == otherConcept) {
187 if (getClass() != otherConcept.getClass()) {
188 return getClass().getName().compareTo(otherConcept.getClass().getName());
191 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
192 return new CompareToBuilder()
193 .append(this.name, other.name)
194 .append(this.version, other.version)
195 .append(this.generatedId, other.generatedId)
196 .append(this.timeStamp, other.timeStamp)
197 .append(this.pdpGroupName, other.pdpGroupName)
198 .append(this.pdpSubGroupName, other.pdpSubGroupName)
199 .append(this.policyDeployCount, other.policyDeployCount)
200 .append(this.policyDeployFailCount, other.policyDeployFailCount)
201 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
202 .append(this.policyUndeployCount, other.policyUndeployCount)
203 .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
204 .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
205 .append(this.policyExecutedCount, other.policyExecutedCount)
206 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
207 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
211 public PdpStatistics toAuthorative() {
212 var pdpStatistics = new PdpStatistics();
213 pdpStatistics.setPdpInstanceId(name);
214 pdpStatistics.setGeneratedId(generatedId);
215 pdpStatistics.setTimeStamp(timeStamp.toInstant());
216 pdpStatistics.setPdpGroupName(pdpGroupName);
217 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
218 pdpStatistics.setPolicyDeployCount(policyDeployCount);
219 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
220 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
221 pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
222 pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
223 pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
224 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
225 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
226 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
227 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
229 return pdpStatistics;
233 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
234 if (pdpStatistics.getGeneratedId() != null) {
235 this.setGeneratedId(pdpStatistics.getGeneratedId());
237 this.setName(pdpStatistics.getPdpInstanceId());
238 this.setVersion(PfKey.NULL_KEY_VERSION);
239 if (pdpStatistics.getTimeStamp() == null) {
240 this.setTimeStamp(Date.from(Instant.EPOCH));
242 this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
244 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
245 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
246 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
247 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
248 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
249 this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
250 this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
251 this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
252 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
253 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
254 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
256 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
260 public List<PfKey> getKeys() {
261 final List<PfKey> keyList = new ArrayList<>();
262 keyList.add(getKey());
267 public PfKey getKey() {
268 return new PfConceptKey(name, version);
272 public void clean() {
273 pdpGroupName = pdpGroupName.trim();
274 pdpSubGroupName = pdpSubGroupName.trim();
275 if (engineStats != null) {
276 for (PdpEngineWorkerStatistics engineStat : engineStats) {
283 public BeanValidationResult validate(@NonNull String fieldName) {
284 BeanValidationResult result = super.validate(fieldName);
285 if (PfKey.NULL_KEY_NAME.equals(name)) {
286 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);