49163ee8c7f3e810dd6f4b7e83e5ab7eca7b7725
[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  * Modifications Copyright (C) 2020 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.util.Date;
28 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
36 import lombok.AllArgsConstructor;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.builder.CompareToBuilder;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfTimestampKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
48 import org.onap.policy.models.pdp.concepts.PdpStatistics;
49
50
51 /**
52  * Class to represent a PDP statistics in the database.
53  *
54  */
55 @Entity
56 @Table(name = "PdpStatistics")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @AllArgsConstructor
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStatistics>, Serializable {
62     private static final long serialVersionUID = -7312974966820980659L;
63     private static final String NULL_NAME = "NULL";
64
65     @EmbeddedId
66     private PfTimestampKey key;
67
68     @Column(length = 120)
69     private String pdpGroupName;
70
71     @Column(length = 120)
72     private String pdpSubGroupName;
73
74     @Column
75     private long policyDeployCount;
76
77     @Column
78     private long policyDeploySuccessCount;
79
80     @Column
81     private long policyDeployFailCount;
82
83     @Column
84     private long policyExecutedCount;
85
86     @Column
87     private long policyExecutedSuccessCount;
88
89     @Column
90     private long policyExecutedFailCount;
91
92     @ElementCollection
93     private List<PdpEngineWorkerStatistics> engineStats;
94
95     /**
96      * The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
97      */
98     public JpaPdpStatistics() {
99         this(new PfTimestampKey());
100     }
101
102     /**
103      * The Key Constructor creates a {@link JpaPdpStatistics} object with the given concept key.
104      *
105      * @param key the key
106      */
107     public JpaPdpStatistics(@NonNull final PfTimestampKey key) {
108         this(key, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
109     }
110
111
112     /**
113      * Copy constructor.
114      *
115      * @param copyConcept the concept to copy from
116      */
117     public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
118         super(copyConcept);
119         this.key = new PfTimestampKey(copyConcept.key);
120         this.pdpGroupName = copyConcept.pdpGroupName;
121         this.pdpSubGroupName = copyConcept.pdpSubGroupName;
122         this.policyDeployCount = copyConcept.policyDeployCount;
123         this.policyDeploySuccessCount = copyConcept.policyDeploySuccessCount;
124         this.policyDeployFailCount = copyConcept.policyDeployFailCount;
125         this.policyExecutedCount = copyConcept.policyExecutedCount;
126         this.policyExecutedSuccessCount = copyConcept.policyExecutedSuccessCount;
127         this.policyExecutedFailCount = copyConcept.policyExecutedFailCount;
128         this.engineStats = PfUtils.mapList(copyConcept.engineStats, PdpEngineWorkerStatistics::new, null);
129     }
130
131     /**
132      * Authorative constructor.
133      *
134      * @param authorativeConcept the authorative concept to copy from
135      */
136     public JpaPdpStatistics(@NonNull final PdpStatistics authorativeConcept) {
137         this.fromAuthorative(authorativeConcept);
138     }
139
140     @Override
141     public int compareTo(PfConcept otherConcept) {
142         if (otherConcept == null) {
143             return -1;
144         }
145         if (this == otherConcept) {
146             return 0;
147         }
148         if (getClass() != otherConcept.getClass()) {
149             return getClass().getName().compareTo(otherConcept.getClass().getName());
150         }
151
152         final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
153         return new CompareToBuilder().append(this.key, other.key).append(this.pdpGroupName, other.pdpGroupName)
154                 .append(this.pdpSubGroupName, other.pdpSubGroupName)
155                 .append(this.policyDeployCount, other.policyDeployCount)
156                 .append(this.policyDeployFailCount, other.policyDeployFailCount)
157                 .append(this.policyDeploySuccessCount, other.policyDeploySuccessCount)
158                 .append(this.policyExecutedCount, other.policyExecutedCount)
159                 .append(this.policyExecutedFailCount, other.policyExecutedFailCount)
160                 .append(this.policyExecutedSuccessCount, other.policyExecutedSuccessCount).toComparison();
161     }
162
163     @Override
164     public PdpStatistics toAuthorative() {
165         PdpStatistics pdpStatistics = new PdpStatistics();
166         pdpStatistics.setPdpInstanceId(key.getName());
167         pdpStatistics.setTimeStamp(new Date(key.getTimeStamp().getTime()));
168         pdpStatistics.setPdpGroupName(pdpGroupName);
169         pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
170         pdpStatistics.setPolicyDeployCount(policyDeployCount);
171         pdpStatistics.setPolicyDeployFailCount(policyDeployFailCount);
172         pdpStatistics.setPolicyDeploySuccessCount(policyDeploySuccessCount);
173         pdpStatistics.setPolicyExecutedCount(policyExecutedCount);
174         pdpStatistics.setPolicyExecutedFailCount(policyExecutedFailCount);
175         pdpStatistics.setPolicyExecutedSuccessCount(policyExecutedSuccessCount);
176         pdpStatistics.setEngineStats(PfUtils.mapList(engineStats, PdpEngineWorkerStatistics::new, null));
177
178         return pdpStatistics;
179     }
180
181     @Override
182     public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
183         if (this.key == null || this.getKey().isNullKey()) {
184             this.setKey(new PfTimestampKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION,
185                     new Date(pdpStatistics.getTimeStamp() == null ? 0 : pdpStatistics.getTimeStamp().getTime())));
186         }
187         this.setPdpGroupName(pdpStatistics.getPdpGroupName());
188         this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
189         this.setPolicyDeployCount(pdpStatistics.getPolicyDeployCount());
190         this.setPolicyDeployFailCount(pdpStatistics.getPolicyDeployFailCount());
191         this.setPolicyDeploySuccessCount(pdpStatistics.getPolicyDeploySuccessCount());
192         this.setPolicyExecutedCount(pdpStatistics.getPolicyExecutedCount());
193         this.setPolicyExecutedFailCount(pdpStatistics.getPolicyExecutedFailCount());
194         this.setPolicyExecutedSuccessCount(pdpStatistics.getPolicyExecutedSuccessCount());
195         this.setEngineStats(
196                 PfUtils.mapList(pdpStatistics.getEngineStats(), PdpEngineWorkerStatistics::new, null));
197     }
198
199     @Override
200     public List<PfKey> getKeys() {
201         return getKey().getKeys();
202     }
203
204     @Override
205     public BeanValidationResult validate(@NonNull String fieldName) {
206         BeanValidationResult result = new BeanValidationResult(fieldName, this);
207
208         result.addResult(validateKeyNotNull("key", key));
209
210         return result;
211     }
212
213     @Override
214     public void clean() {
215         key.clean();
216         pdpGroupName = pdpGroupName.trim();
217         pdpSubGroupName = pdpSubGroupName.trim();
218         if (engineStats != null) {
219             for (PdpEngineWorkerStatistics engineStat : engineStats) {
220                 engineStat.clean();
221             }
222         }
223     }
224 }