import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
+
+import java.io.IOException;
import java.util.AbstractMap.SimpleEntry;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeWriterFactory;
import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class BindingNormalizedNodeCodecRegistry implements DataObjectSerializerRegistry, BindingNormalizedNodeWriterFactory, BindingNormalizedNodeSerializer {
+ private static final Logger LOG = LoggerFactory.getLogger(BindingNormalizedNodeCodecRegistry.class);
private final DataObjectSerializerGenerator generator;
private final LoadingCache<Class<? extends DataObject>, DataObjectSerializer> serializers;
// We create Binding Stream Writer wchich translates from Binding to Normalized Nodes
Entry<YangInstanceIdentifier, BindingStreamEventWriter> writeCtx = codecContext.newWriter(path, domWriter);
- // We get serializer which reads binding data and uses Binding To NOrmalized Node writer to write result
- getSerializer(path.getTargetType()).serialize(data, writeCtx.getValue());
+ // We get serializer which reads binding data and uses Binding To Normalized Node writer to write result
+ try {
+ getSerializer(path.getTargetType()).serialize(data, writeCtx.getValue());
+ } catch (IOException e) {
+ LOG.error("Unexpected failure while serializing path {} data {}", path, data, e);
+ throw new IllegalStateException("Failed to create normalized node", e);
+ }
return new SimpleEntry<YangInstanceIdentifier,NormalizedNode<?,?>>(writeCtx.getKey(),result.getResult());
}
}
@Override
- public void serialize(final DataObject obj, final BindingStreamEventWriter stream) {
+ public void serialize(final DataObject obj, final BindingStreamEventWriter stream) throws IOException {
delegate.serialize(BindingNormalizedNodeCodecRegistry.this, obj, stream);
}
}
import com.google.common.base.Preconditions;
+import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.Deque;
}
@Override
- public void endNode() {
+ public void endNode() throws IOException {
NodeCodecContext left = schema.pop();
// NormalizedNode writer does not have entry into case, but into choice
// so for leaving case, we do not emit endNode.
}
@Override
- public void leafNode(final String localName, final Object value) throws IllegalArgumentException {
+ public void leafNode(final String localName, final Object value) throws IOException, IllegalArgumentException {
Entry<NodeIdentifier, Object> dom = serializeLeaf(localName, value);
getDelegate().leafNode(dom.getKey(), dom.getValue());
};
@Override
- public void anyxmlNode(final String name, final Object value) throws IllegalArgumentException {
+ public void anyxmlNode(final String name, final Object value) throws IOException, IllegalArgumentException {
Entry<NodeIdentifier, Object> dom = serializeLeaf(name, value);
getDelegate().anyxmlNode(dom.getKey(), dom.getValue());
}
@Override
- public void leafSetEntryNode(final Object value) throws IllegalArgumentException {
+ public void leafSetEntryNode(final Object value) throws IOException, IllegalArgumentException {
LeafNodeCodecContext ctx = (LeafNodeCodecContext) current();
getDelegate().leafSetEntryNode(ctx.getValueCodec().serialize(value));
}
@Override
public void startAugmentationNode(final Class<? extends Augmentation<?>> augmentationType)
- throws IllegalArgumentException {
+ throws IOException, IllegalArgumentException {
getDelegate().startAugmentationNode(enter(augmentationType, AugmentationIdentifier.class));
}
@Override
public void startChoiceNode(final Class<? extends DataContainer> type, final int childSizeHint)
- throws IllegalArgumentException {
+ throws IOException, IllegalArgumentException {
getDelegate().startChoiceNode(enter(type, NodeIdentifier.class), childSizeHint);
}
@Override
public void startContainerNode(final Class<? extends DataObject> object, final int childSizeHint)
- throws IllegalArgumentException {
+ throws IOException, IllegalArgumentException {
getDelegate().startContainerNode(enter(object, NodeIdentifier.class), childSizeHint);
}
@Override
- public void startLeafSet(final String localName, final int childSizeHint) throws IllegalArgumentException {
+ public void startLeafSet(final String localName, final int childSizeHint) throws IOException, IllegalArgumentException {
getDelegate().startLeafSet(enter(localName, NodeIdentifier.class), childSizeHint);
};
@Override
- public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IllegalArgumentException {
+ public void startMapEntryNode(final Identifier<?> key, final int childSizeHint) throws IOException, IllegalArgumentException {
duplicateSchemaEnter();
NodeIdentifierWithPredicates identifier = ((ListNodeCodecContext) current()).serialize(key);
getDelegate().startMapEntryNode(identifier, childSizeHint);
@Override
public <T extends DataObject & Identifiable<?>> void startMapNode(final Class<T> mapEntryType,
- final int childSizeHint) throws IllegalArgumentException {
+ final int childSizeHint) throws IOException, IllegalArgumentException {
getDelegate().startMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
};
@Override
public <T extends DataObject & Identifiable<?>> void startOrderedMapNode(final Class<T> mapEntryType,
- final int childSizeHint) throws IllegalArgumentException {
+ final int childSizeHint) throws IOException, IllegalArgumentException {
getDelegate().startOrderedMapNode(enter(mapEntryType, NodeIdentifier.class), childSizeHint);
};
@Override
public void startUnkeyedList(final Class<? extends DataObject> obj, final int childSizeHint)
- throws IllegalArgumentException {
+ throws IOException, IllegalArgumentException {
getDelegate().startUnkeyedList(enter(obj, NodeIdentifier.class), childSizeHint);
};
@Override
- public void startUnkeyedListItem(final int childSizeHint) throws IllegalStateException {
+ public void startUnkeyedListItem(final int childSizeHint) throws IllegalStateException, IOException {
getDelegate().startUnkeyedListItem(duplicateSchemaEnter(), childSizeHint);
}
+ @Override
+ public void flush() throws IOException {
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
}
*/
package org.opendaylight.yangtools.yang.binding;
+import java.io.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+
/**
* Event Stream Writer for Binding Representation
* and finished using {@link #endNode()}.</li>
*
* <li><code>UnkeyedList</code> - Unkeyed list represent list without keys,
- * unkeyed list start is emmited using {@link #startUnkeyedList(Class, int)} list
- * end is emmited using {@link #endNode()}. Each list item is emmited using
+ * unkeyed list start is emitted using {@link #startUnkeyedList(Class, int)} list
+ * end is emitted using {@link #endNode()}. Each list item is emitted using
* {@link #startUnkeyedListItem()} and ended using {@link #endNode()}.</li>
* </ul>
*
* <li><code>leaf</code> - Leaf node event is emitted using
- * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emmited for
+ * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emitted for
* leaf node.</li>
*
* <li><code>leaf-list</code> - Leaf list start is emitted using
* {@link #startLeafSet(String, int)}. Leaf list end is emitted using
- * {@link #endNode()}. Leaf list entries are emmited using
+ * {@link #endNode()}. Leaf list entries are emitted using
* {@link #leafSetEntryNode(Object).
*
* <li><code>anyxml - Anyxml node event is emitted using
- * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emmited
+ * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emitted
* for anyxml node.</code></li>
*
*
- * <li><code>choice</code> Choice node event is emmited by
+ * <li><code>choice</code> Choice node event is emitted by
* {@link #startChoiceNode(Class, int)} event and must be immediately followed by
- * {@link #startCase(Class, int)} event. Choice node is finished by emitting
+ * {@link #startCase(Class, int)} event. Choice node is finished by emitting an
* {@link #endNode()} event.</li>
*
* <li>
* <code>case</code> - Case node may be emitted only inside choice node by
- * invoking {@link #startCase(Class, int)}. Case node is finished be emitting
+ * invoking {@link #startCase(Class, int)}. Case node is finished be emitting an
* {@link #endNode()} event.</li>
*
* <li>
*
*
*/
-public interface BindingStreamEventWriter {
+public interface BindingStreamEventWriter extends Closeable, Flushable {
/**
* Methods in this interface allow users to hint the underlying
- * implementation about the sizing of container-like constructurs
+ * implementation about the sizing of container-like constructors
* (leafLists, containers, etc.). These hints may be taken into account by a
* particular implementation to improve performance, but clients are not
* required to provide hints. This constant should be used by clients who
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void leafNode(String localName, Object value) throws IllegalArgumentException;
+ void leafNode(String localName, Object value) throws IOException, IllegalArgumentException;
/**
*
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startLeafSet(String localName, int childSizeHint) throws IllegalArgumentException;
+ void startLeafSet(String localName, int childSizeHint) throws IOException, IllegalArgumentException;
/**
* Emits a leaf set entry node
* If emitted leaf node has invalid value.
* @throws IllegalStateException
* If node was emitted outside <code>leaf set</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void leafSetEntryNode(Object value) throws IllegalArgumentException;
+ void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException;
/**
*
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startContainerNode(Class<? extends DataObject> container, int childSizeHint) throws IllegalArgumentException;
+ void startContainerNode(Class<? extends DataObject> container, int childSizeHint) throws IOException, IllegalArgumentException;
/**
*
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startUnkeyedList(Class<? extends DataObject> localName, int childSizeHint) throws IllegalArgumentException;
+ void startUnkeyedList(Class<? extends DataObject> localName, int childSizeHint) throws IOException, IllegalArgumentException;
/**
* Emits start of new unkeyed list item.
* events than count.
* @throws IllegalStateException
* If node was emitted outside <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startUnkeyedListItem(int childSizeHint) throws IllegalStateException;
+ void startUnkeyedListItem(int childSizeHint) throws IOException, IllegalStateException;
/**
*
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
<T extends DataObject & Identifiable<?>> void startMapNode(Class<T> mapEntryType, int childSizeHint)
- throws IllegalArgumentException;
-
+ throws IOException, IllegalArgumentException;
/**
- *
- * Emits start of ordered map node event.
- *
- * <p>
- * End of map node event is emitted by invoking {@link #endNode()}. Valid
- * subevents is only {@link #startMapEntryNode(Identifier, int)}. All other methods will
- * throw {@link IllegalArgumentException}.
- *
- * @param mapEntryType
- * Class of list item, which has defined key.
- * @param childSizeHint
- * Non-negative count of expected direct child nodes or
- * {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
- * and should not fail writing of child events, if there are more
- * events than count.
- * @throws IllegalArgumentException
- * @throws IllegalStateException
- * If node was emitted inside <code>map</code>,
- * <code>choice</code> <code>unkeyed list</code> node.
- */
- <T extends DataObject & Identifiable<?>> void startOrderedMapNode(Class<T> mapEntryType, int childSizeHint)
- throws IllegalArgumentException;
+ *
+ * Emits start of ordered map node event.
+ *
+ * <p>
+ * End of map node event is emitted by invoking {@link #endNode()}. Valid
+ * subevents is only {@link #startMapEntryNode(Identifier, int)}. All other methods will
+ * throw {@link IllegalArgumentException}.
+ *
+ * @param mapEntryType
+ * Class of list item, which has defined key.
+ * @param childSizeHint
+ * Non-negative count of expected direct child nodes or
+ * {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
+ * and should not fail writing of child events, if there are more
+ * events than count.
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ * If node was emitted inside <code>map</code>,
+ * <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
+ */
+ <T extends DataObject & Identifiable<?>> void startOrderedMapNode(Class<T> mapEntryType, int childSizeHint)
+ throws IOException, IllegalArgumentException;
/**
*
* If key contains incorrect value.
* @throws IllegalStateException
* If node was emitted outside <code>map entry</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startMapEntryNode(Identifier<?> keyValues, int childSizeHint) throws IllegalArgumentException;
+ void startMapEntryNode(Identifier<?> keyValues, int childSizeHint) throws IOException, IllegalArgumentException;
/**
* Emits start of choice node.
* @throws IllegalStateException
* If node was emitted inside <code>map</code>, <code>choice
* </code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void startChoiceNode(Class<? extends DataContainer> choice, int childSizeHint) throws IllegalArgumentException;
+ void startChoiceNode(Class<? extends DataContainer> choice, int childSizeHint) throws IOException, IllegalArgumentException;
/**
*
*
* @param name
* @throws IllegalArgumentException
+ * @throws IOException if an underlying IO error occurs
*/
- void startCase(Class<? extends DataObject> caze, int childSizeHint) throws IllegalArgumentException;
+ void startCase(Class<? extends DataObject> caze, int childSizeHint) throws IOException, IllegalArgumentException;
/**
* Emits start of augmentation node.
* Local names of all valid children defined by augmentation.
* @throws IllegalArgumentException
* If augmentation is invalid in current context.
+ * @throws IOException if an underlying IO error occurs
*/
- void startAugmentationNode(Class<? extends Augmentation<?>> augmentationType) throws IllegalArgumentException;
+ void startAugmentationNode(Class<? extends Augmentation<?>> augmentationType) throws IOException, IllegalArgumentException;
/**
* Emits anyxml node event.
* @throws IllegalStateException
* If node was emitted inside <code>map</code>,
* <code>choice</code> <code>unkeyed list</code> node.
+ * @throws IOException if an underlying IO error occurs
*/
- void anyxmlNode(String name, Object value) throws IllegalArgumentException;
+ void anyxmlNode(String name, Object value) throws IOException, IllegalArgumentException;
/**
* Emits end event for node.
*
* @throws IllegalStateException If there is no open node.
+ * @throws IOException if an underlying IO error occurs
*/
- void endNode() throws IllegalStateException;
+ void endNode() throws IOException, IllegalStateException;
+
+ @Override
+ void flush() throws IOException;
+
+ @Override
+ void close() throws IOException;
}