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.annotations.NotNull;
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.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
49 import org.onap.policy.models.pdp.concepts.PdpStatistics;
53 * Class to represent a PDP statistics in the database.
57 @Table(name = "PdpStatistics")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
63 private static final long serialVersionUID = -7312974966820980659L;
64 private static final String NULL_NAME = "NULL";
69 private PfTimestampKey key;
72 private String pdpGroupName;
75 private String pdpSubGroupName;
78 private long policyDeployCount;
81 private long policyDeploySuccessCount;
84 private long policyDeployFailCount;
87 private long policyExecutedCount;
90 private long policyExecutedSuccessCount;
93 private long policyExecutedFailCount;
96 private List<PdpEngineWorkerStatistics> engineStats;
99 * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
101 public JpaPdpStatistics() {
102 this(new PfTimestampKey());
106 * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
110 public JpaPdpStatistics(@NonNull final PfTimestampKey key) {
111 this(key, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
118 * @param copyConcept the concept to copy from
120 public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
122 this.key = new PfTimestampKey(copyConcept.key);
123 this.pdpGroupName = copyConcept.pdpGroupName;
124 this.pdpSubGroupName = copyConcept.pdpSubGroupName;
125 this.policyDeployCount = copyConcept.policyDeployCount;
126 this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
127 this.policyDeployFailCount = copyConcept.policyDeployFailCount;
128 this.policyExecutedCount = copyConcept.policyExecutedCount;
129 this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
130 this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
131 this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
135 * Authorative constructor.
137 * @param authorativeConcept the authorative concept to copy from
139 public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
140 this.fromAuthorative(authorativeConcept);
144 public int compareTo(PfConcept otherConcept) {
145 if (otherConcept == null) {
148 if (this == otherConcept) {
151 if (getClass() != otherConcept.getClass()) {
152 return getClass().getName().compareTo(otherConcept.getClass().getName());
155 final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
156 return new CompareToBuilder().append(this.key, other.key).append(this.pdpGroupName, other.pdpGroupName)
157 .append(this.pdpSubGroupName, other.pdpSubGroupName)
158 .append(this.policyDeployCount, other.policyDeployCount)
159 .append(this.policyDeployFailCount, other.policyDeployFailCount)
160 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
161 .append(this.policyExecutedCount, other.policyExecutedCount)
162 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
163 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
167 public PdpStatistics toAuthorative() {
168 PdpStatistics pdpStatistics = new PdpStatistics();
169 pdpStatistics.setPdpInstanceId(key.getName());
170 pdpStatistics.setTimeStamp(new Date(key.getTimeStamp().getTime()));
171 pdpStatistics.setPdpGroupName(pdpGroupName);
172 pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
173 pdpStatistics.setPolicyDeployCount(policyDeployCount);
174 pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
175 pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
176 pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
177 pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
178 pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
179 pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
181 return pdpStatistics;
185 public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
186 if (this.key == null || this.getKey().isNullKey()) {
187 this.setKey(new PfTimestampKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION,
188 new Date(pdpStatistics.getTimeStamp() == null ? 0 : pdpStatistics.getTimeStamp().getTime())));
190 this.setPdpGroupName(pdpStatistics.getPdpGroupName());
191 this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
192 this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
193 this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
194 this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
195 this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
196 this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
197 this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
199 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
203 public List<PfKey> getKeys() {
204 return getKey().getKeys();
208 public void clean() {
210 pdpGroupName = pdpGroupName.trim();
211 pdpSubGroupName = pdpSubGroupName.trim();
212 if (engineStats != null) {
213 for (PdpEngineWorkerStatistics engineStat : engineStats) {