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