From b7f0a78887d425ba296101a71a7e1a7f1b08adea Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 9 Oct 2015 18:31:02 +0200 Subject: [PATCH] BUG-4261: introduce DataSchemaNodeAware NormalizedNodeStreamWriter This variant allows passing a DataSchemaNode from the user. Implementations can take advantage of this information to optimize their result. Utility no-op adaptors are provided, too. Change-Id: I990197baf3a80f9826cd35d3bb3155d1b7a10cc3 Signed-off-by: Robert Varga --- .../schema/stream/DataSchemaNodeAware.java | 28 ++++++ .../stream/DataSchemaNodeAwareAdaptor.java | 74 +++++++++++++++ ...ngNormalizedNodeStreamAttributeWriter.java | 48 ++++++++++ .../ForwardingNormalizedNodeStreamWriter.java | 95 +++++++++++++++++++ ...reNormalizedNodeStreamAttributeWriter.java | 21 ++++ ...SchemaAwareNormalizedNodeStreamWriter.java | 20 ++++ 6 files changed, 286 insertions(+) create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAware.java create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAwareAdaptor.java create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamAttributeWriter.java create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamWriter.java create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamAttributeWriter.java create mode 100644 yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamWriter.java diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAware.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAware.java new file mode 100644 index 0000000000..f2108ae0ac --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAware.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import com.google.common.annotations.Beta; +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; + +/** + * Mixin interface for {@link NormalizedNodeStreamWriter} allowing callers to inform the writer of the + * {@link DataSchemaNode} corresponding to the next node which will either be started or emitted. This interface should + * not be implemented directly. + */ +@Beta +public interface DataSchemaNodeAware { + /** + * Attach the specified {@link DataSchemaNode} to the next node which will get started or emitted. + * + * @param schema DataSchemaNode + * @throws NullPointerException if the argument is null + */ + void nextDataSchemaNode(@Nonnull DataSchemaNode schema); +} diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAwareAdaptor.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAwareAdaptor.java new file mode 100644 index 0000000000..9264913e9d --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/DataSchemaNodeAwareAdaptor.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; + +/** + * Utility class for adapting {@link NormalizedNodeStreamWriter}s to their {@link DataSchemaNodeAware} counterparts + * which ignore the provided schema node. + */ +@Beta +public final class DataSchemaNodeAwareAdaptor { + private DataSchemaNodeAwareAdaptor() { + throw new UnsupportedOperationException(); + } + + public static SchemaAwareNormalizedNodeStreamAttributeWriter forAttributeWriter( + @Nonnull final NormalizedNodeStreamAttributeWriter writer) { + Preconditions.checkNotNull(writer, "Writer must not be null"); + + if (writer instanceof SchemaAwareNormalizedNodeStreamAttributeWriter) { + return (SchemaAwareNormalizedNodeStreamAttributeWriter) writer; + } + + return new AttributeWriter() { + @Override + protected NormalizedNodeStreamAttributeWriter delegate() { + return writer; + } + }; + } + + public static SchemaAwareNormalizedNodeStreamWriter forWriter(@Nonnull final NormalizedNodeStreamWriter writer) { + Preconditions.checkNotNull(writer, "Writer must not be null"); + + if (writer instanceof SchemaAwareNormalizedNodeStreamWriter) { + return (SchemaAwareNormalizedNodeStreamWriter) writer; + } + if (writer instanceof NormalizedNodeStreamAttributeWriter) { + return forAttributeWriter((NormalizedNodeStreamAttributeWriter) writer); + } + + return new Writer() { + @Override + protected NormalizedNodeStreamWriter delegate() { + return writer; + } + }; + } + + private static abstract class AttributeWriter extends ForwardingNormalizedNodeStreamAttributeWriter + implements SchemaAwareNormalizedNodeStreamAttributeWriter { + @Override + public void nextDataSchemaNode(final DataSchemaNode schema) { + // Intentional no-op + } + } + + private static abstract class Writer extends ForwardingNormalizedNodeStreamWriter + implements SchemaAwareNormalizedNodeStreamWriter { + @Override + public void nextDataSchemaNode(final DataSchemaNode schema) { + // Intentional no-op + } + } +} diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamAttributeWriter.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamAttributeWriter.java new file mode 100644 index 0000000000..0c7ed69fb9 --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamAttributeWriter.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import java.io.IOException; +import java.util.Map; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; + +abstract class ForwardingNormalizedNodeStreamAttributeWriter extends ForwardingNormalizedNodeStreamWriter implements NormalizedNodeStreamAttributeWriter { + @Override + protected abstract NormalizedNodeStreamAttributeWriter delegate(); + + @Override + public void leafNode(final NodeIdentifier name, final Object value, final Map attributes) + throws IOException { + delegate().leafNode(name, value, attributes); + } + + @Override + public void leafSetEntryNode(final Object value, final Map attributes) throws IOException { + delegate().leafSetEntryNode(value, attributes); + } + + @Override + public void startContainerNode(final NodeIdentifier name, final int childSizeHint, + final Map attributes) throws IOException { + delegate().startContainerNode(name, childSizeHint, attributes); + } + + @Override + public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint, + final Map attributes) throws IOException { + delegate().startUnkeyedListItem(name, childSizeHint, attributes); + } + + @Override + public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint, + final Map attributes) throws IOException { + delegate().startMapEntryNode(identifier, childSizeHint, attributes); + } +} diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamWriter.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamWriter.java new file mode 100644 index 0000000000..28ccf74f54 --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/ForwardingNormalizedNodeStreamWriter.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import com.google.common.collect.ForwardingObject; +import java.io.IOException; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; + +abstract class ForwardingNormalizedNodeStreamWriter extends ForwardingObject implements NormalizedNodeStreamWriter { + @Override + protected abstract NormalizedNodeStreamWriter delegate(); + + @Override + public void leafNode(final NodeIdentifier name, final Object value) throws IOException { + delegate().leafNode(name, value); + } + + @Override + public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startLeafSet(name, childSizeHint); + } + + @Override + public void leafSetEntryNode(final Object value) throws IOException { + delegate().leafSetEntryNode(value); + } + + @Override + public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startContainerNode(name, childSizeHint); + } + + @Override + public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startUnkeyedList(name, childSizeHint); + } + + @Override + public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startUnkeyedListItem(name, childSizeHint); + } + + @Override + public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startMapNode(name, childSizeHint); + } + + @Override + public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) + throws IOException { + delegate().startMapEntryNode(identifier, childSizeHint); + } + + @Override + public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startOrderedMapNode(name, childSizeHint); + } + + @Override + public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException { + delegate().startChoiceNode(name, childSizeHint); + } + + @Override + public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException { + delegate().startAugmentationNode(identifier); + } + + @Override + public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException { + delegate().anyxmlNode(name, value); + } + + @Override + public void endNode() throws IOException { + delegate().endNode(); + } + + @Override + public void close() throws IOException { + delegate().close(); + } + + @Override + public void flush() throws IOException { + delegate().flush(); + } +} diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamAttributeWriter.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamAttributeWriter.java new file mode 100644 index 0000000000..ca63be50a4 --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamAttributeWriter.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import com.google.common.annotations.Beta; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; + +/** + * Marker interface for {@link SchemaAwareNormalizedNodeStreamAttributeWriter}s which can take advantage of + * {@link DataSchemaNode} information when writing the nodes. + */ +@Beta +public interface SchemaAwareNormalizedNodeStreamAttributeWriter + extends NormalizedNodeStreamAttributeWriter, SchemaAwareNormalizedNodeStreamWriter { + +} diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamWriter.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamWriter.java new file mode 100644 index 0000000000..dbdabcae69 --- /dev/null +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/stream/SchemaAwareNormalizedNodeStreamWriter.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.data.api.schema.stream; + +import com.google.common.annotations.Beta; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; + +/** + * Marker interface for {@link NormalizedNodeStreamWriter}s which can take advantage of {@link DataSchemaNode} + * information when writing the nodes. + */ +@Beta +public interface SchemaAwareNormalizedNodeStreamWriter extends NormalizedNodeStreamWriter, DataSchemaNodeAware { + +} -- 2.36.6