Merge "Use ${project.version} for internal dependencies"
[ovsdb.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.List;
21 import java.util.Map;
22
23 public class NeutronCacheUtils {
24
25     /**
26      * Look up in the NeutronPortsCRUD cache and return the MAC address for a corresponding IP address
27      * @param neutronPortsCache Reference to port cache to get existing port related data. This interface
28      * basically read data from the md-sal data store.
29      * @param subnetID subnet to which given port is attached
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 subnetID, String ipAddr) {
34         if (ipAddr == null || subnetID == null) {
35             return null;
36         }
37
38         List<NeutronPort> allPorts = neutronPortsCache.getAllPorts();
39         for (NeutronPort port : allPorts) {
40             List<Neutron_IPs> fixedIPs = port.getFixedIPs();
41             if (fixedIPs != null && !fixedIPs.isEmpty()) {
42                 for (Neutron_IPs ip : fixedIPs) {
43                     if (ip.getIpAddress().equals(ipAddr) && ip.getSubnetUUID().equals(subnetID)) {
44                         return port.getMacAddress();
45                     }
46                 }
47             }
48         }
49         return null;
50     }
51
52     /**
53      * Look up in the NeutronNetworkCRUD cache and NeutronSubnetCRUD cache for
54      * extracting the provider segmentation_type and segmentation_id
55      * @param neutronNetworkCache Reference to neutron network cache to get existing network related data.
56      * This interface basically read data from the md-sal data store.
57      * @param neutronSubnetCache Reference to neutron subnet cache to get existing subnet related data.
58      * This interface basically read data from the md-sal data store.
59      * @param subnetID Subnet UUID
60      * @return {Type: ID} pair for that subnet ID
61      */
62     public static Map.Entry<String,String> getProviderInformation(INeutronNetworkCRUD neutronNetworkCache,
63                 INeutronSubnetCRUD neutronSubnetCache, String subnetID) {
64
65         String networkID = null;
66
67         List<NeutronSubnet> allSubnets = neutronSubnetCache.getAllSubnets();
68         for (NeutronSubnet subnet: allSubnets) {
69             if (subnet.getID().equals(subnetID)) {
70                 networkID = subnet.getNetworkUUID();
71                 break;
72             }
73         }
74         if (networkID == null) {
75             return null;
76         }
77
78         List<NeutronNetwork> allNetworks = neutronNetworkCache.getAllNetworks();
79         for (NeutronNetwork network: allNetworks) {
80             if (network.getID().equals(networkID)) {
81                 return new AbstractMap.SimpleEntry<>(
82                         network.getProviderNetworkType(), network.getProviderSegmentationID());
83             }
84         }
85         return null;
86     }
87 }