Changes for checkstyle 8.32
[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 / AvroBytesObjectMapper.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.nio.ByteBuffer;
25 import org.apache.avro.Schema;
26 import org.apache.avro.Schema.Type;
27 import org.onap.policy.apex.context.ContextRuntimeException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * This class does string mapping from the Avro BYTES type to a Java byte array.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class AvroBytesObjectMapper implements AvroObjectMapper {
38     // Get a reference to the logger
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroBytesObjectMapper.class);
40
41     // The user keyAvro type for direct mapping
42     private AxKey userKey;
43     private Type avroType;
44
45     // The Apex compatible class
46     private static final Class<Byte[]> schemaClass = Byte[].class;
47
48     /**
49      * {@inheritDoc}.
50      */
51     @Override
52     public Class<?> getJavaClass() {
53         return schemaClass;
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     public void init(final AxKey intUserKey, final Type initAvroType) {
61         this.userKey = intUserKey;
62         this.avroType = initAvroType;
63     }
64
65     /**
66      * {@inheritDoc}.
67      */
68     @Override
69     public Object createNewInstance(final Schema avroSchema) {
70         // By default, we do not create an instance, normal Java object creation for byte arrays is
71         // sufficient
72         return null;
73     }
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public Type getAvroType() {
80         return avroType;
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public Object mapFromAvro(final Object avroObject) {
88         // The Avro object should be a Utf8 object
89         if (!(avroObject instanceof ByteBuffer)) {
90             final String returnString = userKey.getId() + ": object \"" + avroObject + "\" of class \""
91                             + avroObject.getClass() + "\" cannot be decoded to an object of class \""
92                             + schemaClass.getName() + "\"";
93             LOGGER.warn(returnString);
94             throw new ContextRuntimeException(returnString);
95         }
96
97         // Cast the byte buffer object so we get access to its methods
98         final ByteBuffer byteBufferAvroObject = (ByteBuffer) avroObject;
99
100         // read the byte buffer into a byte array
101         final byte[] byteArray = new byte[byteBufferAvroObject.remaining()];
102         byteBufferAvroObject.get(byteArray);
103
104         return byteArray;
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public Object mapToAvro(final Object object) {
112         if (object == null) {
113             final String returnString = userKey.getId() + ": cannot encode a null object of class \""
114                             + schemaClass.getName() + "\"";
115             LOGGER.warn(returnString);
116             throw new ContextRuntimeException(returnString);
117         }
118
119         // The incoming object should be a byte array
120         if (!(object instanceof byte[])) {
121             final String returnString = userKey.getId() + ": object \"" + object + "\" of class \"" + object.getClass()
122                             + "\" cannot be decoded to an object of class \"" + schemaClass.getName() + "\"";
123             LOGGER.warn(returnString);
124             throw new ContextRuntimeException(returnString);
125         }
126
127         // Create a ByteBuffer object to serialize the bytes
128         return ByteBuffer.wrap((byte[]) object);
129     }
130 }