Upgrade to Neon base platform
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundUtil.java
index a62820cac51127402c420c992345834f2b3ee41f..5411a0fd76b8de53e03b0acddd2cbef7e48fb164 100644 (file)
@@ -7,69 +7,49 @@
  */
 package org.opendaylight.ovsdb.southbound;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
-
 import java.net.InetAddress;
 import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.util.Enumeration;
+import java.util.concurrent.ExecutionException;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class SouthboundUtil {
+public final class SouthboundUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(SouthboundUtil.class);
-
-    private static InstanceIdentifierCodec instanceIdentifierCodec;
+    private static final String SCHEMA_VERSION_MISMATCH =
+            "{} column for {} table is not supported by this version of the {} schema: {}";
 
     private SouthboundUtil() {
         // Prevent instantiating a utility class
     }
 
-    public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
-        instanceIdentifierCodec = iidc;
-    }
-
-    public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
-        return instanceIdentifierCodec;
-    }
-
-    public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
-        return instanceIdentifierCodec.serialize(iid);
-    }
-
-    public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
-        InstanceIdentifier<?> result = null;
-        try {
-            result = instanceIdentifierCodec.bindingDeserializer(iidString);
-        } catch (DeserializationException e) {
-            LOG.warn("Unable to deserialize iidString", e);
-        }
-        return result;
-    }
-
-
     public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
         Preconditions.checkNotNull(mn);
         try {
             OvsdbNodeRef ref = mn.getManagedBy();
             if (ref != null && ref.getValue() != null) {
                 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
-                @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
+                @SuppressWarnings("unchecked")
+                // Note: erasure makes this safe in combination with the typecheck below
                 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
 
                 CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction.read(
@@ -82,7 +62,7 @@ public class SouthboundUtil {
                     if (node instanceof OvsdbNodeAugmentation) {
                         ovsdbNode = (OvsdbNodeAugmentation) node;
                     } else if (node != null) {
-                        ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
+                        ovsdbNode = node.augmentation(OvsdbNodeAugmentation.class);
                     }
                     if (ovsdbNode != null) {
                         return Optional.of(ovsdbNode);
@@ -99,7 +79,7 @@ public class SouthboundUtil {
                 LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
                 return Optional.absent();
             }
-        } catch (Exception e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
             return Optional.absent();
         }
@@ -116,22 +96,27 @@ public class SouthboundUtil {
         return node;
     }
 
-    private static String getLocalControllerHostIpAddress() {
+    @VisibleForTesting
+    static String getLocalControllerHostIpAddress() {
         String ipaddress = null;
         try {
-            for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
-                 ifaces.hasMoreElements();) {
-                NetworkInterface iface = ifaces.nextElement();
-
-                for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
-                    InetAddress inetAddr = inetAddrs.nextElement();
-                    if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
-                        ipaddress = inetAddr.getHostAddress();
-                        break;
+            Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
+            if (ifaces != null) {
+                while (ifaces.hasMoreElements()) {
+                    NetworkInterface iface = ifaces.nextElement();
+
+                    for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
+                        InetAddress inetAddr = inetAddrs.nextElement();
+                        if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
+                            ipaddress = inetAddr.getHostAddress();
+                            break;
+                        }
                     }
                 }
+            } else {
+                LOG.warn("Local Host don't have any associated IP address");
             }
-        } catch (Exception e) {
+        } catch (SocketException e) {
             LOG.warn("Exception while fetching local host ip address ",e);
         }
         return ipaddress;
@@ -140,11 +125,11 @@ public class SouthboundUtil {
     public static String getControllerTarget(Node ovsdbNode) {
         String target = null;
         String ipAddr = null;
-        OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
+        OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.augmentation(OvsdbNodeAugmentation.class);
         ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
         LOG.info("connectionInfo: {}", connectionInfo);
         if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
-            ipAddr = String.valueOf(connectionInfo.getLocalIp().getValue());
+            ipAddr = connectionInfo.getLocalIp().stringValue();
         }
         if (ipAddr == null) {
             ipAddr = getLocalControllerHostIpAddress();
@@ -159,7 +144,10 @@ public class SouthboundUtil {
     }
 
     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
-        return String.valueOf(
-                connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
+        return connectionInfo.getRemoteIp().stringValue() + ":" + connectionInfo.getRemotePort().getValue();
+    }
+
+    public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
+        LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, SouthboundConstants.OPEN_V_SWITCH, ex.getMessage());
     }
 }