2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
24 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
26 import java.io.Serializable;
27 import java.util.List;
28 import javax.persistence.AttributeOverride;
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 = "ParticipantStatistics")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaParticipantStatistics extends PfConcept implements PfAuthorative<ParticipantStatistics>, Serializable {
65 private static final long serialVersionUID = -5992214428190133190L;
70 private PfTimestampKey key;
74 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
75 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
76 private PfConceptKey participantId;
80 private ParticipantState state;
84 private ParticipantHealthStatus healthStatus;
87 private long eventCount;
90 private long lastExecutionTime;
93 private double averageExecutionTime;
99 private long lastEnterTime;
102 private long lastStart;
106 * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
108 public JpaParticipantStatistics() {
109 this(new PfTimestampKey());
113 * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
117 public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
118 this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L, 0.0d, 0L, 0L,
124 * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
127 * @param participantId the TOSCA definition of the control loop participant
129 public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
131 this.participantId = participantId;
138 * @param copyConcept the concept to copy from
140 public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics copyConcept) {
142 this.key = new PfTimestampKey(copyConcept.key);
143 this.participantId = new PfConceptKey(copyConcept.participantId);
144 this.state = copyConcept.state;
145 this.healthStatus = copyConcept.healthStatus;
146 this.eventCount = copyConcept.eventCount;
147 this.lastExecutionTime = copyConcept.lastExecutionTime;
148 this.averageExecutionTime = copyConcept.averageExecutionTime;
149 this.upTime = copyConcept.upTime;
150 this.lastEnterTime = copyConcept.lastEnterTime;
151 this.lastStart = copyConcept.lastStart;
155 * Authorative constructor.
157 * @param authorativeConcept the authorative concept to copy from
159 public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
160 this.fromAuthorative(authorativeConcept);
165 public int compareTo(PfConcept otherConcept) {
166 if (otherConcept == null) {
169 if (this == otherConcept) {
172 if (getClass() != otherConcept.getClass()) {
173 return getClass().getName().compareTo(otherConcept.getClass().getName());
176 final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
178 return new CompareToBuilder()
179 .append(this.key, other.key)
180 .append(this.participantId, other.participantId)
181 .append(this.state, other.state)
182 .append(this.healthStatus, other.healthStatus)
183 .append(this.eventCount, other.eventCount)
184 .append(this.lastExecutionTime, other.lastExecutionTime)
185 .append(this.averageExecutionTime, other.averageExecutionTime)
186 .append(this.upTime, other.upTime)
187 .append(this.lastEnterTime, other.lastEnterTime)
188 .append(this.lastStart, other.lastStart).toComparison();
193 public ParticipantStatistics toAuthorative() {
194 var participantStatistics = new ParticipantStatistics();
195 participantStatistics.setTimeStamp(key.getTimeStamp().toInstant());
196 participantStatistics.setParticipantId(new ToscaConceptIdentifier(participantId));
197 participantStatistics.setState(state);
198 participantStatistics.setHealthStatus(healthStatus);
199 participantStatistics.setAverageExecutionTime(averageExecutionTime);
200 participantStatistics.setEventCount(eventCount);
201 participantStatistics.setLastExecutionTime(lastExecutionTime);
202 participantStatistics.setUpTime(upTime);
203 participantStatistics.setLastEnterTime(lastEnterTime);
204 participantStatistics.setLastStart(lastStart);
206 return participantStatistics;
210 public void fromAuthorative(@NonNull final ParticipantStatistics participantStatistics) {
211 if (this.key == null || this.getKey().isNullKey()) {
212 this.setKey(new PfTimestampKey(participantStatistics.getParticipantId().getName(),
213 participantStatistics.getParticipantId().getVersion(), participantStatistics.getTimeStamp()));
215 this.setParticipantId(participantStatistics.getParticipantId().asConceptKey());
216 this.setState(participantStatistics.getState());
217 this.setHealthStatus(participantStatistics.getHealthStatus());
218 this.setAverageExecutionTime(participantStatistics.getAverageExecutionTime());
219 this.setEventCount(participantStatistics.getEventCount());
220 this.setLastExecutionTime(participantStatistics.getLastExecutionTime());
221 this.setUpTime(participantStatistics.getUpTime());
222 this.setLastEnterTime(participantStatistics.getLastEnterTime());
223 this.setLastStart(participantStatistics.getLastStart());
228 public List<PfKey> getKeys() {
229 List<PfKey> keyList = getKey().getKeys();
230 keyList.addAll(participantId.getKeys());
235 public void clean() {
237 participantId.clean();