BUG-1690: catch wildcard InstanceIdentifiers 67/10767/2
authorRobert Varga <rovarga@cisco.com>
Thu, 4 Sep 2014 12:19:01 +0000 (14:19 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 4 Sep 2014 20:30:21 +0000 (20:30 +0000)
Read/write/merge/delete of wildcarded instance identifiers is not
defined, so catch them early on in the binding broker implementation.

Change-Id: I97161f88b8496a78575d6a7bc1b7bab8fee1da5b
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/AbstractForwardedTransaction.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/AbstractWriteTransaction.java

index 088e228..eadde73 100644 (file)
@@ -58,6 +58,7 @@ abstract class AbstractForwardedTransaction<T extends AsyncTransaction<YangInsta
     protected final <D extends DataObject> CheckedFuture<Optional<D>,ReadFailedException> doRead(
             final DOMDataReadTransaction readTx, final LogicalDatastoreType store,
             final InstanceIdentifier<D> path) {
+        Preconditions.checkArgument(!path.isWildcarded(), "Invalid read of wildcarded path %s", path);
 
         return MappingCheckedFuture.create(
                     Futures.transform(readTx.read(store, codec.toNormalized(path)),
index 65a9c1a..4597f06 100644 (file)
@@ -7,23 +7,21 @@
  */
 package org.opendaylight.controller.md.sal.binding.impl;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.CheckedFuture;
 import java.util.Collections;
 import java.util.Map.Entry;
-
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Identifiable;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+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.NormalizedNode;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Optional;
-import com.google.common.collect.Iterables;
-import com.google.common.util.concurrent.CheckedFuture;
 
 /**
  *
@@ -33,34 +31,30 @@ import com.google.common.util.concurrent.CheckedFuture;
 public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction> extends
         AbstractForwardedTransaction<T> {
 
-    private static final Logger LOG = LoggerFactory.getLogger(AbstractWriteTransaction.class);
-
-    protected AbstractWriteTransaction(final T delegate,
-            final BindingToNormalizedNodeCodec codec) {
+    protected AbstractWriteTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
         super(delegate, codec);
     }
 
-
     public final <U extends DataObject> void put(final LogicalDatastoreType store,
             final InstanceIdentifier<U> path, final U data, final boolean createParents) {
-       final Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec()
-                .toNormalizedNode(path, data);
-        if(createParents) {
+        Preconditions.checkArgument(!path.isWildcarded(), "Cannot put data into wildcarded path %s", path);
+
+        final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
+        if (createParents) {
             ensureParentsByMerge(store, normalized.getKey(), path);
         } else {
             ensureListParentIfNeeded(store,path,normalized);
         }
+
         getDelegate().put(store, normalized.getKey(), normalized.getValue());
     }
 
-
     public final <U extends DataObject> void merge(final LogicalDatastoreType store,
             final InstanceIdentifier<U> path, final U data,final boolean createParents) {
+        Preconditions.checkArgument(!path.isWildcarded(), "Cannot merge data into wildcarded path %s", path);
 
-        final Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec()
-                .toNormalizedNode(path, data);
-
-        if(createParents) {
+        final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec().toNormalizedNode(path, data);
+        if (createParents) {
             ensureParentsByMerge(store, normalized.getKey(), path);
         } else {
             ensureListParentIfNeeded(store,path,normalized);
@@ -69,7 +63,6 @@ public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction
         getDelegate().merge(store, normalized.getKey(), normalized.getValue());
     }
 
-
     /**
      *
      * Ensures list parent if item is list, otherwise noop.
@@ -103,25 +96,24 @@ public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction
      * @param normalized Normalized version of data to be written
      */
     private void ensureListParentIfNeeded(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
-            final Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, NormalizedNode<?, ?>> normalized) {
-        if(Identifiable.class.isAssignableFrom(path.getTargetType())) {
-            org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier parentMapPath = getParent(normalized.getKey()).get();
+            final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized) {
+        if (Identifiable.class.isAssignableFrom(path.getTargetType())) {
+            YangInstanceIdentifier parentMapPath = getParent(normalized.getKey()).get();
             NormalizedNode<?, ?> emptyParent = getCodec().getDefaultNodeFor(parentMapPath);
             getDelegate().merge(store, parentMapPath, emptyParent);
         }
-
     }
 
     // FIXME (should be probaly part of InstanceIdentifier)
-    protected static Optional<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> getParent(
-            final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier child) {
+    protected static Optional<YangInstanceIdentifier> getParent(
+            final YangInstanceIdentifier child) {
 
         Iterable<PathArgument> mapEntryItemPath = child.getPathArguments();
         int parentPathSize = Iterables.size(mapEntryItemPath) - 1;
-        if(parentPathSize > 1) {
-            return Optional.of(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create(Iterables.limit(mapEntryItemPath,  parentPathSize)));
+        if (parentPathSize > 1) {
+            return Optional.of(YangInstanceIdentifier.create(Iterables.limit(mapEntryItemPath,  parentPathSize)));
         } else if(parentPathSize == 0) {
-            return Optional.of(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create(Collections.<PathArgument>emptyList()));
+            return Optional.of(YangInstanceIdentifier.create(Collections.<PathArgument>emptyList()));
         } else {
             return Optional.absent();
         }
@@ -136,11 +128,13 @@ public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction
      * @param path
      */
     protected abstract void ensureParentsByMerge(LogicalDatastoreType store,
-            org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier key, InstanceIdentifier<?> path);
+            YangInstanceIdentifier key, InstanceIdentifier<?> path);
 
     protected final void doDelete(final LogicalDatastoreType store,
             final InstanceIdentifier<?> path) {
-        final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier normalized = getCodec().toNormalized(path);
+        Preconditions.checkArgument(!path.isWildcarded(), "Cannot delete wildcarded path %s", path);
+
+        final YangInstanceIdentifier normalized = getCodec().toNormalized(path);
         getDelegate().delete(store, normalized);
     }