Unit test for ovsdb.southbound.ovsdb.transact
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / NeutronCacheUtils.java
1 /*
2  * Copyright (c) 2014, 2015 SDN Hub, LLC. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
10
11 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
12 import org.opendaylight.neutron.spi.INeutronPortCRUD;
13 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
14 import org.opendaylight.neutron.spi.NeutronNetwork;
15 import org.opendaylight.neutron.spi.NeutronPort;
16 import org.opendaylight.neutron.spi.NeutronSubnet;
17 import org.opendaylight.neutron.spi.Neutron_IPs;
18
19 import java.util.AbstractMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 public class NeutronCacheUtils {
25
26     /**
27      * Look up in the NeutronPortsCRUD cache and return the MAC address for a corresponding IP address
28      * @param ipAddr IP address of a member or VM
29      * @return MAC address registered with that IP address
30      */
31     public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String subnetID, String ipAddr) {
32         if (ipAddr == null || subnetID == null) {
33             return null;
34         }
35
36         List<Neutron_IPs> fixedIPs;
37         Iterator<Neutron_IPs> fixedIPIterator;
38         Neutron_IPs ip;
39
40         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
41         Iterator<NeutronPort> i = allPorts.iterator();
42         while (i.hasNext()) {
43             NeutronPort port = i.next();
44             fixedIPs = port.getFixedIPs();
45             if (fixedIPs != null && fixedIPs.size() > 0) {
46                 fixedIPIterator = fixedIPs.iterator();
47                 while (fixedIPIterator.hasNext()) {
48                     ip = fixedIPIterator.next();
49                     if (ip.getIpAddress().equals(ipAddr) && ip.getSubnetUUID().equals(subnetID)) {
50                         return port.getMacAddress();
51                     }
52                 }
53             }
54         }
55         return null;
56     }
57
58     /**
59      * Look up in the NeutronNetworkCRUD cache and NeutronSubnetCRUD cache for
60      * extracting the provider segmentation_type and segmentation_id
61      * @param subnetID Subnet UUID
62      * @return {Type: ID} pair for that subnet ID
63      */
64     public static Map.Entry<String,String> getProviderInformation(INeutronNetworkCRUD neutronNetworkCache,
65                 INeutronSubnetCRUD neutronSubnetCache, String subnetID) {
66
67         String networkID = null;
68
69         List<NeutronSubnet> allSubnets = neutronSubnetCache.getAllSubnets();
70         for (NeutronSubnet subnet: allSubnets) {
71             if (subnet.getID().equals(subnetID)) {
72                 networkID = subnet.getNetworkUUID();
73                 break;
74             }
75         }
76         if (networkID == null) {
77             return null;
78         }
79
80         List<NeutronNetwork> allNetworks = neutronNetworkCache.getAllNetworks();
81         for (NeutronNetwork network: allNetworks) {
82             if (network.getID().equals(networkID)) {
83                 Map.Entry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(
84                         network.getProviderNetworkType(), network.getProviderSegmentationID());
85                 return entry;
86             }
87         }
88         return null;
89     }
90 }