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