diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-06-13 16:50:59 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-06-14 11:22:00 +0100 |
commit | 4fcf04234e5e1b1d4338f12d982a45edf317d795 (patch) | |
tree | a17fb2a9dd70064145312b75c9d1019086cb11c1 /context/context-management | |
parent | 2e1da7176952e013276ef29638a313f3a1b49c00 (diff) |
Add YAML plugin and cleanup for checkstyle
Bringing in the APEX YAML plugin, allows events with YAML bodies
to be consumed by APEX.
Also clean up of event protocol interface to make it not specific
for JSON.
Also some checkstyle changes on classes impacted by event
protocol interface change.
Change-Id: I7c3867ac508096fd8acad2488e61db87dfa1d6bd
Issue-ID: POLICY-862
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'context/context-management')
2 files changed, 36 insertions, 24 deletions
diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java index aa6ea9ffe..30af48195 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java @@ -20,8 +20,6 @@ package org.onap.policy.apex.context; -import com.google.gson.JsonElement; - import org.onap.policy.apex.model.basicmodel.concepts.AxKey; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; @@ -86,12 +84,12 @@ public interface SchemaHelper { Object createNewInstance(String stringValue); /** - * Create a new instance of the schema class from a GSON JsonElement using whatever schema technology is being used. + * Create a new instance of the schema class from an object using whatever schema technology is being used. * - * @param jsonElement the JSON element that holds the Json representation of the object + * @param incomingObject the incoming object that holds the raw representation of the object to be created * @return the new instance */ - Object createNewInstance(JsonElement jsonElement); + Object createNewInstance(Object incomingObject); /** * Unmarshal an object in schema format into a Java object. @@ -107,13 +105,13 @@ public interface SchemaHelper { * @param schemaObject the object in schema format * @return the object as a Json string */ - String marshal2Json(Object schemaObject); + String marshal2String(Object schemaObject); /** - * Marshal a Java object into a GSON json element. + * Marshal a Java object into an output object of an arbitrary type. * * @param schemaObject the object in schema format - * @return the object as a GSON Json element + * @return the object as output object of an arbitrary type */ - JsonElement marshal2JsonElement(Object schemaObject); + Object marshal2Object(Object schemaObject); } diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java index b89efbf91..e5d92395a 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java @@ -49,7 +49,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { // This map defines the built in types in types in Java // @formatter:off private static final Map<String, Class<?>> BUILT_IN_MAP = new HashMap<>(); - { + static { BUILT_IN_MAP.put("int", Integer .TYPE); BUILT_IN_MAP.put("long", Long .TYPE); BUILT_IN_MAP.put("double", Double .TYPE); @@ -69,7 +69,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { * concepts. AxKey, org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema) */ @Override - public void init(final AxKey userKey, final AxContextSchema schema) throws ContextRuntimeException { + public void init(final AxKey userKey, final AxContextSchema schema) { super.init(userKey, schema); final String javatype = schema.getSchema(); @@ -80,7 +80,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { } catch (final IllegalArgumentException e) { String resultSting = userKey.getID() + ": class/type " + schema.getSchema() + " for context schema \"" - + schema.getID() + "\" not found."; + + schema.getID() + "\" not found."; if (JavaSchemaHelper.BUILT_IN_MAP.get(javatype) != null) { resultSting += " Primitive types are not supported. Use the appropriate Java boxing type instead."; } else { @@ -94,13 +94,25 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { /* * (non-Javadoc) * - * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(com.google.gson.JsonElement) + * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.Object) */ @Override - public Object createNewInstance(final JsonElement jsonElement) { - final String elementJsonString = new Gson().toJson(jsonElement); + public Object createNewInstance(final Object incomingObject) { + if (incomingObject instanceof JsonElement) { + final String elementJsonString = new Gson().toJson((JsonElement) incomingObject); + return new Gson().fromJson(elementJsonString, this.getSchemaClass()); + } + + if (getSchemaClass().isAssignableFrom(incomingObject.getClass())) { + return incomingObject; + } - return new Gson().fromJson(elementJsonString, this.getSchemaClass()); + final String returnString = getUserKey().getID() + ": the object \"" + incomingObject + "\" of type \"" + + incomingObject.getClass().getCanonicalName() + + "\" is not an instance of JsonObject and is not assignable to \"" + + getSchemaClass().getCanonicalName() + "\""; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); } /* @@ -137,7 +149,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { * @see org.onap.policy.apex.context.SchemaHelper#schemaObject2Json(java.lang.Object) */ @Override - public String marshal2Json(final Object schemaObject) { + public String marshal2String(final Object schemaObject) { if (schemaObject == null) { return "null"; } @@ -148,8 +160,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { return new Gson().toJson(schemaObject); } else { final String returnString = getUserKey().getID() + ": object \"" + schemaObject.toString() - + "\" of class \"" + schemaObject.getClass().getCanonicalName() + "\" not compatible with class \"" - + getSchemaClass().getCanonicalName() + "\""; + + "\" of class \"" + schemaObject.getClass().getCanonicalName() + + "\" not compatible with class \"" + getSchemaClass().getCanonicalName() + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -161,7 +173,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { * @see org.onap.policy.apex.context.SchemaHelper#marshal2JsonElement(java.lang.Object) */ @Override - public JsonElement marshal2JsonElement(final Object schemaObject) { + public Object marshal2Object(final Object schemaObject) { // Use Gson to marshal the schema object into a Json element to return return new Gson().toJsonTree(schemaObject, getSchemaClass()); } @@ -169,7 +181,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { /** * Do a numeric conversion between numeric types. * - * @param object The incoming numeric object + * @param object + * The incoming numeric object * @return The converted object */ private Object numericConversion(final Object object) { @@ -195,7 +208,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { /** * Do a string conversion to the class type. * - * @param object The incoming numeric object + * @param object + * The incoming numeric object * @return The converted object */ private Object stringConversion(final Object object) { @@ -205,8 +219,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { return stringConstructor.newInstance(object.toString()); } catch (final Exception e) { final String returnString = getUserKey().getID() + ": object \"" + object.toString() + "\" of class \"" - + object.getClass().getCanonicalName() + "\" not compatible with class \"" - + getSchemaClass().getCanonicalName() + "\""; + + object.getClass().getCanonicalName() + "\" not compatible with class \"" + + getSchemaClass().getCanonicalName() + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } |