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