diff options
author | Jan Srnicek <jsrnicek@cisco.com> | 2017-02-10 08:55:55 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-02-10 08:53:27 +0000 |
commit | de55d1e7c1fa5517ee6eabcd3fa23e5b5136d64b (patch) | |
tree | 057cd5f38c18da91852c2d79168ae41c0c6bfed6 /infra/translate-utils | |
parent | 8450b69800c827ba221351eb0c374fcbd9146593 (diff) |
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 <jsrnicek@cisco.com>
Diffstat (limited to 'infra/translate-utils')
-rw-r--r-- | infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/JsonUtils.java | 47 |
1 files changed, 41 insertions, 6 deletions
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<YangInstanceIdentifier.NodeIdentifier, ContainerNode> 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<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> 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<? extends YangInstanceIdentifier.PathArgument, ?> child : data.getValue()) { nnWriter.write(child); |