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.Date;
26 import java.util.List;
27 import javax.persistence.AttributeOverride;
28 import javax.persistence.AttributeOverrides;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import lombok.AllArgsConstructor;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.apache.commons.lang3.builder.CompareToBuilder;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
43 import org.onap.policy.common.parameters.annotations.NotNull;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfTimestampKey;
49 import org.onap.policy.models.base.validation.annotations.VerifyKey;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 * Class to represent a control loop statistics in the database.
55 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
58 @Table(name = "ControlLoopStatistics")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaParticipantStatistics extends PfConcept
64 implements PfAuthorative<ParticipantStatistics>, Serializable {
66 private static final long serialVersionUID = -5992214428190133190L;
71 private PfTimestampKey key;
76 @AttributeOverride(name = "name", column = @Column(name = "participant_name")),
77 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
80 private PfConceptKey participantId;
84 private ParticipantState state;
88 private ParticipantHealthStatus healthStatus;
91 private long eventCount;
94 private long lastExecutionTime;
97 private double averageExecutionTime;
103 private long lastEnterTime;
106 private long lastStart;
110 * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
112 public JpaParticipantStatistics() {
113 this(new PfTimestampKey());
117 * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
121 public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
122 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L,
128 * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
131 * @param participantId the TOSCA definition of the control loop participant
133 public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
135 this.participantId = participantId;
142 * @param copyConcept the concept to copy from
144 public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics copyConcept) {
146 this.key = new PfTimestampKey(copyConcept.key);
147 this.participantId = new PfConceptKey(copyConcept.participantId);
148 this.state = copyConcept.state;
149 this.healthStatus = copyConcept.healthStatus;
150 this.eventCount = copyConcept.eventCount;
151 this.lastExecutionTime = copyConcept.lastExecutionTime;
152 this.averageExecutionTime = copyConcept.averageExecutionTime;
153 this.upTime = copyConcept.upTime;
154 this.lastEnterTime = copyConcept.lastEnterTime;
155 this.lastStart = copyConcept.lastStart;
159 * Authorative constructor.
161 * @param authorativeConcept the authorative concept to copy from
163 public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
164 this.fromAuthorative(authorativeConcept);
169 public int compareTo(PfConcept otherConcept) {
170 if (otherConcept == null) {
173 if (this == otherConcept) {
176 if (getClass() != otherConcept.getClass()) {
177 return getClass().getName().compareTo(otherConcept.getClass().getName());
180 final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
182 return new CompareToBuilder()
183 .append(this.key, other.key)
184 .append(this.participantId, other.participantId)
185 .append(this.state, other.state)
186 .append(this.healthStatus, other.healthStatus)
187 .append(this.eventCount, other.eventCount)
188 .append(this.lastExecutionTime, other.lastExecutionTime)
189 .append(this.averageExecutionTime, other.averageExecutionTime)
190 .append(this.upTime, other.upTime)
191 .append(this.lastEnterTime, other.lastEnterTime)
192 .append(this.lastStart, other.lastStart).toComparison();
197 public ParticipantStatistics toAuthorative() {
198 ParticipantStatistics participantStatistics = new ParticipantStatistics();
199 participantStatistics.setTimeStamp(new Date(key.getTimeStamp().getTime()));
200 participantStatistics.setParticipantId(new ToscaConceptIdentifier(participantId));
201 participantStatistics.setState(state);
202 participantStatistics.setHealthStatus(healthStatus);
203 participantStatistics.setAverageExecutionTime(averageExecutionTime);
204 participantStatistics.setEventCount(eventCount);
205 participantStatistics.setLastExecutionTime(lastExecutionTime);
206 participantStatistics.setUpTime(upTime);
207 participantStatistics.setLastEnterTime(lastEnterTime);
208 participantStatistics.setLastStart(lastStart);
210 return participantStatistics;
214 public void fromAuthorative(@NonNull final ParticipantStatistics participantStatistics) {
215 if (this.key == null || this.getKey().isNullKey()) {
216 this.setKey(new PfTimestampKey(participantStatistics.getParticipantId().getName(),
217 participantStatistics.getParticipantId().getVersion(),
218 participantStatistics.getTimeStamp()));
220 this.setParticipantId(participantStatistics.getParticipantId().asConceptKey());
221 this.setState(participantStatistics.getState());
222 this.setHealthStatus(participantStatistics.getHealthStatus());
223 this.setAverageExecutionTime(participantStatistics.getAverageExecutionTime());
224 this.setEventCount(participantStatistics.getEventCount());
225 this.setLastExecutionTime(participantStatistics.getLastExecutionTime());
226 this.setUpTime(participantStatistics.getUpTime());
227 this.setLastEnterTime(participantStatistics.getLastEnterTime());
228 this.setLastStart(participantStatistics.getLastStart());
233 public List<PfKey> getKeys() {
234 List<PfKey> keyList = getKey().getKeys();
235 keyList.addAll(participantId.getKeys());
240 public void clean() {
242 participantId.clean();