Use lombok in apex-pdp #3
[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  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.context.schema.avro;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import lombok.Getter;
29 import org.apache.avro.Schema;
30 import org.apache.avro.Schema.Type;
31 import org.apache.avro.generic.GenericData;
32 import org.onap.policy.apex.context.ContextRuntimeException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * This class does direct mapping from Avro classes to Java classes, used for Avro primitive types that directly produce
39  * Java objects.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class AvroDirectObjectMapper implements AvroObjectMapper {
44     // Get a reference to the logger
45     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroDirectObjectMapper.class);
46
47     // Map for Avro primitive types to Java primitive types
48     private static final Map<Schema.Type, Class<?>> AVRO_JAVA_TYPE_MAP = new TreeMap<>();
49
50     // @formatter:off
51     // Initialize the mapping
52     static {
53         AVRO_JAVA_TYPE_MAP.put(Schema.Type.ARRAY,   GenericData.Array.class);
54         AVRO_JAVA_TYPE_MAP.put(Schema.Type.BOOLEAN, Boolean.class);
55         AVRO_JAVA_TYPE_MAP.put(Schema.Type.DOUBLE,  Double.class);
56         AVRO_JAVA_TYPE_MAP.put(Schema.Type.ENUM,    GenericData.EnumSymbol.class);
57         AVRO_JAVA_TYPE_MAP.put(Schema.Type.FIXED,   GenericData.Fixed.class);
58         AVRO_JAVA_TYPE_MAP.put(Schema.Type.FLOAT,   Float.class);
59         AVRO_JAVA_TYPE_MAP.put(Schema.Type.INT,     Integer.class);
60         AVRO_JAVA_TYPE_MAP.put(Schema.Type.LONG,    Long.class);
61         AVRO_JAVA_TYPE_MAP.put(Schema.Type.MAP,     HashMap.class);
62         AVRO_JAVA_TYPE_MAP.put(Schema.Type.NULL,    null);
63         AVRO_JAVA_TYPE_MAP.put(Schema.Type.RECORD,  GenericData.Record.class);
64     }
65     // @formatter:on
66
67     // The user keyAvro type for direct mapping
68     private AxKey userKey;
69     @Getter
70     private Type avroType;
71
72     // The Apex compatible class
73     private Class<?> schemaClass;
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public Class<?> getJavaClass() {
80         return schemaClass;
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public void init(final AxKey initUserKey, final Type initAvroType) {
88         this.userKey = initUserKey;
89         this.avroType = initAvroType;
90         schemaClass = AVRO_JAVA_TYPE_MAP.get(avroType);
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public Object createNewInstance(final Schema avroSchema) {
98         // By default, we do not create an instance, normal Java object creation is sufficient
99         return null;
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public Object mapFromAvro(final Object avroObject) {
107         // Always return null if the schema is a null schema
108         if (schemaClass == null) {
109             return null;
110         }
111
112         // It is legal for the schema class to be null, if the Avro schema has a "null" type then
113         // the decoded object is always returned as a null
114         if (!schemaClass.isAssignableFrom(avroObject.getClass())) {
115             final var returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
116                             + avroObject.getClass() + "\" cannot be decoded to an object of class \""
117                             + schemaClass.getName() + "\"";
118             LOGGER.warn(returnString);
119             throw new ContextRuntimeException(returnString);
120         }
121
122         return avroObject;
123     }
124
125     /**
126      * {@inheritDoc}.
127      */
128     @Override
129     public Object mapToAvro(final Object object) {
130         // Null values are only allowed if the schema class is null
131         if (object == null && schemaClass != null) {
132             final var returnString = userKey.getId() + ": cannot encode a null object of class \""
133                             + schemaClass.getName() + "\"";
134             LOGGER.warn(returnString);
135             throw new ContextRuntimeException(returnString);
136         }
137
138         // For direct mappings, just work directly with the Java objects
139         return object;
140     }
141 }