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