-class RouteHandler(Handler):
-
- def __init__(self, devices):
- self.devices = devices
-
- @handler(u'Adds a route to a device')
- def add(self, device, net_addr, prefix, gateway):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- r = Route(net_addr, prefix, gateway)
- try:
- self.devices[device].routes.index(r)
- raise RouteAlreadyExistsError(net_addr + '/' + prefix + '->' + gateway)
- except ValueError:
- self.devices[device].routes.append(r)
-
- @handler(u'Deletes a route from a device')
- def delete(self, device, net_addr, prefix, gateway):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- r = Route(net_addr, prefix, gateway)
- try:
- self.devices[device].routes.remove(r)
- except ValueError:
- raise RouteNotFoundError(net_addr + '/' + prefix + '->' + gateway)
-
- @handler(u'Flushes routes from a device')
- def flush(self, device):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- self.devices[device].routes = list()
-
-
- @handler(u'List routes')
- def list(self, device):
- try:
- k = self.devices[device].routes.keys()
- except ValueError:
- k = list()
- return k
-
- @handler(u'Get information about all routes')
- def show(self):
- try:
- k = self.devices[device].routes.values()
- except ValueError:
- k = list()
- return k
-
-
-class Address(Sequence):
-
- def __init__(self, ip, prefix, broadcast):
- self.ip = ip
- self.prefix = prefix
- self.broadcast = broadcast
-
- def as_tuple(self):
- return (self.ip, self.prefix, self.broadcast)
-
-class AddressHandler(Handler):
-
- def __init__(self, devices):
- self.devices = devices
-
- @handler(u'Adds an address to a device')
- def add(self, device, ip, prefix, broadcast='+'):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- if ip in self.devices[device].addrs:
- raise AddressAlreadyExistsError(ip)
- self.devices[device].addrs[ip] = Address(ip, prefix, broadcast)
-
- @handler(u'Deletes an address from a device')
- def delete(self, device, ip):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- if not ip in self.devices[device].addrs:
- raise AddressNotFoundError(ip)
- del self.devices[device].addrs[ip]
-
- @handler(u'Flushes addresses from a device')
- def flush(self, device):
- if not device in self.devices:
- raise DeviceNotFoundError(device)
- self.devices[device].addrs = dict()