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.policymodel.concepts;
24 import java.util.List;
25 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
27 import javax.persistence.Column;
28 import javax.persistence.Embedded;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
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.AxKeyUse;
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.common.utils.validation.Assertions;
48 * This class defines a single output that a state can have. A state can have many outputs with each
49 * output defined as an instance of this class. Each state output defines the output event that will
50 * be emitted when this output is selected and optionally the next state that is executed when this
51 * state output is selected. If no next state is defined (the next state is a null
52 * {@link AxReferenceKey} key), then this state output outputs its event to an external system and
53 * is an output state for the full policy.
55 * <p>During validation of a state output, the validation checks listed below are executed:
57 * <li>The state output key must not be a null key and must be valid, see validation in
58 * {@link AxReferenceKey}
59 * <li>The outgoing event key must not be a null key and must be valid, see validation in
60 * {@link AxArtifactKey}
61 * <li>The next state key must be valid, see validation in {@link AxReferenceKey}
66 @Table(name = "AxStateOutput")
68 @XmlAccessorType(XmlAccessType.FIELD)
69 @XmlRootElement(name = "apexStateOutput", namespace = "http://www.onap.org/policy/apex-pdp")
70 @XmlType(name = "AxStateOutput", namespace = "http://www.onap.org/policy/apex-pdp",
71 propOrder = {"key", "outgoingEvent", "nextState"})
73 public class AxStateOutput extends AxConcept {
74 private static final long serialVersionUID = 8041771382337655535L;
77 @XmlElement(name = "key", required = true)
78 private AxReferenceKey key;
82 @AttributeOverrides({@AttributeOverride(name = "name", column = @Column(name = "outgoingEventName")),
83 @AttributeOverride(name = "version", column = @Column(name = "outgoingEventVersion"))})
84 @Column(name = "outgoingEvent")
85 @XmlElement(required = true)
86 private AxArtifactKey outgoingEvent;
89 @AttributeOverrides({@AttributeOverride(name = "parentKeyName", column = @Column(name = "nextStateParentKeyName")),
90 @AttributeOverride(name = "parentKeyVersion", column = @Column(name = "nextStateParentKeyVersion")),
91 @AttributeOverride(name = "parentLocalName", column = @Column(name = "nextStateParentLocalName")),
92 @AttributeOverride(name = "localName", column = @Column(name = "nextStateLocalName"))})
93 @Column(name = "nextState")
94 @XmlElement(required = true)
95 private AxReferenceKey nextState;
99 * The Default Constructor creates a state output instance with a null reference key, outgoing
100 * event key and next state reference key.
102 public AxStateOutput() {
103 this(new AxReferenceKey());
109 * @param copyConcept the concept to copy from
111 public AxStateOutput(final AxStateOutput copyConcept) {
116 * The Keyed Constructor creates a state output instance with the given reference key, outgoing
117 * event key and next state reference key.
119 * @param key the reference key for the state output
121 public AxStateOutput(final AxReferenceKey key) {
123 AxArtifactKey.getNullKey(), // Outgoing Event
124 AxReferenceKey.getNullKey() // Next State
129 * This Constructor creates a state output with a reference key composed of the given parent key
130 * and with a local name composed from the parent key local name concatenated with the next
131 * state's local name. The next state and outgoing event of the state output are set as
134 * @param parentKey the parent key of the state output
135 * @param nextState the next state to which execution will pass on use of this state output
136 * @param outgoingEvent the outgoing event emitted on use of this state output
138 public AxStateOutput(final AxReferenceKey parentKey, final AxReferenceKey nextState,
139 final AxArtifactKey outgoingEvent) {
140 this(new AxReferenceKey(parentKey, parentKey.getLocalName() + '_' + nextState.getLocalName()), outgoingEvent,
145 * This Constructor creates a state output with the specified reference key. The next state and
146 * outgoing event of the state output are set as specified.
149 * @param nextState the next state to which execution will pass on use of this state output
150 * @param outgoingEvent the outgoing event emitted on use of this state output
152 public AxStateOutput(final AxReferenceKey key, final AxArtifactKey outgoingEvent, final AxReferenceKey nextState) {
154 Assertions.argumentNotNull(key, "key may not be null");
155 Assertions.argumentNotNull(outgoingEvent, "outgoingEvent may not be null");
156 Assertions.argumentNotNull(nextState, "nextState may not be null");
159 this.outgoingEvent = outgoingEvent;
160 this.nextState = nextState;
167 public AxReferenceKey getKey() {
175 public List<AxKey> getKeys() {
176 final List<AxKey> keyList = key.getKeys();
177 keyList.add(new AxKeyUse(outgoingEvent));
179 if (!nextState.equals(AxReferenceKey.getNullKey())) {
180 keyList.add(new AxKeyUse(nextState));
187 * Sets the reference key for the state output.
189 * @param key the reference key for the state output
191 public void setKey(final AxReferenceKey key) {
192 Assertions.argumentNotNull(key, "key may not be null");
197 * Gets the outgoing event emitted on use of this state output.
199 * @return the outgoing event emitted on use of this state output
201 public AxArtifactKey getOutgingEvent() {
202 return outgoingEvent;
206 * Sets the outgoing event emitted on use of this state output.
208 * @param outgoingEvent the outgoing event emitted on use of this state output
210 public void setOutgoingEvent(final AxArtifactKey outgoingEvent) {
211 Assertions.argumentNotNull(outgoingEvent, "outgoingEvent may not be null");
212 this.outgoingEvent = outgoingEvent;
216 * Gets the next state to which execution will pass on use of this state output.
218 * @return the next state to which execution will pass on use of this state output
220 public AxReferenceKey getNextState() {
225 * Sets the next state to which execution will pass on use of this state output.
227 * @param nextState the next state to which execution will pass on use of this state output
229 public void setNextState(final AxReferenceKey nextState) {
230 Assertions.argumentNotNull(nextState, "nextState may not be null");
231 this.nextState = nextState;
238 public AxValidationResult validate(final AxValidationResult resultIn) {
239 AxValidationResult result = resultIn;
241 if (key.equals(AxReferenceKey.getNullKey())) {
242 result.addValidationMessage(
243 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
246 result = key.validate(result);
248 if (outgoingEvent.equals(AxArtifactKey.getNullKey())) {
249 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
250 "outgoingEvent reference is a null key, an outgoing event must be specified"));
252 result = outgoingEvent.validate(result);
254 // Note: Null keys are allowed on nextState as there may not be a next state
255 result = nextState.validate(result);
264 public void clean() {
266 outgoingEvent.clean();
274 public String toString() {
275 final StringBuilder builder = new StringBuilder();
276 builder.append(this.getClass().getSimpleName());
277 builder.append(":(");
278 builder.append("stateKey=");
280 builder.append(",outgoingEvent=");
281 builder.append(outgoingEvent);
282 builder.append(",nextState=");
283 builder.append(nextState);
285 return builder.toString();
292 public AxConcept copyTo(final AxConcept targetObject) {
293 Assertions.argumentNotNull(targetObject, "target may not be null");
295 final Object copyObject = targetObject;
296 Assertions.instanceOf(copyObject, AxStateOutput.class);
298 final AxStateOutput copy = ((AxStateOutput) copyObject);
299 copy.setKey(new AxReferenceKey(key));
300 copy.setOutgoingEvent(new AxArtifactKey(outgoingEvent));
301 copy.setNextState(nextState);
310 public int hashCode() {
311 final int prime = 31;
313 result = prime * result + key.hashCode();
314 result = prime * result + outgoingEvent.hashCode();
315 result = prime * result + nextState.hashCode();
323 public boolean equals(final Object obj) {
330 if (getClass() != obj.getClass()) {
334 final AxStateOutput other = (AxStateOutput) obj;
335 if (!key.equals(other.key)) {
338 if (!outgoingEvent.equals(other.outgoingEvent)) {
341 return nextState.equals(other.nextState);
348 public int compareTo(final AxConcept otherObj) {
349 if (otherObj == null) {
352 if (this == otherObj) {
355 if (getClass() != otherObj.getClass()) {
356 return this.hashCode() - otherObj.hashCode();
359 final AxStateOutput other = (AxStateOutput) otherObj;
360 if (!key.equals(other.key)) {
361 return key.compareTo(other.key);
363 if (!outgoingEvent.equals(other.outgoingEvent)) {
364 return outgoingEvent.compareTo(other.outgoingEvent);
366 return nextState.compareTo(other.nextState);