2014-01-31 23:31:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 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 gns3server.modules import IModule
|
|
|
|
from ..nodes.frame_relay_switch import FrameRelaySwitch
|
|
|
|
from ..dynamips_error import DynamipsError
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
from ..schemas.frsw import FRSW_CREATE_SCHEMA
|
|
|
|
from ..schemas.frsw import FRSW_DELETE_SCHEMA
|
|
|
|
from ..schemas.frsw import FRSW_UPDATE_SCHEMA
|
|
|
|
from ..schemas.frsw import FRSW_ALLOCATE_UDP_PORT_SCHEMA
|
|
|
|
from ..schemas.frsw import FRSW_ADD_NIO_SCHEMA
|
|
|
|
from ..schemas.frsw import FRSW_DELETE_NIO_SCHEMA
|
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FRSW(object):
|
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.create")
|
|
|
|
def frsw_create(self, request):
|
|
|
|
"""
|
|
|
|
Creates a new Frame-Relay switch.
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Optional request parameters:
|
|
|
|
- name (switch name)
|
|
|
|
|
|
|
|
Response parameters:
|
|
|
|
- id (switch identifier)
|
|
|
|
- name (switch name)
|
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if request and not self.validate_request(request, FRSW_CREATE_SCHEMA):
|
|
|
|
return
|
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
name = None
|
|
|
|
if request and "name" in request:
|
|
|
|
name = request["name"]
|
|
|
|
|
|
|
|
try:
|
2014-03-16 03:41:04 +00:00
|
|
|
if not self._hypervisor_manager:
|
|
|
|
self.start_hypervisor_manager()
|
|
|
|
|
2014-02-21 00:39:03 +00:00
|
|
|
hypervisor = self._hypervisor_manager.allocate_hypervisor_for_simulated_device()
|
2014-01-31 23:31:34 +00:00
|
|
|
frsw = FrameRelaySwitch(hypervisor, name)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
response = {"name": frsw.name,
|
|
|
|
"id": frsw.id}
|
|
|
|
|
|
|
|
self._frame_relay_switches[frsw.id] = frsw
|
|
|
|
self.send_response(response)
|
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.delete")
|
|
|
|
def frsw_delete(self, request):
|
|
|
|
"""
|
|
|
|
Deletes a Frame Relay switch.
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (switch identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 18:31:33 +00:00
|
|
|
- True on success
|
2014-02-26 18:47:12 +00:00
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, FRSW_DELETE_SCHEMA):
|
2014-02-26 18:47:12 +00:00
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# get the Frame relay switch instance
|
2014-01-31 23:31:34 +00:00
|
|
|
frsw_id = request["id"]
|
2014-04-23 18:31:33 +00:00
|
|
|
frsw = self.get_device_instance(frsw_id, self._frame_relay_switches)
|
|
|
|
if not frsw:
|
2014-04-12 20:43:30 +00:00
|
|
|
return
|
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
try:
|
|
|
|
frsw.delete()
|
2014-02-21 00:39:03 +00:00
|
|
|
self._hypervisor_manager.unallocate_hypervisor_for_simulated_device(frsw)
|
2014-03-11 21:45:04 +00:00
|
|
|
del self._frame_relay_switches[frsw_id]
|
2014-01-31 23:31:34 +00:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 18:31:33 +00:00
|
|
|
|
|
|
|
self.send_response(True)
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.update")
|
|
|
|
def frsw_update(self, request):
|
|
|
|
"""
|
|
|
|
Updates a Frame Relay switch.
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (switch identifier)
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- name (new switch name)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 18:31:33 +00:00
|
|
|
- name if updated
|
2014-02-26 18:47:12 +00:00
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, FRSW_UPDATE_SCHEMA):
|
2014-02-26 18:47:12 +00:00
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# get the Frame relay switch instance
|
|
|
|
frsw = self.get_device_instance(request["id"], self._frame_relay_switches)
|
|
|
|
if not frsw:
|
2014-04-12 20:43:30 +00:00
|
|
|
return
|
2014-02-26 18:47:12 +00:00
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
response = {}
|
2014-02-26 18:47:12 +00:00
|
|
|
# rename the switch if requested
|
|
|
|
if "name" in request and frsw.name != request["name"]:
|
|
|
|
try:
|
|
|
|
frsw.name = request["name"]
|
2014-04-23 18:31:33 +00:00
|
|
|
response["name"] = frsw.name
|
2014-02-26 18:47:12 +00:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 18:31:33 +00:00
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
self.send_response(request)
|
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.allocate_udp_port")
|
|
|
|
def frsw_allocate_udp_port(self, request):
|
|
|
|
"""
|
|
|
|
Allocates a UDP port in order to create an UDP NIO for an
|
|
|
|
Frame Relay switch.
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (switch identifier)
|
2014-02-28 04:50:46 +00:00
|
|
|
- port_id (port identifier)
|
2014-02-26 18:47:12 +00:00
|
|
|
|
|
|
|
Response parameters:
|
2014-02-28 04:50:46 +00:00
|
|
|
- port_id (port identifier)
|
2014-02-26 18:47:12 +00:00
|
|
|
- lport (allocated local port)
|
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, FRSW_ALLOCATE_UDP_PORT_SCHEMA):
|
2014-02-26 18:47:12 +00:00
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# get the Frame relay switch instance
|
|
|
|
frsw = self.get_device_instance(request["id"], self._frame_relay_switches)
|
|
|
|
if not frsw:
|
2014-04-12 20:43:30 +00:00
|
|
|
return
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# allocate a new UDP port
|
|
|
|
response = self.allocate_udp_port(frsw)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-02-28 04:50:46 +00:00
|
|
|
response["port_id"] = request["port_id"]
|
2014-01-31 23:31:34 +00:00
|
|
|
self.send_response(response)
|
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.add_nio")
|
|
|
|
def frsw_add_nio(self, request):
|
|
|
|
"""
|
|
|
|
Adds an NIO (Network Input/Output) for an Frame-Relay switch.
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (switch identifier)
|
|
|
|
- port (port identifier)
|
2014-02-28 04:50:46 +00:00
|
|
|
- port_id (port identifier)
|
2014-02-26 18:47:12 +00:00
|
|
|
- mappings (VCs mapped to the port)
|
2014-04-23 18:31:33 +00:00
|
|
|
- nio (one of the following)
|
|
|
|
- type "nio_udp"
|
2014-02-26 18:47:12 +00:00
|
|
|
- lport (local port)
|
|
|
|
- rhost (remote host)
|
|
|
|
- rport (remote port)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_generic_ethernet"
|
2014-02-26 18:47:12 +00:00
|
|
|
- ethernet_device (Ethernet device name e.g. eth0)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_linux_ethernet"
|
2014-02-26 18:47:12 +00:00
|
|
|
- ethernet_device (Ethernet device name e.g. eth0)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_tap"
|
2014-02-26 18:47:12 +00:00
|
|
|
- tap_device (TAP device name e.g. tap0)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_unix"
|
2014-02-26 18:47:12 +00:00
|
|
|
- local_file (path to UNIX socket file)
|
|
|
|
- remote_file (path to UNIX socket file)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_vde"
|
2014-02-26 18:47:12 +00:00
|
|
|
- control_file (path to VDE control file)
|
|
|
|
- local_file (path to VDE local file)
|
2014-04-23 18:31:33 +00:00
|
|
|
- type "nio_null"
|
2014-02-26 18:47:12 +00:00
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 18:31:33 +00:00
|
|
|
- port_id (unique port identifier)
|
2014-02-26 18:47:12 +00:00
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, FRSW_ADD_NIO_SCHEMA):
|
2014-02-26 18:47:12 +00:00
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# get the Frame relay switch instance
|
|
|
|
frsw = self.get_device_instance(request["id"], self._frame_relay_switches)
|
|
|
|
if not frsw:
|
2014-04-12 20:43:30 +00:00
|
|
|
return
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
port = request["port"]
|
2014-02-21 00:39:03 +00:00
|
|
|
mappings = request["mappings"]
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
nio = self.create_nio(frsw, request)
|
2014-02-26 18:47:12 +00:00
|
|
|
if not nio:
|
|
|
|
raise DynamipsError("Requested NIO doesn't exist: {}".format(request["nio"]))
|
2014-01-31 23:31:34 +00:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
frsw.add_nio(nio, port)
|
|
|
|
|
|
|
|
# add the VCs mapped with this port/nio
|
2014-02-21 00:39:03 +00:00
|
|
|
for source, destination in mappings.items():
|
2014-01-31 23:31:34 +00:00
|
|
|
source_port, source_dlci = map(int, source.split(':'))
|
|
|
|
destination_port, destination_dlci = map(int, destination.split(':'))
|
|
|
|
if frsw.has_port(destination_port):
|
|
|
|
if (source_port, source_dlci) not in frsw.mapping and (destination_port, destination_dlci) not in frsw.mapping:
|
|
|
|
frsw.map_vc(source_port, source_dlci, destination_port, destination_dlci)
|
|
|
|
frsw.map_vc(destination_port, destination_dlci, source_port, source_dlci)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
self.send_response({"port_id": request["port_id"]})
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
@IModule.route("dynamips.frsw.delete_nio")
|
|
|
|
def frsw_delete_nio(self, request):
|
|
|
|
"""
|
|
|
|
Deletes an NIO (Network Input/Output).
|
|
|
|
|
2014-02-26 18:47:12 +00:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (switch identifier)
|
|
|
|
- port (port identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 18:31:33 +00:00
|
|
|
- True on success
|
2014-02-26 18:47:12 +00:00
|
|
|
|
2014-01-31 23:31:34 +00:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, FRSW_DELETE_NIO_SCHEMA):
|
2014-02-26 18:47:12 +00:00
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
# get the Frame relay switch instance
|
|
|
|
frsw = self.get_device_instance(request["id"], self._frame_relay_switches)
|
|
|
|
if not frsw:
|
2014-04-12 20:43:30 +00:00
|
|
|
return
|
2014-01-31 23:31:34 +00:00
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
port = request["port"]
|
2014-01-31 23:31:34 +00:00
|
|
|
try:
|
|
|
|
# remove the VCs mapped with this port/nio
|
|
|
|
for source, destination in frsw.mapping.copy().items():
|
|
|
|
source_port, source_dlci = source
|
|
|
|
destination_port, destination_dlci = destination
|
|
|
|
if port == source_port:
|
|
|
|
frsw.unmap_vc(source_port, source_dlci, destination_port, destination_dlci)
|
|
|
|
frsw.unmap_vc(destination_port, destination_dlci, source_port, source_dlci)
|
|
|
|
|
|
|
|
nio = frsw.remove_nio(port)
|
|
|
|
nio.delete()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
self.send_response(True)
|