ec5dbb32cfe3060c6ec921a26da93902f81c1eb1
[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.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;
35 import lombok.Data;
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;
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 @AllArgsConstructor
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 PfReferenceTimestampKey key = new PfReferenceTimestampKey();
69
70
71     @VerifyKey
72     @NotNull
73     // @formatter:off
74     @AttributeOverride(name = "name",    column = @Column(name = "participant_name"))
75     @AttributeOverride(name = "version", column = @Column(name = "participant_version"))
76     private PfConceptKey participantId;
77     // @formatter: on
78
79     @Column
80     @NotNull
81     private ControlLoopState state;
82
83     @Column
84     private long clElementUptime;
85
86
87     /**
88      * The Default Constructor creates a {@link JpaClElementStatistics} object with a null key.
89      */
90     public JpaClElementStatistics() {
91         this(new PfReferenceTimestampKey());
92     }
93
94
95     /**
96      * The Key Constructor creates a {@link JpaClElementStatistics} object with the given Reference Timestamp key.
97      *
98      * @param key the key
99      */
100     public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key) {
101         this(key, new PfConceptKey(), ControlLoopState.PASSIVE, 0L);
102     }
103
104     /**
105      * The Key Constructor creates a {@link JpaClElementStatistics} object with all mandatory fields.
106      *
107      * @param key the key
108      * @param participantId the TOSCA definition of the control loop element
109      */
110     public JpaClElementStatistics(@NonNull final PfReferenceTimestampKey key,
111                                   @NonNull final PfConceptKey participantId) {
112         this.key = key;
113         this.participantId = participantId;
114     }
115
116     /**
117      * Copy constructor.
118      *
119      * @param copyConcept the concept to copy from
120      */
121     public JpaClElementStatistics(@NonNull final JpaClElementStatistics copyConcept) {
122         super(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;
127     }
128
129
130     /**
131      * Authorative constructor.
132      *
133      * @param authorativeConcept the authorative concept to copy from
134      */
135     public JpaClElementStatistics(@NonNull final ClElementStatistics authorativeConcept) {
136         this.fromAuthorative(authorativeConcept);
137     }
138
139
140
141     @Override
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);
149
150         return clElementStatistics;
151     }
152
153     @Override
154     public void fromAuthorative(@NonNull ClElementStatistics clElementStatistics) {
155         // @formatter:off
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()));
160         }
161         // @formatter:on
162         this.setParticipantId(clElementStatistics.getParticipantId().asConceptKey());
163         this.setState(clElementStatistics.getControlLoopState());
164         this.setClElementUptime(clElementStatistics.getClElementUptime());
165     }
166
167     @Override
168     public List<PfKey> getKeys() {
169         return getKey().getKeys();
170     }
171
172     @Override
173     public void clean() {
174         key.clean();
175         participantId.clean();
176     }
177
178
179     @Override
180     public int compareTo(PfConcept otherConcept) {
181         if (otherConcept == null) {
182             return -1;
183         }
184         if (this == otherConcept) {
185             return 0;
186         }
187         if (getClass() != otherConcept.getClass()) {
188             return getClass().getName().compareTo(otherConcept.getClass().getName());
189         }
190
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();
194     }
195 }