Dynamips devices support (packet capture to complete).

This commit is contained in:
grossmj committed 2015-02-15 12:18:12 -07:00
1 parent 4f38d96522
commit f99e834c37
18 files changed
+971 -539

No files matched your search

+83 -1
View File
@@ -32,11 +32,14 @@ log = logging.getLogger(__name__)
from gns3server.utils.interfaces import get_windows_interfaces
from pkg_resources import parse_version
from uuid import UUID, uuid4
from ..base_manager import BaseManager
from ..project_manager import ProjectManager
from .dynamips_error import DynamipsError
from .hypervisor import Hypervisor
from .nodes.router import Router
from .dynamips_vm import DynamipsVM
from .dynamips_device import DynamipsDevice
# NIOs
from .nios.nio_udp import NIOUDP
@@ -54,10 +57,12 @@ from .nios.nio_null import NIONull
class Dynamips(BaseManager):
_VM_CLASS = DynamipsVM
_DEVICE_CLASS = DynamipsDevice
def __init__(self):
super().__init__()
self._devices = {}
self._dynamips_path = None
# FIXME: temporary
@@ -67,7 +72,19 @@ class Dynamips(BaseManager):
def unload(self):
yield from BaseManager.unload(self)
Router.reset()
tasks = []
for device in self._devices.values():
tasks.append(asyncio.async(device.hypervisor.stop()))
if tasks:
done, _ = yield from asyncio.wait(tasks)
for future in done:
try:
future.result()
except Exception as e:
log.error("Could not stop device hypervisor {}".format(e), exc_info=1)
continue
# files = glob.glob(os.path.join(self._working_dir, "dynamips", "*.ghost"))
# files += glob.glob(os.path.join(self._working_dir, "dynamips", "*_lock"))
@@ -92,6 +109,71 @@ class Dynamips(BaseManager):
return self._dynamips_path
@asyncio.coroutine
def create_device(self, name, project_id, device_id, device_type, *args, **kwargs):
"""
Create a new Dynamips device.
:param name: Device name
:param project_id: Project identifier
:param vm_id: restore a VM identifier
"""
project = ProjectManager.instance().get_project(project_id)
if not device_id:
device_id = str(uuid4())
device = self._DEVICE_CLASS(name, device_id, project, self, device_type, *args, **kwargs)
yield from device.create()
self._devices[device.id] = device
project.add_device(device)
return device
def get_device(self, device_id, project_id=None):
"""
Returns a device instance.
:param device_id: Device identifier
:param project_id: Project identifier
:returns: Device instance
"""
if project_id:
# check the project_id exists
project = ProjectManager.instance().get_project(project_id)
try:
UUID(device_id, version=4)
except ValueError:
raise aiohttp.web.HTTPBadRequest(text="Device ID} is not a valid UUID".format(device_id))
if device_id not in self._devices:
raise aiohttp.web.HTTPNotFound(text="Device ID {} doesn't exist".format(device_id))
device = self._devices[device_id]
if project_id:
if device.project.id != project.id:
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't belong to device {}".format(project_id, device.name))
return device
@asyncio.coroutine
def delete_device(self, device_id):
"""
Delete a device
:param device_id: Device identifier
:returns: Device instance
"""
device = self.get_device(device_id)
yield from device.delete()
device.project.remove_device(device)
del self._devices[device.id]
return device
def find_dynamips(self):
# look for Dynamips
@@ -25,10 +25,10 @@ from .nodes.frame_relay_switch import FrameRelaySwitch
import logging
log = logging.getLogger(__name__)
DEVICES = {'atmsw': ATMSwitch,
'frsw': FrameRelaySwitch,
'ethsw': EthernetSwitch,
'ethhub': EthernetHub}
DEVICES = {'atm_switch': ATMSwitch,
'frame_relay_switch': FrameRelaySwitch,
'ethernet_switch': EthernetSwitch,
'ethernet_hub': EthernetHub}
class DynamipsDevice:
@@ -37,9 +37,9 @@ class DynamipsDevice:
Factory to create an Device object based on the type
"""
def __new__(cls, name, vm_id, project, manager, device_type, **kwargs):
def __new__(cls, name, device_id, project, manager, device_type, **kwargs):
if type not in DEVICES:
if device_type not in DEVICES:
raise DynamipsError("Unknown device type: {}".format(device_type))
return DEVICES[device_type](name, vm_id, project, manager, **kwargs)
return DEVICES[device_type](name, device_id, project, manager, **kwargs)
@@ -128,9 +128,16 @@ class DynamipsHypervisor:
Stops this hypervisor (will no longer run).
"""
yield from self.send("hypervisor stop")
yield from self._writer.drain()
self._writer.close()
try:
# try to properly stop the hypervisor
yield from self.send("hypervisor stop")
except DynamipsError:
pass
try:
yield from self._writer.drain()
self._writer.close()
except OSError as e:
log.debug("Stopping hypervisor {}:{} {}".format(self._host, self._port, e))
self._reader = self._writer = None
self._nio_udp_auto_instances.clear()
@@ -186,137 +193,6 @@ class DynamipsHypervisor:
return self._devices
# @devices.setter
# def devices(self, devices):
# """
# Sets the list of devices managed by this hypervisor instance.
# This method is for internal use.
#
# :param devices: a list of device objects
# """
#
# self._devices = devices
# @property
# def console_start_port_range(self):
# """
# Returns the console start port range value
#
# :returns: console start port range value (integer)
# """
#
# return self._console_start_port_range
# @console_start_port_range.setter
# def console_start_port_range(self, console_start_port_range):
# """
# Set a new console start port range value
#
# :param console_start_port_range: console start port range value (integer)
# """
#
# self._console_start_port_range = console_start_port_range
#
# @property
# def console_end_port_range(self):
# """
# Returns the console end port range value
#
# :returns: console end port range value (integer)
# """
#
# return self._console_end_port_range
#
# @console_end_port_range.setter
# def console_end_port_range(self, console_end_port_range):
# """
# Set a new console end port range value
#
# :param console_end_port_range: console end port range value (integer)
# """
#
# self._console_end_port_range = console_end_port_range
# @property
# def aux_start_port_range(self):
# """
# Returns the auxiliary console start port range value
#
# :returns: auxiliary console start port range value (integer)
# """
#
# return self._aux_start_port_range
#
# @aux_start_port_range.setter
# def aux_start_port_range(self, aux_start_port_range):
# """
# Sets a new auxiliary console start port range value
#
# :param aux_start_port_range: auxiliary console start port range value (integer)
# """
#
# self._aux_start_port_range = aux_start_port_range
#
# @property
# def aux_end_port_range(self):
# """
# Returns the auxiliary console end port range value
#
# :returns: auxiliary console end port range value (integer)
# """
#
# return self._aux_end_port_range
#
# @aux_end_port_range.setter
# def aux_end_port_range(self, aux_end_port_range):
# """
# Sets a new auxiliary console end port range value
#
# :param aux_end_port_range: auxiliary console end port range value (integer)
# """
#
# self._aux_end_port_range = aux_end_port_range
# @property
# def udp_start_port_range(self):
# """
# Returns the UDP start port range value
#
# :returns: UDP start port range value (integer)
# """
#
# return self._udp_start_port_range
#
# @udp_start_port_range.setter
# def udp_start_port_range(self, udp_start_port_range):
# """
# Sets a new UDP start port range value
#
# :param udp_start_port_range: UDP start port range value (integer)
# """
#
# self._udp_start_port_range = udp_start_port_range
#
# @property
# def udp_end_port_range(self):
# """
# Returns the UDP end port range value
#
# :returns: UDP end port range value (integer)
# """
#
# return self._udp_end_port_range
#
# @udp_end_port_range.setter
# def udp_end_port_range(self, udp_end_port_range):
# """
# Sets an new UDP end port range value
#
# :param udp_end_port_range: UDP end port range value (integer)
# """
#
# self._udp_end_port_range = udp_end_port_range
@property
def ghosts(self):
"""
@@ -337,26 +213,6 @@ class DynamipsHypervisor:
self._ghosts[image_name] = router
@property
def jitsharing_groups(self):
"""
Returns a list of the JIT sharing groups hosted by this hypervisor.
:returns: JIT sharing groups dict (image_name -> group number)
"""
return self._jitsharing_groups
def add_jitsharing_group(self, image_name, group_number):
"""
Adds a JIT blocks sharing group name to the list of groups created on this hypervisor.
:param image_name: name of the ghost image
:param group_number: group (integer)
"""
self._jitsharing_groups[image_name] = group_number
@property
def port(self):
"""
@@ -462,6 +318,9 @@ class DynamipsHypervisor:
while True:
try:
chunk = yield from self._reader.read(1024) # match to Dynamips' buffer size
if not chunk:
raise DynamipsError("No data returned from {host}:{port}, Dynamips process running: {run}"
.format(host=self._host, port=self._port, run=self.is_running()))
buf += chunk.decode()
except OSError as e:
raise DynamipsError("Communication timed out with {host}:{port} :{error}, Dynamips process running: {run}"
@@ -480,10 +339,6 @@ class DynamipsHypervisor:
data.pop()
buf = ''
if len(data) == 0:
raise DynamipsError("no data returned from {host}:{port}, Dynamips process running: {run}"
.format(host=self._host, port=self._port, run=self.is_running()))
# Does it contain an error code?
if self.error_re.search(data[-1]):
raise DynamipsError(data[-1][4:])
+57 -15
View File
@@ -21,6 +21,7 @@ http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L593
"""
import asyncio
import re
from .device import Device
from ..dynamips_error import DynamipsError
@@ -34,17 +35,24 @@ class ATMSwitch(Device):
Dynamips ATM switch.
:param name: name for this switch
:param node_id: Node instance identifier
:param device_id: Device instance identifier
:param project: Project instance
:param manager: Parent VM Manager
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, hypervisor=None):
def __init__(self, name, device_id, project, manager, hypervisor=None):
super().__init__(name, node_id, project, manager)
super().__init__(name, device_id, project, manager, hypervisor)
self._nios = {}
self._mapping = {}
self._mappings = {}
def __json__(self):
return {"name": self.name,
"device_id": self.id,
"project_id": self.project.id,
"mappings": self._mappings}
@asyncio.coroutine
def create(self):
@@ -82,14 +90,14 @@ class ATMSwitch(Device):
return self._nios
@property
def mapping(self):
def mappings(self):
"""
Returns port mapping
Returns port mappings
:returns: mapping list
:returns: mappings list
"""
return self._mapping
return self._mappings
@asyncio.coroutine
def delete(self):
@@ -97,10 +105,14 @@ class ATMSwitch(Device):
Deletes this ATM switch.
"""
yield from self._hypervisor.send('atmsw delete "{}"'.format(self._name))
log.info('ATM switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
try:
yield from self._hypervisor.send('atmsw delete "{}"'.format(self._name))
log.info('ATM switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
except DynamipsError:
log.debug("Could not properly delete ATM switch {}".format(self._name))
self._hypervisor.devices.remove(self)
self._instances.remove(self._id)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
def has_port(self, port):
"""
@@ -150,6 +162,36 @@ class ATMSwitch(Device):
del self._nios[port_number]
return nio
@asyncio.coroutine
def set_mappings(self, mappings):
"""
Applies VC mappings
:param mappings: mappings (dict)
"""
pvc_entry = re.compile(r"""^([0-9]*):([0-9]*):([0-9]*)$""")
for source, destination in mappings.items():
match_source_pvc = pvc_entry.search(source)
match_destination_pvc = pvc_entry.search(destination)
if match_source_pvc and match_destination_pvc:
# add the virtual channels
source_port, source_vpi, source_vci = map(int, match_source_pvc.group(1, 2, 3))
destination_port, destination_vpi, destination_vci = map(int, match_destination_pvc.group(1, 2, 3))
if self.has_port(destination_port):
if (source_port, source_vpi, source_vci) not in self.mapping and \
(destination_port, destination_vpi, destination_vci) not in self.mappings:
yield from self.map_pvc(source_port, source_vpi, source_vci, destination_port, destination_vpi, destination_vci)
yield from self.map_pvc(destination_port, destination_vpi, destination_vci, source_port, source_vpi, source_vci)
else:
# add the virtual paths
source_port, source_vpi = map(int, source.split(':'))
destination_port, destination_vpi = map(int, destination.split(':'))
if self.has_port(destination_port):
if (source_port, source_vpi) not in self.mappings and (destination_port, destination_vpi) not in self.mappings:
yield from self.map_vp(source_port, source_vpi, destination_port, destination_vpi)
yield from self.map_vp(destination_port, destination_vpi, source_port, source_vpi)
@asyncio.coroutine
def map_vp(self, port1, vpi1, port2, vpi2):
"""
@@ -183,7 +225,7 @@ class ATMSwitch(Device):
port2=port2,
vpi2=vpi2))
self._mapping[(port1, vpi1)] = (port2, vpi2)
self._mappings[(port1, vpi1)] = (port2, vpi2)
@asyncio.coroutine
def unmap_vp(self, port1, vpi1, port2, vpi2):
@@ -218,7 +260,7 @@ class ATMSwitch(Device):
port2=port2,
vpi2=vpi2))
del self._mapping[(port1, vpi1)]
del self._mappings[(port1, vpi1)]
@asyncio.coroutine
def map_pvc(self, port1, vpi1, vci1, port2, vpi2, vci2):
@@ -259,7 +301,7 @@ class ATMSwitch(Device):
vpi2=vpi2,
vci2=vci2))
self._mapping[(port1, vpi1, vci1)] = (port2, vpi2, vci2)
self._mappings[(port1, vpi1, vci1)] = (port2, vpi2, vci2)
@asyncio.coroutine
def unmap_pvc(self, port1, vpi1, vci1, port2, vpi2, vci2):
@@ -299,7 +341,7 @@ class ATMSwitch(Device):
port2=port2,
vpi2=vpi2,
vci2=vci2))
del self._mapping[(port1, vpi1, vci1)]
del self._mappings[(port1, vpi1, vci1)]
@asyncio.coroutine
def start_capture(self, port_number, output_file, data_link_type="DLT_ATM_RFC1483"):
+1 -1
View File
@@ -78,8 +78,8 @@ class Bridge(Device):
Deletes this bridge.
"""
yield from self._hypervisor.send('nio_bridge delete "{}"'.format(self._name))
self._hypervisor.devices.remove(self)
yield from self._hypervisor.send('nio_bridge delete "{}"'.format(self._name))
@asyncio.coroutine
def add_nio(self, nio):
+62 -13
View File
@@ -16,23 +16,23 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ...base_vm import BaseVM
class Device(BaseVM):
class Device:
"""
Base device for switches and hubs
:param name: name for this bridge
:param vm_id: Node instance identifier
:param name: name for this device
:param device_id: Device instance identifier
:param project: Project instance
:param manager: Parent VM Manager
:param manager: Parent manager
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, hypervisor=None):
def __init__(self, name, device_id, project, manager, hypervisor=None):
super().__init__(name, node_id, project, manager)
self._name = name
self._id = device_id
self._project = project
self._manager = manager
self._hypervisor = hypervisor
@property
@@ -45,10 +45,59 @@ class Device(BaseVM):
return self._hypervisor
def start(self):
@property
def project(self):
"""
Returns the device current project.
pass # Dynamips switches and hubs are always on
:returns: Project instance.
"""
def stop(self):
return self._project
pass # Dynamips switches and hubs are always on
@property
def name(self):
"""
Returns the name for this device.
:returns: name
"""
return self._name
@name.setter
def name(self, new_name):
"""
Sets the name of this VM.
:param new_name: name
"""
self._name = new_name
@property
def id(self):
"""
Returns the ID for this device.
:returns: device identifier (string)
"""
return self._id
@property
def manager(self):
"""
Returns the manager for this device.
:returns: instance of manager
"""
return self._manager
def create(self):
"""
Creates the device.
"""
raise NotImplementedError
@@ -33,32 +33,38 @@ class EthernetHub(Bridge):
Dynamips Ethernet hub (based on Bridge)
:param name: name for this hub
:param node_id: Node instance identifier
:param device_id: Device instance identifier
:param project: Project instance
:param manager: Parent VM Manager
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, hypervisor=None):
def __init__(self, name, device_id, project, manager, hypervisor=None):
Bridge.__init__(self, name, node_id, project, manager, hypervisor)
self._mapping = {}
Bridge.__init__(self, name, device_id, project, manager, hypervisor)
self._mappings = {}
def __json__(self):
return {"name": self.name,
"device_id": self.id,
"project_id": self.project.id}
@asyncio.coroutine
def create(self):
yield from Bridge.create()
yield from Bridge.create(self)
log.info('Ethernet hub "{name}" [{id}] has been created'.format(name=self._name, id=self._id))
@property
def mapping(self):
def mappings(self):
"""
Returns port mapping
Returns port mappings
:returns: mapping list
:returns: mappings list
"""
return self._mapping
return self._mappings
@asyncio.coroutine
def delete(self):
@@ -66,9 +72,13 @@ class EthernetHub(Bridge):
Deletes this hub.
"""
yield from Bridge.delete(self)
log.info('Ethernet hub "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
self._instances.remove(self._id)
try:
yield from Bridge.delete(self)
log.info('Ethernet hub "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
except DynamipsError:
log.debug("Could not properly delete Ethernet hub {}".format(self._name))
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
@asyncio.coroutine
def add_nio(self, nio, port_number):
@@ -79,7 +89,7 @@ class EthernetHub(Bridge):
:param port_number: port to allocate for the NIO
"""
if port_number in self._mapping:
if port_number in self._mappings:
raise DynamipsError("Port {} isn't free".format(port_number))
yield from Bridge.add_nio(self, nio)
@@ -88,7 +98,7 @@ class EthernetHub(Bridge):
id=self._id,
nio=nio,
port=port_number))
self._mapping[port_number] = nio
self._mappings[port_number] = nio
@asyncio.coroutine
def remove_nio(self, port_number):
@@ -100,10 +110,10 @@ class EthernetHub(Bridge):
:returns: the NIO that was bound to the allocated port
"""
if port_number not in self._mapping:
if port_number not in self._mappings:
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._mapping[port_number]
nio = self._mappings[port_number]
yield from Bridge.remove_nio(self, nio)
log.info('Ethernet switch "{name}" [{id}]: NIO {nio} removed from port {port}'.format(name=self._name,
@@ -111,7 +121,7 @@ class EthernetHub(Bridge):
nio=nio,
port=port_number))
del self._mapping[port_number]
del self._mappings[port_number]
return nio
@asyncio.coroutine
@@ -124,10 +134,10 @@ class EthernetHub(Bridge):
:param data_link_type: PCAP data link type (DLT_*), default is DLT_EN10MB
"""
if port_number not in self._mapping:
if port_number not in self._mappings:
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._mapping[port_number]
nio = self._mappings[port_number]
data_link_type = data_link_type.lower()
if data_link_type.startswith("dlt_"):
@@ -151,10 +161,10 @@ class EthernetHub(Bridge):
:param port_number: allocated port number
"""
if port_number not in self._mapping:
if port_number not in self._mappings:
raise DynamipsError("Port {} is not allocated".format(port_number))
nio = self._mapping[port_number]
nio = self._mappings[port_number]
yield from nio.unbind_filter("both")
log.info('Ethernet hub "{name}" [{id}]: stopping packet capture on {port}'.format(name=self._name,
id=self._id,
@@ -35,17 +35,32 @@ class EthernetSwitch(Device):
Dynamips Ethernet switch.
:param name: name for this switch
:param node_id: Node instance identifier
:param device_id: Device instance identifier
:param project: Project instance
:param manager: Parent VM Manager
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, hypervisor=None):
def __init__(self, name, device_id, project, manager, hypervisor=None):
super().__init__(name, node_id, project, manager, hypervisor)
super().__init__(name, device_id, project, manager, hypervisor)
self._nios = {}
self._mapping = {}
self._mappings = {}
def __json__(self):
ethernet_switch_info = {"name": self.name,
"device_id": self.id,
"project_id": self.project.id}
ports = []
for port_number, settings in self._mappings.items():
ports.append({"port": port_number,
"type": settings[0],
"vlan": settings[1]})
ethernet_switch_info["ports"] = ports
return ethernet_switch_info
@asyncio.coroutine
def create(self):
@@ -82,14 +97,14 @@ class EthernetSwitch(Device):
return self._nios
@property
def mapping(self):
def mappings(self):
"""
Returns port mapping
Returns port mappings
:returns: mapping list
:returns: mappings list
"""
return self._mapping
return self._mappings
@asyncio.coroutine
def delete(self):
@@ -97,10 +112,14 @@ class EthernetSwitch(Device):
Deletes this Ethernet switch.
"""
yield from self._hypervisor.send('ethsw delete "{}"'.format(self._name))
log.info('Ethernet switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
try:
yield from self._hypervisor.send('ethsw delete "{}"'.format(self._name))
log.info('Ethernet switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
except DynamipsError:
log.debug("Could not properly delete Ethernet switch {}".format(self._name))
self._hypervisor.devices.remove(self)
self._instances.remove(self._id)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
@asyncio.coroutine
def add_nio(self, nio, port_number):
@@ -144,11 +163,27 @@ class EthernetSwitch(Device):
port=port_number))
del self._nios[port_number]
if port_number in self._mapping:
del self._mapping[port_number]
if port_number in self._mappings:
del self._mappings[port_number]
return nio
@asyncio.coroutine
def set_port_settings(self, port_number, settings):
"""
Applies port settings to a specific port.
:param port_number: port number to set the settings
:param settings: port settings
"""
if settings["type"] == "access":
yield from self.set_access_port(port_number, settings["vlan"])
elif settings["type"] == "dot1q":
yield from self.set_dot1q_port(port_number, settings["vlan"])
elif settings["type"] == "qinq":
yield from self.set_qinq_port(port_number, settings["vlan"])
@asyncio.coroutine
def set_access_port(self, port_number, vlan_id):
"""
@@ -170,7 +205,7 @@ class EthernetSwitch(Device):
id=self._id,
port=port_number,
vlan_id=vlan_id))
self._mapping[port_number] = ("access", vlan_id)
self._mappings[port_number] = ("access", vlan_id)
@asyncio.coroutine
def set_dot1q_port(self, port_number, native_vlan):
@@ -194,7 +229,7 @@ class EthernetSwitch(Device):
port=port_number,
vlan_id=native_vlan))
self._mapping[port_number] = ("dot1q", native_vlan)
self._mappings[port_number] = ("dot1q", native_vlan)
@asyncio.coroutine
def set_qinq_port(self, port_number, outer_vlan):
@@ -217,7 +252,7 @@ class EthernetSwitch(Device):
id=self._id,
port=port_number,
vlan_id=outer_vlan))
self._mapping[port_number] = ("qinq", outer_vlan)
self._mappings[port_number] = ("qinq", outer_vlan)
@asyncio.coroutine
def get_mac_addr_table(self):
@@ -34,17 +34,24 @@ class FrameRelaySwitch(Device):
Dynamips Frame Relay switch.
:param name: name for this switch
:param node_id: Node instance identifier
:param device_id: Device instance identifier
:param project: Project instance
:param manager: Parent VM Manager
:param hypervisor: Dynamips hypervisor instance
"""
def __init__(self, name, node_id, project, manager, hypervisor=None):
def __init__(self, name, device_id, project, manager, hypervisor=None):
super().__init__(name, node_id, project, manager, hypervisor)
super().__init__(name, device_id, project, manager, hypervisor)
self._nios = {}
self._mapping = {}
self._mappings = {}
def __json__(self):
return {"name": self.name,
"device_id": self.id,
"project_id": self.project.id,
"mappings": self._mappings}
@asyncio.coroutine
def create(self):
@@ -81,14 +88,14 @@ class FrameRelaySwitch(Device):
return self._nios
@property
def mapping(self):
def mappings(self):
"""
Returns port mapping
Returns port mappings
:returns: mapping list
:returns: mappings list
"""
return self._mapping
return self._mappings
@asyncio.coroutine
def delete(self):
@@ -96,10 +103,14 @@ class FrameRelaySwitch(Device):
Deletes this Frame Relay switch.
"""
yield from self._hypervisor.send('frsw delete "{}"'.format(self._name))
log.info('Frame Relay switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
try:
yield from self._hypervisor.send('frsw delete "{}"'.format(self._name))
log.info('Frame Relay switch "{name}" [{id}] has been deleted'.format(name=self._name, id=self._id))
except DynamipsError:
log.debug("Could not properly delete Frame relay switch {}".format(self._name))
self._hypervisor.devices.remove(self)
self._instances.remove(self._id)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
def has_port(self, port):
"""
@@ -151,6 +162,22 @@ class FrameRelaySwitch(Device):
del self._nios[port_number]
return nio
@asyncio.coroutine
def set_mappings(self, mappings):
"""
Applies VC mappings
:param mappings: mappings (dict)
"""
for source, destination in mappings.items():
source_port, source_dlci = map(int, source.split(':'))
destination_port, destination_dlci = map(int, destination.split(':'))
if self.has_port(destination_port):
if (source_port, source_dlci) not in self.mappings and (destination_port, destination_dlci) not in self.mappings:
yield from self.map_vc(source_port, source_dlci, destination_port, destination_dlci)
yield from self.map_vc(destination_port, destination_dlci, source_port, source_dlci)
@asyncio.coroutine
def map_vc(self, port1, dlci1, port2, dlci2):
"""
@@ -184,7 +211,7 @@ class FrameRelaySwitch(Device):
port2=port2,
dlci2=dlci2))
self._mapping[(port1, dlci1)] = (port2, dlci2)
self._mappings[(port1, dlci1)] = (port2, dlci2)
@asyncio.coroutine
def unmap_vc(self, port1, dlci1, port2, dlci2):
@@ -218,7 +245,7 @@ class FrameRelaySwitch(Device):
dlci1=dlci1,
port2=port2,
dlci2=dlci2))
del self._mapping[(port1, dlci1)]
del self._mappings[(port1, dlci1)]
@asyncio.coroutine
def start_capture(self, port_number, output_file, data_link_type="DLT_FRELAY"):
+6 -2
View File
@@ -294,8 +294,12 @@ class Router(BaseVM):
# router is already closed
return
if self._hypervisor:
yield from self.stop()
self._hypervisor.devices.remove(self)
if self._hypervisor and not self._hypervisor.devices:
try:
yield from self.stop()
except DynamipsError:
pass
yield from self.hypervisor.stop()
if self._console: