diff options
Diffstat (limited to 'plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java')
6 files changed, 41 insertions, 45 deletions
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java index 20e701bc7..bee2f5957 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroBytesObjectMapper.java @@ -43,7 +43,7 @@ public class AvroBytesObjectMapper implements AvroObjectMapper { private Type avroType; // The Apex compatible class - private final Class<Byte[]> schemaClass = Byte[].class; + private static final Class<Byte[]> schemaClass = Byte[].class; /* * (non-Javadoc) @@ -142,8 +142,6 @@ public class AvroBytesObjectMapper implements AvroObjectMapper { } // Create a ByteBuffer object to serialize the bytes - final ByteBuffer byteBuffer = ByteBuffer.wrap((byte[]) object); - - return byteBuffer; + return ByteBuffer.wrap((byte[]) object); } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java index 35e811dec..ca771d957 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroDirectObjectMapper.java @@ -132,7 +132,7 @@ public class AvroDirectObjectMapper implements AvroObjectMapper { // the decoded object is always returned as a null if (!schemaClass.isAssignableFrom(avroObject.getClass())) { final String returnString = - userKey.getId() + ": object \"" + avroObject + "\" of class \"" + avroObject.getClass() + userKey.getId() + ": object \"" + avroObject + "\" of class \"" + avroObject.getClass() + "\" cannot be decoded to an object of class \"" + schemaClass.getCanonicalName() + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); @@ -150,13 +150,11 @@ public class AvroDirectObjectMapper implements AvroObjectMapper { @Override public Object mapToAvro(final Object object) { // Null values are only allowed if the schema class is null - if (object == null) { - if (schemaClass != null) { - final String returnString = userKey.getId() + ": cannot encode a null object of class \"" - + schemaClass.getCanonicalName() + "\""; - LOGGER.warn(returnString); - throw new ContextRuntimeException(returnString); - } + if (object == null && schemaClass != null) { + final String returnString = userKey.getId() + ": cannot encode a null object of class \"" + + schemaClass.getCanonicalName() + "\""; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); } // For direct mappings, just work directly with the Java objects diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java index a48ca8089..21e4d76a8 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjectMapperFactory.java @@ -75,12 +75,12 @@ public class AvroObjectMapperFactory { if (Schema.Type.UNION.equals(schema.getType())) { final List<Schema> types = schema.getTypes(); - // TODO: properly support UNIONS + // currently only support unions with 2 types, one of which is NULL final Schema nullschema = Schema.create(Schema.Type.NULL); if (types.size() != 2 || !types.contains(nullschema)) { final String resultSting = userKey.getId() - + ": Apex currently only supports UNION schemas with 2 options, one must be NULL"; + + ": Apex currently only supports UNION schemas with 2 options, one must be NULL"; LOGGER.warn(resultSting); throw new ContextRuntimeException(resultSting); } @@ -92,7 +92,8 @@ public class AvroObjectMapperFactory { } if (Schema.Type.NULL.equals(schema.getType())) { final String resultSting = userKey.getId() - + ": Apex currently only supports UNION schema2 with 2 options, only one can be NULL, and the other cannot be another UNION"; + + ": Apex currently only supports UNION schema2 with 2 options, " + + "only one can be NULL, and the other cannot be another UNION"; LOGGER.warn(resultSting); throw new ContextRuntimeException(resultSting); } @@ -102,8 +103,8 @@ public class AvroObjectMapperFactory { // Check that there is a definition for the mapper for this type if (!AVRO_OBJECT_MAPPER_MAP.containsKey(avroType) || AVRO_OBJECT_MAPPER_MAP.get(avroType) == null) { - final String resultSting = - userKey.getId() + ": no Avro object mapper defined for Avro type \"" + avroType + "\""; + final String resultSting = userKey.getId() + ": no Avro object mapper defined for Avro type \"" + avroType + + "\""; LOGGER.warn(resultSting); throw new ContextRuntimeException(resultSting); } @@ -118,7 +119,7 @@ public class AvroObjectMapperFactory { } catch (final Exception e) { final String resultSting = userKey.getId() + ": could not create an Avro object mapper of type \"" - + AVRO_OBJECT_MAPPER_MAP.get(avroType) + "\" for Avro type \"" + avroType + "\" : " + e; + + AVRO_OBJECT_MAPPER_MAP.get(avroType) + "\" for Avro type \"" + avroType + "\" : " + e; LOGGER.warn(resultSting, e); throw new ContextRuntimeException(resultSting, e); } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java index df430b683..9cdb5845b 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java @@ -20,6 +20,10 @@ package org.onap.policy.apex.plugins.context.schema.avro; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; + import java.io.ByteArrayOutputStream; import org.apache.avro.Schema; @@ -38,13 +42,8 @@ import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; - /** - * This class is the implementation of the {@link org.onap.policy.apex.context.SchemaHelper} - * interface for Avro schemas. + * This class is the implementation of the {@link org.onap.policy.apex.context.SchemaHelper} interface for Avro schemas. * * @author Liam Fallon (liam.fallon@ericsson.com) */ @@ -52,6 +51,9 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(AvroSchemaHelper.class); + // Recurring string constants + private static final String OBJECT_TAG = ": object \""; + // The Avro schema for this context schema private Schema avroSchema; @@ -59,7 +61,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { private AvroObjectMapper avroObjectMapper; @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); // Configure the Avro schema @@ -67,7 +69,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { avroSchema = new Schema.Parser().parse(schema.getSchema()); } catch (final Exception e) { final String resultSting = userKey.getId() + ": avro context schema \"" + schema.getId() - + "\" schema is invalid: " + e.getMessage() + ", schema: " + schema.getSchema(); + + "\" schema is invalid: " + e.getMessage() + ", schema: " + schema.getSchema(); LOGGER.warn(resultSting); throw new ContextRuntimeException(resultSting); } @@ -91,7 +93,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { @Override public Object getSchemaObject() { - return avroSchema; + return getAvroSchema(); } @Override @@ -119,10 +121,9 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { final String elementJsonString = gson.toJson((JsonElement) incomingObject); return createNewInstance(elementJsonString); - } - else { + } else { final String returnString = getUserKey().getId() + ": the object \"" + incomingObject - + "\" is not an instance of JsonObject"; + + "\" is not an instance of JsonObject"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -146,8 +147,8 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { final JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString); decodedObject = new GenericDatumReader<GenericRecord>(avroSchema).read(null, jsonDecoder); } catch (final Exception e) { - final String returnString = getUserKey().getId() + ": object \"" + objectString - + "\" Avro unmarshalling failed: " + e.getMessage(); + final String returnString = getUserKey().getId() + OBJECT_TAG + objectString + + "\" Avro unmarshalling failed: " + e.getMessage(); LOGGER.warn(returnString, e); throw new ContextRuntimeException(returnString, e); } @@ -157,8 +158,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { } /** - * Check that the incoming object is a string, the incoming object must be a string containing - * Json + * Check that the incoming object is a string, the incoming object must be a string containing Json. * * @param object incoming object * @return object as String @@ -185,10 +185,10 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { return (String) object; } } catch (final ClassCastException e) { - final String returnString = getUserKey().getId() + ": object \"" + object + "\" of type \"" - + (object != null ? object.getClass().getCanonicalName() : "null") + "\" must be assignable to \"" - + getSchemaClass().getCanonicalName() - + "\" or be a Json string representation of it for Avro unmarshalling"; + final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" of type \"" + + (object != null ? object.getClass().getCanonicalName() : "null") + + "\" must be assignable to \"" + getSchemaClass().getCanonicalName() + + "\" or be a Json string representation of it for Avro unmarshalling"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -217,8 +217,8 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { jsonEncoder.flush(); return new String(output.toByteArray()); } catch (final Exception e) { - final String returnString = - getUserKey().getId() + ": object \"" + object + "\" Avro marshalling failed: " + e.getMessage(); + final String returnString = getUserKey().getId() + OBJECT_TAG + object + "\" Avro marshalling failed: " + + e.getMessage(); LOGGER.warn(returnString); throw new ContextRuntimeException(returnString, e); } @@ -239,8 +239,7 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { } /** - * Check if we can pass this object straight through encoding or decoding, is it an object - * native to the schema. + * Check if we can pass this object straight through encoding or decoding, is it an object native to the schema. * * @param object the object to check * @return true if it's a straight pass through diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java index dc3770a43..b4c8737dd 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaKeyTranslationUtilities.java @@ -20,13 +20,13 @@ package org.onap.policy.apex.plugins.context.schema.avro; -import java.util.Map.Entry; - import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.Map.Entry; + /** * This static final class contains utility methods for Avro schemas. * diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java index 09d1d9f1f..a9acb1c12 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroStringObjectMapper.java @@ -42,7 +42,7 @@ public class AvroStringObjectMapper implements AvroObjectMapper { private Type avroType; // The Apex compatible class - private final Class<String> schemaClass = String.class; + private static final Class<String> schemaClass = String.class; /* * (non-Javadoc) |