Files
neutron/neutron/agent/windows/ip_lib.py
Rodolfo Alonso Hernandez 18e2e2def5 Add IPLink class to Windows ip_lib implementation
This new class is used to read the link address from
a network device.

Partial-Bug: #1644878
Change-Id: Ieeaa1d3633b942e549c0513ca7360c0a91d10d04
2017-01-17 17:39:38 +00:00

86 lines
2.4 KiB
Python

# Copyright 2016 Cloudbase Solutions.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import netifaces
from oslo_log import log as logging
from neutron._i18n import _LE
LOG = logging.getLogger(__name__)
OPTS = []
class IPWrapper(object):
def get_device_by_ip(self, ip):
if not ip:
return
for device in self.get_devices():
if device.device_has_ip(ip):
return device
def get_devices(self):
try:
return [IPDevice(iface) for iface in netifaces.interfaces()]
except (OSError, MemoryError):
LOG.error(_LE("Failed to get network interfaces."))
return []
class IPDevice(object):
def __init__(self, name):
self.name = name
self.link = IPLink(self)
def read_ifaddresses(self):
try:
device_addresses = netifaces.ifaddresses(self.name)
except ValueError:
LOG.error(_LE("The device does not exist on the system: %s."),
self.name)
return
except OSError:
LOG.error(_LE("Failed to get interface addresses: %s."),
self.name)
return
return device_addresses
def device_has_ip(self, ip):
device_addresses = self.read_ifaddresses()
if device_addresses is None:
return False
addresses = [ip_addr['addr'] for ip_addr in
device_addresses.get(netifaces.AF_INET, [])]
return ip in addresses
class IPLink(object):
def __init__(self, parent):
self._parent = parent
@property
def address(self):
device_addresses = self._parent.read_ifaddresses()
if device_addresses is None:
return False
return [eth_addr['addr'] for eth_addr in
device_addresses.get(netifaces.AF_LINK, [])]