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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34 import org.onap.policy.common.utils.validation.Assertions;
37 * This class defines the type of output handling that will be used when a task in a state completes
38 * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
39 * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
40 * associated instance of this class that defines how the state output of the state is selected and
41 * how the output fields of the task are marshaled onto the fields of the output event. A
42 * {@link AxStateTaskReference} instance defines the task output handling as either
43 * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
44 * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
45 * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
46 * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
47 * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
48 * class. See the explanation in the {@link AxState} class for a full description of this handling.
50 * <p>During validation of a state task reference, the validation checks listed below are executed:
52 * <li>The state task reference key must not be a null key and must be valid, see validation in
53 * {@link AxReferenceKey}
54 * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
55 * <li>The output key must not be a null key and must be valid, see validation in
56 * {@link AxReferenceKey}
59 public class AxStateTaskReference extends AxConcept {
60 private static final long serialVersionUID = 8041771382337655535L;
62 private AxReferenceKey key;
63 private AxStateTaskOutputType outputType;
64 private AxReferenceKey output;
67 * The Default Constructor creates a state task reference with a null reference key, an
68 * undefined output type and a null output reference key.
70 public AxStateTaskReference() {
71 this(new AxReferenceKey());
77 * @param copyConcept the concept to copy from
79 public AxStateTaskReference(final AxStateTaskReference copyConcept) {
84 * The Keyed Constructor creates a state task reference with the given reference key, an
85 * undefined output type and a null output reference key.
89 public AxStateTaskReference(final AxReferenceKey key) {
91 AxStateTaskOutputType.UNDEFINED, // Output type
92 AxReferenceKey.getNullKey()); // Output
96 * This Constructor creates a state task reference instance with a reference key composed from
97 * the given parent key with a local name composed by concatenating the name of the task key
98 * with the local name of the output. The output type and output are set to the given values.
100 * @param parentKey the parent key to use for the key of the state task reference
101 * @param taskKey the task key to use for the first part of the state task reference local name
102 * @param outputType the type of output to perform when this state task reference instance is
104 * @param output the output to perform when this state task reference instance is used
106 public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
107 final AxStateTaskOutputType outputType, final AxReferenceKey output) {
108 this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
113 * This Constructor creates a state task reference instance with the given reference key and all
114 * its fields defined.
116 * @param key the key of the state task reference
117 * @param outputType the type of output to perform when this state task reference instance is
119 * @param output the output to perform when this state task reference instance is used
121 public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
122 final AxReferenceKey output) {
124 Assertions.argumentNotNull(key, "key may not be null");
125 Assertions.argumentNotNull(outputType, "outputType may not be null");
126 Assertions.argumentNotNull(output, "output may not be null");
129 this.outputType = outputType;
130 this.output = output;
137 public AxReferenceKey getKey() {
145 public List<AxKey> getKeys() {
146 final List<AxKey> keyList = key.getKeys();
148 if (!output.equals(AxReferenceKey.getNullKey())) {
149 keyList.add(new AxKeyUse(output));
156 * Sets the key of the state task reference.
158 * @param key the key of the state task reference
160 public void setKey(final AxReferenceKey key) {
161 Assertions.argumentNotNull(key, "key may not be null");
166 * Gets the type of output to perform when this state task reference instance is used.
168 * @return the the type of output to perform when this state task reference instance is used
170 public AxStateTaskOutputType getStateTaskOutputType() {
175 * Sets the type of output to perform when this state task reference instance is used.
177 * @param stateTaskOutputType the type of output to perform when this state task reference
180 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
181 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
182 this.outputType = stateTaskOutputType;
186 * Gets the output to perform when this state task reference instance is used.
188 * @return the output to perform when this state task reference instance is used
190 public AxReferenceKey getOutput() {
195 * Sets the output to perform when this state task reference instance is used.
197 * @param output the output to perform when this state task reference instance is used
199 public void setOutput(final AxReferenceKey output) {
200 Assertions.argumentNotNull(output, "output may not be null");
201 this.output = output;
208 public AxValidationResult validate(final AxValidationResult resultIn) {
209 AxValidationResult result = resultIn;
211 if (key.equals(AxReferenceKey.getNullKey())) {
212 result.addValidationMessage(
213 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
216 result = key.validate(result);
218 if (outputType == AxStateTaskOutputType.UNDEFINED) {
219 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "outputType may not be UNDEFINED"));
223 if (output.equals(AxReferenceKey.getNullKey())) {
224 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
225 "output key " + output.getId() + " is a null key"));
227 result = output.validate(result);
236 public void clean() {
245 public String toString() {
246 final StringBuilder builder = new StringBuilder();
247 builder.append(this.getClass().getSimpleName());
248 builder.append(":(");
249 builder.append("stateKey=");
251 builder.append(",outputType=");
252 builder.append(outputType);
253 builder.append(",output=");
254 builder.append(output);
256 return builder.toString();
263 public AxConcept copyTo(final AxConcept targetObject) {
264 Assertions.argumentNotNull(targetObject, "target may not be null");
266 final Object copyObject = targetObject;
267 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
269 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
270 copy.setKey(new AxReferenceKey(key));
271 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
272 copy.setOutput(output);
281 public int hashCode() {
282 final int prime = 31;
284 result = prime * result + key.hashCode();
285 result = prime * result + outputType.hashCode();
286 result = prime * result + output.hashCode();
294 public boolean equals(final Object obj) {
301 if (getClass() != obj.getClass()) {
305 final AxStateTaskReference other = (AxStateTaskReference) obj;
306 if (!key.equals(other.key)) {
309 if (outputType != other.outputType) {
312 return output.equals(other.output);
319 public int compareTo(final AxConcept otherObj) {
320 if (otherObj == null) {
323 if (this == otherObj) {
326 if (getClass() != otherObj.getClass()) {
327 return this.hashCode() - otherObj.hashCode();
330 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
331 if (!key.equals(other.key)) {
332 return key.compareTo(other.key);
334 if (!outputType.equals(other.outputType)) {
335 return outputType.compareTo(other.outputType);
337 return output.compareTo(other.output);