2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019,2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.policymodel.concepts;
25 import java.util.List;
26 import javax.xml.bind.annotation.XmlAccessType;
27 import javax.xml.bind.annotation.XmlAccessorType;
28 import javax.xml.bind.annotation.XmlElement;
29 import javax.xml.bind.annotation.XmlRootElement;
30 import javax.xml.bind.annotation.XmlType;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
39 import org.onap.policy.common.utils.validation.Assertions;
42 * This class defines the type of output handling that will be used when a task in a state completes
43 * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
44 * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
45 * associated instance of this class that defines how the state output of the state is selected and
46 * how the output fields of the task are marshaled onto the fields of the output event. A
47 * {@link AxStateTaskReference} instance defines the task output handling as either
48 * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
49 * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
50 * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
51 * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
52 * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
53 * class. See the explanation in the {@link AxState} class for a full description of this handling.
55 * <p>During validation of a state task reference, the validation checks listed below are executed:
57 * <li>The state task reference key must not be a null key and must be valid, see validation in
58 * {@link AxReferenceKey}
59 * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
60 * <li>The output key must not be a null key and must be valid, see validation in
61 * {@link AxReferenceKey}
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlRootElement(name = "apexStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp")
67 @XmlType(name = "AxStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp",
68 propOrder = {"key", "outputType", "output"})
70 public class AxStateTaskReference extends AxConcept {
71 private static final long serialVersionUID = 8041771382337655535L;
73 @XmlElement(name = "key", required = true)
74 private AxReferenceKey key;
76 @XmlElement(required = true)
77 private AxStateTaskOutputType outputType;
79 @XmlElement(required = true)
80 private AxReferenceKey output;
83 * The Default Constructor creates a state task reference with a null reference key, an
84 * undefined output type and a null output reference key.
86 public AxStateTaskReference() {
87 this(new AxReferenceKey());
93 * @param copyConcept the concept to copy from
95 public AxStateTaskReference(final AxStateTaskReference copyConcept) {
100 * The Keyed Constructor creates a state task reference with the given reference key, an
101 * undefined output type and a null output reference key.
105 public AxStateTaskReference(final AxReferenceKey key) {
107 AxStateTaskOutputType.UNDEFINED, // Output type
108 AxReferenceKey.getNullKey()); // Output
112 * This Constructor creates a state task reference instance with a reference key composed from
113 * the given parent key with a local name composed by concatenating the name of the task key
114 * with the local name of the output. The output type and output are set to the given values.
116 * @param parentKey the parent key to use for the key of the state task reference
117 * @param taskKey the task key to use for the first part of the state task reference local name
118 * @param outputType the type of output to perform when this state task reference instance is
120 * @param output the output to perform when this state task reference instance is used
122 public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
123 final AxStateTaskOutputType outputType, final AxReferenceKey output) {
124 this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
129 * This Constructor creates a state task reference instance with the given reference key and all
130 * its fields defined.
132 * @param key the key of the state task reference
133 * @param outputType the type of output to perform when this state task reference instance is
135 * @param output the output to perform when this state task reference instance is used
137 public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
138 final AxReferenceKey output) {
140 Assertions.argumentNotNull(key, "key may not be null");
141 Assertions.argumentNotNull(outputType, "outputType may not be null");
142 Assertions.argumentNotNull(output, "output may not be null");
145 this.outputType = outputType;
146 this.output = output;
153 public AxReferenceKey getKey() {
161 public List<AxKey> getKeys() {
162 final List<AxKey> keyList = key.getKeys();
164 if (!output.equals(AxReferenceKey.getNullKey())) {
165 keyList.add(new AxKeyUse(output));
172 * Sets the key of the state task reference.
174 * @param key the key of the state task reference
176 public void setKey(final AxReferenceKey key) {
177 Assertions.argumentNotNull(key, "key may not be null");
182 * Gets the type of output to perform when this state task reference instance is used.
184 * @return the the type of output to perform when this state task reference instance is used
186 public AxStateTaskOutputType getStateTaskOutputType() {
191 * Sets the type of output to perform when this state task reference instance is used.
193 * @param stateTaskOutputType the type of output to perform when this state task reference
196 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
197 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
198 this.outputType = stateTaskOutputType;
202 * Gets the output to perform when this state task reference instance is used.
204 * @return the output to perform when this state task reference instance is used
206 public AxReferenceKey getOutput() {
211 * Sets the output to perform when this state task reference instance is used.
213 * @param output the output to perform when this state task reference instance is used
215 public void setOutput(final AxReferenceKey output) {
216 Assertions.argumentNotNull(output, "output may not be null");
217 this.output = output;
224 public AxValidationResult validate(final AxValidationResult resultIn) {
225 AxValidationResult result = resultIn;
227 if (key.equals(AxReferenceKey.getNullKey())) {
228 result.addValidationMessage(
229 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
232 result = key.validate(result);
234 if (outputType == AxStateTaskOutputType.UNDEFINED) {
235 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
236 "outputType may not be UNDEFINED"));
239 if (output.equals(AxReferenceKey.getNullKey())) {
240 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
241 "output key " + output.getId() + " is a null key"));
243 result = output.validate(result);
252 public void clean() {
261 public String toString() {
262 final StringBuilder builder = new StringBuilder();
263 builder.append(this.getClass().getSimpleName());
264 builder.append(":(");
265 builder.append("stateKey=");
267 builder.append(",outputType=");
268 builder.append(outputType);
269 builder.append(",output=");
270 builder.append(output);
272 return builder.toString();
279 public AxConcept copyTo(final AxConcept targetObject) {
280 Assertions.argumentNotNull(targetObject, "target may not be null");
282 final Object copyObject = targetObject;
283 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
285 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
286 copy.setKey(new AxReferenceKey(key));
287 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
288 copy.setOutput(output);
297 public int hashCode() {
298 final int prime = 31;
300 result = prime * result + key.hashCode();
301 result = prime * result + outputType.hashCode();
302 result = prime * result + output.hashCode();
310 public boolean equals(final Object obj) {
317 if (getClass() != obj.getClass()) {
321 final AxStateTaskReference other = (AxStateTaskReference) obj;
322 if (!key.equals(other.key)) {
325 if (outputType != other.outputType) {
328 return output.equals(other.output);
335 public int compareTo(final AxConcept otherObj) {
336 if (otherObj == null) {
339 if (this == otherObj) {
342 if (getClass() != otherObj.getClass()) {
343 return this.hashCode() - otherObj.hashCode();
346 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
347 if (!key.equals(other.key)) {
348 return key.compareTo(other.key);
350 if (!outputType.equals(other.outputType)) {
351 return outputType.compareTo(other.outputType);
353 return output.compareTo(other.output);