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.plugins.context.schema.avro;
25 import java.util.HashMap;
27 import java.util.TreeMap;
29 import org.apache.avro.Schema;
30 import org.apache.avro.Schema.Type;
31 import org.apache.avro.generic.GenericData;
32 import org.onap.policy.apex.context.ContextRuntimeException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * This class does direct mapping from Avro classes to Java classes, used for Avro primitive types that directly produce
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class AvroDirectObjectMapper implements AvroObjectMapper {
44 // Get a reference to the logger
45 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroDirectObjectMapper.class);
47 // Map for Avro primitive types to Java primitive types
48 private static final Map<Schema.Type, Class<?>> AVRO_JAVA_TYPE_MAP = new TreeMap<>();
51 // Initialize the mapping
53 AVRO_JAVA_TYPE_MAP.put(Schema.Type.ARRAY, GenericData.Array.class);
54 AVRO_JAVA_TYPE_MAP.put(Schema.Type.BOOLEAN, Boolean.class);
55 AVRO_JAVA_TYPE_MAP.put(Schema.Type.DOUBLE, Double.class);
56 AVRO_JAVA_TYPE_MAP.put(Schema.Type.ENUM, GenericData.EnumSymbol.class);
57 AVRO_JAVA_TYPE_MAP.put(Schema.Type.FIXED, GenericData.Fixed.class);
58 AVRO_JAVA_TYPE_MAP.put(Schema.Type.FLOAT, Float.class);
59 AVRO_JAVA_TYPE_MAP.put(Schema.Type.INT, Integer.class);
60 AVRO_JAVA_TYPE_MAP.put(Schema.Type.LONG, Long.class);
61 AVRO_JAVA_TYPE_MAP.put(Schema.Type.MAP, HashMap.class);
62 AVRO_JAVA_TYPE_MAP.put(Schema.Type.NULL, null);
63 AVRO_JAVA_TYPE_MAP.put(Schema.Type.RECORD, GenericData.Record.class);
67 // The user keyAvro type for direct mapping
68 private AxKey userKey;
70 private Type avroType;
72 // The Apex compatible class
73 private Class<?> schemaClass;
79 public Class<?> getJavaClass() {
87 public void init(final AxKey initUserKey, final Type initAvroType) {
88 this.userKey = initUserKey;
89 this.avroType = initAvroType;
90 schemaClass = AVRO_JAVA_TYPE_MAP.get(avroType);
97 public Object createNewInstance(final Schema avroSchema) {
98 // By default, we do not create an instance, normal Java object creation is sufficient
106 public Object mapFromAvro(final Object avroObject) {
107 // Always return null if the schema is a null schema
108 if (schemaClass == null) {
112 // It is legal for the schema class to be null, if the Avro schema has a "null" type then
113 // the decoded object is always returned as a null
114 if (!schemaClass.isAssignableFrom(avroObject.getClass())) {
115 final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
116 + avroObject.getClass() + "\" cannot be decoded to an object of class \""
117 + schemaClass.getName() + "\"";
118 LOGGER.warn(returnString);
119 throw new ContextRuntimeException(returnString);
129 public Object mapToAvro(final Object object) {
130 // Null values are only allowed if the schema class is null
131 if (object == null && schemaClass != null) {
132 final var returnString = userKey.getId() + ": cannot encode a null object of class \""
133 + schemaClass.getName() + "\"";
134 LOGGER.warn(returnString);
135 throw new ContextRuntimeException(returnString);
138 // For direct mappings, just work directly with the Java objects