Replace Eclipselink with Hibernate
[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,2023 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.Index;
39 import javax.persistence.Inheritance;
40 import javax.persistence.InheritanceType;
41 import javax.persistence.Table;
42 import javax.persistence.TableGenerator;
43 import javax.persistence.Temporal;
44 import javax.persistence.TemporalType;
45 import lombok.AllArgsConstructor;
46 import lombok.Data;
47 import lombok.EqualsAndHashCode;
48 import lombok.NonNull;
49 import org.apache.commons.lang3.builder.CompareToBuilder;
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(
68     name = "PdpStatistics",
69     indexes = {
70         @Index(
71             name = "IDXTSIDX1",
72             columnList = "timeStamp,name,version",
73             unique = true
74             )
75     }
76 )
77 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
78 @Data
79 @AllArgsConstructor
80 @EqualsAndHashCode(callSuper = false)
81 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
82     private static final long serialVersionUID = -7312974966820980659L;
83
84     @Id
85     @Column(name = "ID")
86     @GeneratedValue(strategy = GenerationType.TABLE, generator = "statisticsIdGen")
87     @TableGenerator(
88         name = "statisticsIdGen",
89         table = "statistics_sequence",
90         pkColumnName = "SEQ_NAME",
91         valueColumnName = "SEQ_COUNT",
92         pkColumnValue = "SEQ_GEN")
93     private Long generatedId;
94
95     @Column(name = "name", length = 120)
96     @Pattern(regexp = PfKey.NAME_REGEXP)
97     private String name;
98
99     @Column(name = "version", length = 20)
100     @Pattern(regexp = PfKey.VERSION_REGEXP)
101     private String version;
102
103     @Column(precision = 3)
104     @Temporal(TemporalType.TIMESTAMP)
105     private Date timeStamp;
106
107     @Column(length = 120)
108     private String pdpGroupName;
109
110     @Column(length = 120)
111     private String pdpSubGroupName;
112
113     @Column
114     private long policyDeployCount;
115
116     @Column
117     private long policyDeploySuccessCount;
118
119     @Column
120     private long policyDeployFailCount;
121
122     @Column
123     private long policyExecutedCount;
124
125     @Column
126     private long policyExecutedSuccessCount;
127
128     @Column
129     private long policyExecutedFailCount;
130
131     @Column
132     private long policyUndeployCount;
133
134     @Column
135     private long policyUndeploySuccessCount;
136
137     @Column
138     private long policyUndeployFailCount;
139
140     @ElementCollection
141     private List<PdpEngineWorkerStatistics> engineStats;
142
143     /**
144      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
145      */
146     public JpaPdpStatistics() {
147         this.setName(PfKey.NULL_KEY_NAME);
148         this.setVersion(PfKey.NULL_KEY_VERSION);
149     }
150
151     /**
152      * Copy constructor.
153      *
154      * @param copyConcept the concept to copy from
155      */
156     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
157         super(copyConcept);
158         this.name = copyConcept.name;
159         this.version = copyConcept.version;
160         this.generatedId = copyConcept.generatedId;
161         this.timeStamp = copyConcept.timeStamp;
162         this.pdpGroupName = copyConcept.pdpGroupName;
163         this.pdpSubGroupName = copyConcept.pdpSubGroupName;
164         this.policyDeployCount = copyConcept.policyDeployCount;
165         this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
166         this.policyDeployFailCount = copyConcept.policyDeployFailCount;
167         this.policyUndeployCount = copyConcept.policyUndeployCount;
168         this.policyUndeploySuccessCount = copyConcept.policyUndeploySuccessCount;
169         this.policyUndeployFailCount = copyConcept.policyUndeployFailCount;
170         this.policyExecutedCount = copyConcept.policyExecutedCount;
171         this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
172         this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
173         this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
174     }
175
176     /**
177      * Authorative constructor.
178      *
179      * @param authorativeConcept the authorative concept to copy from
180      */
181     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
182         this.fromAuthorative(authorativeConcept);
183     }
184
185     @Override
186     public int compareTo(PfConcept otherConcept) {
187         if (otherConcept == null) {
188             return -1;
189         }
190         if (this == otherConcept) {
191             return 0;
192         }
193         if (getClass() != otherConcept.getClass()) {
194             return getClass().getName().compareTo(otherConcept.getClass().getName());
195         }
196
197         final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
198         return new CompareToBuilder()
199             .append(this.name, other.name)
200             .append(this.version, other.version)
201             .append(this.generatedId, other.generatedId)
202             .append(this.timeStamp, other.timeStamp)
203             .append(this.pdpGroupName, other.pdpGroupName)
204             .append(this.pdpSubGroupName, other.pdpSubGroupName)
205             .append(this.policyDeployCount, other.policyDeployCount)
206             .append(this.policyDeployFailCount, other.policyDeployFailCount)
207             .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
208             .append(this.policyUndeployCount, other.policyUndeployCount)
209             .append(this.policyUndeployFailCount, other.policyUndeployFailCount)
210             .append(this.policyUndeploySuccessCount, other.policyUndeploySuccessCount)
211             .append(this.policyExecutedCount, other.policyExecutedCount)
212             .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
213             .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
214     }
215
216     @Override
217     public PdpStatistics toAuthorative() {
218         var pdpStatistics = new PdpStatistics();
219         pdpStatistics.setPdpInstanceId(name);
220         pdpStatistics.setGeneratedId(generatedId);
221         pdpStatistics.setTimeStamp(timeStamp.toInstant());
222         pdpStatistics.setPdpGroupName(pdpGroupName);
223         pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
224         pdpStatistics.setPolicyDeployCount(policyDeployCount);
225         pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
226         pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
227         pdpStatistics.setPolicyUndeployCount(policyUndeployCount);
228         pdpStatistics.setPolicyUndeployFailCount(policyUndeployFailCount);
229         pdpStatistics.setPolicyUndeploySuccessCount(policyUndeploySuccessCount);
230         pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
231         pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
232         pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
233         pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
234
235         return pdpStatistics;
236     }
237
238     @Override
239     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
240         if (pdpStatistics.getGeneratedId() != null) {
241             this.setGeneratedId(pdpStatistics.getGeneratedId());
242         }
243         this.setName(pdpStatistics.getPdpInstanceId());
244         this.setVersion(PfKey.NULL_KEY_VERSION);
245         if (pdpStatistics.getTimeStamp() == null) {
246             this.setTimeStamp(Date.from(Instant.EPOCH));
247         } else {
248             this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
249         }
250         this.setPdpGroupName(pdpStatistics.getPdpGroupName());
251         this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
252         this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
253         this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
254         this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
255         this.setPolicyUndeployCount(pdpStatistics.getPolicyUndeployCount());
256         this.setPolicyUndeployFailCount(pdpStatistics.getPolicyUndeployFailCount());
257         this.setPolicyUndeploySuccessCount(pdpStatistics.getPolicyUndeploySuccessCount());
258         this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
259         this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
260         this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
261         this.setEngineStats(
262             PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
263     }
264
265     @Override
266     public List<PfKey> getKeys() {
267         final List<PfKey> keyList = new ArrayList<>();
268         keyList.add(getKey());
269         return keyList;
270     }
271
272     @Override
273     public PfKey getKey() {
274         return new PfConceptKey(name, version);
275     }
276
277     @Override
278     public void clean() {
279         pdpGroupName = pdpGroupName.trim();
280         pdpSubGroupName = pdpSubGroupName.trim();
281         if (engineStats != null) {
282             for (PdpEngineWorkerStatistics engineStat : engineStats) {
283                 engineStat.clean();
284             }
285         }
286     }
287
288     @Override
289     public BeanValidationResult validate(@NonNull String fieldName) {
290         BeanValidationResult result = super.validate(fieldName);
291         if (PfKey.NULL_KEY_NAME.equals(name)) {
292             result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);
293         }
294         return result;
295     }
296 }