Merge "Bug 4734 - netvirt/GatewayMacResolverService: null pinter exception"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / NeutronCacheUtils.java
index bed81ab6dccc1057c8986dc152810a6df9a01312..c0e48e37f5be97139d4400c8832217983381368f 100755 (executable)
@@ -1,25 +1,22 @@
 /*
- * Copyright (C) 2014 SDN Hub, LLC.
+ * Copyright (c) 2014, 2015 SDN Hub, LLC. 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
- *
- * Authors : Srini Seetharaman
  */
 
 package org.opendaylight.ovsdb.openstack.netvirt;
 
-import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
-import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
-import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
-import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
-import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
-import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
-import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
 
 import java.util.AbstractMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -27,28 +24,25 @@ public class NeutronCacheUtils {
 
     /**
      * Look up in the NeutronPortsCRUD cache and return the MAC address for a corresponding IP address
+     * @param neutronPortsCache Reference to port cache to get existing port related data. This interface
+     * basically read data from the md-sal data store.
+     * @param subnetID subnet to which given port is attached
      * @param ipAddr IP address of a member or VM
      * @return MAC address registered with that IP address
      */
-    public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String ipAddr) {
-        if (ipAddr == null)
+    public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String subnetID, String ipAddr) {
+        if (ipAddr == null || subnetID == null) {
             return null;
-
-        List<Neutron_IPs> fixedIPs;
-        Iterator<Neutron_IPs> fixedIPIterator;
-        Neutron_IPs ip;
+        }
 
         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
-        Iterator<NeutronPort> i = allPorts.iterator();
-        while (i.hasNext()) {
-            NeutronPort port = i.next();
-            fixedIPs = port.getFixedIPs();
-            if (fixedIPs != null && fixedIPs.size() > 0) {
-                fixedIPIterator = fixedIPs.iterator();
-                while (fixedIPIterator.hasNext()) {
-                    ip = fixedIPIterator.next();
-                    if (ip.getIpAddress().equals(ipAddr))
+        for (NeutronPort port : allPorts) {
+            List<Neutron_IPs> fixedIPs = port.getFixedIPs();
+            if (fixedIPs != null && !fixedIPs.isEmpty()) {
+                for (Neutron_IPs ip : fixedIPs) {
+                    if (ip.getIpAddress().equals(ipAddr) && ip.getSubnetUUID().equals(subnetID)) {
                         return port.getMacAddress();
+                    }
                 }
             }
         }
@@ -58,7 +52,11 @@ public class NeutronCacheUtils {
     /**
      * Look up in the NeutronNetworkCRUD cache and NeutronSubnetCRUD cache for
      * extracting the provider segmentation_type and segmentation_id
-     * @param subnetId Subnet UUID
+     * @param neutronNetworkCache Reference to neutron network cache to get existing network related data.
+     * This interface basically read data from the md-sal data store.
+     * @param neutronSubnetCache Reference to neutron subnet cache to get existing subnet related data.
+     * This interface basically read data from the md-sal data store.
+     * @param subnetID Subnet UUID
      * @return {Type: ID} pair for that subnet ID
      */
     public static Map.Entry<String,String> getProviderInformation(INeutronNetworkCRUD neutronNetworkCache,
@@ -73,15 +71,15 @@ public class NeutronCacheUtils {
                 break;
             }
         }
-        if (networkID == null)
+        if (networkID == null) {
             return null;
+        }
 
         List<NeutronNetwork> allNetworks = neutronNetworkCache.getAllNetworks();
         for (NeutronNetwork network: allNetworks) {
             if (network.getID().equals(networkID)) {
-                Map.Entry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(
+                return new AbstractMap.SimpleEntry<>(
                         network.getProviderNetworkType(), network.getProviderSegmentationID());
-                return entry;
             }
         }
         return null;