private NormalizedNodeWriter normalizedNodeWriter;
private boolean headerWritten;
private QName lastLeafSetQName;
+ private boolean inSimple;
AbstractNormalizedNodeDataOutput(final DataOutput output) {
this.output = Preconditions.checkNotNull(output);
}
@Override
- public void leafNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
+ public void startLeafNode(final NodeIdentifier name) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
- LOG.trace("Writing a new leaf node");
+ LOG.trace("Starting a new leaf node");
startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
-
- writeObject(value);
+ inSimple = true;
}
@Override
- public void startLeafSet(final NodeIdentifier name, final int childSizeHint)
-
- throws IOException, IllegalArgumentException {
+ public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new leaf set");
}
@Override
- public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new ordered leaf set");
}
@Override
- public void leafSetEntryNode(final QName name, final Object value) throws IOException, IllegalArgumentException {
- LOG.trace("Writing a new leaf set entry node");
+ public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
+ LOG.trace("Starting a new leaf set entry node");
output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
// lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
// stand alone LeafSetEntryNode so write out it's name here.
if (lastLeafSetQName == null) {
- writeQName(name);
+ writeQName(name.getNodeType());
}
-
- writeObject(value);
+ inSimple = true;
}
@Override
- public void startContainerNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new container node");
}
@Override
- public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new yang modeled anyXml node");
}
@Override
- public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new unkeyed list");
}
@Override
- public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalStateException {
+ public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new unkeyed list item");
}
@Override
- public void startMapNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new map node");
@Override
public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ throws IOException {
Preconditions.checkNotNull(identifier, "Node identifier should not be null");
LOG.trace("Starting a new map entry node");
startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
writeKeyValueMap(identifier.getKeyValues());
-
}
@Override
- public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new ordered map node");
}
@Override
- public void startChoiceNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException, IllegalArgumentException {
+ public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
LOG.trace("Starting a new choice node");
}
@Override
- public void startAugmentationNode(final AugmentationIdentifier identifier)
- throws IOException, IllegalArgumentException {
+ public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
Preconditions.checkNotNull(identifier, "Node identifier should not be null");
LOG.trace("Starting a new augmentation node");
}
@Override
- public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
+ public void startAnyxmlNode(final NodeIdentifier name) throws IOException {
Preconditions.checkNotNull(name, "Node identifier should not be null");
- LOG.trace("Writing any xml node");
-
+ LOG.trace("Starting any xml node");
startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
+ inSimple = true;
+ }
+ @Override
+ public void scalarValue(final Object value) throws IOException {
+ writeObject(value);
+ }
+
+ @Override
+ public void domSourceValue(final DOMSource value) throws IOException {
try {
StreamResult xmlOutput = new StreamResult(new StringWriter());
- TransformerFactory.newInstance().newTransformer().transform((DOMSource)value, xmlOutput);
+ TransformerFactory.newInstance().newTransformer().transform(value, xmlOutput);
writeObject(xmlOutput.getWriter().toString());
} catch (TransformerException | TransformerFactoryConfigurationError e) {
throw new IOException("Error writing anyXml", e);
}
@Override
- public void endNode() throws IOException, IllegalStateException {
+ public void endNode() throws IOException {
LOG.trace("Ending the node");
- lastLeafSetQName = null;
- output.writeByte(NodeTypes.END_NODE);
+ if (!inSimple) {
+ lastLeafSetQName = null;
+ output.writeByte(NodeTypes.END_NODE);
+ }
+ inSimple = false;
}
@Override
private void startNode(final QName qname, final byte nodeType) throws IOException {
Preconditions.checkNotNull(qname, "QName of node identifier should not be null.");
+ Preconditions.checkState(!inSimple, "Attempted to start a child in a simple node");
ensureHeaderWritten();
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.slf4j.Logger;
private QName lastLeafSetQName;
- private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
- Object, LeafNode<Object>> leafBuilder;
+ private NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> leafBuilder;
@SuppressWarnings("rawtypes")
- private NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
+ private NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
private final StringBuilder reusableStringBuilder = new StringBuilder(50);
}
}
- private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
- Object, LeafNode<Object>> leafBuilder() {
+ private NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> leafBuilder() {
if (leafBuilder == null) {
leafBuilder = Builders.leafBuilder();
}
}
@SuppressWarnings("rawtypes")
- private NormalizedNodeAttrBuilder<NodeWithValue, Object,
- LeafSetEntryNode<Object>> leafSetEntryBuilder() {
+ private NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder() {
if (leafSetEntryBuilder == null) {
leafSetEntryBuilder = Builders.leafSetEntryBuilder();
}
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
+import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;
import java.util.Optional;
import javax.xml.transform.dom.DOMSource;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
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.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
state = State.OPEN;
}
- @SuppressWarnings("unchecked")
@Override
- public void leafNode(final NodeIdentifier nodeIdentifier, final Object value) {
- checkNotSealed();
-
- NormalizedNodeBuilderWrapper parent = stack.peek();
- LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(nodeIdentifier).withValue(value).build();
- if (parent != null) {
- if (hasValidSchema(nodeIdentifier.getNodeType(), parent)) {
- parent.builder().addChild(leafNode);
- }
- } else {
- // If there's no parent node then this is a stand alone LeafNode.
- if (nodePathSchemaNode != null) {
- this.normalizedNode = leafNode;
- }
-
- state = State.CLOSED;
- }
+ public void startLeafNode(final NodeIdentifier name) {
+ addBuilder(Builders.leafBuilder().withNodeIdentifier(name), name);
}
@Override
addBuilder(Builders.orderedLeafSetBuilder().withNodeIdentifier(nodeIdentifier), nodeIdentifier);
}
- @SuppressWarnings("unchecked")
@Override
- public void leafSetEntryNode(final QName name, final Object value) {
- checkNotSealed();
-
- NormalizedNodeBuilderWrapper parent = stack.peek();
- if (parent != null) {
- if (hasValidSchema(name, parent)) {
- parent.builder().addChild(Builders.leafSetEntryBuilder().withValue(value)
- .withNodeIdentifier(new NodeWithValue<>(parent.nodeType(), value))
- .build());
- }
- } else {
- // If there's no parent LeafSetNode then this is a stand alone
- // LeafSetEntryNode.
- if (nodePathSchemaNode != null) {
- this.normalizedNode = Builders.leafSetEntryBuilder().withValue(value).withNodeIdentifier(
- new NodeWithValue<>(name, value)).build();
- }
-
- state = State.CLOSED;
- }
+ public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
+ addBuilder(Builders.leafSetEntryBuilder().withNodeIdentifier(name), name);
}
@Override
addBuilder(Builders.augmentationBuilder().withNodeIdentifier(augmentationIdentifier), augmentationIdentifier);
}
- @SuppressWarnings("unchecked")
@Override
- public void anyxmlNode(final NodeIdentifier nodeIdentifier, final Object value) {
- checkNotSealed();
+ public void startAnyxmlNode(final NodeIdentifier name) {
+ addBuilder(Builders.anyXmlBuilder().withNodeIdentifier(name), name);
+ }
- NormalizedNodeBuilderWrapper parent = stack.peek();
- AnyXmlNode anyXmlNode = Builders.anyXmlBuilder().withNodeIdentifier(nodeIdentifier).withValue((DOMSource) value)
- .build();
- if (parent != null) {
- if (hasValidSchema(nodeIdentifier.getNodeType(), parent)) {
- parent.builder().addChild(anyXmlNode);
- }
- } else {
- // If there's no parent node then this is a stand alone AnyXmlNode.
- if (nodePathSchemaNode != null) {
- this.normalizedNode = anyXmlNode;
- }
+ @Override
+ public void domSourceValue(final DOMSource value) {
+ setValue(value);
+ }
- state = State.CLOSED;
- }
+ @Override
+ public void scalarValue(final Object value) {
+ setValue(value);
}
- @SuppressWarnings("unchecked")
@Override
public void endNode() {
checkNotSealed();
return;
}
- final NormalizedNode<?, ?> newNode = child.builder().build();
+ final NormalizedNode<?, ?> newNode = child.build();
final NormalizedNodeBuilderWrapper parent = stack.peek();
if (parent == null) {
normalizedNode = newNode;
state = State.CLOSED;
} else {
- parent.builder().addChild(newNode);
+ parent.addChild(newNode);
}
}
checkState(state == State.OPEN, "Illegal operation in state %s", state);
}
- private static boolean hasValidSchema(final QName name, final NormalizedNodeBuilderWrapper parent) {
- final DataSchemaContextNode<?> parentSchema = parent.getSchema();
- final boolean valid = parentSchema != null && parentSchema.getChild(name) != null;
- if (!valid) {
- LOG.debug("Schema not found for {}", name);
- }
-
- return valid;
+ private void setValue(final Object value) {
+ checkNotSealed();
+ final NormalizedNodeBuilderWrapper current = stack.peek();
+ checkState(current != null, "Attempted to set value %s while no node is open", value);
+ current.setValue(value);
}
- private NormalizedNodeBuilderWrapper addBuilder(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder,
+ private <T extends NormalizedNodeBuilder<?, ?, ?>> NormalizedNodeBuilderWrapper addBuilder(final T builder,
final PathArgument identifier) {
checkNotSealed();
final DataSchemaContextNode<?> schemaNode;
final NormalizedNodeBuilderWrapper parent = stack.peek();
if (parent != null) {
- final DataSchemaContextNode<?> parentSchema = parent.getSchema();
- schemaNode = parentSchema == null ? null : parentSchema.getChild(identifier);
+ schemaNode = parent.childSchema(identifier);
} else {
schemaNode = nodePathSchemaNode;
}
*/
package org.opendaylight.controller.cluster.datastore.node.utils.transformer;
+import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
final class NormalizedNodeBuilderWrapper {
- private final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder;
+ @SuppressWarnings("rawtypes")
+ private final NormalizedNodeBuilder builder;
private final PathArgument identifier;
private final DataSchemaContextNode<?> schemaNode;
- NormalizedNodeBuilderWrapper(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder,
+ NormalizedNodeBuilderWrapper(final NormalizedNodeBuilder<?, ?, ?> builder,
final PathArgument identifier, final @Nullable DataSchemaContextNode<?> schemaNode) {
this.builder = requireNonNull(builder);
this.identifier = requireNonNull(identifier);
this.schemaNode = schemaNode;
}
- @SuppressWarnings("rawtypes")
- NormalizedNodeContainerBuilder builder() {
- return builder;
- }
-
- QName nodeType() {
- return identifier.getNodeType();
- }
-
PathArgument identifier() {
return identifier;
}
@Nullable DataSchemaContextNode<?> getSchema() {
return schemaNode;
}
+
+ @Nullable DataSchemaContextNode<?> childSchema(final PathArgument child) {
+ if (schemaNode == null) {
+ return null;
+ }
+
+ checkState(builder instanceof NormalizedNodeContainerBuilder,
+ "Attempted to lookup child %s in non-container %s", schemaNode);
+ return schemaNode.getChild(child);
+ }
+
+ NormalizedNode<?, ?> build() {
+ return builder.build();
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ void addChild(final NormalizedNode<?, ?> child) {
+ checkState(builder instanceof NormalizedNodeContainerBuilder,
+ "Attempted to add child %s to non-container builder %s", child, builder);
+ ((NormalizedNodeContainerBuilder) builder).addChild(child);
+ }
+
+ @SuppressWarnings("unchecked")
+ void setValue(final Object value) {
+ checkState(!(builder instanceof NormalizedNodeContainerBuilder),
+ "Attempted to set value %s on container builder %s", value, builder);
+ builder.withValue(value);
+ }
}