Merge "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.ovsdb.openstack.netvirt.translator.NeutronNetwork;
12 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
13 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
14 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
15 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
16 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
17 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
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 neutronPortsCache Reference to port cache to get existing port related data. This interface
29      * basically read data from the md-sal data store.
30      * @param subnetID subnet to which given port is attached
31      * @param ipAddr IP address of a member or VM
32      * @return MAC address registered with that IP address
33      */
34     public static String getMacAddress(INeutronPortCRUD neutronPortsCache, String subnetID, String ipAddr) {
35         if (ipAddr == null || subnetID == null) {
36             return null;
37         }
38
39         List<Neutron_IPs> fixedIPs;
40         Iterator<Neutron_IPs> fixedIPIterator;
41         Neutron_IPs ip;
42
43         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
44         Iterator<NeutronPort> i = allPorts.iterator();
45         while (i.hasNext()) {
46             NeutronPort port = i.next();
47             fixedIPs = port.getFixedIPs();
48             if (fixedIPs != null && fixedIPs.size() > 0) {
49                 fixedIPIterator = fixedIPs.iterator();
50                 while (fixedIPIterator.hasNext()) {
51                     ip = fixedIPIterator.next();
52                     if (ip.getIpAddress().equals(ipAddr) && ip.getSubnetUUID().equals(subnetID)) {
53                         return port.getMacAddress();
54                     }
55                 }
56             }
57         }
58         return null;
59     }
60
61     /**
62      * Look up in the NeutronNetworkCRUD cache and NeutronSubnetCRUD cache for
63      * extracting the provider segmentation_type and segmentation_id
64      * @param neutronNetworkCache Reference to neutron network cache to get existing network related data.
65      * This interface basically read data from the md-sal data store.
66      * @param neutronSubnetCache Reference to neutron subnet cache to get existing subnet related data.
67      * This interface basically read data from the md-sal data store.
68      * @param subnetID Subnet UUID
69      * @return {Type: ID} pair for that subnet ID
70      */
71     public static Map.Entry<String,String> getProviderInformation(INeutronNetworkCRUD neutronNetworkCache,
72                 INeutronSubnetCRUD neutronSubnetCache, String subnetID) {
73
74         String networkID = null;
75
76         List<NeutronSubnet> allSubnets = neutronSubnetCache.getAllSubnets();
77         for (NeutronSubnet subnet: allSubnets) {
78             if (subnet.getID().equals(subnetID)) {
79                 networkID = subnet.getNetworkUUID();
80                 break;
81             }
82         }
83         if (networkID == null) {
84             return null;
85         }
86
87         List<NeutronNetwork> allNetworks = neutronNetworkCache.getAllNetworks();
88         for (NeutronNetwork network: allNetworks) {
89             if (network.getID().equals(networkID)) {
90                 Map.Entry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(
91                         network.getProviderNetworkType(), network.getProviderSegmentationID());
92                 return entry;
93             }
94         }
95         return null;
96     }
97 }