748eca92dd050727341b4d9f96f071b882ccf133
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019,2022 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.policymodel.concepts;
24
25 import java.util.List;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34 import org.onap.policy.common.utils.validation.Assertions;
35
36 /**
37  * This class defines the type of output handling that will be used when a task in a state completes
38  * execution. Each task {@link AxTask} in a state {@link AxState} must select a state output
39  * {@link AxStateOutput} in order to pass its fields to an output event. Therefore, each task has an
40  * associated instance of this class that defines how the state output of the state is selected and
41  * how the output fields of the task are marshaled onto the fields of the output event. A
42  * {@link AxStateTaskReference} instance defines the task output handling as either
43  * {@link AxStateTaskOutputType#DIRECT} or {@link AxStateTaskOutputType#LOGIC}. In the case of
44  * {@link AxStateTaskOutputType#DIRECT} output selection, the output reference key held in this
45  * {@link AxStateTaskReference} instance to an instance of an {@link AxStateOutput} class. In the
46  * case of {@link AxStateTaskOutputType#LOGIC} output selection, the output reference key held in
47  * this {@link AxStateTaskReference} instance to an instance of an {@link AxStateFinalizerLogic}
48  * class. See the explanation in the {@link AxState} class for a full description of this handling.
49  *
50  * <p>During validation of a state task reference, the validation checks listed below are executed:
51  * <ol>
52  * <li>The state task reference key must not be a null key and must be valid, see validation in
53  * {@link AxReferenceKey}
54  * <li>The output type must be defined, that is not equal to {@link AxStateTaskOutputType#UNDEFINED}
55  * <li>The output key must not be a null key and must be valid, see validation in
56  * {@link AxReferenceKey}
57  * </ol>
58  */
59 public class AxStateTaskReference extends AxConcept {
60     private static final long serialVersionUID = 8041771382337655535L;
61
62     private AxReferenceKey key;
63     private AxStateTaskOutputType outputType;
64     private AxReferenceKey output;
65
66     /**
67      * The Default Constructor creates a state task reference with a null reference key, an
68      * undefined output type and a null output reference key.
69      */
70     public AxStateTaskReference() {
71         this(new AxReferenceKey());
72     }
73
74     /**
75      * Copy constructor.
76      *
77      * @param copyConcept the concept to copy from
78      */
79     public AxStateTaskReference(final AxStateTaskReference copyConcept) {
80         super(copyConcept);
81     }
82
83     /**
84      * The Keyed Constructor creates a state task reference with the given reference key, an
85      * undefined output type and a null output reference key.
86      *
87      * @param key the key
88      */
89     public AxStateTaskReference(final AxReferenceKey key) {
90         this(key, // Key
91                 AxStateTaskOutputType.UNDEFINED, // Output type
92                 AxReferenceKey.getNullKey()); // Output
93     }
94
95     /**
96      * This Constructor creates a state task reference instance with a reference key composed from
97      * the given parent key with a local name composed by concatenating the name of the task key
98      * with the local name of the output. The output type and output are set to the given values.
99      *
100      * @param parentKey the parent key to use for the key of the state task reference
101      * @param taskKey the task key to use for the first part of the state task reference local name
102      * @param outputType the type of output to perform when this state task reference instance is
103      *        used
104      * @param output the output to perform when this state task reference instance is used
105      */
106     public AxStateTaskReference(final AxReferenceKey parentKey, final AxArtifactKey taskKey,
107             final AxStateTaskOutputType outputType, final AxReferenceKey output) {
108         this(new AxReferenceKey(parentKey, taskKey.getName() + '_' + outputType.name() + '_' + output.getLocalName()),
109                 outputType, output);
110     }
111
112     /**
113      * This Constructor creates a state task reference instance with the given reference key and all
114      * its fields defined.
115      *
116      * @param key the key of the state task reference
117      * @param outputType the type of output to perform when this state task reference instance is
118      *        used
119      * @param output the output to perform when this state task reference instance is used
120      */
121     public AxStateTaskReference(final AxReferenceKey key, final AxStateTaskOutputType outputType,
122             final AxReferenceKey output) {
123         super();
124         Assertions.argumentNotNull(key, "key may not be null");
125         Assertions.argumentNotNull(outputType, "outputType may not be null");
126         Assertions.argumentNotNull(output, "output may not be null");
127
128         this.key = key;
129         this.outputType = outputType;
130         this.output = output;
131     }
132
133     /**
134      * {@inheritDoc}.
135      */
136     @Override
137     public AxReferenceKey getKey() {
138         return key;
139     }
140
141     /**
142      * {@inheritDoc}.
143      */
144     @Override
145     public List<AxKey> getKeys() {
146         final List<AxKey> keyList = key.getKeys();
147
148         if (!output.equals(AxReferenceKey.getNullKey())) {
149             keyList.add(new AxKeyUse(output));
150         }
151
152         return keyList;
153     }
154
155     /**
156      * Sets the key of the state task reference.
157      *
158      * @param key the key of the state task reference
159      */
160     public void setKey(final AxReferenceKey key) {
161         Assertions.argumentNotNull(key, "key may not be null");
162         this.key = key;
163     }
164
165     /**
166      * Gets the type of output to perform when this state task reference instance is used.
167      *
168      * @return the the type of output to perform when this state task reference instance is used
169      */
170     public AxStateTaskOutputType getStateTaskOutputType() {
171         return outputType;
172     }
173
174     /**
175      * Sets the type of output to perform when this state task reference instance is used.
176      *
177      * @param stateTaskOutputType the type of output to perform when this state task reference
178      *        instance is used
179      */
180     public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
181         Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
182         this.outputType = stateTaskOutputType;
183     }
184
185     /**
186      * Gets the output to perform when this state task reference instance is used.
187      *
188      * @return the output to perform when this state task reference instance is used
189      */
190     public AxReferenceKey getOutput() {
191         return output;
192     }
193
194     /**
195      * Sets the output to perform when this state task reference instance is used.
196      *
197      * @param output the output to perform when this state task reference instance is used
198      */
199     public void setOutput(final AxReferenceKey output) {
200         Assertions.argumentNotNull(output, "output may not be null");
201         this.output = output;
202     }
203
204     /**
205      * {@inheritDoc}.
206      */
207     @Override
208     public AxValidationResult validate(final AxValidationResult resultIn) {
209         AxValidationResult result = resultIn;
210
211         if (key.equals(AxReferenceKey.getNullKey())) {
212             result.addValidationMessage(
213                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
214         }
215
216         result = key.validate(result);
217
218         if (outputType == AxStateTaskOutputType.UNDEFINED) {
219             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220                     "outputType may not be UNDEFINED"));
221         }
222
223         if (output.equals(AxReferenceKey.getNullKey())) {
224             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
225                     "output key " + output.getId() + " is a null key"));
226         }
227         result = output.validate(result);
228
229         return result;
230     }
231
232     /**
233      * {@inheritDoc}.
234      */
235     @Override
236     public void clean() {
237         key.clean();
238         output.clean();
239     }
240
241     /**
242      * {@inheritDoc}.
243      */
244     @Override
245     public String toString() {
246         final StringBuilder builder = new StringBuilder();
247         builder.append(this.getClass().getSimpleName());
248         builder.append(":(");
249         builder.append("stateKey=");
250         builder.append(key);
251         builder.append(",outputType=");
252         builder.append(outputType);
253         builder.append(",output=");
254         builder.append(output);
255         builder.append(")");
256         return builder.toString();
257     }
258
259     /**
260      * {@inheritDoc}.
261      */
262     @Override
263     public AxConcept copyTo(final AxConcept targetObject) {
264         Assertions.argumentNotNull(targetObject, "target may not be null");
265
266         final Object copyObject = targetObject;
267         Assertions.instanceOf(copyObject, AxStateTaskReference.class);
268
269         final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
270         copy.setKey(new AxReferenceKey(key));
271         copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
272         copy.setOutput(output);
273
274         return copy;
275     }
276
277     /**
278      * {@inheritDoc}.
279      */
280     @Override
281     public int hashCode() {
282         final int prime = 31;
283         int result = 1;
284         result = prime * result + key.hashCode();
285         result = prime * result + outputType.hashCode();
286         result = prime * result + output.hashCode();
287         return result;
288     }
289
290     /**
291      * {@inheritDoc}.
292      */
293     @Override
294     public boolean equals(final Object obj) {
295         if (obj == null) {
296             return false;
297         }
298         if (this == obj) {
299             return true;
300         }
301         if (getClass() != obj.getClass()) {
302             return false;
303         }
304
305         final AxStateTaskReference other = (AxStateTaskReference) obj;
306         if (!key.equals(other.key)) {
307             return false;
308         }
309         if (outputType != other.outputType) {
310             return false;
311         }
312         return output.equals(other.output);
313     }
314
315     /**
316      * {@inheritDoc}.
317      */
318     @Override
319     public int compareTo(final AxConcept otherObj) {
320         if (otherObj == null) {
321             return -1;
322         }
323         if (this == otherObj) {
324             return 0;
325         }
326         if (getClass() != otherObj.getClass()) {
327             return this.hashCode() - otherObj.hashCode();
328         }
329
330         final AxStateTaskReference other = (AxStateTaskReference) otherObj;
331         if (!key.equals(other.key)) {
332             return key.compareTo(other.key);
333         }
334         if (!outputType.equals(other.outputType)) {
335             return outputType.compareTo(other.outputType);
336         }
337         return output.compareTo(other.output);
338     }
339 }