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.Enumerated;
33 import javax.persistence.Table;
34 import javax.xml.bind.annotation.XmlAccessType;
35 import javax.xml.bind.annotation.XmlAccessorType;
36 import javax.xml.bind.annotation.XmlElement;
37 import javax.xml.bind.annotation.XmlRootElement;
38 import javax.xml.bind.annotation.XmlType;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
48 import org.onap.policy.common.utils.validation.Assertions;
51 * This class defines the type of output handling that will be used when a task in a state completes
52 * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
53 * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
54 * associated instance of this class that defines how the state output of the state is selected and
55 * how the output fields of the task are marshaled onto the fields of the output event. A
56 * {@link AxStateTaskReference} instance defines the task output handling as either
57 * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
58 * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
59 * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
60 * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
61 * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
62 * class. See the explanation in the {@link AxState} class for a full description of this handling.
64 * <p>During validation of a state task reference, the validation checks listed below are executed:
66 * <li>The state task reference key must not be a null key and must be valid, see validation in
67 * {@link AxReferenceKey}
68 * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
69 * <li>The output key must not be a null key and must be valid, see validation in
70 * {@link AxReferenceKey}
75 @Table(name = "AxStateTaskReference")
77 @XmlAccessorType(XmlAccessType.FIELD)
78 @XmlRootElement(name = "apexStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp")
79 @XmlType(name = "AxStateTaskReference", namespace = "http://www.onap.org/policy/apex-pdp",
80 propOrder = {"key", "outputType", "output"})
82 public class AxStateTaskReference extends AxConcept {
83 private static final long serialVersionUID = 8041771382337655535L;
86 @XmlElement(name = "key", required = true)
87 private AxReferenceKey key;
90 @Column(name = "outputType")
91 @XmlElement(required = true)
92 private AxStateTaskOutputType outputType;
96 @AttributeOverrides({@AttributeOverride(name = "parentKeyName", column = @Column(name = "outputParentKeyName")),
97 @AttributeOverride(name = "parentKeyVersion", column = @Column(name = "outputParentKeyVersion")),
98 @AttributeOverride(name = "parentLocalName", column = @Column(name = "outputParentLocalName")),
99 @AttributeOverride(name = "localName", column = @Column(name = "outputLocalName"))})
100 @Column(name = "output")
101 @XmlElement(required = true)
102 private AxReferenceKey output;
106 * The Default Constructor creates a state task reference with a null reference key, an
107 * undefined output type and a null output reference key.
109 public AxStateTaskReference() {
110 this(new AxReferenceKey());
116 * @param copyConcept the concept to copy from
118 public AxStateTaskReference(final AxStateTaskReference copyConcept) {
123 * The Keyed Constructor creates a state task reference with the given reference key, an
124 * undefined output type and a null output reference key.
128 public AxStateTaskReference(final AxReferenceKey key) {
130 AxStateTaskOutputType.UNDEFINED, // Output type
131 AxReferenceKey.getNullKey()); // Output
135 * This Constructor creates a state task reference instance with a reference key composed from
136 * the given parent key with a local name composed by concatenating the name of the task key
137 * with the local name of the output. The output type and output are set to the given values.
139 * @param parentKey the parent key to use for the key of the state task reference
140 * @param taskKey the task key to use for the first part of the state task reference local name
141 * @param outputType the type of output to perform when this state task reference instance is
143 * @param output the output to perform when this state task reference instance is used
145 public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
146 final AxStateTaskOutputType outputType, final AxReferenceKey output) {
147 this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
152 * This Constructor creates a state task reference instance with the given reference key and all
153 * its fields defined.
155 * @param key the key of the state task reference
156 * @param outputType the type of output to perform when this state task reference instance is
158 * @param output the output to perform when this state task reference instance is used
160 public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
161 final AxReferenceKey output) {
163 Assertions.argumentNotNull(key, "key may not be null");
164 Assertions.argumentNotNull(outputType, "outputType may not be null");
165 Assertions.argumentNotNull(output, "output may not be null");
168 this.outputType = outputType;
169 this.output = output;
175 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
178 public AxReferenceKey getKey() {
185 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
188 public List<AxKey> getKeys() {
189 final List<AxKey> keyList = key.getKeys();
191 if (!output.equals(AxReferenceKey.getNullKey())) {
192 keyList.add(new AxKeyUse(output));
199 * Sets the key of the state task reference.
201 * @param key the key of the state task reference
203 public void setKey(final AxReferenceKey key) {
204 Assertions.argumentNotNull(key, "key may not be null");
209 * Gets the type of output to perform when this state task reference instance is used.
211 * @return the the type of output to perform when this state task reference instance is used
213 public AxStateTaskOutputType getStateTaskOutputType() {
218 * Sets the type of output to perform when this state task reference instance is used.
220 * @param stateTaskOutputType the type of output to perform when this state task reference
223 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
224 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
225 this.outputType = stateTaskOutputType;
229 * Gets the output to perform when this state task reference instance is used.
231 * @return the output to perform when this state task reference instance is used
233 public AxReferenceKey getOutput() {
238 * Sets the output to perform when this state task reference instance is used.
240 * @param output the output to perform when this state task reference instance is used
242 public void setOutput(final AxReferenceKey output) {
243 Assertions.argumentNotNull(output, "output may not be null");
244 this.output = output;
251 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
252 * basicmodel.concepts.AxValidationResult)
255 public AxValidationResult validate(final AxValidationResult resultIn) {
256 AxValidationResult result = resultIn;
258 if (key.equals(AxReferenceKey.getNullKey())) {
259 result.addValidationMessage(
260 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
263 result = key.validate(result);
265 if (outputType == AxStateTaskOutputType.UNDEFINED) {
266 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267 "outputType may not be UNDEFINED"));
270 if (output.equals(AxReferenceKey.getNullKey())) {
271 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
272 "output key " + output.getId() + " is a null key"));
274 result = output.validate(result);
282 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
285 public void clean() {
293 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
296 public String toString() {
297 final StringBuilder builder = new StringBuilder();
298 builder.append(this.getClass().getSimpleName());
299 builder.append(":(");
300 builder.append("stateKey=");
302 builder.append(",outputType=");
303 builder.append(outputType);
304 builder.append(",output=");
305 builder.append(output);
307 return builder.toString();
314 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
315 * basicmodel.concepts.AxConcept)
318 public AxConcept copyTo(final AxConcept targetObject) {
319 Assertions.argumentNotNull(targetObject, "target may not be null");
321 final Object copyObject = targetObject;
322 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
324 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
325 copy.setKey(new AxReferenceKey(key));
326 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
327 copy.setOutput(output);
335 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
338 public int hashCode() {
339 final int prime = 31;
341 result = prime * result + key.hashCode();
342 result = prime * result + outputType.hashCode();
343 result = prime * result + output.hashCode();
350 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
353 public boolean equals(final Object obj) {
360 if (getClass() != obj.getClass()) {
364 final AxStateTaskReference other = (AxStateTaskReference) obj;
365 if (!key.equals(other.key)) {
368 if (outputType != other.outputType) {
371 return output.equals(other.output);
377 * @see java.lang.Comparable#compareTo(java.lang.Object)
380 public int compareTo(final AxConcept otherObj) {
381 if (otherObj == null) {
384 if (this == otherObj) {
387 if (getClass() != otherObj.getClass()) {
388 return this.hashCode() - otherObj.hashCode();
391 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
392 if (!key.equals(other.key)) {
393 return key.compareTo(other.key);
395 if (!outputType.equals(other.outputType)) {
396 return outputType.compareTo(other.outputType);
398 return output.compareTo(other.output);