Fix trouble with builtin devices when we free ports

Fix #825
This commit is contained in:
Julien Duponchelle committed 2016-12-12 19:17:06 +01:00
1 parent 9872fc09b7
commit d2f0cddbd7
12 files changed
+125 -221

No files matched your search

+2 -71
View File
@@ -43,8 +43,7 @@ from ..port_manager import PortManager
from .dynamips_error import DynamipsError
from .hypervisor import Hypervisor
from .nodes.router import Router
from .dynamips_vm_factory import DynamipsVMFactory
from .dynamips_device_factory import DynamipsDeviceFactory
from .dynamips_factory import DynamipsFactory
# NIOs
from .nios.nio_udp import NIOUDP
@@ -112,9 +111,8 @@ PLATFORMS_DEFAULT_RAM = {"c1700": 160,
class Dynamips(BaseManager):
_NODE_CLASS = DynamipsVMFactory
_NODE_CLASS = DynamipsFactory
_NODE_TYPE = "dynamips"
_DEVICE_CLASS = DynamipsDeviceFactory
_ghost_ios_lock = None
def __init__(self):
@@ -250,73 +248,6 @@ class Dynamips(BaseManager):
return self._dynamips_path
@asyncio.coroutine
def create_device(self, name, project_id, node_id, device_type, *args, **kwargs):
"""
Create a new Dynamips device.
:param name: Device name
:param project_id: Project identifier
:param node_id: Node identifier
"""
project = ProjectManager.instance().get_project(project_id)
if node_id and isinstance(node_id, int):
with (yield from BaseManager._convert_lock):
node_id = yield from self.convert_old_project(project, node_id, name)
if not node_id:
node_id = str(uuid4())
device = self._DEVICE_CLASS(name, node_id, project, self, device_type, *args, **kwargs)
yield from device.create()
self._devices[device.id] = device
return device
def get_device(self, node_id, project_id=None):
"""
Returns a device instance.
:param node_id: Node 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(node_id, version=4)
except ValueError:
raise aiohttp.web.HTTPBadRequest(text="Node ID {} is not a valid UUID".format(node_id))
if node_id not in self._devices:
raise aiohttp.web.HTTPNotFound(text="Node ID {} doesn't exist".format(node_id))
device = self._devices[node_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, node_id):
"""
Delete a device
:param node_id: Node identifier
:returns: Device instance
"""
device = self.get_device(node_id)
yield from device.delete()
del self._devices[device.id]
return device
def find_dynamips(self):
# look for Dynamips
@@ -1,45 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .dynamips_error import DynamipsError
from .nodes.atm_switch import ATMSwitch
from .nodes.ethernet_switch import EthernetSwitch
from .nodes.ethernet_hub import EthernetHub
from .nodes.frame_relay_switch import FrameRelaySwitch
import logging
log = logging.getLogger(__name__)
DEVICES = {'atm_switch': ATMSwitch,
'frame_relay_switch': FrameRelaySwitch,
'ethernet_switch': EthernetSwitch,
'ethernet_hub': EthernetHub}
class DynamipsDeviceFactory:
"""
Factory to create an Device object based on the type
"""
def __new__(cls, name, node_id, project, manager, device_type, **kwargs):
if device_type not in DEVICES:
raise DynamipsError("Unknown device type: {}".format(device_type))
return DEVICES[device_type](name, node_id, project, manager, **kwargs)
@@ -24,6 +24,10 @@ from .nodes.c3600 import C3600
from .nodes.c3725 import C3725
from .nodes.c3745 import C3745
from .nodes.c7200 import C7200
from .nodes.atm_switch import ATMSwitch
from .nodes.ethernet_switch import EthernetSwitch
from .nodes.ethernet_hub import EthernetHub
from .nodes.frame_relay_switch import FrameRelaySwitch
import logging
log = logging.getLogger(__name__)
@@ -37,15 +41,27 @@ PLATFORMS = {'c1700': C1700,
'c7200': C7200}
class DynamipsVMFactory:
DEVICES = {'atm_switch': ATMSwitch,
'frame_relay_switch': FrameRelaySwitch,
'ethernet_switch': EthernetSwitch,
'ethernet_hub': EthernetHub}
class DynamipsFactory:
"""
Factory to create an Router object based on the correct platform.
"""
def __new__(cls, name, node_id, project, manager, dynamips_id, platform, **kwargs):
def __new__(cls, name, node_id, project, manager, node_type="dynamips", dynamips_id=None, platform=None, **kwargs):
if platform not in PLATFORMS:
raise DynamipsError("Unknown router platform: {}".format(platform))
if node_type == "dynamips":
if platform not in PLATFORMS:
raise DynamipsError("Unknown router platform: {}".format(platform))
return PLATFORMS[platform](name, node_id, project, manager, dynamips_id, **kwargs)
return PLATFORMS[platform](name, node_id, project, manager, dynamips_id, **kwargs)
else:
if node_type not in DEVICES:
raise DynamipsError("Unknown device type: {}".format(node_type))
return DEVICES[node_type](name, node_id, project, manager, **kwargs)
+16 -10
View File
@@ -120,24 +120,30 @@ class ATMSwitch(Device):
self._mappings = mappings
@asyncio.coroutine
def delete(self):
"""
Deletes this ATM switch.
"""
def close(self):
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport, self._project)
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))
if self._hypervisor:
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))
if self._hypervisor and self in self._hypervisor.devices:
self._hypervisor.devices.remove(self)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
self._hypervisor = None
return True
@asyncio.coroutine
def delete(self):
"""
Deletes this ATM switch.
"""
yield from self.close()
def has_port(self, port):
"""
@@ -111,6 +111,10 @@ class EthernetHub(Bridge):
@asyncio.coroutine
def delete(self):
return (yield from self.close())
@asyncio.coroutine
def close(self):
"""
Deletes this hub.
"""
@@ -126,6 +130,8 @@ class EthernetHub(Bridge):
log.debug("Could not properly delete Ethernet hub {}".format(self._name))
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
self._hypervisor = None
return True
@asyncio.coroutine
def add_nio(self, nio, port_number):
@@ -146,6 +146,10 @@ class EthernetSwitch(Device):
@asyncio.coroutine
def delete(self):
return (yield from self.close())
@asyncio.coroutine
def close(self):
"""
Deletes this Ethernet switch.
"""
@@ -154,15 +158,18 @@ class EthernetSwitch(Device):
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport, self._project)
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))
if self._hypervisor:
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))
if self._hypervisor and self in self._hypervisor.devices:
self._hypervisor.devices.remove(self)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
self._hypervisor = None
return True
@asyncio.coroutine
def add_nio(self, nio, port_number):
@@ -119,24 +119,31 @@ class FrameRelaySwitch(Device):
self._mappings = mappings
@asyncio.coroutine
def delete(self):
"""
Deletes this Frame Relay switch.
"""
def close(self):
for nio in self._nios.values():
if nio and isinstance(nio, NIOUDP):
self.manager.port_manager.release_udp_port(nio.lport, self._project)
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))
if self._hypervisor:
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))
if self._hypervisor and self in self._hypervisor.devices:
self._hypervisor.devices.remove(self)
if self._hypervisor and not self._hypervisor.devices:
yield from self.hypervisor.stop()
self._hypervisor = None
@asyncio.coroutine
def delete(self):
"""
Deletes this Frame Relay switch.
"""
yield from self.close()
return True
def has_port(self, port):
"""