bee2f5957654af296872d54b7b971c2273f35522
[policy/apex-pdp.git] /
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.nio.ByteBuffer;
24
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      * (non-Javadoc)
50      *
51      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#getJavaClass()
52      */
53     @Override
54     public Class<?> getJavaClass() {
55         return schemaClass;
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see
62      * org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#setAvroType(org.apache.
63      * avro. Schema.Type)
64      */
65     @Override
66     public void init(final AxKey intUserKey, final Type initAvroType) {
67         this.userKey = intUserKey;
68         this.avroType = initAvroType;
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#createNewinstance(org.
75      * apache. avro.Schema)
76      */
77     @Override
78     public Object createNewInstance(final Schema avroSchema) {
79         // By default, we do not create an instance, normal Java object creation for byte arrays is
80         // sufficient
81         return null;
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#getAvroType()
88      */
89     @Override
90     public Type getAvroType() {
91         return avroType;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#mapFromAvro(java.lang.
98      * Object)
99      */
100     @Override
101     public Object mapFromAvro(final Object avroObject) {
102         // The Avro object should be a Utf8 object
103         if (!(avroObject instanceof ByteBuffer)) {
104             final String returnString =
105                     userKey.getId() + ": object \"" + avroObject + "\" of class \"" + avroObject.getClass()
106                             + "\" cannot be decoded to an object of class \"" + schemaClass.getCanonicalName() + "\"";
107             LOGGER.warn(returnString);
108             throw new ContextRuntimeException(returnString);
109         }
110
111         // Cast the byte buffer object so we get access to its methods
112         final ByteBuffer byteBufferAvroObject = (ByteBuffer) avroObject;
113
114         // read the byte buffer into a byte array
115         final byte[] byteArray = new byte[byteBufferAvroObject.remaining()];
116         byteBufferAvroObject.get(byteArray);
117
118         return byteArray;
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see
125      * org.onap.policy.apex.plugins.context.schema.avro.AvroObjectMapper#mapToAvro(java.lang.Object)
126      */
127     @Override
128     public Object mapToAvro(final Object object) {
129         if (object == null) {
130             final String returnString = userKey.getId() + ": cannot encode a null object of class \""
131                     + schemaClass.getCanonicalName() + "\"";
132             LOGGER.warn(returnString);
133             throw new ContextRuntimeException(returnString);
134         }
135
136         // The incoming object should be a byte array
137         if (!(object instanceof byte[])) {
138             final String returnString = userKey.getId() + ": object \"" + object + "\" of class \"" + object.getClass()
139                     + "\" cannot be decoded to an object of class \"" + schemaClass.getCanonicalName() + "\"";
140             LOGGER.warn(returnString);
141             throw new ContextRuntimeException(returnString);
142         }
143
144         // Create a ByteBuffer object to serialize the bytes
145         return ByteBuffer.wrap((byte[]) object);
146     }
147 }