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;
26 import javax.persistence.AttributeOverride;
27 import javax.persistence.AttributeOverrides;
28 import javax.persistence.Column;
29 import javax.persistence.Embedded;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
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.AxKeyUse;
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.common.utils.validation.Assertions;
50 * This class defines a single output that a state can have. A state can have many outputs with each
51 * output defined as an instance of this class. Each state output defines the output event that will
52 * be emitted when this output is selected and optionally the next state that is executed when this
53 * state output is selected. If no next state is defined (the next state is a null
54 * {@link AxReferenceKey} key), then this state output outputs its event to an external system and
55 * is an output state for the full policy.
57 * <p>During validation of a state output, the validation checks listed below are executed:
59 * <li>The state output key must not be a null key and must be valid, see validation in
60 * {@link AxReferenceKey}
61 * <li>The outgoing event key must not be a null key and must be valid, see validation in
62 * {@link AxArtifactKey}
63 * <li>The next state key must be valid, see validation in {@link AxReferenceKey}
68 @Table(name = "AxStateOutput")
70 @XmlAccessorType(XmlAccessType.FIELD)
71 @XmlRootElement(name = "apexStateOutput", namespace = "http://www.onap.org/policy/apex-pdp")
72 @XmlType(name = "AxStateOutput", namespace = "http://www.onap.org/policy/apex-pdp",
73 propOrder = {"key", "outgoingEvent", "nextState"})
75 public class AxStateOutput extends AxConcept {
76 private static final long serialVersionUID = 8041771382337655535L;
79 @XmlElement(name = "key", required = true)
80 private AxReferenceKey key;
84 @AttributeOverrides({@AttributeOverride(name = "name", column = @Column(name = "outgoingEventName")),
85 @AttributeOverride(name = "version", column = @Column(name = "outgoingEventVersion"))})
86 @Column(name = "outgoingEvent")
87 @XmlElement(required = true)
88 private AxArtifactKey outgoingEvent;
91 @AttributeOverrides({@AttributeOverride(name = "parentKeyName", column = @Column(name = "nextStateParentKeyName")),
92 @AttributeOverride(name = "parentKeyVersion", column = @Column(name = "nextStateParentKeyVersion")),
93 @AttributeOverride(name = "parentLocalName", column = @Column(name = "nextStateParentLocalName")),
94 @AttributeOverride(name = "localName", column = @Column(name = "nextStateLocalName"))})
95 @Column(name = "nextState")
96 @XmlElement(required = true)
97 private AxReferenceKey nextState;
101 * The Default Constructor creates a state output instance with a null reference key, outgoing
102 * event key and next state reference key.
104 public AxStateOutput() {
105 this(new AxReferenceKey());
111 * @param copyConcept the concept to copy from
113 public AxStateOutput(final AxStateOutput copyConcept) {
118 * The Keyed Constructor creates a state output instance with the given reference key, outgoing
119 * event key and next state reference key.
121 * @param key the reference key for the state output
123 public AxStateOutput(final AxReferenceKey key) {
125 AxArtifactKey.getNullKey(), // Outgoing Event
126 AxReferenceKey.getNullKey() // Next State
131 * This Constructor creates a state output with a reference key composed of the given parent key
132 * and with a local name composed from the parent key local name concatenated with the next
133 * state's local name. The next state and outgoing event of the state output are set as
136 * @param parentKey the parent key of the state output
137 * @param nextState the next state to which execution will pass on use of this state output
138 * @param outgoingEvent the outgoing event emitted on use of this state output
140 public AxStateOutput(final AxReferenceKey parentKey, final AxReferenceKey nextState,
141 final AxArtifactKey outgoingEvent) {
142 this(new AxReferenceKey(parentKey, parentKey.getLocalName() + '_' + nextState.getLocalName()), outgoingEvent,
147 * This Constructor creates a state output with the specified reference key. The next state and
148 * outgoing event of the state output are set as specified.
151 * @param nextState the next state to which execution will pass on use of this state output
152 * @param outgoingEvent the outgoing event emitted on use of this state output
154 public AxStateOutput(final AxReferenceKey key, final AxArtifactKey outgoingEvent, final AxReferenceKey nextState) {
156 Assertions.argumentNotNull(key, "key may not be null");
157 Assertions.argumentNotNull(outgoingEvent, "outgoingEvent may not be null");
158 Assertions.argumentNotNull(nextState, "nextState may not be null");
161 this.outgoingEvent = outgoingEvent;
162 this.nextState = nextState;
169 public AxReferenceKey getKey() {
177 public List<AxKey> getKeys() {
178 final List<AxKey> keyList = key.getKeys();
179 keyList.add(new AxKeyUse(outgoingEvent));
181 if (!nextState.equals(AxReferenceKey.getNullKey())) {
182 keyList.add(new AxKeyUse(nextState));
189 * Sets the reference key for the state output.
191 * @param key the reference key for the state output
193 public void setKey(final AxReferenceKey key) {
194 Assertions.argumentNotNull(key, "key may not be null");
199 * Gets the outgoing event emitted on use of this state output.
201 * @return the outgoing event emitted on use of this state output
203 public AxArtifactKey getOutgingEvent() {
204 return outgoingEvent;
208 * Sets the outgoing event emitted on use of this state output.
210 * @param outgoingEvent the outgoing event emitted on use of this state output
212 public void setOutgoingEvent(final AxArtifactKey outgoingEvent) {
213 Assertions.argumentNotNull(outgoingEvent, "outgoingEvent may not be null");
214 this.outgoingEvent = outgoingEvent;
218 * Gets the next state to which execution will pass on use of this state output.
220 * @return the next state to which execution will pass on use of this state output
222 public AxReferenceKey getNextState() {
227 * Sets the next state to which execution will pass on use of this state output.
229 * @param nextState the next state to which execution will pass on use of this state output
231 public void setNextState(final AxReferenceKey nextState) {
232 Assertions.argumentNotNull(nextState, "nextState may not be null");
233 this.nextState = nextState;
240 public AxValidationResult validate(final AxValidationResult resultIn) {
241 AxValidationResult result = resultIn;
243 if (key.equals(AxReferenceKey.getNullKey())) {
244 result.addValidationMessage(
245 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
248 result = key.validate(result);
250 if (outgoingEvent.equals(AxArtifactKey.getNullKey())) {
251 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
252 "outgoingEvent reference is a null key, an outgoing event must be specified"));
254 result = outgoingEvent.validate(result);
256 // Note: Null keys are allowed on nextState as there may not be a next state
257 result = nextState.validate(result);
266 public void clean() {
268 outgoingEvent.clean();
276 public String toString() {
277 final StringBuilder builder = new StringBuilder();
278 builder.append(this.getClass().getSimpleName());
279 builder.append(":(");
280 builder.append("stateKey=");
282 builder.append(",outgoingEvent=");
283 builder.append(outgoingEvent);
284 builder.append(",nextState=");
285 builder.append(nextState);
287 return builder.toString();
294 public AxConcept copyTo(final AxConcept targetObject) {
295 Assertions.argumentNotNull(targetObject, "target may not be null");
297 final Object copyObject = targetObject;
298 Assertions.instanceOf(copyObject, AxStateOutput.class);
300 final AxStateOutput copy = ((AxStateOutput) copyObject);
301 copy.setKey(new AxReferenceKey(key));
302 copy.setOutgoingEvent(new AxArtifactKey(outgoingEvent));
303 copy.setNextState(nextState);
312 public int hashCode() {
313 final int prime = 31;
315 result = prime * result + key.hashCode();
316 result = prime * result + outgoingEvent.hashCode();
317 result = prime * result + nextState.hashCode();
325 public boolean equals(final Object obj) {
332 if (getClass() != obj.getClass()) {
336 final AxStateOutput other = (AxStateOutput) obj;
337 if (!key.equals(other.key)) {
340 if (!outgoingEvent.equals(other.outgoingEvent)) {
343 return nextState.equals(other.nextState);
350 public int compareTo(final AxConcept otherObj) {
351 if (otherObj == null) {
354 if (this == otherObj) {
357 if (getClass() != otherObj.getClass()) {
358 return this.hashCode() - otherObj.hashCode();
361 final AxStateOutput other = (AxStateOutput) otherObj;
362 if (!key.equals(other.key)) {
363 return key.compareTo(other.key);
365 if (!outgoingEvent.equals(other.outgoingEvent)) {
366 return outgoingEvent.compareTo(other.outgoingEvent);
368 return nextState.compareTo(other.nextState);