13c21b65ebc553cacb457b612116a79db4b71648
[policy/apex-pdp.git] /
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      * (non-Javadoc)
174      *
175      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
176      */
177     @Override
178     public AxReferenceKey getKey() {
179         return key;
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
186      */
187     @Override
188     public List<AxKey> getKeys() {
189         final List<AxKey> keyList = key.getKeys();
190
191         if (!output.equals(AxReferenceKey.getNullKey())) {
192             keyList.add(new AxKeyUse(output));
193         }
194
195         return keyList;
196     }
197
198     /**
199      * Sets the key of the state task reference.
200      *
201      * @param key the key of the state task reference
202      */
203     public void setKey(final AxReferenceKey key) {
204         Assertions.argumentNotNull(key, "key may not be null");
205         this.key = key;
206     }
207
208     /**
209      * Gets the type of output to perform when this state task reference instance is used.
210      *
211      * @return the the type of output to perform when this state task reference instance is used
212      */
213     public AxStateTaskOutputType getStateTaskOutputType() {
214         return outputType;
215     }
216
217     /**
218      * Sets the type of output to perform when this state task reference instance is used.
219      *
220      * @param stateTaskOutputType the type of output to perform when this state task reference
221      *        instance is used
222      */
223     public void setStateTaskOutputType(final AxStateTaskOutputType stateTaskOutputType) {
224         Assertions.argumentNotNull(stateTaskOutputType, "outputType may not be null");
225         this.outputType = stateTaskOutputType;
226     }
227
228     /**
229      * Gets the output to perform when this state task reference instance is used.
230      *
231      * @return the output to perform when this state task reference instance is used
232      */
233     public AxReferenceKey getOutput() {
234         return output;
235     }
236
237     /**
238      * Sets the output to perform when this state task reference instance is used.
239      *
240      * @param output the output to perform when this state task reference instance is used
241      */
242     public void setOutput(final AxReferenceKey output) {
243         Assertions.argumentNotNull(output, "output may not be null");
244         this.output = output;
245     }
246
247     /*
248      * (non-Javadoc)
249      *
250      * @see
251      * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
252      * basicmodel.concepts.AxValidationResult)
253      */
254     @Override
255     public AxValidationResult validate(final AxValidationResult resultIn) {
256         AxValidationResult result = resultIn;
257
258         if (key.equals(AxReferenceKey.getNullKey())) {
259             result.addValidationMessage(
260                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
261         }
262
263         result = key.validate(result);
264
265         if (outputType == AxStateTaskOutputType.UNDEFINED) {
266             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267                     "outputType may not be UNDEFINED"));
268         }
269
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"));
273         }
274         result = output.validate(result);
275
276         return result;
277     }
278
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
283      */
284     @Override
285     public void clean() {
286         key.clean();
287         output.clean();
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
294      */
295     @Override
296     public String toString() {
297         final StringBuilder builder = new StringBuilder();
298         builder.append(this.getClass().getSimpleName());
299         builder.append(":(");
300         builder.append("stateKey=");
301         builder.append(key);
302         builder.append(",outputType=");
303         builder.append(outputType);
304         builder.append(",output=");
305         builder.append(output);
306         builder.append(")");
307         return builder.toString();
308     }
309
310     /*
311      * (non-Javadoc)
312      *
313      * @see
314      * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
315      * basicmodel.concepts.AxConcept)
316      */
317     @Override
318     public AxConcept copyTo(final AxConcept targetObject) {
319         Assertions.argumentNotNull(targetObject, "target may not be null");
320
321         final Object copyObject = targetObject;
322         Assertions.instanceOf(copyObject, AxStateTaskReference.class);
323
324         final AxStateTaskReference copy = ((AxStateTaskReference) copyObject);
325         copy.setKey(new AxReferenceKey(key));
326         copy.setStateTaskOutputType(AxStateTaskOutputType.valueOf(outputType.name()));
327         copy.setOutput(output);
328
329         return copy;
330     }
331
332     /*
333      * (non-Javadoc)
334      *
335      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
336      */
337     @Override
338     public int hashCode() {
339         final int prime = 31;
340         int result = 1;
341         result = prime * result + key.hashCode();
342         result = prime * result + outputType.hashCode();
343         result = prime * result + output.hashCode();
344         return result;
345     }
346
347     /*
348      * (non-Javadoc)
349      *
350      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
351      */
352     @Override
353     public boolean equals(final Object obj) {
354         if (obj == null) {
355             return false;
356         }
357         if (this == obj) {
358             return true;
359         }
360         if (getClass() != obj.getClass()) {
361             return false;
362         }
363
364         final AxStateTaskReference other = (AxStateTaskReference) obj;
365         if (!key.equals(other.key)) {
366             return false;
367         }
368         if (outputType != other.outputType) {
369             return false;
370         }
371         return output.equals(other.output);
372     }
373
374     /*
375      * (non-Javadoc)
376      *
377      * @see java.lang.Comparable#compareTo(java.lang.Object)
378      */
379     @Override
380     public int compareTo(final AxConcept otherObj) {
381         if (otherObj == null) {
382             return -1;
383         }
384         if (this == otherObj) {
385             return 0;
386         }
387         if (getClass() != otherObj.getClass()) {
388             return this.hashCode() - otherObj.hashCode();
389         }
390
391         final AxStateTaskReference other = (AxStateTaskReference) otherObj;
392         if (!key.equals(other.key)) {
393             return key.compareTo(other.key);
394         }
395         if (!outputType.equals(other.outputType)) {
396             return outputType.compareTo(other.outputType);
397         }
398         return output.compareTo(other.output);
399     }
400 }