2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.concepts;
23 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.Enumerated;
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.apex.model.utilities.Assertions;
50 * This class defines the type of output handling that will be used when a task in a state completes
51 * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
52 * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
53 * associated instance of this class that defines how the state output of the state is selected and
54 * how the output fields of the task are marshaled onto the fields of the output event. A
55 * {@link AxStateTaskReference} instance defines the task output handling as either
56 * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
57 * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
58 * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
59 * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
60 * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
61 * class. See the explanation in the {@link AxState} class for a full description of this handling.
63 * <p>During validation of a state task reference, the validation checks listed below are executed:
65 * <li>The state task reference key must not be a null key and must be valid, see validation in
66 * {@link AxReferenceKey}
67 * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
68 * <li>The output key must not be a null key and must be valid, see validation in
69 * {@link AxReferenceKey}
74 @Table(name = "AxStateTaskReference")
76 @XmlAccessorType(XmlAccessType.FIELD)
77 @XmlRootElement(name = "apexStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp")
78 @XmlType(name = "AxStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp",
79 propOrder = {"key", "outputType", "output"})
81 public class AxStateTaskReference extends AxConcept {
82 private static final long serialVersionUID = 8041771382337655535L;
85 @XmlElement(name = "key", required = true)
86 private AxReferenceKey key;
89 @Column(name = "outputType")
90 @XmlElement(required = true)
91 private AxStateTaskOutputType outputType;
95 @AttributeOverrides({@AttributeOverride(name = "parentKeyName", column = @Column(name = "outputParentKeyName")),
96 @AttributeOverride(name = "parentKeyVersion", column = @Column(name = "outputParentKeyVersion")),
97 @AttributeOverride(name = "parentLocalName", column = @Column(name = "outputParentLocalName")),
98 @AttributeOverride(name = "localName", column = @Column(name = "outputLocalName"))})
99 @Column(name = "output")
100 @XmlElement(required = true)
101 private AxReferenceKey output;
105 * The Default Constructor creates a state task reference with a null reference key, an
106 * undefined output type and a null output reference key.
108 public AxStateTaskReference() {
109 this(new AxReferenceKey());
115 * @param copyConcept the concept to copy from
117 public AxStateTaskReference(final AxStateTaskReference copyConcept) {
122 * The Keyed Constructor creates a state task reference with the given reference key, an
123 * undefined output type and a null output reference key.
127 public AxStateTaskReference(final AxReferenceKey key) {
129 AxStateTaskOutputType.UNDEFINED, // Output type
130 AxReferenceKey.getNullKey()); // Output
134 * This Constructor creates a state task reference instance with a reference key composed from
135 * the given parent key with a local name composed by concatenating the name of the task key
136 * with the local name of the output. The output type and output are set to the given values.
138 * @param parentKey the parent key to use for the key of the state task reference
139 * @param taskKey the task key to use for the first part of the state task reference local name
140 * @param outputType the type of output to perform when this state task reference instance is
142 * @param output the output to perform when this state task reference instance is used
144 public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
145 final AxStateTaskOutputType outputType, final AxReferenceKey output) {
146 this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
151 * This Constructor creates a state task reference instance with the given reference key and all
152 * its fields defined.
154 * @param key the key of the state task reference
155 * @param outputType the type of output to perform when this state task reference instance is
157 * @param output the output to perform when this state task reference instance is used
159 public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
160 final AxReferenceKey output) {
162 Assertions.argumentNotNull(key, "key may not be null");
163 Assertions.argumentNotNull(outputType, "outputType may not be null");
164 Assertions.argumentNotNull(output, "output may not be null");
167 this.outputType = outputType;
168 this.output = output;
174 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
177 public AxReferenceKey getKey() {
184 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
187 public List<AxKey> getKeys() {
188 final List<AxKey> keyList = key.getKeys();
190 if (!output.equals(AxReferenceKey.getNullKey())) {
191 keyList.add(new AxKeyUse(output));
198 * Sets the key of the state task reference.
200 * @param key the key of the state task reference
202 public void setKey(final AxReferenceKey key) {
203 Assertions.argumentNotNull(key, "key may not be null");
208 * Gets the type of output to perform when this state task reference instance is used.
210 * @return the the type of output to perform when this state task reference instance is used
212 public AxStateTaskOutputType getStateTaskOutputType() {
217 * Sets the type of output to perform when this state task reference instance is used.
219 * @param stateTaskOutputType the type of output to perform when this state task reference
222 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
223 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
224 this.outputType = stateTaskOutputType;
228 * Gets the output to perform when this state task reference instance is used.
230 * @return the output to perform when this state task reference instance is used
232 public AxReferenceKey getOutput() {
237 * Sets the output to perform when this state task reference instance is used.
239 * @param output the output to perform when this state task reference instance is used
241 public void setOutput(final AxReferenceKey output) {
242 Assertions.argumentNotNull(output, "output may not be null");
243 this.output = output;
250 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
251 * basicmodel.concepts.AxValidationResult)
254 public AxValidationResult validate(final AxValidationResult resultIn) {
255 AxValidationResult result = resultIn;
257 if (key.equals(AxReferenceKey.getNullKey())) {
258 result.addValidationMessage(
259 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
262 result = key.validate(result);
264 if (outputType == AxStateTaskOutputType.UNDEFINED) {
265 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266 "outputType may not be UNDEFINED"));
269 if (output.equals(AxReferenceKey.getNullKey())) {
270 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271 "output key " + output.getId() + " is a null key"));
273 result = output.validate(result);
281 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
284 public void clean() {
292 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
295 public String toString() {
296 final StringBuilder builder = new StringBuilder();
297 builder.append(this.getClass().getSimpleName());
298 builder.append(":(");
299 builder.append("stateKey=");
301 builder.append(",outputType=");
302 builder.append(outputType);
303 builder.append(",output=");
304 builder.append(output);
306 return builder.toString();
313 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
314 * basicmodel.concepts.AxConcept)
317 public AxConcept copyTo(final AxConcept targetObject) {
318 Assertions.argumentNotNull(targetObject, "target may not be null");
320 final Object copyObject = targetObject;
321 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
323 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
324 copy.setKey(new AxReferenceKey(key));
325 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
326 copy.setOutput(output);
334 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
337 public int hashCode() {
338 final int prime = 31;
340 result = prime * result + key.hashCode();
341 result = prime * result + outputType.hashCode();
342 result = prime * result + output.hashCode();
349 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
352 public boolean equals(final Object obj) {
359 if (getClass() != obj.getClass()) {
363 final AxStateTaskReference other = (AxStateTaskReference) obj;
364 if (!key.equals(other.key)) {
367 if (outputType != other.outputType) {
370 return output.equals(other.output);
376 * @see java.lang.Comparable#compareTo(java.lang.Object)
379 public int compareTo(final AxConcept otherObj) {
380 if (otherObj == null) {
383 if (this == otherObj) {
386 if (getClass() != otherObj.getClass()) {
387 return this.hashCode() - otherObj.hashCode();
390 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
391 if (!key.equals(other.key)) {
392 return key.compareTo(other.key);
394 if (!outputType.equals(other.outputType)) {
395 return outputType.compareTo(other.outputType);
397 return output.compareTo(other.output);