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