import java.util.Map;
import java.util.Map.Entry;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
/**
* Serialization service, which provides two-way serialization between
* Java Binding Data representation and NormalizedNode representation.
- *
*/
public interface BindingNormalizedNodeSerializer {
* @param binding Binding Instance Identifier
* @return DOM Instance Identifier
*/
- YangInstanceIdentifier toYangInstanceIdentifier(InstanceIdentifier<?> binding);
+ YangInstanceIdentifier toYangInstanceIdentifier(@Nonnull InstanceIdentifier<?> binding);
/**
* Translates supplied YANG Instance Identifier into Binding instance identifier.
*
* @param dom YANG Instance Identifier
- * @return Binding Instance Identifier
+ * @return Binding Instance Identifier, or null if the instance identifier is not
+ * representable.
*/
- InstanceIdentifier<?> fromYangInstanceIdentifier(YangInstanceIdentifier dom);
+ @Nullable InstanceIdentifier<?> fromYangInstanceIdentifier(@Nonnull YangInstanceIdentifier dom);
/**
* Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
* @param data Data object representing data
* @return NormalizedNode representation
*/
- <T extends DataObject> Entry<YangInstanceIdentifier,NormalizedNode<?,?>> toNormalizedNode(InstanceIdentifier<T> path, T data);
+ <T extends DataObject> Entry<YangInstanceIdentifier, NormalizedNode<?,?>> toNormalizedNode(InstanceIdentifier<T> path, T data);
/**
* Translates supplied YANG Instance Identifier and NormalizedNode into Binding data.
* @param data NormalizedNode representing data
* @return DOM Instance Identifier
*/
- Entry<InstanceIdentifier<?>,DataObject> fromNormalizedNode(YangInstanceIdentifier path, NormalizedNode<?, ?> data);
+ @Nullable Entry<InstanceIdentifier<?>,DataObject> fromNormalizedNode(@Nonnull YangInstanceIdentifier path, NormalizedNode<?, ?> data);
/**
* Returns map view which contains translated set of entries to normalized nodes.
import java.lang.reflect.Type;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.opendaylight.yangtools.binding.data.codec.impl.NodeCodecContext.CodecContextFactory;
import org.opendaylight.yangtools.concepts.Codec;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
class BindingCodecContext implements CodecContextFactory, Immutable {
-
+ private static final Logger LOG = LoggerFactory.getLogger(BindingCodecContext.class);
private static final String GETTER_PREFIX = "get";
+
private final SchemaRootCodecContext root;
private final BindingRuntimeContext context;
private final Codec<YangInstanceIdentifier, InstanceIdentifier<?>> instanceIdentifierCodec =
return context;
}
- public Codec<YangInstanceIdentifier, InstanceIdentifier<?>> getInstanceIdentifierCodec() {
+ Codec<YangInstanceIdentifier, InstanceIdentifier<?>> getInstanceIdentifierCodec() {
return instanceIdentifierCodec;
}
return currentNode;
}
- public NodeCodecContext getCodecContextNode(final YangInstanceIdentifier dom,
- final List<InstanceIdentifier.PathArgument> builder) {
+ /**
+ * Multi-purpose utility function. Traverse the codec tree, looking for
+ * the appropriate codec for the specified {@link YangInstanceIdentifier}.
+ * As a side-effect, gather all traversed binding {@link InstanceIdentifier.PathArgument}s
+ * into the supplied collection.
+ *
+ * @param dom {@link YangInstanceIdentifier} which is to be translated
+ * @param bindingArguments Collection for traversed path arguments
+ * @return Codec for target node, or @null if the node does not have a
+ * binding representation (choice, case, leaf).
+ */
+ @Nullable NodeCodecContext getCodecContextNode(final @Nonnull YangInstanceIdentifier dom,
+ final @Nonnull Collection<InstanceIdentifier.PathArgument> bindingArguments) {
NodeCodecContext currentNode = root;
ListNodeCodecContext currentList = null;
+
for (YangInstanceIdentifier.PathArgument domArg : dom.getPathArguments()) {
- Preconditions.checkArgument(currentNode instanceof DataContainerCodecContext<?>);
- DataContainerCodecContext<?> previous = (DataContainerCodecContext<?>) currentNode;
- NodeCodecContext nextNode = previous.getYangIdentifierChild(domArg);
+ Preconditions.checkArgument(currentNode instanceof DataContainerCodecContext<?>, "Unexpected child of non-container node %s", currentNode);
+ final DataContainerCodecContext<?> previous = (DataContainerCodecContext<?>) currentNode;
+ final NodeCodecContext nextNode = previous.getYangIdentifierChild(domArg);
+
/*
* List representation in YANG Instance Identifier consists of two
* arguments: first is list as a whole, second is list as an item so
* Identifier as Item or IdentifiableItem
*/
if (currentList != null) {
+ Preconditions.checkArgument(currentList == nextNode, "List should be referenced two times in YANG Instance Identifier %s", dom);
- if (currentList == nextNode) {
-
- // We entered list, so now we have all information to emit
- // list
- // path using second list argument.
- builder.add(currentList.getBindingPathArgument(domArg));
- currentList = null;
- currentNode = nextNode;
- } else {
- throw new IllegalArgumentException(
- "List should be referenced two times in YANG Instance Identifier");
- }
+ // We entered list, so now we have all information to emit
+ // list path using second list argument.
+ bindingArguments.add(currentList.getBindingPathArgument(domArg));
+ currentList = null;
+ currentNode = nextNode;
} else if (nextNode instanceof ListNodeCodecContext) {
// We enter list, we do not update current Node yet,
// since we need to verify
// it is not supported by binding instance identifier.
currentNode = nextNode;
} else if (nextNode instanceof DataContainerCodecContext<?>) {
- builder.add(((DataContainerCodecContext<?>) nextNode).getBindingPathArgument(domArg));
+ bindingArguments.add(((DataContainerCodecContext<?>) nextNode).getBindingPathArgument(domArg));
currentNode = nextNode;
} else if (nextNode instanceof LeafNodeCodecContext) {
- Preconditions.checkArgument(builder == null, "Instance Identifier for leaf is not representable.");
-
+ LOG.debug("Instance identifier referencing a leaf is not representable (%s)", dom);
+ return null;
}
}
+
// Algorithm ended in list as whole representation
// we sill need to emit identifier for list
-
- if (builder != null) {
- Preconditions.checkArgument(!(currentNode instanceof ChoiceNodeCodecContext),
- "Instance Identifier for choice is not representable.");
- Preconditions.checkArgument(!(currentNode instanceof CaseNodeCodecContext),
- "Instance Identifier for case is not representable.");
+ if (currentNode instanceof ChoiceNodeCodecContext) {
+ LOG.debug("Instance identifier targeting a choice is not representable (%s)", dom);
+ return null;
+ }
+ if (currentNode instanceof CaseNodeCodecContext) {
+ LOG.debug("Instance identifier targeting a case is not representable (%s)", dom);
+ return null;
}
+
if (currentList != null) {
- builder.add(currentList.getBindingPathArgument(null));
+ bindingArguments.add(currentList.getBindingPathArgument(null));
return currentList;
}
return currentNode;
@Override
public YangInstanceIdentifier serialize(final InstanceIdentifier<?> input) {
- List<YangInstanceIdentifier.PathArgument> domArgs = new LinkedList<>();
+ List<YangInstanceIdentifier.PathArgument> domArgs = new ArrayList<>();
getCodecContextNode(input, domArgs);
return YangInstanceIdentifier.create(domArgs);
}
@Override
public InstanceIdentifier<?> deserialize(final YangInstanceIdentifier input) {
- List<InstanceIdentifier.PathArgument> builder = new LinkedList<>();
- getCodecContextNode(input, builder);
- return InstanceIdentifier.create(builder);
+ final List<InstanceIdentifier.PathArgument> builder = new ArrayList<>();
+ final NodeCodecContext codec = getCodecContextNode(input, builder);
+ return codec == null ? null : InstanceIdentifier.create(builder);
}
}
import java.io.IOException;
import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Override
public InstanceIdentifier<?> fromYangInstanceIdentifier(final YangInstanceIdentifier dom) {
return codecContext.getInstanceIdentifierCodec().deserialize(dom);
- }
+ }
@Override
public <T extends DataObject> Entry<YangInstanceIdentifier,NormalizedNode<?,?>> toNormalizedNode(final InstanceIdentifier<T> path, final T data) {
NormalizedNodeResult result = new NormalizedNodeResult();
- // We create dom stream writer which produces normalized nodes
+ // We create DOM stream writer which produces normalized nodes
NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
- // We create Binding Stream Writer wchich translates from Binding to Normalized Nodes
+ // We create Binding Stream Writer which 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
return new SimpleEntry<YangInstanceIdentifier,NormalizedNode<?,?>>(writeCtx.getKey(),result.getResult());
}
- private boolean isBindingRepresentable(final NormalizedNode<?, ?> data) {
- return !(data instanceof MapNode) && !(data instanceof UnkeyedListNode) && !(data instanceof LeafSetNode) && !(data instanceof LeafNode<?>);
- }
+ private static boolean isBindingRepresentable(final NormalizedNode<?, ?> data) {
+ if (data instanceof LeafNode<?>) {
+ return false;
+ }
+ if (data instanceof LeafSetNode) {
+ return false;
+ }
+ if (data instanceof MapNode) {
+ return false;
+ }
+ if (data instanceof UnkeyedListNode) {
+ return false;
+ }
+ return true;
+ }
@Override
- public Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final YangInstanceIdentifier path,
- final NormalizedNode<?, ?> data) {
- List<PathArgument> builder = new LinkedList<>();
- if(isBindingRepresentable(data)) {
- DataObject lazyObj = (DataObject) codecContext.getCodecContextNode(path, builder).dataFromNormalizedNode(data);
- InstanceIdentifier<?> bindingPath = InstanceIdentifier.create(builder);
- return new SimpleEntry<InstanceIdentifier<?>, DataObject>(bindingPath,lazyObj);
+ public Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
+ if (!isBindingRepresentable(data)) {
+ return null;
}
- return null;
+
+ final List<PathArgument> builder = new ArrayList<>();
+ final NodeCodecContext codec = codecContext.getCodecContextNode(path, builder);
+ if (codec == null) {
+ // FIXME: do we allow data == null?
+ if (data != null) {
+ LOG.warn("Path %s does not have a binding equivalent, should have been caught earlier", path);
+ }
+ return null;
+ }
+
+ final DataObject lazyObj = (DataObject) codec.dataFromNormalizedNode(data);
+ final InstanceIdentifier<?> bindingPath = InstanceIdentifier.create(builder);
+ return new SimpleEntry<InstanceIdentifier<?>, DataObject>(bindingPath, lazyObj);
}
@Override
- public Map<InstanceIdentifier<?>, DataObject> fromNormalizedNodes(
- final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> dom) {
+ public Map<InstanceIdentifier<?>, DataObject> fromNormalizedNodes(final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> dom) {
throw new UnsupportedOperationException("Not implemented yet");
}
private static class DeserializeFunction<T> implements Function<Optional<NormalizedNode<?, ?>>, Optional<T>> {
-
private final DataObjectCodecContext<?> ctx;
+
public DeserializeFunction(final DataObjectCodecContext<?> ctx) {
super();
this.ctx = ctx;
}
return Optional.absent();
}
-
-
}
private class GeneratorLoader extends CacheLoader<Class<? extends DataObject>, DataObjectSerializer> {