2e5188abed1625f9c4ad57807a2b8366cb552c96
[policy/clamp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23
24 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
25
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;
36 import lombok.Data;
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;
51
52 /**
53  * Class to represent a control loop statistics in the database.
54  *
55  * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
56  */
57 @Entity
58 @Table(name = "ParticipantStatistics")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @AllArgsConstructor
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaParticipantStatistics extends PfConcept implements PfAuthorative<ParticipantStatistics>, Serializable {
64
65     private static final long serialVersionUID = -5992214428190133190L;
66
67     @EmbeddedId
68     @VerifyKey
69     @NotNull
70     private PfTimestampKey key;
71
72     @VerifyKey
73     @NotNull
74     @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
75     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
76     private PfConceptKey participantId;
77
78     @Column
79     @NotNull
80     private ParticipantState state;
81
82     @Column
83     @NotNull
84     private ParticipantHealthStatus healthStatus;
85
86     @Column
87     private long eventCount;
88
89     @Column
90     private long lastExecutionTime;
91
92     @Column
93     private double averageExecutionTime;
94
95     @Column
96     private long upTime;
97
98     @Column
99     private long lastEnterTime;
100
101     @Column
102     private long lastStart;
103
104
105     /**
106      * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
107      */
108     public JpaParticipantStatistics() {
109         this(new PfTimestampKey());
110     }
111
112     /**
113      * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
114      *
115      * @param key the key
116      */
117     public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
118         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L, 0.0d, 0L, 0L,
119                 0L);
120     }
121
122
123     /**
124      * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
125      *
126      * @param key the key
127      * @param participantId the TOSCA definition of the control loop participant
128      */
129     public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
130         this.key = key;
131         this.participantId = participantId;
132     }
133
134
135     /**
136      * Copy constructor.
137      *
138      * @param copyConcept the concept to copy from
139      */
140     public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics copyConcept) {
141         super(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;
152     }
153
154     /**
155      * Authorative constructor.
156      *
157      * @param authorativeConcept the authorative concept to copy from
158      */
159     public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
160         this.fromAuthorative(authorativeConcept);
161     }
162
163
164     @Override
165     public int compareTo(PfConcept otherConcept) {
166         if (otherConcept == null) {
167             return -1;
168         }
169         if (this == otherConcept) {
170             return 0;
171         }
172         if (getClass() != otherConcept.getClass()) {
173             return getClass().getName().compareTo(otherConcept.getClass().getName());
174         }
175
176         final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
177         // @formatter:off
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();
189         // @formatter:on
190     }
191
192     @Override
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);
205
206         return participantStatistics;
207     }
208
209     @Override
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()));
214         }
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());
224
225     }
226
227     @Override
228     public List<PfKey> getKeys() {
229         List<PfKey> keyList = getKey().getKeys();
230         keyList.addAll(participantId.getKeys());
231         return keyList;
232     }
233
234     @Override
235     public void clean() {
236         key.clean();
237         participantId.clean();
238     }
239 }