Merge "Bug 2013 - Vlan flows needs merging from traditional rules to a single L2Fwdin...
[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.INeutronNetworkCRUD;
14 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
15 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
16 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
17 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
18 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
19 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
20
21 import java.util.AbstractMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 public class NeutronCacheUtils {
27
28     /**
29      * Look up in the NeutronPortsCRUD cache and return the MAC address for a corresponding IP address
30      * @param ipAddr IP address of a member or VM
31      * @return MAC address registered with that IP address
32      */
33     public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String ipAddr) {
34         if (ipAddr == null)
35             return null;
36
37         List<Neutron_IPs> fixedIPs;
38         Iterator<Neutron_IPs> fixedIPIterator;
39         Neutron_IPs ip;
40
41         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
42         Iterator<NeutronPort> i = allPorts.iterator();
43         while (i.hasNext()) {
44             NeutronPort port = i.next();
45             fixedIPs = port.getFixedIPs();
46             if (fixedIPs != null && fixedIPs.size() > 0) {
47                 fixedIPIterator = fixedIPs.iterator();
48                 while (fixedIPIterator.hasNext()) {
49                     ip = fixedIPIterator.next();
50                     if (ip.getIpAddress().equals(ipAddr))
51                         return port.getMacAddress();
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         List<NeutronNetwork> allNetworks = neutronNetworkCache.getAllNetworks();
80         for (NeutronNetwork network: allNetworks) {
81             if (network.getID().equals(networkID)) {
82                 Map.Entry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(
83                         network.getProviderNetworkType(), network.getProviderSegmentationID());
84                 return entry;
85             }
86         }
87         return null;
88     }
89 }