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;
73 private PfGeneratedIdKey key;
75 @Column(precision = 3)
76 @Temporal(TemporalType.TIMESTAMP)
77 private Date timeStamp;
80 private String pdpGroupName;
83 private String pdpSubGroupName;
86 private long policyDeployCount;
89 private long policyDeploySuccessCount;
92 private long policyDeployFailCount;
95 private long policyExecutedCount;
98 private long policyExecutedSuccessCount;
101 private long policyExecutedFailCount;
104 private long policyUndeployCount;
107 private long policyUndeploySuccessCount;
110 private long policyUndeployFailCount;
113 private List<PdpEngineWorkerStatistics> engineStats;
116 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
118 public JpaPdpStatistics() {
119 this(new PfGeneratedIdKey());
123 * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
127 public JpaPdpStatistics(@NonNull final PfGeneratedIdKey key) {
128 this.key = new PfGeneratedIdKey(key);
134 * @param copyConcept the concept to copy from
136 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
138 this.key = new PfGeneratedIdKey(copyConcept.key);
139 this.timeStamp = copyConcept.timeStamp;
140 this.pdpGroupName = copyConcept.pdpGroupName;
141 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
142 this.policyDeployCount = copyConcept.policyDeployCount;
143 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
144 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
145 this.policyUndeployCount = copyConcept.policyUndeployCount;
146 this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
147 this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
148 this.policyExecutedCount = copyConcept.policyExecutedCount;
149 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
150 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
151 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
155 * Authorative constructor.
157 * @param authorativeConcept the authorative concept to copy from
159 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
160 this.fromAuthorative(authorativeConcept);
164 public int compareTo(PfConcept otherConcept) {
165 if (otherConcept == null) {
168 if (this == otherConcept) {
171 if (getClass() != otherConcept.getClass()) {
172 return getClass().getName().compareTo(otherConcept.getClass().getName());
175 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
176 return new CompareToBuilder()
177 .append(this.key, other.key)
178 .append(this.timeStamp, other.timeStamp)
179 .append(this.pdpGroupName, other.pdpGroupName)
180 .append(this.pdpSubGroupName, other.pdpSubGroupName)
181 .append(this.policyDeployCount, other.policyDeployCount)
182 .append(this.policyDeployFailCount, other.policyDeployFailCount)
183 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
184 .append(this.policyUndeployCount, other.policyUndeployCount)
185 .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
186 .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
187 .append(this.policyExecutedCount, other.policyExecutedCount)
188 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
189 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
193 public PdpStatistics toAuthorative() {
194 var pdpStatistics = new PdpStatistics();
195 pdpStatistics.setPdpInstanceId(key.getName());
196 pdpStatistics.setGeneratedId(key.getGeneratedId());
197 pdpStatistics.setTimeStamp(timeStamp.toInstant());
198 pdpStatistics.setPdpGroupName(pdpGroupName);
199 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
200 pdpStatistics.setPolicyDeployCount(policyDeployCount);
201 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
202 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
203 pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
204 pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
205 pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
206 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
207 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
208 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
209 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
211 return pdpStatistics;
215 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
216 if (pdpStatistics.getGeneratedId() == null) {
217 this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION));
219 this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(),
220 PfKey.NULL_KEY_VERSION, pdpStatistics.getGeneratedId()));
222 if (pdpStatistics.getTimeStamp() == null) {
223 this.setTimeStamp(Date.from(Instant.EPOCH));
225 this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
227 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
228 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
229 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
230 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
231 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
232 this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
233 this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
234 this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
235 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
236 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
237 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
239 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
243 public List<PfKey> getKeys() {
244 return getKey().getKeys();
248 public void clean() {
250 pdpGroupName = pdpGroupName.trim();
251 pdpSubGroupName = pdpSubGroupName.trim();
252 if (engineStats != null) {
253 for (PdpEngineWorkerStatistics engineStat : engineStats) {