7eb4e7c39bda9fe77d743bbb0667e10acb014f1b
[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  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.models.pdp.persistence.concepts;
26
27 import java.io.Serializable;
28 import java.time.Instant;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
32 import javax.persistence.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.Entity;
35 import javax.persistence.GeneratedValue;
36 import javax.persistence.GenerationType;
37 import javax.persistence.Id;
38 import javax.persistence.Inheritance;
39 import javax.persistence.InheritanceType;
40 import javax.persistence.Table;
41 import javax.persistence.TableGenerator;
42 import javax.persistence.Temporal;
43 import javax.persistence.TemporalType;
44 import lombok.AllArgsConstructor;
45 import lombok.Data;
46 import lombok.EqualsAndHashCode;
47 import lombok.NonNull;
48 import org.apache.commons.lang3.builder.CompareToBuilder;
49 import org.eclipse.persistence.annotations.Index;
50 import org.onap.policy.common.parameters.BeanValidationResult;
51 import org.onap.policy.common.parameters.ValidationStatus;
52 import org.onap.policy.common.parameters.annotations.Pattern;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfConcept;
55 import org.onap.policy.models.base.PfConceptKey;
56 import org.onap.policy.models.base.PfKey;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.base.Validated;
59 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
60 import org.onap.policy.models.pdp.concepts.PdpStatistics;
61
62 /**
63  * Class to represent a PDP statistics in the database.
64  *
65  */
66 @Entity
67 @Table(name = "PdpStatistics")
68 @Index(name = "IDXTSIDX1", columnNames = {
69     "timeStamp", "name", "version"
70 })
71 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
72 @Data
73 @AllArgsConstructor
74 @EqualsAndHashCode(callSuper = false)
75 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
76     private static final long serialVersionUID = -7312974966820980659L;
77
78     @Id
79     @Column(name = "ID")
80     @GeneratedValue(strategy = GenerationType.TABLE, generator = "statisticsIdGen")
81     @TableGenerator(
82         name = "statisticsIdGen",
83         table = "sequence",
84         pkColumnName = "SEQ_NAME",
85         valueColumnName = "SEQ_COUNT",
86         pkColumnValue = "SEQ_GEN")
87     private Long generatedId;
88
89     @Column(name = "name", length = 120)
90     @Pattern(regexp = PfKey.NAME_REGEXP)
91     private String name;
92
93     @Column(name = "version", length = 20)
94     @Pattern(regexp = PfKey.VERSION_REGEXP)
95     private String version;
96
97     @Column(precision = 3)
98     @Temporal(TemporalType.TIMESTAMP)
99     private Date timeStamp;
100
101     @Column(length = 120)
102     private String pdpGroupName;
103
104     @Column(length = 120)
105     private String pdpSubGroupName;
106
107     @Column
108     private long policyDeployCount;
109
110     @Column
111     private long policyDeploySuccessCount;
112
113     @Column
114     private long policyDeployFailCount;
115
116     @Column
117     private long policyExecutedCount;
118
119     @Column
120     private long policyExecutedSuccessCount;
121
122     @Column
123     private long policyExecutedFailCount;
124
125     @Column
126     private long policyUndeployCount;
127
128     @Column
129     private long policyUndeploySuccessCount;
130
131     @Column
132     private long policyUndeployFailCount;
133
134     @ElementCollection
135     private List<PdpEngineWorkerStatistics> engineStats;
136
137     /**
138      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
139      */
140     public JpaPdpStatistics() {
141         this.setName(PfKey.NULL_KEY_NAME);
142         this.setVersion(PfKey.NULL_KEY_VERSION);
143     }
144
145     /**
146      * Copy constructor.
147      *
148      * @param copyConcept the concept to copy from
149      */
150     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
151         super(copyConcept);
152         this.name = copyConcept.name;
153         this.version = copyConcept.version;
154         this.generatedId = copyConcept.generatedId;
155         this.timeStamp = copyConcept.timeStamp;
156         this.pdpGroupName = copyConcept.pdpGroupName;
157         this.pdpSubGroupName = copyConcept.pdpSubGroupName;
158         this.policyDeployCount = copyConcept.policyDeployCount;
159         this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
160         this.policyDeployFailCount = copyConcept.policyDeployFailCount;
161         this.policyUndeployCount = copyConcept.policyUndeployCount;
162         this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
163         this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
164         this.policyExecutedCount = copyConcept.policyExecutedCount;
165         this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
166         this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
167         this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
168     }
169
170     /**
171      * Authorative constructor.
172      *
173      * @param authorativeConcept the authorative concept to copy from
174      */
175     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
176         this.fromAuthorative(authorativeConcept);
177     }
178
179     @Override
180     public int compareTo(PfConcept otherConcept) {
181         if (otherConcept == null) {
182             return -1;
183         }
184         if (this == otherConcept) {
185             return 0;
186         }
187         if (getClass() != otherConcept.getClass()) {
188             return getClass().getName().compareTo(otherConcept.getClass().getName());
189         }
190
191         final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
192         return new CompareToBuilder()
193             .append(this.name, other.name)
194             .append(this.version, other.version)
195             .append(this.generatedId, other.generatedId)
196             .append(this.timeStamp, other.timeStamp)
197             .append(this.pdpGroupName, other.pdpGroupName)
198             .append(this.pdpSubGroupName, other.pdpSubGroupName)
199             .append(this.policyDeployCount, other.policyDeployCount)
200             .append(this.policyDeployFailCount, other.policyDeployFailCount)
201             .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
202             .append(this.policyUndeployCount, other.policyUndeployCount)
203             .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
204             .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
205             .append(this.policyExecutedCount, other.policyExecutedCount)
206             .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
207             .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
208     }
209
210     @Override
211     public PdpStatistics toAuthorative() {
212         var pdpStatistics = new PdpStatistics();
213         pdpStatistics.setPdpInstanceId(name);
214         pdpStatistics.setGeneratedId(generatedId);
215         pdpStatistics.setTimeStamp(timeStamp.toInstant());
216         pdpStatistics.setPdpGroupName(pdpGroupName);
217         pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
218         pdpStatistics.setPolicyDeployCount(policyDeployCount);
219         pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
220         pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
221         pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
222         pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
223         pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
224         pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
225         pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
226         pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
227         pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
228
229         return pdpStatistics;
230     }
231
232     @Override
233     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
234         if (pdpStatistics.getGeneratedId() != null) {
235             this.setGeneratedId(pdpStatistics.getGeneratedId());
236         }
237         this.setName(pdpStatistics.getPdpInstanceId());
238         this.setVersion(PfKey.NULL_KEY_VERSION);
239         if (pdpStatistics.getTimeStamp() == null) {
240             this.setTimeStamp(Date.from(Instant.EPOCH));
241         } else {
242             this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
243         }
244         this.setPdpGroupName(pdpStatistics.getPdpGroupName());
245         this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
246         this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
247         this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
248         this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
249         this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
250         this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
251         this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
252         this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
253         this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
254         this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
255         this.setEngineStats(
256             PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
257     }
258
259     @Override
260     public List<PfKey> getKeys() {
261         final List<PfKey> keyList = new ArrayList<>();
262         keyList.add(getKey());
263         return keyList;
264     }
265
266     @Override
267     public PfKey getKey() {
268         return new PfConceptKey(name, version);
269     }
270
271     @Override
272     public void clean() {
273         pdpGroupName = pdpGroupName.trim();
274         pdpSubGroupName = pdpSubGroupName.trim();
275         if (engineStats != null) {
276             for (PdpEngineWorkerStatistics engineStat : engineStats) {
277                 engineStat.clean();
278             }
279         }
280     }
281
282     @Override
283     public BeanValidationResult validate(@NonNull String fieldName) {
284         BeanValidationResult result = super.validate(fieldName);
285         if (PfKey.NULL_KEY_NAME.equals(name)) {
286             result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);
287         }
288         return result;
289     }
290 }