748abdbe16622883923912ce163cbef57e80ce56
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / concepts / AxStateTaskReference.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.concepts;
23
24 import java.util.List;
25
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;
39
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;
49
50 /**
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.
63  *
64  * <p>During validation of a state task reference, the validation checks listed below are executed:
65  * <ol>
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}
71  * </ol>
72  */
73
74 @Entity
75 @Table(name = "AxStateTaskReference")
76
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"})
81
82 public class AxStateTaskReference extends AxConcept {
83     private static final long serialVersionUID = 8041771382337655535L;
84
85     @EmbeddedId
86     @XmlElement(name = "key", required = true)
87     private AxReferenceKey key;
88
89     @Enumerated
90     @Column(name = "outputType")
91     @XmlElement(required = true)
92     private AxStateTaskOutputType outputType;
93
94     // @formatter:off
95     @Embedded
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;
103     // @formatter:on
104
105     /**
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.
108      */
109     public AxStateTaskReference() {
110         this(new AxReferenceKey());
111     }
112
113     /**
114      * Copy constructor.
115      *
116      * @param copyConcept the concept to copy from
117      */
118     public AxStateTaskReference(final AxStateTaskReference copyConcept) {
119         super(copyConcept);
120     }
121
122     /**
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.
125      *
126      * @param key the key
127      */
128     public AxStateTaskReference(final AxReferenceKey key) {
129         this(key, // Key
130                 AxStateTaskOutputType.UNDEFINED, // Output type
131                 AxReferenceKey.getNullKey()); // Output
132     }
133
134     /**
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.
138      *
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
142      *        used
143      * @param output the output to perform when this state task reference instance is used
144      */
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()),
148                 outputType, output);
149     }
150
151     /**
152      * This Constructor creates a state task reference instance with the given reference key and all
153      * its fields defined.
154      *
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
157      *        used
158      * @param output the output to perform when this state task reference instance is used
159      */
160     public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
161             final AxReferenceKey output) {
162         super();
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");
166
167         this.key = key;
168         this.outputType = outputType;
169         this.output = output;
170     }
171
172     /**
173      * {@inheritDoc}.
174      */
175     @Override
176     public AxReferenceKey getKey() {
177         return key;
178     }
179
180     /**
181      * {@inheritDoc}.
182      */
183     @Override
184     public List<AxKey> getKeys() {
185         final List<AxKey> keyList = key.getKeys();
186
187         if (!output.equals(AxReferenceKey.getNullKey())) {
188             keyList.add(new AxKeyUse(output));
189         }
190
191         return keyList;
192     }
193
194     /**
195      * Sets the key of the state task reference.
196      *
197      * @param key the key of the state task reference
198      */
199     public void setKey(final AxReferenceKey key) {
200         Assertions.argumentNotNull(key, "key may not be null");
201         this.key = key;
202     }
203
204     /**
205      * Gets the type of output to perform when this state task reference instance is used.
206      *
207      * @return the the type of output to perform when this state task reference instance is used
208      */
209     public AxStateTaskOutputType getStateTaskOutputType() {
210         return outputType;
211     }
212
213     /**
214      * Sets the type of output to perform when this state task reference instance is used.
215      *
216      * @param stateTaskOutputType the type of output to perform when this state task reference
217      *        instance is used
218      */
219     public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
220         Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
221         this.outputType = stateTaskOutputType;
222     }
223
224     /**
225      * Gets the output to perform when this state task reference instance is used.
226      *
227      * @return the output to perform when this state task reference instance is used
228      */
229     public AxReferenceKey getOutput() {
230         return output;
231     }
232
233     /**
234      * Sets the output to perform when this state task reference instance is used.
235      *
236      * @param output the output to perform when this state task reference instance is used
237      */
238     public void setOutput(final AxReferenceKey output) {
239         Assertions.argumentNotNull(output, "output may not be null");
240         this.output = output;
241     }
242
243     /**
244      * {@inheritDoc}.
245      */
246     @Override
247     public AxValidationResult validate(final AxValidationResult resultIn) {
248         AxValidationResult result = resultIn;
249
250         if (key.equals(AxReferenceKey.getNullKey())) {
251             result.addValidationMessage(
252                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
253         }
254
255         result = key.validate(result);
256
257         if (outputType == AxStateTaskOutputType.UNDEFINED) {
258             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
259                     "outputType may not be UNDEFINED"));
260         }
261
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"));
265         }
266         result = output.validate(result);
267
268         return result;
269     }
270
271     /**
272      * {@inheritDoc}.
273      */
274     @Override
275     public void clean() {
276         key.clean();
277         output.clean();
278     }
279
280     /**
281      * {@inheritDoc}.
282      */
283     @Override
284     public String toString() {
285         final StringBuilder builder = new StringBuilder();
286         builder.append(this.getClass().getSimpleName());
287         builder.append(":(");
288         builder.append("stateKey=");
289         builder.append(key);
290         builder.append(",outputType=");
291         builder.append(outputType);
292         builder.append(",output=");
293         builder.append(output);
294         builder.append(")");
295         return builder.toString();
296     }
297
298     /**
299      * {@inheritDoc}.
300      */
301     @Override
302     public AxConcept copyTo(final AxConcept targetObject) {
303         Assertions.argumentNotNull(targetObject, "target may not be null");
304
305         final Object copyObject = targetObject;
306         Assertions.instanceOf(copyObject, AxStateTaskReference.class);
307
308         final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
309         copy.setKey(new AxReferenceKey(key));
310         copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
311         copy.setOutput(output);
312
313         return copy;
314     }
315
316     /**
317      * {@inheritDoc}.
318      */
319     @Override
320     public int hashCode() {
321         final int prime = 31;
322         int result = 1;
323         result = prime * result + key.hashCode();
324         result = prime * result + outputType.hashCode();
325         result = prime * result + output.hashCode();
326         return result;
327     }
328
329     /**
330      * {@inheritDoc}.
331      */
332     @Override
333     public boolean equals(final Object obj) {
334         if (obj == null) {
335             return false;
336         }
337         if (this == obj) {
338             return true;
339         }
340         if (getClass() != obj.getClass()) {
341             return false;
342         }
343
344         final AxStateTaskReference other = (AxStateTaskReference) obj;
345         if (!key.equals(other.key)) {
346             return false;
347         }
348         if (outputType != other.outputType) {
349             return false;
350         }
351         return output.equals(other.output);
352     }
353
354     /**
355      * {@inheritDoc}.
356      */
357     @Override
358     public int compareTo(final AxConcept otherObj) {
359         if (otherObj == null) {
360             return -1;
361         }
362         if (this == otherObj) {
363             return 0;
364         }
365         if (getClass() != otherObj.getClass()) {
366             return this.hashCode() - otherObj.hashCode();
367         }
368
369         final AxStateTaskReference other = (AxStateTaskReference) otherObj;
370         if (!key.equals(other.key)) {
371             return key.compareTo(other.key);
372         }
373         if (!outputType.equals(other.outputType)) {
374             return outputType.compareTo(other.outputType);
375         }
376         return output.compareTo(other.output);
377     }
378 }