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