Binding2 runtime - Codecs impl - context 25/58925/1
authorJakub Toth <jakub.toth@pantheon.tech>
Wed, 7 Jun 2017 09:18:30 +0000 (11:18 +0200)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Wed, 14 Jun 2017 08:25:13 +0000 (08:25 +0000)
  * derived contexts
  * fixed based contexts

Change-Id: I2900f4356a7750553180a043d450b3f279c99d63
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
(cherry picked from commit fbbb121f810f6fd663a4fc7e3f8a8bddda0d21b5)

binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/CaseNodeCodecContext.java [new file with mode: 0644]
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/KeyedListNodeCodecContext.java [new file with mode: 0644]
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/ListNodeCodecContext.java [new file with mode: 0644]
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/base/DataContainerCodecContext.java
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/base/DataContainerCodecPrototype.java
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/base/LeafNodeCodecContext.java
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/base/NodeCodecContext.java
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/base/TreeNodeCodecContext.java

diff --git a/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/CaseNodeCodecContext.java b/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/CaseNodeCodecContext.java
new file mode 100644 (file)
index 0000000..c1bd1b8
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.impl.context;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.TreeNodeCodecContext;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+
+/**
+ * Codec context for serializing and deserializing choice case node and it's
+ * path.
+ *
+ * @param <D>
+ *            - type of tree node
+ */
+@Beta
+public final class CaseNodeCodecContext<D extends TreeNode> extends TreeNodeCodecContext<D, ChoiceCaseNode> {
+
+    /**
+     * Prepare context for choice case node from prototype.
+     *
+     * @param prototype
+     *            - codec prototype of choice case node
+     */
+    public CaseNodeCodecContext(final DataContainerCodecPrototype<ChoiceCaseNode> prototype) {
+        super(prototype);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    protected void addYangPathArgument(final TreeArgument arg, final List<PathArgument> builder) {
+        // add your implementation
+    }
+
+    @Nonnull
+    @Override
+    public D deserialize(@Nonnull final NormalizedNode<?, ?> normalizedNode) {
+        Preconditions.checkState(normalizedNode instanceof ChoiceNode);
+        return createBindingProxy((ChoiceNode) normalizedNode);
+    }
+
+    @Override
+    protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
+        return deserialize(normalizedNode);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public PathArgument serializePathArgument(final TreeArgument arg) {
+        Preconditions.checkArgument(arg == null);
+        return null;
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
+        Preconditions.checkArgument(arg == null);
+        return null;
+    }
+}
diff --git a/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/KeyedListNodeCodecContext.java b/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/KeyedListNodeCodecContext.java
new file mode 100644 (file)
index 0000000..2a093a0
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.impl.context;
+
+import com.google.common.annotations.Beta;
+import java.lang.reflect.Method;
+import java.util.List;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
+import org.opendaylight.mdsal.binding.javav2.spec.base.IdentifiableItem;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
+import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+
+/**
+ * Codec context for serializing and deserializing keyed list node and it's
+ * path.
+ *
+ * @param <D>
+ *            - type of tree node
+ */
+@Beta
+public final class KeyedListNodeCodecContext<D extends TreeNode & Identifiable<?>> extends ListNodeCodecContext<D> {
+
+    private final Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> codec;
+    private final Method keyGetter;
+
+    /**
+     * Prepare context for keyed list node from prototype.
+     *
+     * @param prototype
+     *            - codec prototype of keyed list node
+     */
+    KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
+        super(prototype);
+        this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema());
+        try {
+            this.keyGetter = getBindingClass().getMethod("getKey");
+        } catch (final NoSuchMethodException e) {
+            throw new IllegalStateException("Required method not available", e);
+        }
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public void addYangPathArgument(final TreeArgument arg, final List<PathArgument> builder) {
+        /*
+         * DOM Instance Identifier for list is always represent by two entries
+         * one for map and one for children. This is also true for wildcarded
+         * instance identifiers
+         */
+        if (builder == null) {
+            return;
+        }
+
+        super.addYangPathArgument(arg, builder);
+        if (arg instanceof IdentifiableItem<?, ?>) {
+            builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
+        } else {
+            // Adding wildcarded
+            super.addYangPathArgument(arg, builder);
+        }
+    }
+
+    @Override
+    @SuppressWarnings("rawtypes")
+    public Object getBindingChildValue(final Method method, final NormalizedNodeContainer dom) {
+        if (dom instanceof MapEntryNode && keyGetter.equals(method)) {
+            final NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
+            return codec.deserialize(identifier).getKey();
+        } else {
+            return super.getBindingChildValue(method, dom);
+        }
+    }
+
+    @Override
+    protected TreeArgument<?> getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
+        if (domArg instanceof NodeIdentifierWithPredicates) {
+            return codec.deserialize((NodeIdentifierWithPredicates) domArg);
+        }
+        return super.getBindingPathArgument(domArg);
+    }
+
+    @SuppressWarnings("rawtypes")
+    public NodeIdentifierWithPredicates serialize(final IdentifiableItem keyValues) {
+        return codec.serialize(keyValues);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public YangInstanceIdentifier.PathArgument serializePathArgument(final TreeArgument arg) {
+        if (arg instanceof IdentifiableItem) {
+            return codec.serialize((IdentifiableItem<?, ?>) arg);
+        }
+        return super.serializePathArgument(arg);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Override
+    public TreeArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
+        if (arg instanceof NodeIdentifierWithPredicates) {
+            return codec.deserialize((NodeIdentifierWithPredicates) arg);
+        }
+        return super.deserializePathArgument(arg);
+    }
+}
+
diff --git a/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/ListNodeCodecContext.java b/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/context/ListNodeCodecContext.java
new file mode 100644 (file)
index 0000000..dcb8c94
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.codec.impl.context;
+
+import com.google.common.annotations.Beta;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.TreeNodeCodecContext;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+
+/**
+ * Codec context for serializing and deserializing list node.
+ *
+ * @param <D>
+ *            - type of tree node
+ */
+@Beta
+class ListNodeCodecContext<D extends TreeNode> extends TreeNodeCodecContext<D, ListSchemaNode> {
+
+    /**
+     * Prepare context for list node from prototype.
+     *
+     * @param prototype
+     *            - codec prototype of list node
+     */
+    protected ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
+        super(prototype);
+    }
+
+    @Nonnull
+    @Override
+    public D deserialize(@Nonnull final NormalizedNode<?, ?> node) {
+        if (node instanceof MapEntryNode) {
+            return fromMapEntry((MapEntryNode) node);
+        } else if (node instanceof UnkeyedListEntryNode) {
+            return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
+        } else {
+            throw new IllegalStateException("Unsupported data type " + node.getClass());
+        }
+    }
+
+    @Override
+    protected Object deserializeObject(final NormalizedNode<?, ?> node) {
+        if (node instanceof MapNode) {
+            return fromMap((MapNode) node);
+        } else if (node instanceof MapEntryNode) {
+            return fromMapEntry((MapEntryNode) node);
+        } else if (node instanceof UnkeyedListNode) {
+            return fromUnkeyedList((UnkeyedListNode) node);
+        } else if (node instanceof UnkeyedListEntryNode) {
+            return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
+        } else {
+            throw new IllegalStateException("Unsupported data type " + node.getClass());
+        }
+    }
+
+    private List<D> fromMap(final MapNode nodes) {
+        final List<D> ret = new ArrayList<>(nodes.getValue().size());
+        for (final MapEntryNode node : nodes.getValue()) {
+            ret.add(fromMapEntry(node));
+        }
+        return ret;
+    }
+
+    private D fromMapEntry(final MapEntryNode node) {
+        return createBindingProxy(node);
+    }
+
+    private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
+        return createBindingProxy(node);
+    }
+
+    private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
+        // FIXME: Could be this lazy transformed list?
+        final List<D> ret = new ArrayList<>(nodes.getValue().size());
+        for (final UnkeyedListEntryNode node : nodes.getValue()) {
+            ret.add(fromUnkeyedListEntry(node));
+        }
+        return ret;
+    }
+}
\ No newline at end of file
index 333ef2a27604cd8eb22167664944eb4dd1a9fe99..bb7b4b586c72249ba5bbff648474e84339ede5dd 100644 (file)
@@ -32,7 +32,7 @@ import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
 
 @Beta
-abstract class DataContainerCodecContext<D extends TreeNode, T> extends NodeCodecContext<D> {
+public abstract class DataContainerCodecContext<D extends TreeNode, T> extends NodeCodecContext<D> {
 
     private final DataContainerCodecPrototype<T> prototype;
     private volatile TreeNodeSerializer eventStreamSerializer;
@@ -190,7 +190,7 @@ abstract class DataContainerCodecContext<D extends TreeNode, T> extends NodeCode
         throw IncorrectNestingException.create(message, args);
     }
 
-    TreeNodeSerializer eventStreamSerializer() {
+    public TreeNodeSerializer eventStreamSerializer() {
         if(eventStreamSerializer == null) {
             eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
         }
index c4a4f60d91bb49ecd3e93309f956720e477e703b..2a531bbc0c38ac25109e06ac5d10c9dccd7dd86c 100644 (file)
@@ -29,7 +29,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
 
 @Beta
-final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
+public final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
 
     private final T schema;
     private final QNameModule namespace;
index f6835b3292c6f2b97bc1acd4efd6d1cffafff960..83f109afb3a25d34a4290785b63b60d9370cd388 100644 (file)
@@ -41,7 +41,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 
 @Beta
-final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D> implements NodeContextSupplier {
+public final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D> implements NodeContextSupplier {
 
     private final YangInstanceIdentifier.PathArgument yangIdentifier;
     private final Codec<Object, Object> valueCodec;
@@ -129,11 +129,11 @@ final class LeafNodeCodecContext<D extends TreeNode> extends NodeCodecContext<D>
     }
 
     @Override
-    protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
+    public YangInstanceIdentifier.PathArgument getDomPathArgument() {
         return yangIdentifier;
     }
 
-    protected Codec<Object, Object> getValueCodec() {
+    public Codec<Object, Object> getValueCodec() {
         return valueCodec;
     }
 
index 196eb861dca8bcde9509e3496519eee7fbaa8a21..d53b3bcd5883720c1ba92f24740d36b7c6ba7926 100644 (file)
@@ -41,7 +41,7 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
  *
  */
 @Beta
-abstract class NodeCodecContext<D extends TreeNode> implements BindingTreeNodeCodec<D>{
+public abstract class NodeCodecContext<D extends TreeNode> implements BindingTreeNodeCodec<D> {
     /**
      * Returns Yang Instance Identifier Path Argument of current node
      *
index 94c2810d9adebbee5d03eddba220713dd70ac25e..749a7c594d2de68042bb747f5cdb2f9204e8d424 100644 (file)
@@ -57,7 +57,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Beta
-abstract class TreeNodeCodecContext<D extends TreeNode, T extends DataNodeContainer>
+public abstract class TreeNodeCodecContext<D extends TreeNode, T extends DataNodeContainer>
         extends DataContainerCodecContext<D, T> {
 
     private static final Logger LOG = LoggerFactory.getLogger(TreeNodeCodecContext.class);
@@ -203,7 +203,7 @@ abstract class TreeNodeCodecContext<D extends TreeNode, T extends DataNodeContai
                 "Argument %s is not valid child of %s", arg, getSchema()).get();
     }
 
-    protected final LeafNodeCodecContext<?> getLeafChild(final String name) {
+    public final LeafNodeCodecContext<?> getLeafChild(final String name) {
         final LeafNodeCodecContext<?> value = leafChild.get(name);
         return IncorrectNestingException.checkNonNull(value, "Leaf %s is not valid for %s", name, getBindingClass());
     }
@@ -315,7 +315,7 @@ abstract class TreeNodeCodecContext<D extends TreeNode, T extends DataNodeContai
     }
 
     @SuppressWarnings("rawtypes")
-    Object getBindingChildValue(final Method method, final NormalizedNodeContainer domData) {
+    public Object getBindingChildValue(final Method method, final NormalizedNodeContainer domData) {
         final NodeCodecContext<?> childContext = byMethod.get(method).get();
         @SuppressWarnings("unchecked")
         final Optional<NormalizedNode<?, ?>> domChild = domData.getChild(childContext.getDomPathArgument());