Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / util / InstanceIdentifierUtils.java
index 64a1e3a18a4a89fb2af3c681a93834d9eeb1589d..312eeef6d5bf7c53ebf2a1c09886ba6690a37512 100644 (file)
@@ -1,15 +1,19 @@
 /*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
- *  Copyright (c) 2014 Cisco Systems, Inc. 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
- *
+ * 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.controller.cluster.datastore.util;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.PathArgumentSerializer;
@@ -21,19 +25,17 @@ import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessa
 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.InstanceIdentifier.Builder;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 /**
  * This class contains utility methods for converting an MD-SAL
  * YangInstanceIdentifier to and from other representations.
- * <p>
+ * <p/>
  * The representations convered for now are,
  *
  * <ul>
@@ -42,12 +44,10 @@ import java.util.Set;
  * </ul>
  */
 public class InstanceIdentifierUtils {
-
-    protected static final Logger logger = LoggerFactory
-        .getLogger(InstanceIdentifierUtils.class);
+    private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierUtils.class);
 
     /**
-     * Convert an MD-SAL YangInstanceIdentifier into a protocol buffer version of it
+     * Convert an MD-SAL YangInstanceIdentifier into a protocol buffer version of it.
      *
      * @param path an MD-SAL YangInstanceIdentifier
      * @return a protocol buffer version of the MD-SAL YangInstanceIdentifier
@@ -63,18 +63,18 @@ public class InstanceIdentifierUtils {
         return toSerializableBuilder(path, context).build();
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     private static NormalizedNodeMessages.InstanceIdentifier.Builder toSerializableBuilder(
             YangInstanceIdentifier path, QNameSerializationContext context) {
         NormalizedNodeMessages.InstanceIdentifier.Builder builder =
             NormalizedNodeMessages.InstanceIdentifier.newBuilder();
 
         try {
-            for (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.
-                                            PathArgument pathArgument : path.getPathArguments()) {
+            for (PathArgument pathArgument : path.getPathArguments()) {
                 NormalizedNodeMessages.PathArgument serializablePathArgument;
-                if(context == null) {
+                if (context == null) {
                     String nodeType = "";
-                    if(!(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier)){
+                    if (!(pathArgument instanceof AugmentationIdentifier)) {
                         nodeType = pathArgument.getNodeType().toString();
                     }
 
@@ -89,8 +89,8 @@ public class InstanceIdentifierUtils {
 
                 builder.addArguments(serializablePathArgument);
             }
-        } catch(Exception e){
-            logger.error("An exception occurred", e);
+        } catch (Exception e) {
+            LOG.error("An exception occurred", e);
         }
 
         return builder;
@@ -99,7 +99,7 @@ public class InstanceIdentifierUtils {
 
     /**
      * Convert a protocol buffer version of the MD-SAL YangInstanceIdentifier into
-     * the MD-SAL version of the YangInstanceIdentifier
+     * the MD-SAL version of the YangInstanceIdentifier.
      *
      * @param path a protocol buffer version of the MD-SAL YangInstanceIdentifier
      * @return  an MD-SAL YangInstanceIdentifier
@@ -111,10 +111,10 @@ public class InstanceIdentifierUtils {
     public static YangInstanceIdentifier fromSerializable(NormalizedNodeMessages.InstanceIdentifier path,
             QNameDeSerializationContext context) {
 
-        List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
+        List<PathArgument> pathArguments = new ArrayList<>();
 
-        for(NormalizedNodeMessages.PathArgument pathArgument : path.getArgumentsList()) {
-            if(context == null || pathArgument.hasType()) {
+        for (NormalizedNodeMessages.PathArgument pathArgument : path.getArgumentsList()) {
+            if (context == null || pathArgument.hasType()) {
                 pathArguments.add(parsePathArgument(pathArgument));
             } else {
                 pathArguments.add(PathArgumentSerializer.deSerialize(context, pathArgument));
@@ -127,8 +127,7 @@ public class InstanceIdentifierUtils {
     /**
      * Take the various attributes of a PathArgument and package them up as
      * protocol buffer attributes.
-     * <p>
-     *
+     * <p/>
      * PathArguments have 4 subtypes and each of the various subtypes have
      * different attributes
      * <ul>
@@ -153,18 +152,13 @@ public class InstanceIdentifierUtils {
      *         an augmentation entry.
      *     </li>
      * </ul>
-     * @param pathArgument
-     * @return
      */
     private static Iterable<? extends NormalizedNodeMessages.Attribute> getPathArgumentAttributes(
-        YangInstanceIdentifier.PathArgument pathArgument) {
+        PathArgument pathArgument) {
         List<NormalizedNodeMessages.Attribute> attributes = new ArrayList<>();
 
-
-
-        if (pathArgument instanceof YangInstanceIdentifier.NodeWithValue) {
-            YangInstanceIdentifier.NodeWithValue identifier
-                = (YangInstanceIdentifier.NodeWithValue) pathArgument;
+        if (pathArgument instanceof NodeWithValue) {
+            NodeWithValue<?> identifier = (NodeWithValue<?>) pathArgument;
 
             NormalizedNodeMessages.Attribute attribute =
                 NormalizedNodeMessages.Attribute.newBuilder()
@@ -174,9 +168,8 @@ public class InstanceIdentifierUtils {
                     .build();
 
             attributes.add(attribute);
-        } else if (pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
-            YangInstanceIdentifier.NodeIdentifierWithPredicates identifier
-                = (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
+        } else if (pathArgument instanceof NodeIdentifierWithPredicates) {
+            NodeIdentifierWithPredicates identifier = (NodeIdentifierWithPredicates) pathArgument;
 
             for (QName key : identifier.getKeyValues().keySet()) {
                 Object value = identifier.getKeyValues().get(key);
@@ -191,9 +184,8 @@ public class InstanceIdentifierUtils {
 
             }
 
-        } else if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier) {
-            YangInstanceIdentifier.AugmentationIdentifier identifier
-                = (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
+        } else if (pathArgument instanceof AugmentationIdentifier) {
+            AugmentationIdentifier identifier = (AugmentationIdentifier) pathArgument;
 
             for (QName key : identifier.getPossibleChildNames()) {
                 Object value = key;
@@ -214,40 +206,38 @@ public class InstanceIdentifierUtils {
 
 
     /**
-     * Parse a protocol buffer PathArgument and return an MD-SAL PathArgument
+     * Parse a protocol buffer PathArgument and return an MD-SAL PathArgument.
      *
      * @param pathArgument protocol buffer PathArgument
      * @return MD-SAL PathArgument
      */
-    private static YangInstanceIdentifier.PathArgument parsePathArgument(
+    private static PathArgument parsePathArgument(
             NormalizedNodeMessages.PathArgument pathArgument) {
-        if (YangInstanceIdentifier.NodeWithValue.class.getSimpleName().equals(pathArgument.getType())) {
+        if (NodeWithValue.class.getSimpleName().equals(pathArgument.getType())) {
 
-            YangInstanceIdentifier.NodeWithValue nodeWithValue =
-                new YangInstanceIdentifier.NodeWithValue(
+            NodeWithValue<?> nodeWithValue = new NodeWithValue<>(
                     QNameFactory.create(pathArgument.getNodeType().getValue()),
                     parseAttribute(pathArgument.getAttributes(0)));
 
             return nodeWithValue;
 
-        } else if(YangInstanceIdentifier.NodeIdentifierWithPredicates.class.getSimpleName().equals(pathArgument.getType())){
+        } else if (NodeIdentifierWithPredicates.class.getSimpleName().equals(pathArgument.getType())) {
 
-            YangInstanceIdentifier.NodeIdentifierWithPredicates
-                nodeIdentifierWithPredicates =
-                new YangInstanceIdentifier.NodeIdentifierWithPredicates(
-                    QNameFactory.create(pathArgument.getNodeType().getValue()), toAttributesMap(pathArgument.getAttributesList()));
+            NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
+                    new NodeIdentifierWithPredicates(QNameFactory.create(pathArgument.getNodeType().getValue()),
+                            toAttributesMap(pathArgument.getAttributesList()));
 
             return nodeIdentifierWithPredicates;
 
-        } else if(YangInstanceIdentifier.AugmentationIdentifier.class.getSimpleName().equals(pathArgument.getType())){
+        } else if (AugmentationIdentifier.class.getSimpleName().equals(pathArgument.getType())) {
 
-            Set<QName> qNameSet = new HashSet<>();
+            Set<QName> qnameSet = new HashSet<>();
 
-            for(NormalizedNodeMessages.Attribute attribute : pathArgument.getAttributesList()){
-                qNameSet.add(QNameFactory.create(attribute.getValue()));
+            for (NormalizedNodeMessages.Attribute attribute : pathArgument.getAttributesList()) {
+                qnameSet.add(QNameFactory.create(attribute.getValue()));
             }
 
-            return new YangInstanceIdentifier.AugmentationIdentifier(qNameSet);
+            return new AugmentationIdentifier(qnameSet);
         }
 
         return NodeIdentifierFactory.getArgument(pathArgument.getValue());
@@ -258,7 +248,7 @@ public class InstanceIdentifierUtils {
 
         Map<QName, Object> map = new HashMap<>();
 
-        for(NormalizedNodeMessages.Attribute attribute : attributesList){
+        for (NormalizedNodeMessages.Attribute attribute : attributesList) {
             String name = attribute.getName();
             Object value = parseAttribute(attribute);
 
@@ -269,19 +259,16 @@ public class InstanceIdentifierUtils {
     }
 
     /**
-     * FIXME: This method only covers a subset of values that may go in an InstanceIdentifier
-     *
-     * @param attribute
-     * @return
+     * FIXME: This method only covers a subset of values that may go in an InstanceIdentifier.
      */
-    private static Object parseAttribute(NormalizedNodeMessages.Attribute attribute){
-        if(Short.class.getSimpleName().equals(attribute.getType())) {
+    private static Object parseAttribute(NormalizedNodeMessages.Attribute attribute) {
+        if (Short.class.getSimpleName().equals(attribute.getType())) {
             return Short.parseShort(attribute.getValue());
-        } else if(Long.class.getSimpleName().equals(attribute.getType())){
+        } else if (Long.class.getSimpleName().equals(attribute.getType())) {
             return Long.parseLong(attribute.getValue());
-        } else if(Boolean.class.getSimpleName().equals(attribute.getType())){
+        } else if (Boolean.class.getSimpleName().equals(attribute.getType())) {
             return Boolean.parseBoolean(attribute.getValue());
-        } else if(Integer.class.getSimpleName().equals(attribute.getType())){
+        } else if (Integer.class.getSimpleName().equals(attribute.getType())) {
             return Integer.parseInt(attribute.getValue());
         }