Update pdp statistics to count deploy/undeploy separately.
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpStatistics.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.pdp.persistence.concepts;
25
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;
40 import lombok.Data;
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;
54
55
56 /**
57  * Class to represent a PDP statistics in the database.
58  *
59  */
60 @Entity
61 @Table(name = "PdpStatistics")
62 @Index(name = "IDX_TSIDX1", columnNames = {"timeStamp", "name", "version"})
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @Data
65 @AllArgsConstructor
66 @EqualsAndHashCode(callSuper = false)
67 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
68     private static final long serialVersionUID = -7312974966820980659L;
69
70     @EmbeddedId
71     @VerifyKey
72     @NotNull
73     private PfGeneratedIdKey key;
74
75     @Column(precision = 3)
76     @Temporal(TemporalType.TIMESTAMP)
77     private Date timeStamp;
78
79     @Column(length = 120)
80     private String pdpGroupName;
81
82     @Column(length = 120)
83     private String pdpSubGroupName;
84
85     @Column
86     private long policyDeployCount;
87
88     @Column
89     private long policyDeploySuccessCount;
90
91     @Column
92     private long policyDeployFailCount;
93
94     @Column
95     private long policyExecutedCount;
96
97     @Column
98     private long policyExecutedSuccessCount;
99
100     @Column
101     private long policyExecutedFailCount;
102
103     @Column
104     private long policyUndeployCount;
105
106     @Column
107     private long policyUndeploySuccessCount;
108
109     @Column
110     private long policyUndeployFailCount;
111
112     @ElementCollection
113     private List<PdpEngineWorkerStatistics> engineStats;
114
115     /**
116      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
117      */
118     public JpaPdpStatistics() {
119         this(new PfGeneratedIdKey());
120     }
121
122     /**
123      * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
124      *
125      * @param key the key
126      */
127     public JpaPdpStatistics(@NonNull final PfGeneratedIdKey key) {
128         this.key = new PfGeneratedIdKey(key);
129     }
130
131     /**
132      * Copy constructor.
133      *
134      * @param copyConcept the concept to copy from
135      */
136     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
137         super(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);
152     }
153
154     /**
155      * Authorative constructor.
156      *
157      * @param authorativeConcept the authorative concept to copy from
158      */
159     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
160         this.fromAuthorative(authorativeConcept);
161     }
162
163     @Override
164     public int compareTo(PfConcept otherConcept) {
165         if (otherConcept == null) {
166             return -1;
167         }
168         if (this == otherConcept) {
169             return 0;
170         }
171         if (getClass() != otherConcept.getClass()) {
172             return getClass().getName().compareTo(otherConcept.getClass().getName());
173         }
174
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();
190     }
191
192     @Override
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));
210
211         return pdpStatistics;
212     }
213
214     @Override
215     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
216         if (pdpStatistics.getGeneratedId() == null) {
217             this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION));
218         } else {
219             this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(),
220                         PfKey.NULL_KEY_VERSION, pdpStatistics.getGeneratedId()));
221         }
222         if (pdpStatistics.getTimeStamp() == null) {
223             this.setTimeStamp(Date.from(Instant.EPOCH));
224         } else {
225             this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
226         }
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());
238         this.setEngineStats(
239                 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
240     }
241
242     @Override
243     public List<PfKey> getKeys() {
244         return getKey().getKeys();
245     }
246
247     @Override
248     public void clean() {
249         key.clean();
250         pdpGroupName = pdpGroupName.trim();
251         pdpSubGroupName = pdpSubGroupName.trim();
252         if (engineStats != null) {
253             for (PdpEngineWorkerStatistics engineStat : engineStats) {
254                 engineStat.clean();
255             }
256         }
257     }
258 }