Merge "Fix incorrect naming on context plugins"
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.schema.avro;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.TreeMap;
26
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;
34
35 /**
36  * This class does direct mapping from Avro classes to Java classes, used for Avro primitive types
37  * that directly produce Java objects.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class AvroDirectObjectMapper implements AvroObjectMapper {
42     // Get a reference to the logger
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroDirectObjectMapper.class);
44
45     // Map for Avro primitive types to Java primitive types
46     private static final Map<Schema.Type, Class<?>> AVRO_JAVA_TYPE_MAP = new TreeMap<>();
47
48     // @formatter:off
49     // Initialize the mapping
50     static {
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);
62     }
63     // @formatter:on
64
65     // The user keyAvro type for direct mapping
66     private AxKey userKey;
67     private Type avroType;
68
69     // The Apex compatible class
70     private Class<?> schemaClass;
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#getJavaClass()
76      */
77     @Override
78     public Class<?> getJavaClass() {
79         return schemaClass;
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see
86      * org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#setAvroType(org.apache.
87      * avro. Schema.Type)
88      */
89     @Override
90     public void init(final AxKey initUserKey, final Type initAvroType) {
91         this.userKey = initUserKey;
92         this.avroType = initAvroType;
93         schemaClass = AVRO_JAVA_TYPE_MAP.get(avroType);
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#createNewinstance(org.
100      * apache. avro.Schema)
101      */
102     @Override
103     public Object createNewInstance(final Schema avroSchema) {
104         // By default, we do not create an instance, normal Java object creation is sufficient
105         return null;
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#getAvroType()
112      */
113     @Override
114     public Type getAvroType() {
115         return avroType;
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#mapFromAvro(java.lang.
122      * Object)
123      */
124     @Override
125     public Object mapFromAvro(final Object avroObject) {
126         // Always return null if the schema is a null schema
127         if (schemaClass == null) {
128             return null;
129         }
130
131         // It is legal for the schema class to be null, if the Avro schema has a "null" type then
132         // the decoded object is always returned as a null
133         if (!schemaClass.isAssignableFrom(avroObject.getClass())) {
134             final String returnString =
135                     userKey.getID() + ": object \"" + avroObject + "\" of class \"" + avroObject.getClass()
136                             + "\" cannot be decoded to an object of class \"" + schemaClass.getCanonicalName() + "\"";
137             LOGGER.warn(returnString);
138             throw new ContextRuntimeException(returnString);
139         }
140
141         return avroObject;
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see
148      * org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#mapToAvro(java.lang.Object)
149      */
150     @Override
151     public Object mapToAvro(final Object object) {
152         // Null values are only allowed if the schema class is null
153         if (object == null) {
154             if (schemaClass != null) {
155                 final String returnString = userKey.getID() + ": cannot encode a null object of class \""
156                         + schemaClass.getCanonicalName() + "\"";
157                 LOGGER.warn(returnString);
158                 throw new ContextRuntimeException(returnString);
159             }
160         }
161
162         // For direct mappings, just work directly with the Java objects
163         return object;
164     }
165 }