Fixing Bug 1900 and 1908
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / NeutronCacheUtils.java
1 /*
2  * Copyright (C) 2014 SDN Hub, LLC.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Srini Seetharaman
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt;
12
13 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
14 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
15 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
16 import java.util.Iterator;
17 import java.util.List;
18
19 public class NeutronCacheUtils {
20
21     /**
22      * Look up in the NeutronPortsCRUD cache and return the MAC address for a corresponding IP address
23      * @param ipAddr IP address of a member or VM
24      * @return MAC address registered with that IP address
25      */
26     public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String ipAddr) {
27         if (ipAddr == null)
28             return null;
29
30         List<Neutron_IPs> fixedIPs;
31         Iterator<Neutron_IPs> fixedIPIterator;
32         Neutron_IPs ip;
33
34         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
35         Iterator<NeutronPort> i = allPorts.iterator();
36         while (i.hasNext()) {
37             NeutronPort port = i.next();
38             fixedIPs = port.getFixedIPs();
39             if (fixedIPs != null && fixedIPs.size() > 0) {
40                 fixedIPIterator = fixedIPs.iterator();
41                 while (fixedIPIterator.hasNext()) {
42                     ip = fixedIPIterator.next();
43                     if (ip.getIpAddress().equals(ipAddr))
44                         return port.getMacAddress();
45                 }
46             }
47         }
48         return null;
49     }
50 }