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;
176 public AxReferenceKey getKey() {
184 public List<AxKey> getKeys() {
185 final List<AxKey> keyList = key.getKeys();
187 if (!output.equals(AxReferenceKey.getNullKey())) {
188 keyList.add(new AxKeyUse(output));
195 * Sets the key of the state task reference.
197 * @param key the key of the state task reference
199 public void setKey(final AxReferenceKey key) {
200 Assertions.argumentNotNull(key, "key may not be null");
205 * Gets the type of output to perform when this state task reference instance is used.
207 * @return the the type of output to perform when this state task reference instance is used
209 public AxStateTaskOutputType getStateTaskOutputType() {
214 * Sets the type of output to perform when this state task reference instance is used.
216 * @param stateTaskOutputType the type of output to perform when this state task reference
219 public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
220 Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
221 this.outputType = stateTaskOutputType;
225 * Gets the output to perform when this state task reference instance is used.
227 * @return the output to perform when this state task reference instance is used
229 public AxReferenceKey getOutput() {
234 * Sets the output to perform when this state task reference instance is used.
236 * @param output the output to perform when this state task reference instance is used
238 public void setOutput(final AxReferenceKey output) {
239 Assertions.argumentNotNull(output, "output may not be null");
240 this.output = output;
247 public AxValidationResult validate(final AxValidationResult resultIn) {
248 AxValidationResult result = resultIn;
250 if (key.equals(AxReferenceKey.getNullKey())) {
251 result.addValidationMessage(
252 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
255 result = key.validate(result);
257 if (outputType == AxStateTaskOutputType.UNDEFINED) {
258 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
259 "outputType may not be UNDEFINED"));
262 if (output.equals(AxReferenceKey.getNullKey())) {
263 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264 "output key " + output.getId() + " is a null key"));
266 result = output.validate(result);
275 public void clean() {
284 public String toString() {
285 final StringBuilder builder = new StringBuilder();
286 builder.append(this.getClass().getSimpleName());
287 builder.append(":(");
288 builder.append("stateKey=");
290 builder.append(",outputType=");
291 builder.append(outputType);
292 builder.append(",output=");
293 builder.append(output);
295 return builder.toString();
302 public AxConcept copyTo(final AxConcept targetObject) {
303 Assertions.argumentNotNull(targetObject, "target may not be null");
305 final Object copyObject = targetObject;
306 Assertions.instanceOf(copyObject, AxStateTaskReference.class);
308 final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
309 copy.setKey(new AxReferenceKey(key));
310 copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
311 copy.setOutput(output);
320 public int hashCode() {
321 final int prime = 31;
323 result = prime * result + key.hashCode();
324 result = prime * result + outputType.hashCode();
325 result = prime * result + output.hashCode();
333 public boolean equals(final Object obj) {
340 if (getClass() != obj.getClass()) {
344 final AxStateTaskReference other = (AxStateTaskReference) obj;
345 if (!key.equals(other.key)) {
348 if (outputType != other.outputType) {
351 return output.equals(other.output);
358 public int compareTo(final AxConcept otherObj) {
359 if (otherObj == null) {
362 if (this == otherObj) {
365 if (getClass() != otherObj.getClass()) {
366 return this.hashCode() - otherObj.hashCode();
369 final AxStateTaskReference other = (AxStateTaskReference) otherObj;
370 if (!key.equals(other.key)) {
371 return key.compareTo(other.key);
373 if (!outputType.equals(other.outputType)) {
374 return outputType.compareTo(other.outputType);
376 return output.compareTo(other.output);