f9ec4ed8563e7a71badf549fcf8c5009eb69ba5c
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-schema / plugins-context-schema-avro / src / main / java / org / onap / policy / apex / plugins / context / schema / avro / AvroDirectObjectMapper.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.TreeMap;
27
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;
35
36 /**
37  * This class does direct mapping from Avro classes to Java classes, used for Avro primitive types that directly produce
38  * Java objects.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class AvroDirectObjectMapper implements AvroObjectMapper {
43     // Get a reference to the logger
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroDirectObjectMapper.class);
45
46     // Map for Avro primitive types to Java primitive types
47     private static final Map<Schema.Type, Class<?>> AVRO_JAVA_TYPE_MAP = new TreeMap<>();
48
49     // @formatter:off
50     // Initialize the mapping
51     static {
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);
63     }
64     // @formatter:on
65
66     // The user keyAvro type for direct mapping
67     private AxKey userKey;
68     private Type avroType;
69
70     // The Apex compatible class
71     private Class<?> schemaClass;
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public Class<?> getJavaClass() {
78         return schemaClass;
79     }
80
81     /**
82      * {@inheritDoc}.
83      */
84     @Override
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);
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public Object createNewInstance(final Schema avroSchema) {
96         // By default, we do not create an instance, normal Java object creation is sufficient
97         return null;
98     }
99
100     /**
101      * {@inheritDoc}.
102      */
103     @Override
104     public Type getAvroType() {
105         return avroType;
106     }
107
108     /**
109      * {@inheritDoc}.
110      */
111     @Override
112     public Object mapFromAvro(final Object avroObject) {
113         // Always return null if the schema is a null schema
114         if (schemaClass == null) {
115             return null;
116         }
117
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);
126         }
127
128         return avroObject;
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
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);
142         }
143
144         // For direct mappings, just work directly with the Java objects
145         return object;
146     }
147 }