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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.enginemodel.concepts;
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;
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.
58 * <p>Validation checks that the current state {@link AxEngineState} is defined and that the time stamp
59 * is set on the engine model.
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"})
68 public class AxEngineModel extends AxContextModel {
69 private static final long serialVersionUID = 6381235864606564046L;
70 private static final int HASH_CODE_PRIME = 32;
72 @Column(name = "timestamp")
73 private long timestamp;
76 @Column(name = "state")
77 @XmlElement(required = true)
78 private AxEngineState state;
82 @JoinColumn(name = "statsParentKeyName", referencedColumnName = "parentKeyName", updatable = false,
84 @JoinColumn(name = "statsParentKeyVersion", referencedColumnName = "parentKeyVersion", updatable = false,
86 @JoinColumn(name = "statsParentLocalName ", referencedColumnName = "parentLocalName", updatable = false,
88 @JoinColumn(name = "statsLocalName", referencedColumnName = "localName", updatable = false,
90 private AxEngineStats stats;
94 * The Default Constructor creates an engine model with a null key and all its fields undefined.
96 public AxEngineModel() {
97 this(new AxArtifactKey());
104 * @param copyConcept the concept to copy from
106 public AxEngineModel(final AxEngineModel copyConcept) {
111 * The Keyed Constructor creates an engine model with the given key and all its fields
114 * @param key the engine model key
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())));
123 * This Constructor creates an engine model with its context model data types all defined, the
124 * state of the engine model is undefined.
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
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())));
138 * This Constructor creates an engine model with all its fields defined.
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
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");
162 public List<AxKey> getKeys() {
163 final List<AxKey> keyList = super.getKeys();
164 keyList.addAll(stats.getKeys());
169 * Gets the time stamp at which the engine model measurements were taken.
171 * @return the time stamp at which the engine model measurements were taken
173 public long getTimestamp() {
178 * Gets the time stamp at which the engine model measurements were taken as a string.
180 * @return the time stamp string
182 public String getTimeStampString() {
183 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);
187 * Sets the time stamp at which the engine model measurements were taken.
189 * @param timestamp the time stamp at which the engine model measurements were taken
191 public void setTimestamp(final long timestamp) {
192 this.timestamp = timestamp;
196 * Gets the state of the engine at the time the measurements were taken.
198 * @return the state of the engine at the time the measurements were taken
200 public AxEngineState getState() {
205 * Sets the state of the engine.
207 * @param state the state of the engine
209 public void setState(final AxEngineState state) {
210 Assertions.argumentNotNull(state, "state may not be null");
215 * Gets the statistics of the engine at the time the measurements were taken.
217 * @return the statistics of the engine at the time the measurements were taken
219 public AxEngineStats getStats() {
224 * Sets the the statistics of the engine.
226 * @param stats the the statistics of the engine
228 public void setStats(final AxEngineStats stats) {
229 Assertions.argumentNotNull(stats, "stats may not be null");
237 public AxValidationResult validate(final AxValidationResult resultIn) {
238 AxValidationResult result = resultIn;
240 result = stats.validate(result);
242 if (timestamp == -1) {
243 result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
244 this.getClass().getSimpleName() + " - timestamp is not set"));
247 if (state == AxEngineState.UNDEFINED) {
248 result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
249 this.getClass().getSimpleName() + " - state is UNDEFINED"));
259 public void clean() {
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);
280 return builder.toString();
287 public AxConcept copyTo(final AxConcept targetObject) {
288 Assertions.argumentNotNull(targetObject, "target may not be null");
290 final Object copyObject = targetObject;
291 Assertions.instanceOf(copyObject, AxEngineModel.class);
293 final AxEngineModel copy = ((AxEngineModel) copyObject);
294 super.copyTo(targetObject);
295 copy.timestamp = timestamp;
296 copy.setState(state);
297 copy.setStats(new AxEngineStats(stats));
306 public int hashCode() {
307 final int prime = 31;
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();
320 public boolean equals(final Object obj) {
322 throw new IllegalArgumentException("comparison object may not be null");
329 if (getClass() != obj.getClass()) {
333 final AxEngineModel other = (AxEngineModel) obj;
334 if (!super.equals(other)) {
337 if (timestamp != other.timestamp) {
340 if (!state.equals(other.state)) {
343 return stats.equals(other.stats);
350 public int compareTo(final AxConcept otherObj) {
351 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
353 if (this == otherObj) {
356 if (getClass() != otherObj.getClass()) {
357 return this.hashCode() - otherObj.hashCode();
360 final AxEngineModel other = (AxEngineModel) otherObj;
361 if (!super.equals(other)) {
362 return super.compareTo(other);
364 if (timestamp != other.timestamp) {
365 return (int) (timestamp - other.timestamp);
367 if (!state.equals(other.state)) {
368 return state.compareTo(other.state);
370 return stats.compareTo(other.stats);