84c7f0d593ef27ac3287c6dc1aa96bc682a7c80e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
23
24 import java.io.Serializable;
25 import java.util.List;
26 import javax.persistence.AttributeOverride;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import lombok.AllArgsConstructor;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.apache.commons.lang3.builder.CompareToBuilder;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
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.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfTimestampKey;
47 import org.onap.policy.models.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49
50 /**
51  * Class to represent a control loop statistics in the database.
52  *
53  * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
54  */
55 @Entity
56 @Table(name = "ControlLoopStatistics")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
58 @Data
59 @AllArgsConstructor
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaParticipantStatistics extends PfConcept implements PfAuthorative<ParticipantStatistics>, Serializable {
62
63     private static final long serialVersionUID = -5992214428190133190L;
64
65     @EmbeddedId
66     @VerifyKey
67     @NotNull
68     private PfTimestampKey key;
69
70     @VerifyKey
71     @NotNull
72     @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
73     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
74     private PfConceptKey participantId;
75
76     @Column
77     @NotNull
78     private ParticipantState state;
79
80     @Column
81     @NotNull
82     private ParticipantHealthStatus healthStatus;
83
84     @Column
85     private long eventCount;
86
87     @Column
88     private long lastExecutionTime;
89
90     @Column
91     private double averageExecutionTime;
92
93     @Column
94     private long upTime;
95
96     @Column
97     private long lastEnterTime;
98
99     @Column
100     private long lastStart;
101
102
103     /**
104      * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
105      */
106     public JpaParticipantStatistics() {
107         this(new PfTimestampKey());
108     }
109
110     /**
111      * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
112      *
113      * @param key the key
114      */
115     public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
116         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L, 0.0d, 0L, 0L,
117                 0L);
118     }
119
120
121     /**
122      * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
123      *
124      * @param key the key
125      * @param participantId the TOSCA definition of the control loop participant
126      */
127     public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
128         this.key = key;
129         this.participantId = participantId;
130     }
131
132
133     /**
134      * Copy constructor.
135      *
136      * @param copyConcept the concept to copy from
137      */
138     public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics copyConcept) {
139         super(copyConcept);
140         this.key = new PfTimestampKey(copyConcept.key);
141         this.participantId = new PfConceptKey(copyConcept.participantId);
142         this.state = copyConcept.state;
143         this.healthStatus = copyConcept.healthStatus;
144         this.eventCount = copyConcept.eventCount;
145         this.lastExecutionTime = copyConcept.lastExecutionTime;
146         this.averageExecutionTime = copyConcept.averageExecutionTime;
147         this.upTime = copyConcept.upTime;
148         this.lastEnterTime = copyConcept.lastEnterTime;
149         this.lastStart = copyConcept.lastStart;
150     }
151
152     /**
153      * Authorative constructor.
154      *
155      * @param authorativeConcept the authorative concept to copy from
156      */
157     public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
158         this.fromAuthorative(authorativeConcept);
159     }
160
161
162     @Override
163     public int compareTo(PfConcept otherConcept) {
164         if (otherConcept == null) {
165             return -1;
166         }
167         if (this == otherConcept) {
168             return 0;
169         }
170         if (getClass() != otherConcept.getClass()) {
171             return getClass().getName().compareTo(otherConcept.getClass().getName());
172         }
173
174         final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
175         // @formatter:off
176         return new CompareToBuilder()
177                 .append(this.key, other.key)
178                 .append(this.participantId, other.participantId)
179                 .append(this.state, other.state)
180                 .append(this.healthStatus, other.healthStatus)
181                 .append(this.eventCount, other.eventCount)
182                 .append(this.lastExecutionTime, other.lastExecutionTime)
183                 .append(this.averageExecutionTime, other.averageExecutionTime)
184                 .append(this.upTime, other.upTime)
185                 .append(this.lastEnterTime, other.lastEnterTime)
186                 .append(this.lastStart, other.lastStart).toComparison();
187         // @formatter:on
188     }
189
190     @Override
191     public ParticipantStatistics toAuthorative() {
192         ParticipantStatistics participantStatistics = new ParticipantStatistics();
193         participantStatistics.setTimeStamp(key.getTimeStamp().toInstant());
194         participantStatistics.setParticipantId(new ToscaConceptIdentifier(participantId));
195         participantStatistics.setState(state);
196         participantStatistics.setHealthStatus(healthStatus);
197         participantStatistics.setAverageExecutionTime(averageExecutionTime);
198         participantStatistics.setEventCount(eventCount);
199         participantStatistics.setLastExecutionTime(lastExecutionTime);
200         participantStatistics.setUpTime(upTime);
201         participantStatistics.setLastEnterTime(lastEnterTime);
202         participantStatistics.setLastStart(lastStart);
203
204         return participantStatistics;
205     }
206
207     @Override
208     public void fromAuthorative(@NonNull final ParticipantStatistics participantStatistics) {
209         if (this.key == null || this.getKey().isNullKey()) {
210             this.setKey(new PfTimestampKey(participantStatistics.getParticipantId().getName(),
211                     participantStatistics.getParticipantId().getVersion(), participantStatistics.getTimeStamp()));
212         }
213         this.setParticipantId(participantStatistics.getParticipantId().asConceptKey());
214         this.setState(participantStatistics.getState());
215         this.setHealthStatus(participantStatistics.getHealthStatus());
216         this.setAverageExecutionTime(participantStatistics.getAverageExecutionTime());
217         this.setEventCount(participantStatistics.getEventCount());
218         this.setLastExecutionTime(participantStatistics.getLastExecutionTime());
219         this.setUpTime(participantStatistics.getUpTime());
220         this.setLastEnterTime(participantStatistics.getLastEnterTime());
221         this.setLastStart(participantStatistics.getLastStart());
222
223     }
224
225     @Override
226     public List<PfKey> getKeys() {
227         List<PfKey> keyList = getKey().getKeys();
228         keyList.addAll(participantId.getKeys());
229         return keyList;
230     }
231
232     @Override
233     public void clean() {
234         key.clean();
235         participantId.clean();
236     }
237 }