2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.Convert;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Table;
31 import javax.xml.bind.annotation.XmlAccessType;
32 import javax.xml.bind.annotation.XmlAccessorType;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
35 import javax.xml.bind.annotation.XmlType;
36 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
37 import lombok.AccessLevel;
39 import lombok.ToString;
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.AxValidationMessage;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
46 import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner;
47 import org.onap.policy.common.utils.validation.Assertions;
50 * This class holds a data schema definition in Apex. A data schema describes the structure of a single atom of data
51 * handled by Apex. This atom of data can be a primitive type such as an integer or a string, or it can be a more
52 * complex data type such as a Java object or an object described using a data definition language such as Avro. The
53 * schema flavour defines the type of schema being defined and the schema itself defines the schema. The schema flavour
54 * is used by Apex to look up and load a plugin class that understands and interprets the schema definition and can
55 * create instances of classes for the schema.
57 * <p>An {@link AxContextSchema} is used to define each parameter in Apex events, the messages that enter, exit, and are
58 * passed internally in Apex. In addition, an Apex {@link AxContextAlbum} instances hold a map of
59 * {@link AxContextSchema} instances to represent the context being managed as an {@link AxContextAlbum}. For example,
60 * the state of all cells in a mobile network might be represented as an {@link AxContextAlbum} with its
61 * {@link AxContextSchema} being defined as @code cell} objects.
63 * <p>Validation checks that the schema key is not null. It also checks that the schema flavour is defined and matches
64 * the regular expression SCHEMA_FLAVOUR_REGEXP. Finally, validation checks that the defined schema is not a blank or
68 @Table(name = "AxContextSchema")
73 @XmlAccessorType(XmlAccessType.FIELD)
74 @XmlRootElement(name = "apexContextSchema", namespace = "http://www.onap.org/policy/apex-pdp")
75 @XmlType(name = "AxContextSchema", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
76 { "key", "schemaFlavour", "schemaDefinition" })
78 public class AxContextSchema extends AxConcept {
79 private static final String SCHEMA_FLAVOUR = "schemaFlavour";
80 private static final String WHITESPACE_REGEXP = "\\s+$";
82 private static final long serialVersionUID = -6443016863162692288L;
84 /** Regular expression that constrains what values a schema flavour can have. */
85 public static final String SCHEMA_FLAVOUR_REGEXP = "[A-Za-z0-9\\-_]+";
87 /** An undefined schema flavour has this value. */
88 public static final String SCHEMA_FLAVOUR_UNDEFINED = "UNDEFINED";
90 /** The maximum permissible size of a schema definition. */
91 public static final int MAX_SCHEMA_SIZE = 32672; // The maximum size supported by Apache Derby
94 @XmlElement(name = "key", required = true)
95 private AxArtifactKey key;
97 @Column(name = SCHEMA_FLAVOUR)
98 @XmlElement(required = true)
99 private String schemaFlavour;
101 @Column(name = "schemaDefinition", length = MAX_SCHEMA_SIZE)
102 @Convert(converter = CDataConditioner.class)
103 @XmlJavaTypeAdapter(value = CDataConditioner.class)
104 @XmlElement(name = "schemaDefinition", required = true)
105 @Getter(AccessLevel.NONE)
106 private String schemaDefinition;
109 * The default constructor creates a context schema with a null artifact key. The flavour of the context album is
110 * set as SCHEMA_FLAVOUR_UNDEFINED and the schema itself is defined as an empty string.
112 public AxContextSchema() {
113 this(new AxArtifactKey());
114 schemaFlavour = SCHEMA_FLAVOUR_UNDEFINED;
120 * @param copyConcept the concept to copy from
122 public AxContextSchema(final AxContextSchema copyConcept) {
127 * The key constructor creates a context schema with the given artifact key. The flavour of the context album is set
128 * as SCHEMA_FLAVOUR_UNDEFINED and the schema itself is defined as an empty string.
132 public AxContextSchema(final AxArtifactKey key) {
133 this(key, SCHEMA_FLAVOUR_UNDEFINED, "");
137 * This Constructor creates a context schema with all of its fields defined.
140 * @param schemaFlavour the schema flavour
141 * @param schemaDefinition the schema definition
143 public AxContextSchema(final AxArtifactKey key, final String schemaFlavour, final String schemaDefinition) {
145 Assertions.argumentNotNull(key, "key may not be null");
146 Assertions.argumentNotNull(schemaFlavour, "schemaFlavour may not be null");
147 Assertions.argumentNotNull(schemaDefinition, "schemaDefinition may not be null");
150 this.schemaFlavour = Assertions.validateStringParameter(SCHEMA_FLAVOUR, schemaFlavour, SCHEMA_FLAVOUR_REGEXP);
151 this.schemaDefinition = schemaDefinition.replaceAll(WHITESPACE_REGEXP, "");
158 public List<AxKey> getKeys() {
159 return key.getKeys();
163 * Sets the key of the context schema.
165 * @param key the key of the context schema
167 public void setKey(final AxArtifactKey key) {
168 Assertions.argumentNotNull(key, "key may not be null");
173 * Sets the schema flavour, which defines the type of schema definition being used.
175 * @param schemaFlavour the schema flavour
177 public void setSchemaFlavour(final String schemaFlavour) {
178 this.schemaFlavour = Assertions.validateStringParameter(SCHEMA_FLAVOUR, schemaFlavour, SCHEMA_FLAVOUR_REGEXP);
182 * Gets the schema, which defines the structure of this data schema atom.
184 * @return the schema definition
186 public String getSchema() {
187 return schemaDefinition;
191 * Sets the schema, which defines the structure of this data schema atom.
193 * @param schema the schema definition
195 public void setSchema(final String schema) {
196 Assertions.argumentNotNull(schema, "schema may not be null");
197 this.schemaDefinition = schema.replaceAll(WHITESPACE_REGEXP, "");
204 public AxValidationResult validate(final AxValidationResult resultIn) {
205 AxValidationResult result = resultIn;
207 if (key.equals(AxArtifactKey.getNullKey())) {
208 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "key is a null key"));
212 result = key.validate(result);
214 if (schemaFlavour.replaceAll(WHITESPACE_REGEXP, "").length() == 0
215 || schemaFlavour.equals(SCHEMA_FLAVOUR_UNDEFINED)) {
216 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
217 "schema flavour is not defined"));
220 var flavourValidationResult = Assertions.getStringParameterValidationMessage(SCHEMA_FLAVOUR, schemaFlavour,
221 SCHEMA_FLAVOUR_REGEXP);
222 if (flavourValidationResult != null) {
223 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
224 "schema flavour invalid-" + flavourValidationResult));
227 if (schemaDefinition.replaceAll(WHITESPACE_REGEXP, "").length() == 0) {
228 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
229 "no schemaDefinition specified, schemaDefinition may not be blank"));
239 public void clean() {
241 schemaFlavour = Assertions.validateStringParameter(SCHEMA_FLAVOUR, schemaFlavour, SCHEMA_FLAVOUR_REGEXP);
242 schemaDefinition = schemaDefinition.replaceAll(WHITESPACE_REGEXP, "");
249 public AxConcept copyTo(final AxConcept target) {
250 Assertions.argumentNotNull(target, "target may not be null");
252 final Object copyObject = target;
253 Assertions.instanceOf(copyObject, AxContextSchema.class);
255 final AxContextSchema copy = ((AxContextSchema) copyObject);
256 copy.setKey(new AxArtifactKey(key));
257 copy.setSchemaFlavour(schemaFlavour);
258 copy.setSchema(schemaDefinition);
267 public int hashCode() {
268 final var prime = 31;
270 result = prime * result + key.hashCode();
271 result = prime * result + schemaFlavour.hashCode();
273 final String thisSchema = CDataConditioner.clean(schemaDefinition).replace("\n", "");
274 result = prime * result + thisSchema.hashCode();
282 public boolean equals(final Object obj) {
290 if (getClass() != obj.getClass()) {
294 final AxContextSchema other = (AxContextSchema) obj;
296 if (!key.equals(other.key)) {
299 if (!schemaFlavour.equals(other.schemaFlavour)) {
302 final String thisSchema = CDataConditioner.clean(schemaDefinition).replace("\n", "");
303 final String otherSchema = CDataConditioner.clean(other.schemaDefinition).replace("\n", "");
304 return thisSchema.equals(otherSchema);
311 public int compareTo(final AxConcept otherObj) {
312 if (otherObj == null) {
315 if (this == otherObj) {
318 if (getClass() != otherObj.getClass()) {
319 return this.hashCode() - otherObj.hashCode();
322 final AxContextSchema other = (AxContextSchema) otherObj;
323 if (!key.equals(other.key)) {
324 return key.compareTo(other.key);
326 if (!schemaFlavour.equals(other.schemaFlavour)) {
327 return schemaFlavour.compareTo(other.schemaFlavour);
329 final String thisSchema = CDataConditioner.clean(schemaDefinition).replace("\n", "");
330 final String otherSchema = CDataConditioner.clean(other.schemaDefinition).replace("\n", "");
331 return thisSchema.compareTo(otherSchema);