Take advantage of YangInstanceIdentifier methods 02/21002/4
authorRobert Varga <rovarga@cisco.com>
Fri, 22 May 2015 16:14:47 +0000 (18:14 +0200)
committerRobert Varga <rovarga@cisco.com>
Sun, 24 May 2015 08:14:49 +0000 (10:14 +0200)
YangInstanceIdentifier provides new, fast methods to acquire parent,
check if it empty and size of path arguments. Take advantage of them.

Change-Id: I527ff8cefb2799cb188ffe294c69e3b81a0d6909
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/AbstractWriteTransaction.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java
opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java

index 4597f0646c030b311ac205613c8d3d4f33a05d39..7e1f112c4ad4ffc20d0e67a3069f434292c47ef7 100644 (file)
@@ -9,9 +9,7 @@ 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;
@@ -20,7 +18,6 @@ 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;
 
 /**
@@ -98,25 +95,20 @@ public abstract class AbstractWriteTransaction<T extends DOMDataWriteTransaction
     private void ensureListParentIfNeeded(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
             final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalized) {
         if (Identifiable.class.isAssignableFrom(path.getTargetType())) {
-            YangInstanceIdentifier parentMapPath = getParent(normalized.getKey()).get();
+            YangInstanceIdentifier parentMapPath = normalized.getKey().getParent();
+            Preconditions.checkArgument(parentMapPath != null, "Map path %s does not have a parent", path);
+
             NormalizedNode<?, ?> emptyParent = getCodec().getDefaultNodeFor(parentMapPath);
             getDelegate().merge(store, parentMapPath, emptyParent);
         }
     }
 
-    // FIXME (should be probaly part of InstanceIdentifier)
-    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(YangInstanceIdentifier.create(Iterables.limit(mapEntryItemPath,  parentPathSize)));
-        } else if(parentPathSize == 0) {
-            return Optional.of(YangInstanceIdentifier.create(Collections.<PathArgument>emptyList()));
-        } else {
-            return Optional.absent();
-        }
+    /**
+     * @deprecated Use {@link YangInstanceIdentifier#getParent()} instead.
+     */
+    @Deprecated
+    protected static Optional<YangInstanceIdentifier> getParent(final YangInstanceIdentifier child) {
+        return Optional.fromNullable(child.getParent());
     }
 
     /**
index 1ea94e9862a455bf7b4b5d2fa976d2c065525aa8..35776bb1a9aa887f38004dba005e48d327262bff 100644 (file)
 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
 import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -270,9 +270,8 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
     }
 
     private void writeYangInstanceIdentifierInternal(YangInstanceIdentifier identifier) throws IOException {
-        Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
-        int size = Iterables.size(pathArguments);
-        output.writeInt(size);
+        Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
+        output.writeInt(pathArguments.size());
 
         for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
             writePathArgument(pathArgument);
index ca1531b896b70eaa5bc050dcea76b307ccb766b6..d9121fb42697c7bb0e28359ffdf4718530c6e5f8 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.controller.sal.connect.netconf.util;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import java.io.IOException;
 import java.net.URI;
@@ -265,7 +264,7 @@ public class NetconfMessageTransformUtil {
                                                                      final Optional<ModifyAction> operation, final Optional<NormalizedNode<?, ?>> lastChildOverride) {
         final NormalizedNode<?, ?> configContent;
 
-        if(Iterables.isEmpty(dataPath.getPathArguments())) {
+        if (dataPath.isEmpty()) {
             Preconditions.checkArgument(lastChildOverride.isPresent(), "Data has to be present when creating structure for top level element");
             Preconditions.checkArgument(lastChildOverride.get() instanceof DataContainerChild<?, ?>,
                     "Data has to be either container or a list node when creating structure for top level element, but was: %s", lastChildOverride.get());
index c329f525a952d162eb96c5ba960dc002ecf3f9cc..51ca1bac39e827c7df9563f227aa170c1148e3db 100644 (file)
@@ -30,7 +30,6 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -541,7 +540,7 @@ public class RestconfImpl implements RestconfService {
 
         final YangInstanceIdentifier pathIdentifier = ((YangInstanceIdentifier) pathValue);
         String streamName = null;
-        if (!Iterables.isEmpty(pathIdentifier.getPathArguments())) {
+        if (!pathIdentifier.isEmpty()) {
             final String fullRestconfIdentifier = controllerContext.toFullRestconfIdentifier(pathIdentifier, null);
 
             LogicalDatastoreType datastore = parseEnumTypeParameter(value, LogicalDatastoreType.class,
@@ -786,19 +785,18 @@ public class RestconfImpl implements RestconfService {
             final YangInstanceIdentifier identifier) {
 
         final String payloadName = node.getData().getNodeType().getLocalName();
-        final Iterator<PathArgument> pathArguments = identifier.getReversePathArguments().iterator();
 
         //no arguments
-        if ( ! pathArguments.hasNext()) {
+        if (identifier.isEmpty()) {
             //no "data" payload
-            if ( ! node.getData().getNodeType().equals(NETCONF_BASE_QNAME)) {
+            if (!node.getData().getNodeType().equals(NETCONF_BASE_QNAME)) {
                 throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument",
                         ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
             }
         //any arguments
         } else {
-            final String identifierName = pathArguments.next().getNodeType().getLocalName();
-            if ( ! payloadName.equals(identifierName)) {
+            final String identifierName = identifier.getLastPathArgument().getNodeType().getLocalName();
+            if (!payloadName.equals(identifierName)) {
                 throw new RestconfDocumentedException("Payload name (" + payloadName
                         + ") is different from identifier name (" + identifierName + ")", ErrorType.PROTOCOL,
                         ErrorTag.MALFORMED_MESSAGE);