json-schema validations.

NIO creation refactoring.
This commit is contained in:
grossmj
2014-04-23 12:31:33 -06:00
parent 2ebac2f20a
commit 9be5625c38
19 changed files with 2486 additions and 517 deletions

View File

@ -20,6 +20,13 @@ from gns3server.modules import IModule
from ..nodes.atm_switch import ATMSwitch
from ..dynamips_error import DynamipsError
from ..schemas.atmsw import ATMSW_CREATE_SCHEMA
from ..schemas.atmsw import ATMSW_DELETE_SCHEMA
from ..schemas.atmsw import ATMSW_UPDATE_SCHEMA
from ..schemas.atmsw import ATMSW_ALLOCATE_UDP_PORT_SCHEMA
from ..schemas.atmsw import ATMSW_ADD_NIO_SCHEMA
from ..schemas.atmsw import ATMSW_DELETE_NIO_SCHEMA
import logging
log = logging.getLogger(__name__)
@ -41,7 +48,10 @@ class ATMSW(object):
:param request: JSON request
"""
#TODO: JSON schema validation for the request
# validate the request
if request and not self.validate_request(request, ATMSW_CREATE_SCHEMA):
return
name = None
if request and "name" in request:
name = request["name"]
@ -71,22 +81,20 @@ class ATMSW(object):
- id (switch identifier)
Response parameters:
- same as original request
- True on success
:param request: JSON request
"""
if request == None:
self.send_param_error()
# validate the request
if not self.validate_request(request, ATMSW_DELETE_SCHEMA):
return
#TODO: JSON schema validation for the request
log.debug("received request {}".format(request))
# get the ATM switch instance
atmsw_id = request["id"]
if atmsw_id not in self._atm_switches:
self.send_custom_error("ATM switch id {} doesn't exist".format(atmsw_id))
atmsw = self.get_device_instance(atmsw_id, self._atm_switches)
if not atmsw:
return
atmsw = self._atm_switches[atmsw_id]
try:
atmsw.delete()
@ -95,7 +103,7 @@ class ATMSW(object):
except DynamipsError as e:
self.send_custom_error(str(e))
return
self.send_response(request)
self.send_response(True)
@IModule.route("dynamips.atmsw.update")
def atmsw_update(self, request):
@ -109,32 +117,31 @@ class ATMSW(object):
- name (new switch name)
Response parameters:
- same as original request
- name if changed
:param request: JSON request
"""
if request == None:
self.send_param_error()
# validate the request
if not self.validate_request(request, ATMSW_UPDATE_SCHEMA):
return
#TODO: JSON schema validation for the request
log.debug("received request {}".format(request))
atmsw_id = request["id"]
if atmsw_id not in self._atm_switches:
self.send_custom_error("ATM switch id {} doesn't exist".format(atmsw_id))
# get the ATM switch instance
atmsw = self.get_device_instance(request["id"], self._atm_switches)
if not atmsw:
return
atmsw = self._atm_switches[atmsw_id]
response = {}
# rename the switch if requested
if "name" in request and atmsw.name != request["name"]:
try:
atmsw.name = request["name"]
response["name"] = atmsw.name
except DynamipsError as e:
self.send_custom_error(str(e))
return
self.send_response(request)
self.send_response(response)
@IModule.route("dynamips.atmsw.allocate_udp_port")
def atmsw_allocate_udp_port(self, request):
@ -153,17 +160,14 @@ class ATMSW(object):
:param request: JSON request
"""
if request == None:
self.send_param_error()
# validate the request
if not self.validate_request(request, ATMSW_ALLOCATE_UDP_PORT_SCHEMA):
return
#TODO: JSON schema validation for the request
log.debug("received request {}".format(request))
atmsw_id = request["id"]
if atmsw_id not in self._atm_switches:
self.send_custom_error("ATM switch id {} doesn't exist".format(atmsw_id))
# get the ATM switch instance
atmsw = self.get_device_instance(request["id"], self._atm_switches)
if not atmsw:
return
atmsw = self._atm_switches[atmsw_id]
try:
# allocate a new UDP port
@ -185,42 +189,39 @@ class ATMSW(object):
- port (port identifier)
- port_id (port identifier)
- mappings (VCs/VPs mapped to the port)
- nio (nio type, one of the following)
- "NIO_UDP"
- nio (one of the following)
- type "nio_udp"
- lport (local port)
- rhost (remote host)
- rport (remote port)
- "NIO_GenericEthernet"
- type "nio_generic_ethernet"
- ethernet_device (Ethernet device name e.g. eth0)
- "NIO_LinuxEthernet"
- type "nio_linux_ethernet"
- ethernet_device (Ethernet device name e.g. eth0)
- "NIO_TAP"
- type "nio_tap"
- tap_device (TAP device name e.g. tap0)
- "NIO_UNIX"
- type "nio_unix"
- local_file (path to UNIX socket file)
- remote_file (path to UNIX socket file)
- "NIO_VDE"
- type "nio_vde"
- control_file (path to VDE control file)
- local_file (path to VDE local file)
- "NIO_Null"
- type "nio_null"
Response parameters:
- same as original request
- port_id (unique port identifier)
:param request: JSON request
"""
if request == None:
self.send_param_error()
# validate the request
if not self.validate_request(request, ATMSW_ADD_NIO_SCHEMA):
return
#TODO: JSON schema validation for the request
log.debug("received request {}".format(request))
atmsw_id = request["id"]
if atmsw_id not in self._atm_switches:
self.send_custom_error("ATM switch id {} doesn't exist".format(atmsw_id))
# get the ATM switch instance
atmsw = self.get_device_instance(request["id"], self._atm_switches)
if not atmsw:
return
atmsw = self._atm_switches[atmsw_id]
port = request["port"]
mappings = request["mappings"]
@ -261,8 +262,7 @@ class ATMSW(object):
self.send_custom_error(str(e))
return
# for now send back the original request
self.send_response(request)
self.send_response({"port_id": request["port_id"]})
@IModule.route("dynamips.atmsw.delete_nio")
def atmsw_delete_nio(self, request):
@ -274,24 +274,21 @@ class ATMSW(object):
- port (port identifier)
Response parameters:
- same as original request
- True on success
:param request: JSON request
"""
if request == None:
self.send_param_error()
# validate the request
if not self.validate_request(request, ATMSW_DELETE_NIO_SCHEMA):
return
#TODO: JSON schema validation for the request
log.debug("received request {}".format(request))
atmsw_id = request["id"]
if atmsw_id not in self._atm_switches:
self.send_custom_error("ATM switch id {} doesn't exist".format(atmsw_id))
# get the ATM switch instance
atmsw = self.get_device_instance(request["id"], self._atm_switches)
if not atmsw:
return
atmsw = self._atm_switches[atmsw_id]
port = request["port"]
try:
for source, destination in atmsw.mapping.copy().items():
if len(source) == 3 and len(destination) == 3:
@ -315,5 +312,4 @@ class ATMSW(object):
self.send_custom_error(str(e))
return
# for now send back the original request
self.send_response(request)
self.send_response(True)