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