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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.eventmodel.concepts;
24 import java.util.List;
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;
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;
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.
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.
61 * <p>Validation checks that the field key and the field schema reference key are not null.
64 @Table(name = "AxField")
65 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
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" })
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";
76 private static final long serialVersionUID = -6443016863162692288L;
78 private static final int HASH_PRIME_0 = 1231;
79 private static final int HASH_PRIME_1 = 1237;
82 @XmlElement(name = "key", required = true)
83 @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
84 private AxReferenceKey key;
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;
95 @Column(name = "optional")
96 @XmlElement(required = false)
97 private boolean optional;
100 * The default constructor creates a field with a null artifact and schema key.
103 this(new AxReferenceKey());
108 * The default constructor creates a field with the given artifact key and a null schema key.
110 * @param key the field key
112 public AxField(final AxReferenceKey key) {
113 this(key, new AxArtifactKey());
119 * @param copyConcept the concept to copy from
121 public AxField(final AxField copyConcept) {
126 * Constructor to create the field with both its keys defined.
128 * @param key the field key
129 * @param fieldSchemaKey the key of the field schema to use for this field
131 public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey) {
133 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
134 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
137 this.fieldSchemaKey = fieldSchemaKey;
141 * Constructor to create the field with all its fields defined.
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
147 public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey, final boolean optional) {
149 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
150 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
153 this.fieldSchemaKey = fieldSchemaKey;
154 this.optional = optional;
158 * Constructor to create the field with the local name of its reference key defined and its schema key defined.
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
163 public AxField(final String localName, final AxArtifactKey fieldSchemaKey) {
165 Assertions.argumentNotNull(localName, "localName may not be null");
166 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
168 key = new AxReferenceKey();
169 key.setLocalName(localName);
170 this.fieldSchemaKey = fieldSchemaKey;
174 * Constructor to create the field with the local name of its reference key defined, its schema key and optionality
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
181 public AxField(final String localName, final AxArtifactKey fieldSchemaKey, final boolean optional) {
183 Assertions.argumentNotNull(localName, "localName may not be null");
184 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
186 key = new AxReferenceKey();
187 key.setLocalName(localName);
188 this.fieldSchemaKey = fieldSchemaKey;
189 this.optional = optional;
196 public AxReferenceKey getKey() {
204 public List<AxKey> getKeys() {
205 final List<AxKey> keyList = key.getKeys();
206 keyList.add(new AxKeyUse(fieldSchemaKey));
211 * Sets the reference key of the field.
213 * @param key the field reference key
215 public void setKey(final AxReferenceKey key) {
216 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
221 * Gets the key of the field schema.
223 * @return the field schema key
225 public AxArtifactKey getSchema() {
226 return fieldSchemaKey;
230 * Sets the key of the field schema.
232 * @param schema the field schema key
234 public void setSchema(final AxArtifactKey schema) {
235 Assertions.argumentNotNull(schema, "schema may not be null");
236 this.fieldSchemaKey = schema;
240 * Gets the optionality of the field.
242 * @return the field optional flag
244 public boolean getOptional() {
249 * Sets the optionality of the field.
251 * @param optional the optionality of the field
253 public void setOptional(final boolean optional) {
254 this.optional = optional;
261 public AxValidationResult validate(final AxValidationResult resultIn) {
262 AxValidationResult result = resultIn;
264 if (key.equals(AxReferenceKey.getNullKey())) {
265 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266 "key is a null key"));
269 result = key.validate(result);
271 if (fieldSchemaKey.equals(AxArtifactKey.getNullKey())) {
272 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
273 "fieldSchemaKey is a null key: " + fieldSchemaKey));
275 return fieldSchemaKey.validate(result);
282 public void clean() {
284 fieldSchemaKey.clean();
291 public String toString() {
292 final StringBuilder builder = new StringBuilder();
293 builder.append(this.getClass().getSimpleName());
294 builder.append(":(");
295 builder.append("key=");
297 builder.append(",fieldSchemaKey=");
298 builder.append(fieldSchemaKey);
299 builder.append(",optional=");
300 builder.append(optional);
302 return builder.toString();
309 public AxConcept copyTo(final AxConcept targetObject) {
310 Assertions.argumentNotNull(targetObject, "target may not be null");
312 final Object copyObject = targetObject;
313 Assertions.instanceOf(copyObject, AxField.class);
315 final AxField copy = ((AxField) copyObject);
316 copy.setKey(new AxReferenceKey(key));
317 copy.setSchema(new AxArtifactKey(fieldSchemaKey));
318 copy.setOptional(optional);
326 public int hashCode() {
327 final int prime = 31;
329 result = prime * result + key.hashCode();
330 result = prime * result + fieldSchemaKey.hashCode();
331 result = prime * result + (optional ? HASH_PRIME_0 : HASH_PRIME_1);
338 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
341 public boolean equals(final Object obj) {
349 if (!(obj instanceof AxField)) {
353 final AxField other = (AxField) obj;
354 if (!key.getLocalName().equals(other.key.getLocalName())) {
357 if (optional != other.optional) {
360 return fieldSchemaKey.equals(other.fieldSchemaKey);
367 public int compareTo(final AxConcept otherObj) {
368 if (otherObj == null) {
371 if (this == otherObj) {
374 if (!(otherObj instanceof AxField)) {
375 return this.hashCode() - otherObj.hashCode();
378 final AxField other = (AxField) otherObj;
379 if (!key.getLocalName().equals(other.key.getLocalName())) {
380 return key.getLocalName().compareTo(other.key.getLocalName());
382 if (optional != other.optional) {
383 return (optional ? 1 : -1);
385 return fieldSchemaKey.compareTo(other.fieldSchemaKey);