c5ce996776c0c1f76ffc41e6fff37b702ba5594a
[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 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
22
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;
34 import lombok.Data;
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;
48
49 /**
50  * Class to represent a controlloop element statistics in the database.
51  *
52  * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
53  */
54 @Entity
55 @Table(name = "ClElementStatistics")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @Data
58 @AllArgsConstructor
59 @EqualsAndHashCode(callSuper = false)
60 public class JpaClElementStatistics extends PfConcept implements PfAuthorative<ClElementStatistics>, Serializable {
61
62     private static final long serialVersionUID = 621426717868738629L;
63
64     @EmbeddedId
65     @VerifyKey
66     @NotNull
67     private PfReferenceTimestampKey key = new PfReferenceTimestampKey();
68
69
70     @VerifyKey
71     @NotNull
72     // @formatter:off
73     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
74     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
75     private PfConceptKey participantId;
76     // @formatter: on
77
78     @Column
79     @NotNull
80     private ControlLoopState state;
81
82     @Column
83     private long clElementUptime;
84
85
86     /**
87      * The Default Constructor creates a {@link JpaClElementStatistics} object with a null key.
88      */
89     public JpaClElementStatistics() {
90         this(new PfReferenceTimestampKey());
91     }
92
93
94     /**
95      * The Key Constructor creates a {@link JpaClElementStatistics} object with the given Reference Timestamp key.
96      *
97      * @param key the key
98      */
99     public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key) {
100         this(key, new PfConceptKey(), ControlLoopState.PASSIVE, 0L);
101     }
102
103     /**
104      * The Key Constructor creates a {@link JpaClElementStatistics} object with all mandatory fields.
105      *
106      * @param key the key
107      * @param participantId the TOSCA definition of the control loop element
108      */
109     public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key,
110                                   @NonNull final PfConceptKey participantId) {
111         this.key = key;
112         this.participantId = participantId;
113     }
114
115     /**
116      * Copy constructor.
117      *
118      * @param copyConcept the concept to copy from
119      */
120     public JpaClElementStatistics(@NonNull final JpaClElementStatistics copyConcept) {
121         super(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;
126     }
127
128
129     /**
130      * Authorative constructor.
131      *
132      * @param authorativeConcept the authorative concept to copy from
133      */
134     public JpaClElementStatistics(@NonNull final ClElementStatistics authorativeConcept) {
135         this.fromAuthorative(authorativeConcept);
136     }
137
138
139
140     @Override
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);
148
149         return clElementStatistics;
150     }
151
152     @Override
153     public void fromAuthorative(@NonNull ClElementStatistics clElementStatistics) {
154         // @formatter:off
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()));
159         }
160         // @formatter:on
161         this.setParticipantId(clElementStatistics.getParticipantId().asConceptKey());
162         this.setState(clElementStatistics.getControlLoopState());
163         this.setClElementUptime(clElementStatistics.getClElementUptime());
164     }
165
166     @Override
167     public List<PfKey> getKeys() {
168         return getKey().getKeys();
169     }
170
171     @Override
172     public void clean() {
173         key.clean();
174         participantId.clean();
175     }
176
177
178     @Override
179     public int compareTo(PfConcept otherConcept) {
180         if (otherConcept == null) {
181             return -1;
182         }
183         if (this == otherConcept) {
184             return 0;
185         }
186         if (getClass() != otherConcept.getClass()) {
187             return getClass().getName().compareTo(otherConcept.getClass().getName());
188         }
189
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();
193     }
194 }