2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nordix Foundation.
6 * Modifications Copyright (C) 2020 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.util.Date;
28 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
36 import lombok.AllArgsConstructor;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.builder.CompareToBuilder;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfTimestampKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
48 import org.onap.policy.models.pdp.concepts.PdpStatistics;
52 * Class to represent a PDP statistics in the database.
56 @Table(name = "PdpStatistics")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
62 private static final long serialVersionUID = -7312974966820980659L;
63 private static final String NULL_NAME = "NULL";
66 private PfTimestampKey key;
69 private String pdpGroupName;
72 private String pdpSubGroupName;
75 private long policyDeployCount;
78 private long policyDeploySuccessCount;
81 private long policyDeployFailCount;
84 private long policyExecutedCount;
87 private long policyExecutedSuccessCount;
90 private long policyExecutedFailCount;
93 private List<PdpEngineWorkerStatistics> engineStats;
96 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
98 public JpaPdpStatistics() {
99 this(new PfTimestampKey());
103 * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
107 public JpaPdpStatistics(@NonNull final PfTimestampKey key) {
108 this(key, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
115 * @param copyConcept the concept to copy from
117 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
119 this.key = new PfTimestampKey(copyConcept.key);
120 this.pdpGroupName = copyConcept.pdpGroupName;
121 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
122 this.policyDeployCount = copyConcept.policyDeployCount;
123 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
124 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
125 this.policyExecutedCount = copyConcept.policyExecutedCount;
126 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
127 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
128 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
132 * Authorative constructor.
134 * @param authorativeConcept the authorative concept to copy from
136 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
137 this.fromAuthorative(authorativeConcept);
141 public int compareTo(PfConcept otherConcept) {
142 if (otherConcept == null) {
145 if (this == otherConcept) {
148 if (getClass() != otherConcept.getClass()) {
149 return getClass().getName().compareTo(otherConcept.getClass().getName());
152 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
153 return new CompareToBuilder().append(this.key, other.key).append(this.pdpGroupName, other.pdpGroupName)
154 .append(this.pdpSubGroupName, other.pdpSubGroupName)
155 .append(this.policyDeployCount, other.policyDeployCount)
156 .append(this.policyDeployFailCount, other.policyDeployFailCount)
157 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
158 .append(this.policyExecutedCount, other.policyExecutedCount)
159 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
160 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
164 public PdpStatistics toAuthorative() {
165 PdpStatistics pdpStatistics = new PdpStatistics();
166 pdpStatistics.setPdpInstanceId(key.getName());
167 pdpStatistics.setTimeStamp(new Date(key.getTimeStamp().getTime()));
168 pdpStatistics.setPdpGroupName(pdpGroupName);
169 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
170 pdpStatistics.setPolicyDeployCount(policyDeployCount);
171 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
172 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
173 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
174 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
175 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
176 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
178 return pdpStatistics;
182 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
183 if (this.key == null || this.getKey().isNullKey()) {
184 this.setKey(new PfTimestampKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION,
185 new Date(pdpStatistics.getTimeStamp() == null ? 0 : pdpStatistics.getTimeStamp().getTime())));
187 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
188 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
189 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
190 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
191 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
192 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
193 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
194 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
196 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
200 public List<PfKey> getKeys() {
201 return getKey().getKeys();
205 public BeanValidationResult validate(@NonNull String fieldName) {
206 BeanValidationResult result = new BeanValidationResult(fieldName, this);
208 result.addResult(validateKeyNotNull("key", key));
214 public void clean() {
216 pdpGroupName = pdpGroupName.trim();
217 pdpSubGroupName = pdpSubGroupName.trim();
218 if (engineStats != null) {
219 for (PdpEngineWorkerStatistics engineStat : engineStats) {