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 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.pdp.persistence.concepts;
26 import java.io.Serializable;
27 import java.time.Instant;
28 import java.util.Date;
29 import java.util.List;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import lombok.AllArgsConstructor;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43 import org.apache.commons.lang3.builder.CompareToBuilder;
44 import org.eclipse.persistence.annotations.Index;
45 import org.onap.policy.common.parameters.annotations.NotNull;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfGeneratedIdKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.base.validation.annotations.VerifyKey;
52 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
53 import org.onap.policy.models.pdp.concepts.PdpStatistics;
57 * Class to represent a PDP statistics in the database.
61 @Table(name = "PdpStatistics")
62 @Index(name = "IDX_TSIDX1", columnNames = {"timeStamp", "name", "version"})
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
68 private static final long serialVersionUID = -7312974966820980659L;
69 private static final String NULL_NAME = "NULL";
74 private PfGeneratedIdKey key;
76 @Column(precision = 3)
77 @Temporal(TemporalType.TIMESTAMP)
78 private Date timeStamp;
81 private String pdpGroupName;
84 private String pdpSubGroupName;
87 private long policyDeployCount;
90 private long policyDeploySuccessCount;
93 private long policyDeployFailCount;
96 private long policyExecutedCount;
99 private long policyExecutedSuccessCount;
102 private long policyExecutedFailCount;
105 private List<PdpEngineWorkerStatistics> engineStats;
108 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
110 public JpaPdpStatistics() {
111 this(new PfGeneratedIdKey());
115 * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
119 public JpaPdpStatistics(@NonNull final PfGeneratedIdKey key) {
120 this(key, null, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
126 * @param copyConcept the concept to copy from
128 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
130 this.key = new PfGeneratedIdKey(copyConcept.key);
131 this.timeStamp = copyConcept.timeStamp;
132 this.pdpGroupName = copyConcept.pdpGroupName;
133 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
134 this.policyDeployCount = copyConcept.policyDeployCount;
135 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
136 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
137 this.policyExecutedCount = copyConcept.policyExecutedCount;
138 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
139 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
140 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
144 * Authorative constructor.
146 * @param authorativeConcept the authorative concept to copy from
148 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
149 this.fromAuthorative(authorativeConcept);
153 public int compareTo(PfConcept otherConcept) {
154 if (otherConcept == null) {
157 if (this == otherConcept) {
160 if (getClass() != otherConcept.getClass()) {
161 return getClass().getName().compareTo(otherConcept.getClass().getName());
164 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
165 return new CompareToBuilder()
166 .append(this.key, other.key)
167 .append(this.timeStamp, other.timeStamp)
168 .append(this.pdpGroupName, other.pdpGroupName)
169 .append(this.pdpSubGroupName, other.pdpSubGroupName)
170 .append(this.policyDeployCount, other.policyDeployCount)
171 .append(this.policyDeployFailCount, other.policyDeployFailCount)
172 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
173 .append(this.policyExecutedCount, other.policyExecutedCount)
174 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
175 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
179 public PdpStatistics toAuthorative() {
180 var pdpStatistics = new PdpStatistics();
181 pdpStatistics.setPdpInstanceId(key.getName());
182 pdpStatistics.setGeneratedId(key.getGeneratedId());
183 pdpStatistics.setTimeStamp(timeStamp.toInstant());
184 pdpStatistics.setPdpGroupName(pdpGroupName);
185 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
186 pdpStatistics.setPolicyDeployCount(policyDeployCount);
187 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
188 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
189 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
190 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
191 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
192 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
194 return pdpStatistics;
198 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
199 if (pdpStatistics.getGeneratedId() == null) {
200 this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION));
202 this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(),
203 PfKey.NULL_KEY_VERSION, pdpStatistics.getGeneratedId()));
205 if (pdpStatistics.getTimeStamp() == null) {
206 this.setTimeStamp(Date.from(Instant.EPOCH));
208 this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
210 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
211 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
212 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
213 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
214 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
215 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
216 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
217 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
219 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
223 public List<PfKey> getKeys() {
224 return getKey().getKeys();
228 public void clean() {
230 pdpGroupName = pdpGroupName.trim();
231 pdpSubGroupName = pdpSubGroupName.trim();
232 if (engineStats != null) {
233 for (PdpEngineWorkerStatistics engineStat : engineStats) {