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;
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;
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;
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.
60 * <p>Validation checks that the current state {@link AxEngineState} is defined and that the time stamp
61 * is set on the engine model.
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"})
70 public class AxEngineModel extends AxContextModel {
71 private static final long serialVersionUID = 6381235864606564046L;
72 private static final int HASH_CODE_PRIME = 32;
74 @Column(name = "timestamp")
75 private long timestamp;
78 @Column(name = "state")
79 @XmlElement(required = true)
80 private AxEngineState state;
84 @JoinColumn(name = "statsParentKeyName", referencedColumnName = "parentKeyName", updatable = false,
86 @JoinColumn(name = "statsParentKeyVersion", referencedColumnName = "parentKeyVersion", updatable = false,
88 @JoinColumn(name = "statsParentLocalName ", referencedColumnName = "parentLocalName", updatable = false,
90 @JoinColumn(name = "statsLocalName", referencedColumnName = "localName", updatable = false,
92 private AxEngineStats stats;
96 * The Default Constructor creates an engine model with a null key and all its fields undefined.
98 public AxEngineModel() {
99 this(new AxArtifactKey());
106 * @param copyConcept the concept to copy from
108 public AxEngineModel(final AxEngineModel copyConcept) {
113 * The Keyed Constructor creates an engine model with the given key and all its fields
116 * @param key the engine model key
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())));
125 * This Constructor creates an engine model with its context model data types all defined, the
126 * state of the engine model is undefined.
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
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())));
140 * This Constructor creates an engine model with all its fields defined.
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
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");
164 public List<AxKey> getKeys() {
165 final List<AxKey> keyList = super.getKeys();
166 keyList.addAll(stats.getKeys());
171 * Gets the time stamp at which the engine model measurements were taken.
173 * @return the time stamp at which the engine model measurements were taken
175 public long getTimestamp() {
180 * Gets the time stamp at which the engine model measurements were taken as a string.
182 * @return the time stamp string
184 public String getTimeStampString() {
185 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);
189 * Sets the time stamp at which the engine model measurements were taken.
191 * @param timestamp the time stamp at which the engine model measurements were taken
193 public void setTimestamp(final long timestamp) {
194 this.timestamp = timestamp;
198 * Gets the state of the engine at the time the measurements were taken.
200 * @return the state of the engine at the time the measurements were taken
202 public AxEngineState getState() {
207 * Sets the state of the engine.
209 * @param state the state of the engine
211 public void setState(final AxEngineState state) {
212 Assertions.argumentNotNull(state, "state may not be null");
217 * Gets the statistics of the engine at the time the measurements were taken.
219 * @return the statistics of the engine at the time the measurements were taken
221 public AxEngineStats getStats() {
226 * Sets the the statistics of the engine.
228 * @param stats the the statistics of the engine
230 public void setStats(final AxEngineStats stats) {
231 Assertions.argumentNotNull(stats, "stats may not be null");
239 public AxValidationResult validate(final AxValidationResult resultIn) {
240 AxValidationResult result = resultIn;
242 result = stats.validate(result);
244 if (timestamp == -1) {
245 result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
246 this.getClass().getSimpleName() + " - timestamp is not set"));
249 if (state == AxEngineState.UNDEFINED) {
250 result.addValidationMessage(new AxValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
251 this.getClass().getSimpleName() + " - state is UNDEFINED"));
261 public void clean() {
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);
282 return builder.toString();
289 public AxConcept copyTo(final AxConcept targetObject) {
290 Assertions.argumentNotNull(targetObject, "target may not be null");
292 final Object copyObject = targetObject;
293 Assertions.instanceOf(copyObject, AxEngineModel.class);
295 final AxEngineModel copy = ((AxEngineModel) copyObject);
296 super.copyTo(targetObject);
297 copy.timestamp = timestamp;
298 copy.setState(state);
299 copy.setStats(new AxEngineStats(stats));
308 public int hashCode() {
309 final int prime = 31;
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();
322 public boolean equals(final Object obj) {
324 throw new IllegalArgumentException("comparison object may not be null");
331 if (getClass() != obj.getClass()) {
335 final AxEngineModel other = (AxEngineModel) obj;
336 if (!super.equals(other)) {
339 if (timestamp != other.timestamp) {
342 if (!state.equals(other.state)) {
345 return stats.equals(other.stats);
352 public int compareTo(final AxConcept otherObj) {
353 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
355 if (this == otherObj) {
358 if (getClass() != otherObj.getClass()) {
359 return this.hashCode() - otherObj.hashCode();
362 final AxEngineModel other = (AxEngineModel) otherObj;
363 if (!super.equals(other)) {
364 return super.compareTo(other);
366 if (timestamp != other.timestamp) {
367 return (int) (timestamp - other.timestamp);
369 if (!state.equals(other.state)) {
370 return state.compareTo(other.state);
372 return stats.compareTo(other.stats);