From de55d1e7c1fa5517ee6eabcd3fa23e5b5136d64b Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Fri, 10 Feb 2017 08:55:55 +0100 Subject: HONEYCOMB-334 - List entry injection for yang data Major changes - mechanism to inject list entries by key - provided processor registry to hide explicit implementations Minor changes - general refactoring Test cases - list in root of model - list under container - list under nested container - list in augmentation Change-Id: I9abe1ce5f9176c132ad88627b135516574e40e06 Signed-off-by: Jan Srnicek --- .../io/fd/honeycomb/translate/util/JsonUtils.java | 47 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'infra/translate-utils') diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/JsonUtils.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/JsonUtils.java index 3216cac20..332bf6767 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/JsonUtils.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/JsonUtils.java @@ -19,15 +19,10 @@ package io.fd.honeycomb.translate.util; import com.google.common.base.Charsets; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; @@ -44,6 +39,9 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import java.io.*; + public final class JsonUtils { private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class); @@ -101,6 +99,43 @@ public final class JsonUtils { return builder.build(); } + public static ContainerNode readContainerEntryJson(@Nonnull final SchemaContext schemaContext, + @Nonnull final InputStream stream, + @Nonnull final SchemaNode parentSchema, + @Nonnull final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) { + final DataContainerNodeAttrBuilder builder = + Builders.containerBuilder().withNodeIdentifier(nodeIdentifier); + final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(builder); + + try (final JsonParserStream jsonParser = JsonParserStream.create(writer, schemaContext, parentSchema)) { + final JsonReader reader = new JsonReader(new InputStreamReader(stream, Charsets.UTF_8)); + jsonParser.parse(reader); + } catch (IOException e) { + LOG.warn("Unable to close json parser. Ignoring exception", e); + } + + return builder.build(); + } + + public static MapEntryNode readListEntryFromJson(@Nonnull final SchemaContext schemaContext, + @Nonnull final InputStream stream, + @Nonnull final SchemaNode parentSchema, + @Nonnull final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) { + final DataContainerNodeAttrBuilder mapEntryBuilder = Builders.mapEntryBuilder() + .withNodeIdentifier(nodeIdentifier); + + + final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(mapEntryBuilder); + try (final JsonParserStream jsonParser = JsonParserStream.create(writer, schemaContext, parentSchema)) { + final JsonReader reader = new JsonReader(new InputStreamReader(stream, Charsets.UTF_8)); + jsonParser.parse(reader); + } catch (IOException e) { + LOG.warn("Unable to close json parser. Ignoring exception", e); + } + + return mapEntryBuilder.build(); + } + private static void writeChildren(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException { for (final DataContainerChild child : data.getValue()) { nnWriter.write(child); -- cgit 1.2.3-korg