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;
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.Inheritance;
32 import javax.persistence.InheritanceType;
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 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
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.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
49 import org.onap.policy.common.utils.validation.Assertions;
52 * In Apex, a field is an input or output parameter to or from a concept. For example, the parameters of an event are
53 * fields and the input and output of a task is defined as a collection of fields.
55 * <p>A field has an {@link AxReferenceKey} key that defines its name and parent, and a {@link AxArtifactKey} key to a
56 * context schema that defines the structure of the data atom that holds the value of the field. Fields can be specified
57 * as being optional but are mandatory by default.
59 * <p>Validation checks that the field key and the field schema reference key are not null.
62 @Table(name = "AxField")
63 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlRootElement(name = "apexField", namespace = "http://www.onap.org/policy/apex-pdp")
67 @XmlType(name = "AxField", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
68 { "key", "fieldSchemaKey", "optional" })
70 public class AxField extends AxConcept {
71 private static final String KEY_MAY_NOT_BE_NULL = "key may not be null";
72 private static final String FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL = "fieldSchemaKey may not be null";
74 private static final long serialVersionUID = -6443016863162692288L;
76 private static final int HASH_PRIME_0 = 1231;
77 private static final int HASH_PRIME_1 = 1237;
80 @XmlElement(name = "key", required = true)
81 @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
82 private AxReferenceKey key;
86 @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "fieldSchemaName")),
87 @AttributeOverride(name = "version", column = @Column(name = "fieldSchemaVersion")) })
88 @Column(name = "fieldSchemaKey")
89 @XmlElement(required = true)
90 private AxArtifactKey fieldSchemaKey;
93 @Column(name = "optional")
94 @XmlElement(required = false)
95 private boolean optional;
98 * The default constructor creates a field with a null artifact and schema key.
101 this(new AxReferenceKey());
106 * The default constructor creates a field with the given artifact key and a null schema key.
108 * @param key the field key
110 public AxField(final AxReferenceKey key) {
111 this(key, new AxArtifactKey());
117 * @param copyConcept the concept to copy from
119 public AxField(final AxField copyConcept) {
124 * Constructor to create the field with both its keys defined.
126 * @param key the field key
127 * @param fieldSchemaKey the key of the field schema to use for this field
129 public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey) {
131 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
132 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
135 this.fieldSchemaKey = fieldSchemaKey;
139 * Constructor to create the field with all its fields defined.
141 * @param key the field key
142 * @param fieldSchemaKey the key of the field schema to use for this field
143 * @param optional true if this field is optional
145 public AxField(final AxReferenceKey key, final AxArtifactKey fieldSchemaKey, final boolean optional) {
147 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
148 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
151 this.fieldSchemaKey = fieldSchemaKey;
152 this.optional = optional;
156 * Constructor to create the field with the local name of its reference key defined and its schema key defined.
158 * @param localName the local name of the field reference key
159 * @param fieldSchemaKey the key of the field schema to use for this field
161 public AxField(final String localName, final AxArtifactKey fieldSchemaKey) {
163 Assertions.argumentNotNull(localName, "localName may not be null");
164 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
166 key = new AxReferenceKey();
167 key.setLocalName(localName);
168 this.fieldSchemaKey = fieldSchemaKey;
172 * Constructor to create the field with the local name of its reference key defined, its schema key and optionality
175 * @param localName the local name of the field reference key
176 * @param fieldSchemaKey the key of the field schema to use for this field
177 * @param optional true if this field is optional
179 public AxField(final String localName, final AxArtifactKey fieldSchemaKey, final boolean optional) {
181 Assertions.argumentNotNull(localName, "localName may not be null");
182 Assertions.argumentNotNull(fieldSchemaKey, FIELD_SCHEMA_KEY_MAY_NOT_BE_NULL);
184 key = new AxReferenceKey();
185 key.setLocalName(localName);
186 this.fieldSchemaKey = fieldSchemaKey;
187 this.optional = optional;
194 public AxReferenceKey getKey() {
202 public List<AxKey> getKeys() {
203 final List<AxKey> keyList = key.getKeys();
204 keyList.add(new AxKeyUse(fieldSchemaKey));
209 * Sets the reference key of the field.
211 * @param key the field reference key
213 public void setKey(final AxReferenceKey key) {
214 Assertions.argumentNotNull(key, KEY_MAY_NOT_BE_NULL);
219 * Gets the key of the field schema.
221 * @return the field schema key
223 public AxArtifactKey getSchema() {
224 return fieldSchemaKey;
228 * Sets the key of the field schema.
230 * @param schema the field schema key
232 public void setSchema(final AxArtifactKey schema) {
233 Assertions.argumentNotNull(schema, "schema may not be null");
234 this.fieldSchemaKey = schema;
238 * Gets the optionality of the field.
240 * @return the field optional flag
242 public boolean getOptional() {
247 * Sets the optionality of the field.
249 * @param optional the optionality of the field
251 public void setOptional(final boolean optional) {
252 this.optional = optional;
259 public AxValidationResult validate(final AxValidationResult resultIn) {
260 AxValidationResult result = resultIn;
262 if (key.equals(AxReferenceKey.getNullKey())) {
263 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264 "key is a null key"));
267 result = key.validate(result);
269 if (fieldSchemaKey.equals(AxArtifactKey.getNullKey())) {
270 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271 "fieldSchemaKey is a null key: " + fieldSchemaKey));
273 return fieldSchemaKey.validate(result);
280 public void clean() {
282 fieldSchemaKey.clean();
289 public String toString() {
290 final StringBuilder builder = new StringBuilder();
291 builder.append(this.getClass().getSimpleName());
292 builder.append(":(");
293 builder.append("key=");
295 builder.append(",fieldSchemaKey=");
296 builder.append(fieldSchemaKey);
297 builder.append(",optional=");
298 builder.append(optional);
300 return builder.toString();
307 public AxConcept copyTo(final AxConcept targetObject) {
308 Assertions.argumentNotNull(targetObject, "target may not be null");
310 final Object copyObject = targetObject;
311 Assertions.instanceOf(copyObject, AxField.class);
313 final AxField copy = ((AxField) copyObject);
314 copy.setKey(new AxReferenceKey(key));
315 copy.setSchema(new AxArtifactKey(fieldSchemaKey));
316 copy.setOptional(optional);
324 public int hashCode() {
325 final int prime = 31;
327 result = prime * result + key.hashCode();
328 result = prime * result + fieldSchemaKey.hashCode();
329 result = prime * result + (optional ? HASH_PRIME_0 : HASH_PRIME_1);
336 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
339 public boolean equals(final Object obj) {
347 if (!(obj instanceof AxField)) {
351 final AxField other = (AxField) obj;
352 if (!key.getLocalName().equals(other.key.getLocalName())) {
355 if (optional != other.optional) {
358 return fieldSchemaKey.equals(other.fieldSchemaKey);
365 public int compareTo(final AxConcept otherObj) {
366 if (otherObj == null) {
369 if (this == otherObj) {
372 if (!(otherObj instanceof AxField)) {
373 return this.hashCode() - otherObj.hashCode();
376 final AxField other = (AxField) otherObj;
377 if (!key.getLocalName().equals(other.key.getLocalName())) {
378 return key.getLocalName().compareTo(other.key.getLocalName());
380 if (optional != other.optional) {
381 return (optional ? 1 : -1);
383 return fieldSchemaKey.compareTo(other.fieldSchemaKey);