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.AttributeOverrides;
28 import javax.persistence.Column;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
34 import lombok.AllArgsConstructor;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38 import org.apache.commons.lang3.builder.CompareToBuilder;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.models.base.PfAuthorative;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceTimestampKey;
47 import org.onap.policy.models.base.validation.annotations.VerifyKey;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 * Class to represent a controlloop element statistics in the database.
53 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
56 @Table(name = "ClElementStatistics")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaClElementStatistics extends PfConcept implements PfAuthorative<ClElementStatistics>, Serializable {
63 private static final long serialVersionUID = 621426717868738629L;
68 private PfReferenceTimestampKey key = new PfReferenceTimestampKey();
74 @AttributeOverride(name = "name", column = @Column(name = "participant_name"))
75 @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
76 private PfConceptKey participantId;
81 private ControlLoopState state;
84 private long clElementUptime;
88 * The Default Constructor creates a {@link JpaClElementStatistics} object with a null key.
90 public JpaClElementStatistics() {
91 this(new PfReferenceTimestampKey());
96 * The Key Constructor creates a {@link JpaClElementStatistics} object with the given Reference Timestamp key.
100 public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key) {
101 this(key, new PfConceptKey(), ControlLoopState.PASSIVE, 0L);
105 * The Key Constructor creates a {@link JpaClElementStatistics} object with all mandatory fields.
108 * @param participantId the TOSCA definition of the control loop element
110 public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key,
111 @NonNull final PfConceptKey participantId) {
113 this.participantId = participantId;
119 * @param copyConcept the concept to copy from
121 public JpaClElementStatistics(@NonNull final JpaClElementStatistics copyConcept) {
123 this.key = new PfReferenceTimestampKey(copyConcept.key);
124 this.participantId = new PfConceptKey(copyConcept.participantId);
125 this.state = copyConcept.state;
126 this.clElementUptime = copyConcept.clElementUptime;
131 * Authorative constructor.
133 * @param authorativeConcept the authorative concept to copy from
135 public JpaClElementStatistics(@NonNull final ClElementStatistics authorativeConcept) {
136 this.fromAuthorative(authorativeConcept);
142 public ClElementStatistics toAuthorative() {
143 ClElementStatistics clElementStatistics = new ClElementStatistics();
144 clElementStatistics.setId(UUID.fromString(getKey().getReferenceKey().getLocalName()));
145 clElementStatistics.setTimeStamp(key.getInstant());
146 clElementStatistics.setParticipantId(new ToscaConceptIdentifier(participantId));
147 clElementStatistics.setControlLoopState(state);
148 clElementStatistics.setClElementUptime(clElementUptime);
150 return clElementStatistics;
154 public void fromAuthorative(@NonNull ClElementStatistics clElementStatistics) {
156 if (this.key == null || this.getKey().isNullKey()) {
157 this.setKey(new PfReferenceTimestampKey(clElementStatistics.getParticipantId().getName(),
158 clElementStatistics.getParticipantId().getVersion(), clElementStatistics.getId().toString(),
159 clElementStatistics.getTimeStamp()));
162 this.setParticipantId(clElementStatistics.getParticipantId().asConceptKey());
163 this.setState(clElementStatistics.getControlLoopState());
164 this.setClElementUptime(clElementStatistics.getClElementUptime());
168 public List<PfKey> getKeys() {
169 return getKey().getKeys();
173 public void clean() {
175 participantId.clean();
180 public int compareTo(PfConcept otherConcept) {
181 if (otherConcept == null) {
184 if (this == otherConcept) {
187 if (getClass() != otherConcept.getClass()) {
188 return getClass().getName().compareTo(otherConcept.getClass().getName());
191 final JpaClElementStatistics other = (JpaClElementStatistics) otherConcept;
192 return new CompareToBuilder().append(this.key, other.key).append(this.state, other.state)
193 .append(this.clElementUptime, other.clElementUptime).toComparison();