2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema.java;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonElement;
27 import java.lang.reflect.Constructor;
28 import java.util.HashMap;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * This class implements translation to and from Apex distributed objects and Java objects when a Java schema is used.
42 * It creates schema items as Java objects and marshals and unmarshals these objects in various formats. All objects
43 * must be of the type of Java class defined in the schema.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class JavaSchemaHelper extends AbstractSchemaHelper {
48 // Get a reference to the logger
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelper.class);
51 // This map defines the built in types in types in Java
53 private static final Map<String, Class<?>> BUILT_IN_MAP = new HashMap<>();
56 BUILT_IN_MAP.put("int", Integer.TYPE);
57 BUILT_IN_MAP.put("long", Long.TYPE);
58 BUILT_IN_MAP.put("double", Double.TYPE);
59 BUILT_IN_MAP.put("float", Float.TYPE);
60 BUILT_IN_MAP.put("bool", Boolean.TYPE);
61 BUILT_IN_MAP.put("char", Character.TYPE);
62 BUILT_IN_MAP.put("byte", Byte.TYPE);
63 BUILT_IN_MAP.put("void", Void.TYPE);
64 BUILT_IN_MAP.put("short", Short.TYPE);
72 public void init(final AxKey userKey, final AxContextSchema schema) {
73 super.init(userKey, schema);
75 final String javatype = schema.getSchema();
76 // For Java, the schema is the Java class canonical path
79 setSchemaClass(Class.forName(schema.getSchema()));
80 } catch (final Exception e) {
82 String resultSting = userKey.getId() + ": class/type " + schema.getSchema() + " for context schema \""
83 + schema.getId() + "\" not found.";
84 if (JavaSchemaHelper.BUILT_IN_MAP.get(javatype) != null) {
85 resultSting += " Primitive types are not supported. Use the appropriate Java boxing type instead.";
87 resultSting += " Check the class path of the JVM";
89 LOGGER.warn(resultSting);
90 throw new ContextRuntimeException(resultSting, e);
98 public Object createNewInstance(final Object incomingObject) {
99 if (incomingObject == null) {
103 if (getSchemaClass() == null) {
104 final String returnString =
105 getUserKey().getId() + ": could not create an instance, schema class for the schema is null";
106 LOGGER.warn(returnString);
107 throw new ContextRuntimeException(returnString);
110 if (incomingObject instanceof JsonElement) {
111 final String elementJsonString = getGson().toJson((JsonElement) incomingObject);
112 return getGson().fromJson(elementJsonString, this.getSchemaClass());
115 if (getSchemaClass().isAssignableFrom(incomingObject.getClass())) {
116 return incomingObject;
119 final String returnString = getUserKey().getId() + ": the object \"" + incomingObject + "\" of type \""
120 + incomingObject.getClass().getName()
121 + "\" is not an instance of JsonObject and is not assignable to \"" + getSchemaClass().getName() + "\"";
122 LOGGER.warn(returnString);
123 throw new ContextRuntimeException(returnString);
130 public Object unmarshal(final Object object) {
131 if (object == null) {
135 // If the object is an instance of the incoming object, carry on
136 if (object.getClass().equals(getSchemaClass())) {
140 // For numeric types, do a numeric conversion
141 if (Number.class.isAssignableFrom(getSchemaClass())) {
142 return numericConversion(object);
145 if (getSchemaClass().isAssignableFrom(object.getClass())) {
148 return stringConversion(object);
156 public String marshal2String(final Object schemaObject) {
157 if (schemaObject == null) {
161 // Check the incoming object is of a correct class
162 if (getSchemaClass().isAssignableFrom(schemaObject.getClass())) {
163 // Use Gson to translate the object
164 return getGson().toJson(schemaObject);
166 final String returnString = getUserKey().getId() + ": object \"" + schemaObject.toString()
167 + "\" of class \"" + schemaObject.getClass().getName() + "\" not compatible with class \""
168 + getSchemaClass().getName() + "\"";
169 LOGGER.warn(returnString);
170 throw new ContextRuntimeException(returnString);
178 public Object marshal2Object(final Object schemaObject) {
179 // Use Gson to marshal the schema object into a Json element to return
180 return getGson().toJsonTree(schemaObject, getSchemaClass());
184 * Do a numeric conversion between numeric types.
186 * @param object The incoming numeric object
187 * @return The converted object
189 private Object numericConversion(final Object object) {
190 // Check if the incoming object is a number, if not do a string conversion
191 if (object instanceof Number) {
192 if (getSchemaClass().isAssignableFrom(Byte.class)) {
193 return ((Number) object).byteValue();
194 } else if (getSchemaClass().isAssignableFrom(Short.class)) {
195 return ((Number) object).shortValue();
196 } else if (getSchemaClass().isAssignableFrom(Integer.class)) {
197 return ((Number) object).intValue();
198 } else if (getSchemaClass().isAssignableFrom(Long.class)) {
199 return ((Number) object).longValue();
200 } else if (getSchemaClass().isAssignableFrom(Float.class)) {
201 return ((Number) object).floatValue();
202 } else if (getSchemaClass().isAssignableFrom(Double.class)) {
203 return ((Number) object).doubleValue();
207 // OK, we'll try and convert from a string representation of the incoming object
208 return stringConversion(object);
212 * Do a string conversion to the class type.
214 * @param object The incoming numeric object
215 * @return The converted object
217 private Object stringConversion(final Object object) {
218 // OK, we'll try and convert from a string representation of the incoming object
220 final Constructor<?> stringConstructor = getSchemaClass().getConstructor(String.class);
221 return stringConstructor.newInstance(object.toString());
222 } catch (final Exception e) {
223 final String returnString = getUserKey().getId() + ": object \"" + object.toString() + "\" of class \""
224 + object.getClass().getName() + "\" not compatible with class \"" + getSchemaClass().getName()
226 LOGGER.warn(returnString, e);
227 throw new ContextRuntimeException(returnString);
232 * Get a GSON instance that has the correct adaptation included.
234 * @return the GSON instance
236 private Gson getGson() {
237 GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting();
239 // Get the Java schema helper parameters from the parameter service
240 SchemaParameters schemaParameters = ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME);
242 JavaSchemaHelperParameters javaSchemaHelperParmeters =
243 (JavaSchemaHelperParameters) schemaParameters.getSchemaHelperParameterMap().get("Java");
245 if (javaSchemaHelperParmeters == null) {
246 javaSchemaHelperParmeters = new JavaSchemaHelperParameters();
249 for (JavaSchemaHelperJsonAdapterParameters jsonAdapterEntry : javaSchemaHelperParmeters.getJsonAdapters()
252 Object adapterObject;
254 adapterObject = jsonAdapterEntry.getAdaptorClazz().getDeclaredConstructor().newInstance();
255 } catch (Exception e) {
256 final String returnString = getUserKey().getId() + ": instantiation of adapter class \""
257 + jsonAdapterEntry.getAdaptorClass() + "\" to decode and encode class \""
258 + jsonAdapterEntry.getAdaptedClass() + "\" failed: " + e.getMessage();
259 LOGGER.warn(returnString, e);
260 throw new ContextRuntimeException(returnString);
263 gsonBuilder.registerTypeAdapter(jsonAdapterEntry.getAdaptedClazz(), adapterObject);
266 return gsonBuilder.create();