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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
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;
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;
51 * Class to represent a control loop statistics in the database.
53 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
56 @Table(name = "ControlLoopStatistics")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaParticipantStatistics extends PfConcept implements PfAuthorative<ParticipantStatistics>, Serializable {
63 private static final long serialVersionUID = -5992214428190133190L;
68 private PfTimestampKey key;
72 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
73 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
74 private PfConceptKey participantId;
78 private ParticipantState state;
82 private ParticipantHealthStatus healthStatus;
85 private long eventCount;
88 private long lastExecutionTime;
91 private double averageExecutionTime;
97 private long lastEnterTime;
100 private long lastStart;
104 * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
106 public JpaParticipantStatistics() {
107 this(new PfTimestampKey());
111 * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
115 public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
116 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L, 0.0d, 0L, 0L,
122 * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
125 * @param participantId the TOSCA definition of the control loop participant
127 public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
129 this.participantId = participantId;
136 * @param copyConcept the concept to copy from
138 public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics 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;
153 * Authorative constructor.
155 * @param authorativeConcept the authorative concept to copy from
157 public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
158 this.fromAuthorative(authorativeConcept);
163 public int compareTo(PfConcept otherConcept) {
164 if (otherConcept == null) {
167 if (this == otherConcept) {
170 if (getClass() != otherConcept.getClass()) {
171 return getClass().getName().compareTo(otherConcept.getClass().getName());
174 final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
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();
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);
204 return participantStatistics;
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()));
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());
226 public List<PfKey> getKeys() {
227 List<PfKey> keyList = getKey().getKeys();
228 keyList.addAll(participantId.getKeys());
233 public void clean() {
235 participantId.clean();