Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / engine-model / src / main / java / org / onap / policy / apex / model / enginemodel / concepts / AxEngineModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.enginemodel.concepts;
23
24 import java.text.SimpleDateFormat;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.Entity;
28 import javax.persistence.Enumerated;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.JoinColumns;
31 import javax.persistence.Table;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.bind.annotation.XmlType;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
45 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
46 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
48 import org.onap.policy.common.utils.validation.Assertions;
49
50 /**
51  * A container class for an Apex engine model. This class is a container class that allows an Apex
52  * model to be constructed that contains the current context {@link AxContextModel}, current state
53  * {@link AxEngineState} and current statistics {@link AxEngineStats} of an Apex engine. This model
54  * is used by an Apex engine to pass its current execution state to any system that wishes to query
55  * that information. The time stamp of the engine model is the time at which the state and
56  * statistics of the engine were read.
57  *
58  * <p>Validation checks that the current state {@link AxEngineState} is defined and that the time stamp
59  * is set on the engine model.
60  */
61 @Entity
62 @Table(name = "AxEngineModel")
63 @XmlRootElement(name = "apexEngineModel", namespace = "http://www.onap.org/policy/apex-pdp")
64 @XmlAccessorType(XmlAccessType.FIELD)
65 @XmlType(name = "AxEngineModel", namespace = "http://www.onap.org/policy/apex-pdp",
66         propOrder = {"timestamp", "state", "stats"})
67
68 public class AxEngineModel extends AxContextModel {
69     private static final long serialVersionUID = 6381235864606564046L;
70     private static final int HASH_CODE_PRIME = 32;
71
72     @Column(name = "timestamp")
73     private long timestamp;
74
75     @Enumerated
76     @Column(name = "state")
77     @XmlElement(required = true)
78     private AxEngineState state;
79
80     // @formatter:off
81     @JoinColumns({
82             @JoinColumn(name = "statsParentKeyName", referencedColumnName = "parentKeyName", updatable = false,
83                     insertable = false),
84             @JoinColumn(name = "statsParentKeyVersion", referencedColumnName = "parentKeyVersion", updatable = false,
85                     insertable = false),
86             @JoinColumn(name = "statsParentLocalName ", referencedColumnName = "parentLocalName", updatable = false,
87                     insertable = false),
88             @JoinColumn(name = "statsLocalName", referencedColumnName = "localName", updatable = false,
89                     insertable = false)})
90     private AxEngineStats stats;
91     // @formatter:on
92
93     /**
94      * The Default Constructor creates an engine model with a null key and all its fields undefined.
95      */
96     public AxEngineModel() {
97         this(new AxArtifactKey());
98         timestamp = -1;
99     }
100
101     /**
102      * Copy constructor.
103      *
104      * @param copyConcept the concept to copy from
105      */
106     public AxEngineModel(final AxEngineModel copyConcept) {
107         super(copyConcept);
108     }
109
110     /**
111      * The Keyed Constructor creates an engine model with the given key and all its fields
112      * undefined.
113      *
114      * @param key the engine model key
115      */
116     public AxEngineModel(final AxArtifactKey key) {
117         this(key, new AxContextSchemas(new AxArtifactKey(key.getName() + "_DataTypes", key.getVersion())),
118                 new AxKeyInformation(new AxArtifactKey(key.getName() + "_KeyInfo", key.getVersion())),
119                 new AxContextAlbums(new AxArtifactKey(key.getName() + "_Context", key.getVersion())));
120     }
121
122     /**
123      * This Constructor creates an engine model with its context model data types all defined, the
124      * state of the engine model is undefined.
125      *
126      * @param key the engine model key
127      * @param contextSchemas the context schemas used by the engine model
128      * @param keyInformation the key information used by the engine model
129      * @param contextAlbums the context albums used by the engine model
130      */
131     public AxEngineModel(final AxArtifactKey key, final AxContextSchemas contextSchemas,
132             final AxKeyInformation keyInformation, final AxContextAlbums contextAlbums) {
133         this(key, contextSchemas, keyInformation, contextAlbums, AxEngineState.UNDEFINED,
134                 new AxEngineStats(new AxReferenceKey(key, "_EngineStats", key.getVersion())));
135     }
136
137     /**
138      * This Constructor creates an engine model with all its fields defined.
139      *
140      * @param key the engine model key
141      * @param contextSchemas the context schemas used by the engine model
142      * @param keyInformation the key information used by the engine model
143      * @param contextAlbums the context albums used by the engine model
144      * @param state the state of the engine in the engine model
145      * @param stats the statistics of the engine in the engine model
146      */
147     public AxEngineModel(final AxArtifactKey key, final AxContextSchemas contextSchemas,
148             final AxKeyInformation keyInformation, final AxContextAlbums contextAlbums, final AxEngineState state,
149             final AxEngineStats stats) {
150         super(key, contextSchemas, contextAlbums, keyInformation);
151         Assertions.argumentNotNull(state, "state may not be null");
152         Assertions.argumentNotNull(stats, "stats may not be null");
153
154         this.state = state;
155         this.stats = stats;
156     }
157
158     /**
159      * {@inheritDoc}.
160      */
161     @Override
162     public List<AxKey> getKeys() {
163         final List<AxKey> keyList = super.getKeys();
164         keyList.addAll(stats.getKeys());
165         return keyList;
166     }
167
168     /**
169      * Gets the time stamp at which the engine model measurements were taken.
170      *
171      * @return the time stamp at which the engine model measurements were taken
172      */
173     public long getTimestamp() {
174         return timestamp;
175     }
176
177     /**
178      * Gets the time stamp at which the engine model measurements were taken as a string.
179      *
180      * @return the time stamp string
181      */
182     public String getTimeStampString() {
183         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);
184     }
185
186     /**
187      * Sets the time stamp at which the engine model measurements were taken.
188      *
189      * @param timestamp the time stamp at which the engine model measurements were taken
190      */
191     public void setTimestamp(final long timestamp) {
192         this.timestamp = timestamp;
193     }
194
195     /**
196      * Gets the state of the engine at the time the measurements were taken.
197      *
198      * @return the state of the engine at the time the measurements were taken
199      */
200     public AxEngineState getState() {
201         return state;
202     }
203
204     /**
205      * Sets the state of the engine.
206      *
207      * @param state the state of the engine
208      */
209     public void setState(final AxEngineState state) {
210         Assertions.argumentNotNull(state, "state may not be null");
211         this.state = state;
212     }
213
214     /**
215      * Gets the statistics of the engine at the time the measurements were taken.
216      *
217      * @return the statistics of the engine at the time the measurements were taken
218      */
219     public AxEngineStats getStats() {
220         return stats;
221     }
222
223     /**
224      * Sets the the statistics of the engine.
225      *
226      * @param stats the the statistics of the engine
227      */
228     public void setStats(final AxEngineStats stats) {
229         Assertions.argumentNotNull(stats, "stats may not be null");
230         this.stats = stats;
231     }
232
233     /**
234      * {@inheritDoc}.
235      */
236     @Override
237     public AxValidationResult validate(final AxValidationResult resultIn) {
238         AxValidationResult result = resultIn;
239
240         result = stats.validate(result);
241
242         if (timestamp == -1) {
243             result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
244                     this.getClass().getSimpleName() + " - timestamp is not set"));
245         }
246
247         if (state == AxEngineState.UNDEFINED) {
248             result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
249                     this.getClass().getSimpleName() + " - state is UNDEFINED"));
250         }
251
252         return result;
253     }
254
255     /**
256      * {@inheritDoc}.
257      */
258     @Override
259     public void clean() {
260         super.clean();
261         stats.clean();
262     }
263
264     /**
265      * {@inheritDoc}.
266      */
267     @Override
268     public String toString() {
269         final StringBuilder builder = new StringBuilder();
270         builder.append(this.getClass().getSimpleName());
271         builder.append(":(");
272         builder.append(super.toString());
273         builder.append(",timestamp=");
274         builder.append(timestamp);
275         builder.append(",state=");
276         builder.append(state);
277         builder.append(",stats=");
278         builder.append(stats);
279         builder.append(")");
280         return builder.toString();
281     }
282
283     /**
284      * {@inheritDoc}.
285      */
286     @Override
287     public AxConcept copyTo(final AxConcept targetObject) {
288         Assertions.argumentNotNull(targetObject, "target may not be null");
289
290         final Object copyObject = targetObject;
291         Assertions.instanceOf(copyObject, AxEngineModel.class);
292
293         final AxEngineModel copy = ((AxEngineModel) copyObject);
294         super.copyTo(targetObject);
295         copy.timestamp = timestamp;
296         copy.setState(state);
297         copy.setStats(new AxEngineStats(stats));
298
299         return copy;
300     }
301
302     /**
303      * {@inheritDoc}.
304      */
305     @Override
306     public int hashCode() {
307         final int prime = 31;
308         int result = 1;
309         result = prime * result + super.hashCode();
310         result = prime * result + (int) (timestamp ^ (timestamp >>> HASH_CODE_PRIME));
311         result = prime * result + state.hashCode();
312         result = prime * result + stats.hashCode();
313         return result;
314     }
315
316     /**
317      * {@inheritDoc}.
318      */
319     @Override
320     public boolean equals(final Object obj) {
321         if (obj == null) {
322             throw new IllegalArgumentException("comparison object may not be null");
323         }
324
325         if (this == obj) {
326             return true;
327         }
328
329         if (getClass() != obj.getClass()) {
330             return false;
331         }
332
333         final AxEngineModel other = (AxEngineModel) obj;
334         if (!super.equals(other)) {
335             return false;
336         }
337         if (timestamp != other.timestamp) {
338             return false;
339         }
340         if (!state.equals(other.state)) {
341             return false;
342         }
343         return stats.equals(other.stats);
344     }
345
346     /**
347      * {@inheritDoc}.
348      */
349     @Override
350     public int compareTo(final AxConcept otherObj) {
351         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
352
353         if (this == otherObj) {
354             return 0;
355         }
356         if (getClass() != otherObj.getClass()) {
357             return this.hashCode() - otherObj.hashCode();
358         }
359
360         final AxEngineModel other = (AxEngineModel) otherObj;
361         if (!super.equals(other)) {
362             return super.compareTo(other);
363         }
364         if (timestamp != other.timestamp) {
365             return (int) (timestamp - other.timestamp);
366         }
367         if (!state.equals(other.state)) {
368             return state.compareTo(other.state);
369         }
370         return stats.compareTo(other.stats);
371     }
372 }