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.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;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
46 import org.onap.policy.common.utils.validation.Assertions;
49 * This class defines the type of output handling that will be used when a task in a state completes
50 * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
51 * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
52 * associated instance of this class that defines how the state output of the state is selected and
53 * how the output fields of the task are marshaled onto the fields of the output event. A
54 * {@link AxStateTaskReference} instance defines the task output handling as either
55 * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
56 * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
57 * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
58 * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
59 * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
60 * class. See the explanation in the {@link AxState} class for a full description of this handling.
62 * <p>During validation of a state task reference, the validation checks listed below are executed:
64 * <li>The state task reference key must not be a null key and must be valid, see validation in
65 * {@link AxReferenceKey}
66 * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
67 * <li>The output key must not be a null key and must be valid, see validation in
68 * {@link AxReferenceKey}
73 @Table(name = "AxStateTaskReference")
75 @XmlAccessorType(XmlAccessType.FIELD)
76 @XmlRootElement(name = "apexStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp")
77 @XmlType(name = "AxStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp",
78 propOrder = {"key", "outputType", "output"})
80 public class AxStateTaskReference extends AxConcept {
81 private static final long serialVersionUID = 8041771382337655535L;
84 @XmlElement(name = "key", required = true)
85 private AxReferenceKey key;
88 @Column(name = "outputType")
89 @XmlElement(required = true)
90 private AxStateTaskOutputType outputType;
94 @AttributeOverrides({@AttributeOverride(name = "parentKeyName", column = @Column(name = "outputParentKeyName")),
95 @AttributeOverride(name = "parentKeyVersion", column = @Column(name = "outputParentKeyVersion")),
96 @AttributeOverride(name = "parentLocalName", column = @Column(name = "outputParentLocalName")),
97 @AttributeOverride(name = "localName", column = @Column(name = "outputLocalName"))})
98 @Column(name = "output")
99 @XmlElement(required = true)
100 private AxReferenceKey output;
104 * The Default Constructor creates a state task reference with a null reference key, an
105 * undefined output type and a null output reference key.
107 public AxStateTaskReference() {
108 this(new AxReferenceKey());
114 * @param copyConcept the concept to copy from
116 public AxStateTaskReference(final AxStateTaskReference copyConcept) {
121 * The Keyed Constructor creates a state task reference with the given reference key, an
122 * undefined output type and a null output reference key.
126 public AxStateTaskReference(final AxReferenceKey key) {
128 AxStateTaskOutputType.UNDEFINED, // Output type
129 AxReferenceKey.getNullKey()); // Output
133 * This Constructor creates a state task reference instance with a reference key composed from
134 * the given parent key with a local name composed by concatenating the name of the task key
135 * with the local name of the output. The output type and output are set to the given values.
137 * @param parentKey the parent key to use for the key of the state task reference
138 * @param taskKey the task key to use for the first part of the state task reference local name
139 * @param outputType the type of output to perform when this state task reference instance is
141 * @param output the output to perform when this state task reference instance is used
143 public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
144 final AxStateTaskOutputType outputType, final AxReferenceKey output) {
145 this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
150 * This Constructor creates a state task reference instance with the given reference key and all
151 * its fields defined.
153 * @param key the key of the state task reference
154 * @param outputType the type of output to perform when this state task reference instance is
156 * @param output the output to perform when this state task reference instance is used
158 public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
159 final AxReferenceKey output) {
161 Assertions.argumentNotNull(key, "key may not be null");
162 Assertions.argumentNotNull(outputType, "outputType may not be null");
163 Assertions.argumentNotNull(output, "output may not be null");
166 this.outputType = outputType;
167 this.output = output;
174 public AxReferenceKey getKey() {
182 public List<AxKey> getKeys() {
183 final List<AxKey> keyList = key.getKeys();
185 if (!output.equals(AxReferenceKey.getNullKey())) {
186 keyList.add(new AxKeyUse(output));
193 * Sets the key of the state task reference.
195 * @param key the key of the state task reference
197 public void setKey(final AxReferenceKey key) {
198 Assertions.argumentNotNull(key, "key may not be null");
203 * Gets the type of output to perform when this state task reference instance is used.
205 * @return the the type of output to perform when this state task reference instance is used
207 public AxStateTaskOutputType getStateTaskOutputType() {
212 * Sets the type of output to perform when this state task reference instance is used.
214 * @param stateTaskOutputType the type of output to perform when this state task reference
217 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
218 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
219 this.outputType = stateTaskOutputType;
223 * Gets the output to perform when this state task reference instance is used.
225 * @return the output to perform when this state task reference instance is used
227 public AxReferenceKey getOutput() {
232 * Sets the output to perform when this state task reference instance is used.
234 * @param output the output to perform when this state task reference instance is used
236 public void setOutput(final AxReferenceKey output) {
237 Assertions.argumentNotNull(output, "output may not be null");
238 this.output = output;
245 public AxValidationResult validate(final AxValidationResult resultIn) {
246 AxValidationResult result = resultIn;
248 if (key.equals(AxReferenceKey.getNullKey())) {
249 result.addValidationMessage(
250 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
253 result = key.validate(result);
255 if (outputType == AxStateTaskOutputType.UNDEFINED) {
256 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
257 "outputType may not be UNDEFINED"));
260 if (output.equals(AxReferenceKey.getNullKey())) {
261 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
262 "output key " + output.getId() + " is a null key"));
264 result = output.validate(result);
273 public void clean() {
282 public String toString() {
283 final StringBuilder builder = new StringBuilder();
284 builder.append(this.getClass().getSimpleName());
285 builder.append(":(");
286 builder.append("stateKey=");
288 builder.append(",outputType=");
289 builder.append(outputType);
290 builder.append(",output=");
291 builder.append(output);
293 return builder.toString();
300 public AxConcept copyTo(final AxConcept targetObject) {
301 Assertions.argumentNotNull(targetObject, "target may not be null");
303 final Object copyObject = targetObject;
304 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
306 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
307 copy.setKey(new AxReferenceKey(key));
308 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
309 copy.setOutput(output);
318 public int hashCode() {
319 final int prime = 31;
321 result = prime * result + key.hashCode();
322 result = prime * result + outputType.hashCode();
323 result = prime * result + output.hashCode();
331 public boolean equals(final Object obj) {
338 if (getClass() != obj.getClass()) {
342 final AxStateTaskReference other = (AxStateTaskReference) obj;
343 if (!key.equals(other.key)) {
346 if (outputType != other.outputType) {
349 return output.equals(other.output);
356 public int compareTo(final AxConcept otherObj) {
357 if (otherObj == null) {
360 if (this == otherObj) {
363 if (getClass() != otherObj.getClass()) {
364 return this.hashCode() - otherObj.hashCode();
367 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
368 if (!key.equals(other.key)) {
369 return key.compareTo(other.key);
371 if (!outputType.equals(other.outputType)) {
372 return outputType.compareTo(other.outputType);
374 return output.compareTo(other.output);