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