1877d98a1131da479c0d4cda297058ace28d0ac6
[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.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;
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 = "ControlLoopStatistics")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @AllArgsConstructor
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaParticipantStatistics extends PfConcept
64     implements PfAuthorative<ParticipantStatistics>, Serializable {
65
66     private static final long serialVersionUID = -5992214428190133190L;
67
68     @EmbeddedId
69     @VerifyKey
70     @NotNull
71     private PfTimestampKey key;
72
73     @VerifyKey
74     @NotNull
75     @AttributeOverrides({
76         @AttributeOverride(name = "name",    column = @Column(name = "participant_name")),
77         @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
78         }
79     )
80     private PfConceptKey participantId;
81
82     @Column
83     @NotNull
84     private ParticipantState state;
85
86     @Column
87     @NotNull
88     private ParticipantHealthStatus healthStatus;
89
90     @Column
91     private long eventCount;
92
93     @Column
94     private long lastExecutionTime;
95
96     @Column
97     private double averageExecutionTime;
98
99     @Column
100     private long upTime;
101
102     @Column
103     private long lastEnterTime;
104
105     @Column
106     private long lastStart;
107
108
109     /**
110      * The Default Constructor creates a {@link JpaParticipantStatistics} object with a null key.
111      */
112     public JpaParticipantStatistics() {
113         this(new PfTimestampKey());
114     }
115
116     /**
117      * The Key Constructor creates a {@link JpaParticipantStatistics} object with the given Timestamp key.
118      *
119      * @param key the key
120      */
121     public JpaParticipantStatistics(@NonNull final PfTimestampKey key) {
122         this(key, new PfConceptKey(), ParticipantState.PASSIVE, ParticipantHealthStatus.HEALTHY, 0L, 0L,
123             0.0d, 0L, 0L, 0L);
124     }
125
126
127     /**
128      * The Key Constructor creates a {@link JpaParticipantStatistics} object with all mandatory fields.
129      *
130      * @param key the key
131      * @param participantId the TOSCA definition of the control loop participant
132      */
133     public JpaParticipantStatistics(@NonNull final PfTimestampKey key, @NonNull final PfConceptKey participantId) {
134         this.key = key;
135         this.participantId = participantId;
136     }
137
138
139     /**
140      * Copy constructor.
141      *
142      * @param copyConcept the concept to copy from
143      */
144     public JpaParticipantStatistics(@NonNull final JpaParticipantStatistics copyConcept) {
145         super(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;
156     }
157
158     /**
159      * Authorative constructor.
160      *
161      * @param authorativeConcept the authorative concept to copy from
162      */
163     public JpaParticipantStatistics(@NonNull final ParticipantStatistics authorativeConcept) {
164         this.fromAuthorative(authorativeConcept);
165     }
166
167
168     @Override
169     public int compareTo(PfConcept otherConcept) {
170         if (otherConcept == null) {
171             return -1;
172         }
173         if (this == otherConcept) {
174             return 0;
175         }
176         if (getClass() != otherConcept.getClass()) {
177             return getClass().getName().compareTo(otherConcept.getClass().getName());
178         }
179
180         final JpaParticipantStatistics other = (JpaParticipantStatistics) otherConcept;
181         // @formatter:off
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();
193         // @formatter:on
194     }
195
196     @Override
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);
209
210         return participantStatistics;
211     }
212
213     @Override
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()));
219         }
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());
229
230     }
231
232     @Override
233     public List<PfKey> getKeys() {
234         List<PfKey> keyList = getKey().getKeys();
235         keyList.addAll(participantId.getKeys());
236         return keyList;
237     }
238
239     @Override
240     public void clean() {
241         key.clean();
242         participantId.clean();
243     }
244 }