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