Add JpaPdpStatistics entity
[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 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.pdp.persistence.concepts;
24
25 import java.io.Serializable;
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import lombok.AllArgsConstructor;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.lang3.builder.CompareToBuilder;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfTimestampKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.PfValidationMessage;
46 import org.onap.policy.models.base.PfValidationResult;
47 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
48 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
49 import org.onap.policy.models.pdp.concepts.PdpStatistics;
50
51
52 /**
53  * Class to represent a PDP statistics in the database.
54  *
55  */
56 @Entity
57 @Table(name = "PdpStatistics")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @Data
60 @AllArgsConstructor
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
63     private static final long serialVersionUID = -7312974966820980659L;
64     private static final String NULL_NAME = "NULL";
65
66     @EmbeddedId
67     private PfTimestampKey key;
68
69     @Column(length = 120)
70     private String pdpGroupName;
71
72     @Column(length = 120)
73     private String pdpSubGroupName;
74
75     @Column
76     private long policyDeployCount;
77
78     @Column
79     private long policyDeploySuccessCount;
80
81     @Column
82     private long policyDeployFailCount;
83
84     @Column
85     private long policyExecutedCount;
86
87     @Column
88     private long policyExecutedSuccessCount;
89
90     @Column
91     private long policyExecutedFailCount;
92
93     @ElementCollection
94     private List<PdpEngineWorkerStatistics> engineStats;
95
96     /**
97      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
98      */
99     public JpaPdpStatistics() {
100         this(new PfTimestampKey());
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
105      *
106      * @param key the key
107      */
108     public JpaPdpStatistics(@NonNull final PfTimestampKey key) {
109         this(key, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
110     }
111
112
113     /**
114      * Copy constructor.
115      *
116      * @param copyConcept the concept to copy from
117      */
118     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
119         super(copyConcept);
120         this.key = new PfTimestampKey(copyConcept.key);
121         this.pdpGroupName = copyConcept.pdpGroupName;
122         this.pdpSubGroupName = copyConcept.pdpSubGroupName;
123         this.policyDeployCount = copyConcept.policyDeployCount;
124         this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
125         this.policyDeployFailCount = copyConcept.policyDeployFailCount;
126         this.policyExecutedCount = copyConcept.policyExecutedCount;
127         this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
128         this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
129         this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
130     }
131
132     /**
133      * Authorative constructor.
134      *
135      * @param authorativeConcept the authorative concept to copy from
136      */
137     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
138         this.fromAuthorative(authorativeConcept);
139     }
140
141     @Override
142     public int compareTo(PfConcept otherConcept) {
143         if (otherConcept == null) {
144             return -1;
145         }
146         if (this == otherConcept) {
147             return 0;
148         }
149         if (getClass() != otherConcept.getClass()) {
150             return getClass().getName().compareTo(otherConcept.getClass().getName());
151         }
152
153         final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
154         return new CompareToBuilder().append(this.key, other.key).append(this.pdpGroupName, other.pdpGroupName)
155                 .append(this.pdpSubGroupName, other.pdpSubGroupName)
156                 .append(this.policyDeployCount, other.policyDeployCount)
157                 .append(this.policyDeployFailCount, other.policyDeployFailCount)
158                 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
159                 .append(this.policyExecutedCount, other.policyExecutedCount)
160                 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
161                 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
162     }
163
164     @Override
165     public PdpStatistics toAuthorative() {
166         PdpStatistics pdpStatistics = new PdpStatistics();
167         pdpStatistics.setPdpInstanceId(key.getName());
168         pdpStatistics.setTimeStamp(new Date(key.getTimeStamp().getTime()));
169         pdpStatistics.setPdpGroupName(pdpGroupName);
170         pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
171         pdpStatistics.setPolicyDeployCount(policyDeployCount);
172         pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
173         pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
174         pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
175         pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
176         pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
177         pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
178
179         return pdpStatistics;
180     }
181
182     @Override
183     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
184         if (this.key == null || this.getKey().isNullKey()) {
185             this.setKey(new PfTimestampKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION,
186                     new Date(pdpStatistics.getTimeStamp() == null ? 0 : pdpStatistics.getTimeStamp().getTime())));
187         }
188         this.setPdpGroupName(pdpStatistics.getPdpGroupName());
189         this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
190         this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
191         this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
192         this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
193         this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
194         this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
195         this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
196         this.setEngineStats(
197                 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
198     }
199
200     @Override
201     public List<PfKey> getKeys() {
202         return getKey().getKeys();
203     }
204
205     @Override
206     public PfValidationResult validate(@NonNull PfValidationResult resultIn) {
207         PfValidationResult result = resultIn;
208
209         if (key.isNullKey()) {
210             result.addValidationMessage(
211                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
212         }
213
214         result = key.validate(result);
215
216         return result;
217     }
218
219     @Override
220     public void clean() {
221         key.clean();
222         pdpGroupName = pdpGroupName.trim();
223         pdpSubGroupName = pdpSubGroupName.trim();
224         if (engineStats != null) {
225             for (PdpEngineWorkerStatistics engineStat : engineStats) {
226                 engineStat.clean();
227             }
228         }
229     }
230 }