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=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
23 import java.io.Serializable;
24 import java.util.List;
25 import java.util.UUID;
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.ClElementStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceTimestampKey;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 * Class to represent a controlloop element statistics in the database.
52 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
55 @Table(name = "ClElementStatistics")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaClElementStatistics extends PfConcept implements PfAuthorative<ClElementStatistics>, Serializable {
62 private static final long serialVersionUID = 621426717868738629L;
67 private PfReferenceTimestampKey key = new PfReferenceTimestampKey();
73 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
74 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
75 private PfConceptKey participantId;
80 private ControlLoopState state;
83 private long clElementUptime;
87 * The Default Constructor creates a {@link JpaClElementStatistics} object with a null key.
89 public JpaClElementStatistics() {
90 this(new PfReferenceTimestampKey());
95 * The Key Constructor creates a {@link JpaClElementStatistics} object with the given Reference Timestamp key.
99 public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key) {
100 this(key, new PfConceptKey(), ControlLoopState.PASSIVE, 0L);
104 * The Key Constructor creates a {@link JpaClElementStatistics} object with all mandatory fields.
107 * @param participantId the TOSCA definition of the control loop element
109 public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key,
110 @NonNull final PfConceptKey participantId) {
112 this.participantId = participantId;
118 * @param copyConcept the concept to copy from
120 public JpaClElementStatistics(@NonNull final JpaClElementStatistics copyConcept) {
122 this.key = new PfReferenceTimestampKey(copyConcept.key);
123 this.participantId = new PfConceptKey(copyConcept.participantId);
124 this.state = copyConcept.state;
125 this.clElementUptime = copyConcept.clElementUptime;
130 * Authorative constructor.
132 * @param authorativeConcept the authorative concept to copy from
134 public JpaClElementStatistics(@NonNull final ClElementStatistics authorativeConcept) {
135 this.fromAuthorative(authorativeConcept);
141 public ClElementStatistics toAuthorative() {
142 ClElementStatistics clElementStatistics = new ClElementStatistics();
143 clElementStatistics.setId(UUID.fromString(getKey().getReferenceKey().getLocalName()));
144 clElementStatistics.setTimeStamp(key.getInstant());
145 clElementStatistics.setParticipantId(new ToscaConceptIdentifier(participantId));
146 clElementStatistics.setControlLoopState(state);
147 clElementStatistics.setClElementUptime(clElementUptime);
149 return clElementStatistics;
153 public void fromAuthorative(@NonNull ClElementStatistics clElementStatistics) {
155 if (this.key == null || this.getKey().isNullKey()) {
156 this.setKey(new PfReferenceTimestampKey(clElementStatistics.getParticipantId().getName(),
157 clElementStatistics.getParticipantId().getVersion(), clElementStatistics.getId().toString(),
158 clElementStatistics.getTimeStamp()));
161 this.setParticipantId(clElementStatistics.getParticipantId().asConceptKey());
162 this.setState(clElementStatistics.getControlLoopState());
163 this.setClElementUptime(clElementStatistics.getClElementUptime());
167 public List<PfKey> getKeys() {
168 return getKey().getKeys();
172 public void clean() {
174 participantId.clean();
179 public int compareTo(PfConcept otherConcept) {
180 if (otherConcept == null) {
183 if (this == otherConcept) {
186 if (getClass() != otherConcept.getClass()) {
187 return getClass().getName().compareTo(otherConcept.getClass().getName());
190 final JpaClElementStatistics other = (JpaClElementStatistics) otherConcept;
191 return new CompareToBuilder().append(this.key, other.key).append(this.state, other.state)
192 .append(this.clElementUptime, other.clElementUptime).toComparison();