e5d92395a98336afc5369dc72a019cd15e863ad3
[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.context.impl.schema.java;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25
26 import java.lang.reflect.Constructor;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
34 import org.onap.policy.apex.model.utilities.typeutils.TypeBuilder;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
37
38 /**
39  * This class implements translation to and from Apex distributed objects and Java objects when a Java schema is used.
40  * It creates schema items as Java objects and marshals and unmarshals these objects in various formats. All objects
41  * must be of the type of Java class defined in the schema.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class JavaSchemaHelper extends AbstractSchemaHelper {
46     // Get a reference to the logger
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelper.class);
48
49     // This map defines the built in types in types in Java
50     // @formatter:off
51     private static final Map<String, Class<?>> BUILT_IN_MAP = new HashMap<>();
52     static {
53         BUILT_IN_MAP.put("int",    Integer  .TYPE);
54         BUILT_IN_MAP.put("long",   Long     .TYPE);
55         BUILT_IN_MAP.put("double", Double   .TYPE);
56         BUILT_IN_MAP.put("float",  Float    .TYPE);
57         BUILT_IN_MAP.put("bool",   Boolean  .TYPE);
58         BUILT_IN_MAP.put("char",   Character.TYPE);
59         BUILT_IN_MAP.put("byte",   Byte     .TYPE);
60         BUILT_IN_MAP.put("void",   Void     .TYPE);
61         BUILT_IN_MAP.put("short",  Short    .TYPE);
62     }
63     // @formatter:on
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper#init(org.onap.policy.apex.model.basicmodel.
69      * concepts. AxKey, org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema)
70      */
71     @Override
72     public void init(final AxKey userKey, final AxContextSchema schema) {
73         super.init(userKey, schema);
74
75         final String javatype = schema.getSchema();
76         // For Java, the schema is the Java class canonical path
77
78         try {
79             setSchemaClass(TypeBuilder.getJavaTypeClass(schema.getSchema()));
80         } catch (final IllegalArgumentException e) {
81
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.";
86             } else {
87                 resultSting += " Check the class path of the JVM";
88             }
89             LOGGER.warn(resultSting);
90             throw new ContextRuntimeException(resultSting, e);
91         }
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.Object)
98      */
99     @Override
100     public Object createNewInstance(final Object incomingObject) {
101         if (incomingObject instanceof JsonElement) {
102             final String elementJsonString = new Gson().toJson((JsonElement) incomingObject);
103             return new Gson().fromJson(elementJsonString, this.getSchemaClass());
104         }
105
106         if (getSchemaClass().isAssignableFrom(incomingObject.getClass())) {
107             return incomingObject;
108         }
109
110         final String returnString = getUserKey().getID() + ": the object \"" + incomingObject + "\" of type \""
111                         + incomingObject.getClass().getCanonicalName()
112                         + "\" is not an instance of JsonObject and is not assignable to \""
113                         + getSchemaClass().getCanonicalName() + "\"";
114         LOGGER.warn(returnString);
115         throw new ContextRuntimeException(returnString);
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.onap.policy.apex.context.SchemaHelper#object2SchemaObject(java.lang.Object)
122      */
123     @Override
124     public Object unmarshal(final Object object) {
125         if (object == null) {
126             return null;
127         }
128
129         // If the object is an instance of the incoming object, carry on
130         if (object.getClass().equals(getSchemaClass())) {
131             return object;
132         }
133
134         // For numeric types, do a numeric conversion
135         if (Number.class.isAssignableFrom(getSchemaClass())) {
136             return numericConversion(object);
137         }
138
139         if (getSchemaClass().isAssignableFrom(object.getClass())) {
140             return object;
141         } else {
142             return stringConversion(object);
143         }
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.onap.policy.apex.context.SchemaHelper#schemaObject2Json(java.lang.Object)
150      */
151     @Override
152     public String marshal2String(final Object schemaObject) {
153         if (schemaObject == null) {
154             return "null";
155         }
156
157         // Check the incoming object is of a correct class
158         if (getSchemaClass().isAssignableFrom(schemaObject.getClass())) {
159             // Use Gson to translate the object
160             return new Gson().toJson(schemaObject);
161         } else {
162             final String returnString = getUserKey().getID() + ": object \"" + schemaObject.toString()
163                             + "\" of class \"" + schemaObject.getClass().getCanonicalName()
164                             + "\" not compatible with class \"" + getSchemaClass().getCanonicalName() + "\"";
165             LOGGER.warn(returnString);
166             throw new ContextRuntimeException(returnString);
167         }
168     }
169
170     /*
171      * (non-Javadoc)
172      *
173      * @see org.onap.policy.apex.context.SchemaHelper#marshal2JsonElement(java.lang.Object)
174      */
175     @Override
176     public Object marshal2Object(final Object schemaObject) {
177         // Use Gson to marshal the schema object into a Json element to return
178         return new Gson().toJsonTree(schemaObject, getSchemaClass());
179     }
180
181     /**
182      * Do a numeric conversion between numeric types.
183      *
184      * @param object
185      *        The incoming numeric object
186      * @return The converted object
187      */
188     private Object numericConversion(final Object object) {
189         // Check if the incoming object is a number, if not do a string conversion
190         if (object instanceof Number) {
191             if (getSchemaClass().isAssignableFrom(Byte.class)) {
192                 return ((Number) object).byteValue();
193             } else if (getSchemaClass().isAssignableFrom(Integer.class)) {
194                 return ((Number) object).intValue();
195             } else if (getSchemaClass().isAssignableFrom(Long.class)) {
196                 return ((Number) object).longValue();
197             } else if (getSchemaClass().isAssignableFrom(Float.class)) {
198                 return ((Number) object).floatValue();
199             } else if (getSchemaClass().isAssignableFrom(Double.class)) {
200                 return ((Number) object).doubleValue();
201             }
202         }
203
204         // OK, we'll try and convert from a string representation of the incoming object
205         return stringConversion(object);
206     }
207
208     /**
209      * Do a string conversion to the class type.
210      *
211      * @param object
212      *        The incoming numeric object
213      * @return The converted object
214      */
215     private Object stringConversion(final Object object) {
216         // OK, we'll try and convert from a string representation of the incoming object
217         try {
218             final Constructor<?> stringConstructor = getSchemaClass().getConstructor(String.class);
219             return stringConstructor.newInstance(object.toString());
220         } catch (final Exception e) {
221             final String returnString = getUserKey().getID() + ": object \"" + object.toString() + "\" of class \""
222                             + object.getClass().getCanonicalName() + "\" not compatible with class \""
223                             + getSchemaClass().getCanonicalName() + "\"";
224             LOGGER.warn(returnString);
225             throw new ContextRuntimeException(returnString);
226         }
227     }
228 }