c3f9d3329385dc6246e90caa1e3d67efb6bfea62
[policy/apex-pdp.git] / model / event-model / src / main / java / org / onap / policy / apex / model / eventmodel / concepts / AxField.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.eventmodel.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.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import javax.xml.bind.annotation.XmlAccessType;
36 import javax.xml.bind.annotation.XmlAccessorType;
37 import javax.xml.bind.annotation.XmlElement;
38 import javax.xml.bind.annotation.XmlRootElement;
39 import javax.xml.bind.annotation.XmlType;
40 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
41
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
50 import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
51 import org.onap.policy.common.utils.validation.Assertions;
52
53 /**
54  * In Apex, a field is an input or output parameter to or from a concept. For example, the parameters of an event are
55  * fields and the input and output of a task is defined as a collection of fields.
56  *
57  * <p>A field has an {@link AxReferenceKey} key that defines its name and parent, and a {@link AxArtifactKey} key to a
58  * context schema that defines the structure of the data atom that holds the value of the field. Fields can be specified
59  * as being optional but are mandatory by default.
60  *
61  * <p>Validation checks that the field key and the field schema reference key are not null.
62  */
63 @Entity
64 @Table(name = "AxField")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
66
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlRootElement(name = "apexField", namespace = "http://www.onap.org/policy/apex-pdp")
69 @XmlType(name = "AxField", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
70     { "key", "fieldSchemaKey", "optional" })
71
72 public class AxField extends AxConcept {
73     private static final String KEY_MAY_NOT_BE_NULL = "key may not be null";
74     private static final String FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL = "fieldSchemaKey may not be null";
75
76     private static final long serialVersionUID = -6443016863162692288L;
77
78     private static final int HASH_PRIME_0 = 1231;
79     private static final int HASH_PRIME_1 = 1237;
80
81     @EmbeddedId()
82     @XmlElement(name = "key", required = true)
83     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
84     private AxReferenceKey key;
85
86     // @formatter:off
87     @Embedded
88     @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "fieldSchemaName")),
89             @AttributeOverride(name = "version", column = @Column(name = "fieldSchemaVersion")) })
90     @Column(name = "fieldSchemaKey")
91     @XmlElement(required = true)
92     private AxArtifactKey fieldSchemaKey;
93     // @formatter:on
94
95     @Column(name = "optional")
96     @XmlElement(required = false)
97     private boolean optional;
98
99     /**
100      * The default constructor creates a field with a null artifact and schema key.
101      */
102     public AxField() {
103         this(new AxReferenceKey());
104         optional = false;
105     }
106
107     /**
108      * The default constructor creates a field with the given artifact key and a null schema key.
109      *
110      * @param key the field key
111      */
112     public AxField(final AxReferenceKey key) {
113         this(key, new AxArtifactKey());
114     }
115
116     /**
117      * Copy constructor.
118      *
119      * @param copyConcept the concept to copy from
120      */
121     public AxField(final AxField copyConcept) {
122         super(copyConcept);
123     }
124
125     /**
126      * Constructor to create the field with both its keys defined.
127      *
128      * @param key the field key
129      * @param fieldSchemaKey the key of the field schema to use for this field
130      */
131     public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey) {
132         super();
133         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
134         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
135
136         this.key = key;
137         this.fieldSchemaKey = fieldSchemaKey;
138     }
139
140     /**
141      * Constructor to create the field with all its fields defined.
142      *
143      * @param key the field key
144      * @param fieldSchemaKey the key of the field schema to use for this field
145      * @param optional true if this field is optional
146      */
147     public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey, final boolean optional) {
148         super();
149         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
150         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
151
152         this.key = key;
153         this.fieldSchemaKey = fieldSchemaKey;
154         this.optional = optional;
155     }
156
157     /**
158      * Constructor to create the field with the local name of its reference key defined and its schema key defined.
159      *
160      * @param localName the local name of the field reference key
161      * @param fieldSchemaKey the key of the field schema to use for this field
162      */
163     public AxField(final String localName, final AxArtifactKey fieldSchemaKey) {
164         super();
165         Assertions.argumentNotNull(localName, "localName may not be null");
166         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
167
168         key = new AxReferenceKey();
169         key.setLocalName(localName);
170         this.fieldSchemaKey = fieldSchemaKey;
171     }
172
173     /**
174      * Constructor to create the field with the local name of its reference key defined, its schema key and optionality
175      * defined.
176      *
177      * @param localName the local name of the field reference key
178      * @param fieldSchemaKey the key of the field schema to use for this field
179      * @param optional true if this field is optional
180      */
181     public AxField(final String localName, final AxArtifactKey fieldSchemaKey, final boolean optional) {
182         super();
183         Assertions.argumentNotNull(localName, "localName may not be null");
184         Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
185
186         key = new AxReferenceKey();
187         key.setLocalName(localName);
188         this.fieldSchemaKey = fieldSchemaKey;
189         this.optional = optional;
190     }
191
192     /**
193      * {@inheritDoc}.
194      */
195     @Override
196     public AxReferenceKey getKey() {
197         return key;
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public List<AxKey> getKeys() {
205         final List<AxKey> keyList = key.getKeys();
206         keyList.add(new AxKeyUse(fieldSchemaKey));
207         return keyList;
208     }
209
210     /**
211      * Sets the reference key of the field.
212      *
213      * @param key the field reference key
214      */
215     public void setKey(final AxReferenceKey key) {
216         Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
217         this.key = key;
218     }
219
220     /**
221      * Gets the key of the field schema.
222      *
223      * @return the field schema key
224      */
225     public AxArtifactKey getSchema() {
226         return fieldSchemaKey;
227     }
228
229     /**
230      * Sets the key of the field schema.
231      *
232      * @param schema the field schema key
233      */
234     public void setSchema(final AxArtifactKey schema) {
235         Assertions.argumentNotNull(schema, "schema may not be null");
236         this.fieldSchemaKey = schema;
237     }
238
239     /**
240      * Gets the optionality of the field.
241      *
242      * @return the field optional flag
243      */
244     public boolean getOptional() {
245         return optional;
246     }
247
248     /**
249      * Sets the optionality of the field.
250      *
251      * @param optional the optionality of the field
252      */
253     public void setOptional(final boolean optional) {
254         this.optional = optional;
255     }
256
257     /**
258      * {@inheritDoc}.
259      */
260     @Override
261     public AxValidationResult validate(final AxValidationResult resultIn) {
262         AxValidationResult result = resultIn;
263
264         if (key.equals(AxReferenceKey.getNullKey())) {
265             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266                             "key is a null key"));
267         }
268
269         result = key.validate(result);
270
271         if (fieldSchemaKey.equals(AxArtifactKey.getNullKey())) {
272             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
273                             "fieldSchemaKey is a null key: " + fieldSchemaKey));
274         }
275         return fieldSchemaKey.validate(result);
276     }
277
278     /**
279      * {@inheritDoc}.
280      */
281     @Override
282     public void clean() {
283         key.clean();
284         fieldSchemaKey.clean();
285     }
286
287     /**
288      * {@inheritDoc}.
289      */
290     @Override
291     public String toString() {
292         final StringBuilder builder = new StringBuilder();
293         builder.append(this.getClass().getSimpleName());
294         builder.append(":(");
295         builder.append("key=");
296         builder.append(key);
297         builder.append(",fieldSchemaKey=");
298         builder.append(fieldSchemaKey);
299         builder.append(",optional=");
300         builder.append(optional);
301         builder.append(")");
302         return builder.toString();
303     }
304
305     /**
306      * {@inheritDoc}.
307      */
308     @Override
309     public AxConcept copyTo(final AxConcept targetObject) {
310         Assertions.argumentNotNull(targetObject, "target may not be null");
311
312         final Object copyObject = targetObject;
313         Assertions.instanceOf(copyObject, AxField.class);
314
315         final AxField copy = ((AxField) copyObject);
316         copy.setKey(new AxReferenceKey(key));
317         copy.setSchema(new AxArtifactKey(fieldSchemaKey));
318         copy.setOptional(optional);
319         return copy;
320     }
321
322     /**
323      * {@inheritDoc}.
324      */
325     @Override
326     public int hashCode() {
327         final int prime = 31;
328         int result = 1;
329         result = prime * result + key.hashCode();
330         result = prime * result + fieldSchemaKey.hashCode();
331         result = prime * result + (optional ? HASH_PRIME_0 : HASH_PRIME_1);
332         return result;
333     }
334
335     /*
336      * (nonJavadoc)
337      *
338      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
339      */
340     @Override
341     public boolean equals(final Object obj) {
342         if (obj == null) {
343             return false;
344         }
345         if (this == obj) {
346             return true;
347         }
348
349         if (!(obj instanceof AxField)) {
350             return false;
351         }
352
353         final AxField other = (AxField) obj;
354         if (!key.getLocalName().equals(other.key.getLocalName())) {
355             return false;
356         }
357         if (optional != other.optional) {
358             return false;
359         }
360         return fieldSchemaKey.equals(other.fieldSchemaKey);
361     }
362
363     /**
364      * {@inheritDoc}.
365      */
366     @Override
367     public int compareTo(final AxConcept otherObj) {
368         if (otherObj == null) {
369             return 1;
370         }
371         if (this == otherObj) {
372             return 0;
373         }
374         if (!(otherObj instanceof AxField)) {
375             return this.hashCode() - otherObj.hashCode();
376         }
377
378         final AxField other = (AxField) otherObj;
379         if (!key.getLocalName().equals(other.key.getLocalName())) {
380             return key.getLocalName().compareTo(other.key.getLocalName());
381         }
382         if (optional != other.optional) {
383             return (optional ? 1 : -1);
384         }
385         return fieldSchemaKey.compareTo(other.fieldSchemaKey);
386     }
387 }