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;
64 * Class to represent a PDP statistics in the database.
68 @Table(name = "PdpStatistics")
69 @Index(name = "IDX_TSIDX1", columnNames = {"timeStamp", "name", "version"})
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
73 @EqualsAndHashCode(callSuper = false)
74 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
75 private static final long serialVersionUID = -7312974966820980659L;
79 @GeneratedValue(strategy = GenerationType.TABLE, generator = "statisticsIdGen")
81 name = "statisticsIdGen",
83 pkColumnName = "SEQ_NAME",
84 valueColumnName = "SEQ_COUNT",
85 pkColumnValue = "SEQ_GEN")
86 private Long generatedId;
88 @Column(name = "name", length = 120)
89 @Pattern(regexp = PfKey.NAME_REGEXP)
92 @Column(name = "version", length = 20)
93 @Pattern(regexp = PfKey.VERSION_REGEXP)
94 private String version;
96 @Column(precision = 3)
97 @Temporal(TemporalType.TIMESTAMP)
98 private Date timeStamp;
100 @Column(length = 120)
101 private String pdpGroupName;
103 @Column(length = 120)
104 private String pdpSubGroupName;
107 private long policyDeployCount;
110 private long policyDeploySuccessCount;
113 private long policyDeployFailCount;
116 private long policyExecutedCount;
119 private long policyExecutedSuccessCount;
122 private long policyExecutedFailCount;
125 private long policyUndeployCount;
128 private long policyUndeploySuccessCount;
131 private long policyUndeployFailCount;
134 private List<PdpEngineWorkerStatistics> engineStats;
137 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
139 public JpaPdpStatistics() {
140 this.setName(PfKey.NULL_KEY_NAME);
141 this.setVersion(PfKey.NULL_KEY_VERSION);
147 * @param copyConcept the concept to copy from
149 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
151 this.name = copyConcept.name;
152 this.version = copyConcept.version;
153 this.generatedId = copyConcept.generatedId;
154 this.timeStamp = copyConcept.timeStamp;
155 this.pdpGroupName = copyConcept.pdpGroupName;
156 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
157 this.policyDeployCount = copyConcept.policyDeployCount;
158 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
159 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
160 this.policyUndeployCount = copyConcept.policyUndeployCount;
161 this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
162 this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
163 this.policyExecutedCount = copyConcept.policyExecutedCount;
164 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
165 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
166 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
170 * Authorative constructor.
172 * @param authorativeConcept the authorative concept to copy from
174 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
175 this.fromAuthorative(authorativeConcept);
179 public int compareTo(PfConcept otherConcept) {
180 if (otherConcept == null) {
183 if (this == otherConcept) {
186 if (getClass() != otherConcept.getClass()) {
187 return getClass().getName().compareTo(otherConcept.getClass().getName());
190 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
191 return new CompareToBuilder()
192 .append(this.name, other.name)
193 .append(this.version, other.version)
194 .append(this.generatedId, other.generatedId)
195 .append(this.timeStamp, other.timeStamp)
196 .append(this.pdpGroupName, other.pdpGroupName)
197 .append(this.pdpSubGroupName, other.pdpSubGroupName)
198 .append(this.policyDeployCount, other.policyDeployCount)
199 .append(this.policyDeployFailCount, other.policyDeployFailCount)
200 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
201 .append(this.policyUndeployCount, other.policyUndeployCount)
202 .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
203 .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
204 .append(this.policyExecutedCount, other.policyExecutedCount)
205 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
206 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
210 public PdpStatistics toAuthorative() {
211 var pdpStatistics = new PdpStatistics();
212 pdpStatistics.setPdpInstanceId(name);
213 pdpStatistics.setGeneratedId(generatedId);
214 pdpStatistics.setTimeStamp(timeStamp.toInstant());
215 pdpStatistics.setPdpGroupName(pdpGroupName);
216 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
217 pdpStatistics.setPolicyDeployCount(policyDeployCount);
218 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
219 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
220 pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
221 pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
222 pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
223 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
224 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
225 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
226 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
228 return pdpStatistics;
232 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
233 if (pdpStatistics.getGeneratedId() != null) {
234 this.setGeneratedId(pdpStatistics.getGeneratedId());
236 this.setName(pdpStatistics.getPdpInstanceId());
237 this.setVersion(PfKey.NULL_KEY_VERSION);
238 if (pdpStatistics.getTimeStamp() == null) {
239 this.setTimeStamp(Date.from(Instant.EPOCH));
241 this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
243 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
244 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
245 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
246 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
247 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
248 this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
249 this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
250 this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
251 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
252 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
253 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
255 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
259 public List<PfKey> getKeys() {
260 final List<PfKey> keyList = new ArrayList<>();
261 keyList.add(getKey());
266 public PfKey getKey() {
267 return new PfConceptKey(name, version);
271 public void clean() {
272 pdpGroupName = pdpGroupName.trim();
273 pdpSubGroupName = pdpSubGroupName.trim();
274 if (engineStats != null) {
275 for (PdpEngineWorkerStatistics engineStat : engineStats) {
282 public BeanValidationResult validate(@NonNull String fieldName) {
283 BeanValidationResult result = super.validate(fieldName);
284 if (PfKey.NULL_KEY_NAME.equals(name)) {
285 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);