Merge "Fix sonars in policy models"
[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     private static final String NULL_NAME = "NULL";
70
71     @EmbeddedId
72     @VerifyKey
73     @NotNull
74     private PfGeneratedIdKey key;
75
76     @Column(precision = 3)
77     @Temporal(TemporalType.TIMESTAMP)
78     private Date timeStamp;
79
80     @Column(length = 120)
81     private String pdpGroupName;
82
83     @Column(length = 120)
84     private String pdpSubGroupName;
85
86     @Column
87     private long policyDeployCount;
88
89     @Column
90     private long policyDeploySuccessCount;
91
92     @Column
93     private long policyDeployFailCount;
94
95     @Column
96     private long policyExecutedCount;
97
98     @Column
99     private long policyExecutedSuccessCount;
100
101     @Column
102     private long policyExecutedFailCount;
103
104     @ElementCollection
105     private List<PdpEngineWorkerStatistics> engineStats;
106
107     /**
108      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
109      */
110     public JpaPdpStatistics() {
111         this(new PfGeneratedIdKey());
112     }
113
114     /**
115      * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
116      *
117      * @param key the key
118      */
119     public JpaPdpStatistics(@NonNull final PfGeneratedIdKey key) {
120         this(key, null, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
121     }
122
123     /**
124      * Copy constructor.
125      *
126      * @param copyConcept the concept to copy from
127      */
128     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
129         super(copyConcept);
130         this.key = new PfGeneratedIdKey(copyConcept.key);
131         this.timeStamp = copyConcept.timeStamp;
132         this.pdpGroupName = copyConcept.pdpGroupName;
133         this.pdpSubGroupName = copyConcept.pdpSubGroupName;
134         this.policyDeployCount = copyConcept.policyDeployCount;
135         this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
136         this.policyDeployFailCount = copyConcept.policyDeployFailCount;
137         this.policyExecutedCount = copyConcept.policyExecutedCount;
138         this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
139         this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
140         this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
141     }
142
143     /**
144      * Authorative constructor.
145      *
146      * @param authorativeConcept the authorative concept to copy from
147      */
148     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
149         this.fromAuthorative(authorativeConcept);
150     }
151
152     @Override
153     public int compareTo(PfConcept otherConcept) {
154         if (otherConcept == null) {
155             return -1;
156         }
157         if (this == otherConcept) {
158             return 0;
159         }
160         if (getClass() != otherConcept.getClass()) {
161             return getClass().getName().compareTo(otherConcept.getClass().getName());
162         }
163
164         final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
165         return new CompareToBuilder()
166                 .append(this.key, other.key)
167                 .append(this.timeStamp, other.timeStamp)
168                 .append(this.pdpGroupName, other.pdpGroupName)
169                 .append(this.pdpSubGroupName, other.pdpSubGroupName)
170                 .append(this.policyDeployCount, other.policyDeployCount)
171                 .append(this.policyDeployFailCount, other.policyDeployFailCount)
172                 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
173                 .append(this.policyExecutedCount, other.policyExecutedCount)
174                 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
175                 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
176     }
177
178     @Override
179     public PdpStatistics toAuthorative() {
180         var pdpStatistics = new PdpStatistics();
181         pdpStatistics.setPdpInstanceId(key.getName());
182         pdpStatistics.setGeneratedId(key.getGeneratedId());
183         pdpStatistics.setTimeStamp(timeStamp.toInstant());
184         pdpStatistics.setPdpGroupName(pdpGroupName);
185         pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
186         pdpStatistics.setPolicyDeployCount(policyDeployCount);
187         pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
188         pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
189         pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
190         pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
191         pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
192         pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
193
194         return pdpStatistics;
195     }
196
197     @Override
198     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
199         if (pdpStatistics.getGeneratedId() == null) {
200             this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION));
201         } else {
202             this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(),
203                         PfKey.NULL_KEY_VERSION, pdpStatistics.getGeneratedId()));
204         }
205         if (pdpStatistics.getTimeStamp() == null) {
206             this.setTimeStamp(Date.from(Instant.EPOCH));
207         } else {
208             this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
209         }
210         this.setPdpGroupName(pdpStatistics.getPdpGroupName());
211         this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
212         this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
213         this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
214         this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
215         this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
216         this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
217         this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
218         this.setEngineStats(
219                 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
220     }
221
222     @Override
223     public List<PfKey> getKeys() {
224         return getKey().getKeys();
225     }
226
227     @Override
228     public void clean() {
229         key.clean();
230         pdpGroupName = pdpGroupName.trim();
231         pdpSubGroupName = pdpSubGroupName.trim();
232         if (engineStats != null) {
233             for (PdpEngineWorkerStatistics engineStat : engineStats) {
234                 engineStat.clean();
235             }
236         }
237     }
238 }