2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 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.plugins.context.schema.avro;
24 import java.util.HashMap;
26 import java.util.TreeMap;
27 import org.apache.avro.Schema;
28 import org.apache.avro.Schema.Type;
29 import org.apache.avro.generic.GenericData;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class does direct mapping from Avro classes to Java classes, used for Avro primitive types that directly produce
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class AvroDirectObjectMapper implements AvroObjectMapper {
42 // Get a reference to the logger
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroDirectObjectMapper.class);
45 // Map for Avro primitive types to Java primitive types
46 private static final Map<Schema.Type, Class<?>> AVRO_JAVA_TYPE_MAP = new TreeMap<>();
49 // Initialize the mapping
51 AVRO_JAVA_TYPE_MAP.put(Schema.Type.ARRAY, GenericData.Array.class);
52 AVRO_JAVA_TYPE_MAP.put(Schema.Type.BOOLEAN, Boolean.class);
53 AVRO_JAVA_TYPE_MAP.put(Schema.Type.DOUBLE, Double.class);
54 AVRO_JAVA_TYPE_MAP.put(Schema.Type.ENUM, GenericData.EnumSymbol.class);
55 AVRO_JAVA_TYPE_MAP.put(Schema.Type.FIXED, GenericData.Fixed.class);
56 AVRO_JAVA_TYPE_MAP.put(Schema.Type.FLOAT, Float.class);
57 AVRO_JAVA_TYPE_MAP.put(Schema.Type.INT, Integer.class);
58 AVRO_JAVA_TYPE_MAP.put(Schema.Type.LONG, Long.class);
59 AVRO_JAVA_TYPE_MAP.put(Schema.Type.MAP, HashMap.class);
60 AVRO_JAVA_TYPE_MAP.put(Schema.Type.NULL, null);
61 AVRO_JAVA_TYPE_MAP.put(Schema.Type.RECORD, GenericData.Record.class);
65 // The user keyAvro type for direct mapping
66 private AxKey userKey;
67 private Type avroType;
69 // The Apex compatible class
70 private Class<?> schemaClass;
76 public Class<?> getJavaClass() {
84 public void init(final AxKey initUserKey, final Type initAvroType) {
85 this.userKey = initUserKey;
86 this.avroType = initAvroType;
87 schemaClass = AVRO_JAVA_TYPE_MAP.get(avroType);
94 public Object createNewInstance(final Schema avroSchema) {
95 // By default, we do not create an instance, normal Java object creation is sufficient
103 public Type getAvroType() {
111 public Object mapFromAvro(final Object avroObject) {
112 // Always return null if the schema is a null schema
113 if (schemaClass == null) {
117 // It is legal for the schema class to be null, if the Avro schema has a "null" type then
118 // the decoded object is always returned as a null
119 if (!schemaClass.isAssignableFrom(avroObject.getClass())) {
120 final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
121 + avroObject.getClass() + "\" cannot be decoded to an object of class \""
122 + schemaClass.getName() + "\"";
123 LOGGER.warn(returnString);
124 throw new ContextRuntimeException(returnString);
134 public Object mapToAvro(final Object object) {
135 // Null values are only allowed if the schema class is null
136 if (object == null && schemaClass != null) {
137 final var returnString = userKey.getId() + ": cannot encode a null object of class \""
138 + schemaClass.getName() + "\"";
139 LOGGER.warn(returnString);
140 throw new ContextRuntimeException(returnString);
143 // For direct mappings, just work directly with the Java objects