Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / engine-model / src / main / java / org / onap / policy / apex / model / enginemodel / concepts / AxEngineStats.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.enginemodel.concepts;
23
24 import java.text.SimpleDateFormat;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Table;
30 import javax.persistence.Transient;
31 import javax.xml.bind.annotation.XmlAccessType;
32 import javax.xml.bind.annotation.XmlAccessorType;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import javax.xml.bind.annotation.XmlType;
36 import lombok.Getter;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
44 import org.onap.policy.common.utils.validation.Assertions;
45
46 /**
47  * This class is a java bean that is used to record statistics on Apex engines as they execute. Statistics on the number
48  * of events, the amount of time taken to execute the last policy, the average policy execution time, the up time of the
49  * engine, and the time stamp of the last engine start are recorded.
50  */
51
52 @Entity
53 @Table(name = "AxEngineStats")
54
55 @XmlAccessorType(XmlAccessType.FIELD)
56 @XmlRootElement(name = "apexEngineStats", namespace = "http://www.onap.org/policy/apex-pdp")
57 @XmlType(name = "AxEngineStats", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "timeStamp",
58     "eventCount", "lastExecutionTime", "averageExecutionTime", "upTime", "lastStart"})
59 public class AxEngineStats extends AxConcept {
60     private static final long serialVersionUID = -6981129081962785368L;
61     private static final int HASH_CODE_PRIME = 32;
62
63     @EmbeddedId
64     @XmlElement(name = "key", required = true)
65     private AxReferenceKey key;
66
67     @Column
68     @XmlElement(required = true)
69     private long timeStamp;
70
71     @Column
72     @XmlElement(required = true)
73     private long eventCount;
74
75     @Column
76     @XmlElement(required = true)
77     private long lastExecutionTime;
78
79     @Column
80     @XmlElement(required = true)
81     private double averageExecutionTime;
82
83     @Column
84     @XmlElement(required = true)
85     private long upTime;
86
87     @Transient
88     @Getter
89     private transient long lastEnterTime;
90
91     @Column
92     @XmlElement(required = true)
93     private long lastStart;
94
95     /**
96      * The Default Constructor creates an engine statistics instance with a null key and with all values cleared.
97      */
98     public AxEngineStats() {
99         this(new AxReferenceKey());
100         timeStamp = 0;
101         eventCount = 0;
102         lastExecutionTime = 0;
103         averageExecutionTime = 0;
104         upTime = 0;
105         lastEnterTime = 0;
106         lastStart = 0;
107     }
108
109     /**
110      * Copy constructor.
111      *
112      * @param copyConcept the concept to copy from
113      */
114     public AxEngineStats(final AxEngineStats copyConcept) {
115         super(copyConcept);
116     }
117
118     /**
119      * The Keyed Constructor creates an engine statistics instance with the given key and all values cleared.
120      *
121      * @param key the key
122      */
123     public AxEngineStats(final AxReferenceKey key) {
124         this(key, 0, 0, 0, 0, 0, 0);
125     }
126
127     /**
128      * This Constructor creates an engine statistics instance with all its fields set.
129      *
130      * @param key the engine statistics key
131      * @param timeStamp the time stamp at which the statistics were recorded
132      * @param eventCount the number of events processed by the engine
133      * @param lastExecutionTime the amount of time taken to execute the last policy
134      * @param averageExecutionTime the average amount of time taken to execute a policy
135      * @param upTime the time that has elapsed since the policy engine was started
136      * @param lastStart the time at which the policy engine was last started
137      */
138     public AxEngineStats(final AxReferenceKey key, final long timeStamp, final long eventCount,
139             final long lastExecutionTime, final double averageExecutionTime, final long upTime, final long lastStart) {
140         super();
141         Assertions.argumentNotNull(key, "key may not be null");
142
143         this.key = key;
144         this.timeStamp = timeStamp;
145         this.eventCount = eventCount;
146         this.lastExecutionTime = lastExecutionTime;
147         this.averageExecutionTime = averageExecutionTime;
148         this.upTime = upTime;
149         this.lastStart = lastStart;
150     }
151
152     /**
153      * {@inheritDoc}.
154      */
155     @Override
156     public AxReferenceKey getKey() {
157         return key;
158     }
159
160     /**
161      * {@inheritDoc}.
162      */
163     @Override
164     public List<AxKey> getKeys() {
165         return key.getKeys();
166     }
167
168     /**
169      * Sets the engine statistics key.
170      *
171      * @param key the engine statistics key
172      */
173     public void setKey(final AxReferenceKey key) {
174         Assertions.argumentNotNull(key, "key may not be null");
175         this.key = key;
176     }
177
178     /**
179      * Gets the time stamp at which the statistics were recorded.
180      *
181      * @return the time stamp at which the statistics were recorded
182      */
183     public long getTimeStamp() {
184         return timeStamp;
185     }
186
187     /**
188      * Gets the time stamp at which the statistics were recorded as a string.
189      *
190      * @return the time stamp at which the statistics were recorded as a string
191      */
192     public String getTimeStampString() {
193         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp);
194     }
195
196     /**
197      * Sets the time stamp at which the statistics were recorded.
198      *
199      * @param timeStamp the time stamp at which the statistics were recorded
200      */
201     public void setTimeStamp(final long timeStamp) {
202         this.timeStamp = timeStamp;
203     }
204
205     /**
206      * Gets the number of events processed by the engine.
207      *
208      * @return the number of events processed by the engine
209      */
210     public long getEventCount() {
211         return eventCount;
212     }
213
214     /**
215      * Sets the number of events processed by the engine.
216      *
217      * @param eventCount the number of events processed by the engine
218      */
219     public void setEventCount(final long eventCount) {
220         this.eventCount = eventCount;
221     }
222
223     /**
224      * Gets the amount of time taken to execute the last policy.
225      *
226      * @return the amount of time taken to execute the last policy
227      */
228     public long getLastExecutionTime() {
229         return lastExecutionTime;
230     }
231
232     /**
233      * Sets the amount of time taken to execute the last policy.
234      *
235      * @param lastExecutionTime the amount of time taken to execute the last policy
236      */
237     public void setLastExecutionTime(final long lastExecutionTime) {
238         this.lastExecutionTime = lastExecutionTime;
239     }
240
241     /**
242      * Gets the average amount of time taken to execute a policy.
243      *
244      * @return the average amount of time taken to execute a policy
245      */
246     public double getAverageExecutionTime() {
247         return averageExecutionTime;
248     }
249
250     /**
251      * Sets the average amount of time taken to execute a policy.
252      *
253      * @param averageExecutionTime the average amount of time taken to execute a policy
254      */
255     public void setAverageExecutionTime(final double averageExecutionTime) {
256         this.averageExecutionTime = averageExecutionTime;
257     }
258
259     /**
260      * Gets the time that has elapsed since the policy engine was started.
261      *
262      * @return the time that has elapsed since the policy engine was started
263      */
264     public long getUpTime() {
265         if (this.getLastStart() != 0) {
266             return upTime + (timeStamp - this.getLastStart());
267         }
268         return upTime;
269     }
270
271     /**
272      * Sets the time that has elapsed since the policy engine was started.
273      *
274      * @param upTime the time that has elapsed since the policy engine was started
275      */
276     public void setUpTime(final long upTime) {
277         this.upTime = upTime;
278     }
279
280     /**
281      * Sets the time at which the policy engine was last started.
282      *
283      * @param lastStart the time at which the policy engine was last started
284      */
285     private void setLastStart(final long lastStart) {
286         this.lastStart = lastStart;
287     }
288
289     /**
290      * Gets the time at which the policy engine was last started.
291      *
292      * @return the time at which the policy engine was last started
293      */
294     public long getLastStart() {
295         return lastStart;
296     }
297
298     /**
299      * Resets all the statistic values to zero.
300      */
301     public synchronized void reset() {
302         timeStamp = 0;
303         eventCount = 0;
304         lastExecutionTime = 0;
305         averageExecutionTime = 0;
306         upTime = 0;
307         lastEnterTime = 0;
308         lastStart = 0;
309     }
310
311     /**
312      * Updates the statistics when called, used by the Apex engine when it starts executing a policy.
313      *
314      * @param eventkey the key of the event that is being executed
315      */
316     public synchronized void executionEnter(final AxArtifactKey eventkey) {
317         final long now = System.currentTimeMillis();
318         eventCount++;
319         if (eventCount < 0) {
320             eventCount = 2;
321         }
322         lastEnterTime = now;
323         timeStamp = now;
324     }
325
326     /**
327      * Updates the statistics when called, used by the Apex engine when it completes executing a policy.
328      */
329     public synchronized void executionExit() {
330         final long now = System.currentTimeMillis();
331         lastExecutionTime = now - lastEnterTime;
332
333         averageExecutionTime = ((averageExecutionTime * (eventCount - 1.0)) + lastExecutionTime) / eventCount;
334         lastEnterTime = 0;
335         timeStamp = System.currentTimeMillis();
336     }
337
338     /**
339      * Updates the statistics when called, used by the Apex engine when it is started.
340      */
341     public synchronized void engineStart() {
342         final long now = System.currentTimeMillis();
343         timeStamp = now;
344         this.setLastStart(now);
345     }
346
347     /**
348      * Updates the statistics when called, used by the Apex engine when it is stopped.
349      */
350     public synchronized void engineStop() {
351         final long now = System.currentTimeMillis();
352         timeStamp = now;
353         upTime += (timeStamp - this.getLastStart());
354         this.setLastStart(0);
355     }
356
357     /**
358      * {@inheritDoc}.
359      */
360     @Override
361     public AxValidationResult validate(final AxValidationResult result) {
362         if (key.equals(AxReferenceKey.getNullKey())) {
363             result.addValidationMessage(
364                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
365         }
366
367         return key.validate(result);
368     }
369
370     /**
371      * {@inheritDoc}.
372      */
373     @Override
374     public void clean() {
375         key.clean();
376     }
377
378     /**
379      * {@inheritDoc}.
380      */
381     @Override
382     public String toString() {
383         final StringBuilder builder = new StringBuilder();
384         builder.append(this.getClass().getSimpleName());
385         builder.append(":(");
386         builder.append("engineKey=");
387         builder.append(key);
388         builder.append(",timeStamp=");
389         builder.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timeStamp));
390         builder.append(",eventCount=");
391         builder.append(eventCount);
392         builder.append(",lastExecutionTime=");
393         builder.append(lastExecutionTime);
394         builder.append(",averageExecutionTime=");
395         builder.append(averageExecutionTime);
396         builder.append(",upTime=");
397         builder.append(getUpTime());
398         builder.append(")");
399         return builder.toString();
400     }
401
402     /**
403      * {@inheritDoc}.
404      */
405     @Override
406     public AxConcept copyTo(final AxConcept targetObject) {
407         Assertions.argumentNotNull(targetObject, "target may not be null");
408
409         final Object copyObject = targetObject;
410         Assertions.instanceOf(copyObject, AxEngineStats.class);
411
412         final AxEngineStats copy = ((AxEngineStats) copyObject);
413         copy.setKey(new AxReferenceKey(key));
414         copy.setTimeStamp(timeStamp);
415         copy.setEventCount(eventCount);
416         copy.setLastExecutionTime(lastExecutionTime);
417         copy.setAverageExecutionTime(averageExecutionTime);
418         copy.setUpTime(upTime);
419         copy.setLastStart(lastStart);
420
421         return copy;
422     }
423
424     /**
425      * {@inheritDoc}.
426      */
427     @Override
428     public int hashCode() {
429         final int prime = 31;
430         int result = 1;
431         result = prime * result + key.hashCode();
432         result = prime * result + (int) (timeStamp ^ (timeStamp >>> HASH_CODE_PRIME));
433         result = prime * result + (int) (eventCount ^ (eventCount >>> HASH_CODE_PRIME));
434         result = prime * result + (int) (lastExecutionTime ^ (lastExecutionTime >>> HASH_CODE_PRIME));
435         result = prime * result + ((int) averageExecutionTime ^ ((int) averageExecutionTime >>> HASH_CODE_PRIME));
436         result = prime * result + (int) (upTime ^ (upTime >>> HASH_CODE_PRIME));
437         result = prime * result + (int) (getLastStart() ^ (getLastStart() >>> HASH_CODE_PRIME));
438         return result;
439     }
440
441     /**
442      * {@inheritDoc}.
443      */
444     @Override
445     public boolean equals(final Object obj) {
446         if (obj == null) {
447             return false;
448         }
449         if (this == obj) {
450             return true;
451         }
452
453         if (getClass() != obj.getClass()) {
454             return false;
455         }
456
457         final AxEngineStats other = (AxEngineStats) obj;
458         if (!key.equals(other.key)) {
459             return false;
460         }
461         if (timeStamp != other.timeStamp) {
462             return false;
463         }
464         if (eventCount != other.eventCount) {
465             return false;
466         }
467         if (lastExecutionTime != other.lastExecutionTime) {
468             return false;
469         }
470         if (Double.compare(averageExecutionTime, other.averageExecutionTime) != 0) {
471             return false;
472         }
473         if (upTime != other.upTime) {
474             return false;
475         }
476         return getLastStart() == other.getLastStart();
477     }
478
479     /**
480      * {@inheritDoc}.
481      */
482     @Override
483     public int compareTo(final AxConcept otherObj) {
484         if (otherObj == null) {
485             return -1;
486         }
487         if (this == otherObj) {
488             return 0;
489         }
490         if (getClass() != otherObj.getClass()) {
491             return this.hashCode() - otherObj.hashCode();
492         }
493
494         final AxEngineStats other = (AxEngineStats) otherObj;
495         if (!key.equals(other.key)) {
496             return key.compareTo(other.key);
497         }
498         if (timeStamp != other.timeStamp) {
499             return (int) (timeStamp - other.timeStamp);
500         }
501         if (eventCount != other.eventCount) {
502             return (int) (eventCount - other.eventCount);
503         }
504         if (lastExecutionTime != other.lastExecutionTime) {
505             return (int) (lastExecutionTime - other.lastExecutionTime);
506         }
507         final int result = Double.compare(averageExecutionTime, other.averageExecutionTime);
508         if (result != 0) {
509             return result;
510         }
511         if (upTime != other.upTime) {
512             return (int) (upTime - other.upTime);
513         }
514
515         return Long.compare(lastStart, other.lastStart);
516     }
517 }