Sonar Issues in Apex plugins-context
[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-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
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 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 that directly produce
37  * 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      * {@inheritDoc}.
74      */
75     @Override
76     public Class<?> getJavaClass() {
77         return schemaClass;
78     }
79
80     /**
81      * {@inheritDoc}.
82      */
83     @Override
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);
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
94     public Object createNewInstance(final Schema avroSchema) {
95         // By default, we do not create an instance, normal Java object creation is sufficient
96         return null;
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
103     public Type getAvroType() {
104         return avroType;
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public Object mapFromAvro(final Object avroObject) {
112         // Always return null if the schema is a null schema
113         if (schemaClass == null) {
114             return null;
115         }
116
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);
125         }
126
127         return avroObject;
128     }
129
130     /**
131      * {@inheritDoc}.
132      */
133     @Override
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);
141         }
142
143         // For direct mappings, just work directly with the Java objects
144         return object;
145     }
146 }