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