aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/interfaces
diff options
context:
space:
mode:
authorJordan Augé <jordan.auge+fdio@email.com>2017-02-24 14:58:01 +0100
committerJordan Augé <jordan.auge+fdio@cisco.com>2017-02-24 18:36:29 +0000
commit85a341d645b57b7cd88a26ed2ea0a314704240ea (patch)
treebdda2b35003aae20103a796f86daced160b8a730 /netmodel/interfaces
parent9b30fc10fb1cbebe651e5a107e8ca5b24de54675 (diff)
Initial commit: vICN
Change-Id: I7ce66c4e84a6a1921c63442f858b49e083adc7a7 Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com>
Diffstat (limited to 'netmodel/interfaces')
-rw-r--r--netmodel/interfaces/__init__.py0
-rw-r--r--netmodel/interfaces/local.py60
-rw-r--r--netmodel/interfaces/process/__init__.py215
-rw-r--r--netmodel/interfaces/socket/__init__.py171
-rw-r--r--netmodel/interfaces/socket/tcp.py86
-rw-r--r--netmodel/interfaces/socket/udp.py88
-rw-r--r--netmodel/interfaces/socket/unix.py87
-rw-r--r--netmodel/interfaces/vicn.py100
-rw-r--r--netmodel/interfaces/websocket/__init__.py358
9 files changed, 1165 insertions, 0 deletions
diff --git a/netmodel/interfaces/__init__.py b/netmodel/interfaces/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/netmodel/interfaces/__init__.py
diff --git a/netmodel/interfaces/local.py b/netmodel/interfaces/local.py
new file mode 100644
index 00000000..c68dec7e
--- /dev/null
+++ b/netmodel/interfaces/local.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from netmodel.model.attribute import Attribute
+from netmodel.model.query import Query, ACTION_INSERT
+from netmodel.model.object import Object
+from netmodel.model.type import String
+from netmodel.network.interface import Interface, InterfaceState
+from netmodel.network.packet import Packet
+from netmodel.network.prefix import Prefix
+from netmodel.util.misc import lookahead
+
+class LocalObjectInterface(Object):
+ __type__ = 'local/interface'
+
+ name = Attribute(String)
+ type = Attribute(String)
+ status = Attribute(String)
+ description = Attribute(String)
+
+ @classmethod
+ def get(cls, query, ingress_interface):
+ cb = ingress_interface._callback
+ interfaces = ingress_interface._router.get_interfaces()
+ for interface, last in lookahead(interfaces):
+ interface_dict = {
+ 'name': interface.name,
+ 'type': interface.__interface__,
+ 'status': interface.get_status(),
+ 'description': interface.get_description(),
+ }
+ reply = Query(ACTION_INSERT, query.object_name, params =
+ interface_dict)
+ reply.last = last
+ packet = Packet.from_query(reply, reply = True)
+ cb(packet, ingress_interface = ingress_interface)
+
+class LocalInterface(Interface):
+ __interface__ = 'local'
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._router = kwargs.pop('router')
+ self.register_object(LocalObjectInterface)
+
diff --git a/netmodel/interfaces/process/__init__.py b/netmodel/interfaces/process/__init__.py
new file mode 100644
index 00000000..b985c32f
--- /dev/null
+++ b/netmodel/interfaces/process/__init__.py
@@ -0,0 +1,215 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import shlex
+import socket
+import subprocess
+import threading
+
+from netmodel.network.interface import Interface as BaseInterface
+from netmodel.network.packet import Packet
+from netmodel.network.prefix import Prefix
+from netmodel.model.attribute import Attribute
+from netmodel.model.filter import Filter
+from netmodel.model.object import Object
+from netmodel.model.query import Query, ACTION_UPDATE
+from netmodel.model.query import ACTION_SUBSCRIBE, FUNCTION_SUM
+from netmodel.model.type import String, Integer, Double
+
+DEFAULT_INTERVAL = 1 # s
+KEY_FIELD = 'device_name'
+
+class Interface(Object):
+ __type__ = 'interface'
+
+ node = Attribute(String)
+ device_name = Attribute(String)
+ bw_upstream = Attribute(Double) # bytes
+ bw_downstream = Attribute(Double) # bytes
+
+class Process(threading.Thread):
+ pass
+
+class BWMThread(Process):
+
+ SEP=';'
+ CMD="stdbuf -oL bwm-ng -t 1000 -N -o csv -c 0 -C '%s'"
+
+ # Parsing information (from README, specs section)
+ # https://github.com/jgjl/bwm-ng/blob/master/README
+ #
+ # Type rate:
+ FIELDS_RATE = ['timestamp', 'iface_name', 'bytes_out_s', 'bytes_in_s',
+ 'bytes_total_s', 'bytes_in', 'bytes_out', 'packets_out_s',
+ 'packets_in_s', 'packets_total_s', 'packets_in', 'packets_out',
+ 'errors_out_s', 'errors_in_s', 'errors_in', 'errors_out']
+ # Type svg, sum, max
+ FIELDS_SUM = ['timestamp', 'iface_name', 'bytes_out', 'bytes_in',
+ 'bytes_total', 'packets_out', 'packets_in', 'packets_total',
+ 'errors_out', 'errors_in']
+
+ def __init__(self, interfaces, callback):
+ threading.Thread.__init__(self)
+
+ # The list of interfaces is used for filtering
+ self.groups_of_interfaces = set(interfaces)
+
+ self._callback = callback
+ self._is_running = False
+
+ def run(self):
+ cmd = self.CMD % (self.SEP)
+ p = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE,
+ stderr = subprocess.STDOUT)
+ stdout = []
+ self._is_running = True
+ self.bwm_stats = dict()
+ while self._is_running:
+ line = p.stdout.readline().decode()
+ if line == '' and p.poll() is not None:
+ break
+ if line:
+ record = self._parse_line(line.strip())
+ # We use 'total' to push the statistics back to VICN
+ if record['iface_name'] == 'total':
+ for interfaces in self.groups_of_interfaces:
+ if not len(interfaces) > 1:
+ # If the tuple contains only one interface, grab
+ # the information from bwm_stats and sends it back
+ # to VICN
+ if interfaces[0] not in self.bwm_stats:
+ continue
+ interface = self.bwm_stats[interfaces[0]]
+ f_list = [[KEY_FIELD, '==', interface.device_name]]
+ query = Query(ACTION_UPDATE, Interface.__type__,
+ filter = Filter.from_list(f_list),
+ params = interface.get_attribute_dict())
+ self._callback(query)
+ else:
+ # Iterate over each tuple of interfaces to create
+ # the aggregated filter and paramters to send back
+ # Currently, we only support sum among the stats
+ # when VICN subscribes to a tuple of interfaces
+ aggregated_filters = list()
+ aggregated_interface = Interface(
+ node = socket.gethostname(),
+ device_name = 'sum',
+ bw_upstream = 0,
+ bw_downstream = 0)
+ predicate = list()
+ predicate.append(KEY_FIELD)
+ predicate.append('INCLUDED')
+ for interface in interfaces:
+ if interface not in self.bwm_stats:
+ continue
+ iface = self.bwm_stats[interface]
+ aggregated_filters.append(iface.device_name)
+ aggregated_interface.bw_upstream += \
+ iface.bw_upstream
+ aggregated_interface.bw_downstream += \
+ iface.bw_downstream
+
+ if not aggregated_filters:
+ continue
+ predicate.append(aggregated_filters)
+
+ # We support mulitple interfaces only if tied up
+ # with the SUM function. The update must have the
+ # sum function specified because it is used to
+ # match the subscribe query
+ attrs = aggregated_interface.get_attribute_dict()
+ query = Query(ACTION_UPDATE, Interface.__type__,
+ filter = Filter.from_list([predicate]),
+ params = attrs,
+ aggregate = FUNCTION_SUM)
+ self._callback(query)
+ else:
+ # Statistics from netmodel.network.interface will be stored
+ # in self.bwm_stats and used later to construct the update
+ # queries
+ interface = Interface(
+ node = socket.gethostname(),
+ device_name = record['iface_name'],
+ bw_upstream = float(record['bytes_out_s']),
+ bw_downstream = float(record['bytes_in_s']),
+ )
+
+ self.bwm_stats[record['iface_name']] = interface
+
+ rc = p.poll()
+ return rc
+
+ def stop(self):
+ self._is_running = False
+
+ def _parse_line(self, line):
+ return dict(zip(self.FIELDS_RATE, line.split(self.SEP)))
+
+class BWMInterface(BaseInterface):
+ __interface__ = 'bwm'
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._thread = None
+
+ self.register_object(Interface)
+
+ def terminate(self):
+ self._thread.stop()
+
+ def _on_reply(self, reply):
+ packet = Packet.from_query(reply, reply = True)
+ self.receive(packet)
+
+ #--------------------------------------------------------------------------
+ # Router interface
+ #--------------------------------------------------------------------------
+
+ def send_impl(self, packet):
+ query = packet.to_query()
+
+ assert query.action == ACTION_SUBSCRIBE
+ interval = query.params.get('interval', DEFAULT_INTERVAL) \
+ if query.params else DEFAULT_INTERVAL
+ assert interval
+
+ # TODO: Add the sum operator. If sum the list of interfaces is
+ # added to the BWMThread as a tuple, otherwise every single
+ # interface will be added singularly
+
+ # We currently simply extract it from the filter
+ interfaces_list = [p.value for p in query.filter if p.key == KEY_FIELD]
+
+ # interfaces is a list of tuple. If someone sbscribe to mulitple
+ # interfaces interfaces will be a list of 1 tuple. The tuple will
+ # contain the set of interfaces
+ assert len(interfaces_list) == 1
+ interfaces = interfaces_list[0] \
+ if isinstance(interfaces_list[0], tuple) \
+ else tuple([interfaces_list[0]])
+
+ # Check if interfaces is more than one. In this case, we only support
+ # The SUM function on the list of field.
+ if len(interfaces) > 1:
+ assert query.aggregate == FUNCTION_SUM
+
+ if self._thread is None:
+ self._thread = BWMThread(tuple([interfaces]), self._on_reply)
+ self._thread.start()
+ else:
+ self._thread.groups_of_interfaces.add(interfaces)
diff --git a/netmodel/interfaces/socket/__init__.py b/netmodel/interfaces/socket/__init__.py
new file mode 100644
index 00000000..00581eb4
--- /dev/null
+++ b/netmodel/interfaces/socket/__init__.py
@@ -0,0 +1,171 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+import json
+
+from netmodel.network.packet import Packet
+from netmodel.network.interface import Interface, InterfaceState
+
+class Protocol(asyncio.Protocol):
+ def __init__(self):
+ self._transport = None
+
+ def terminate(self):
+ if self._transport:
+ self._transport.close()
+
+ def send(self, packet):
+ if isinstance(packet, Packet):
+ data = json.dumps(packet.to_query().to_dict())
+ else:
+ data = packet
+ self.send_impl(data)
+
+ def receive(self, data, ingress_interface):
+ try:
+ packet = Packet.from_query(Query.from_dict(json.loads(data)))
+ except:
+ packet = data
+ self.receive(packet, ingress_interface)
+
+
+class ServerProtocol(Protocol):
+ # asyncio.Protocol
+
+ def __init__(self):
+ super().__init__()
+
+ def connection_made(self, transport):
+ """
+ Called when a connection is made.
+ The argument is the _transport representing the pipe connection.
+ To receive data, wait for data_received() calls.
+ When the connection is closed, connection_lost() is called.
+ """
+ self._transport = transport
+ self.set_state(InterfaceState.Up)
+
+ def connection_lost(self, exc):
+ """
+ Called when the connection is lost or closed.
+ The argument is an exception object or None (the latter
+ meaning a regular EOF is received or the connection was
+ aborted or closed).
+ """
+ self.set_state(InterfaceState.Down)
+
+class ClientProtocol(Protocol):
+ def __init__(self, interface, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._interface = interface
+
+ # asyncio.Protocol
+
+ def connection_made(self, transport):
+ """
+ Called when a connection is made.
+ The argument is the _transport representing the pipe connection.
+ To receive data, wait for data_received() calls.
+ When the connection is closed, connection_lost() is called.
+ """
+ self._transport = transport
+ self._interface.set_state(InterfaceState.Up)
+
+ def connection_lost(self, exc):
+ """
+ Called when the connection is lost or closed.
+ The argument is an exception object or None (the latter
+ meaning a regular EOF is received or the connection was
+ aborted or closed).
+ """
+ self._interface.set_state(InterfaceState.Down)
+
+
+
+#------------------------------------------------------------------------------
+
+class SocketServer:
+ def __init__(self, *args, **kwargs):
+ # For a server, an instance of asyncio.base_events.Server
+ self._transport = None
+ self._clients = list()
+
+ def terminate(self):
+ """Close the server and terminate all clients.
+ """
+ self._socket.close()
+ for client in self._clients:
+ self._clients.terminate()
+
+ def send(self, packet):
+ """Broadcast packet to all connected clients.
+ """
+ for client in self._clients:
+ self._clients.send(packet)
+
+ def receive(self, packet):
+ raise RuntimeError('Unexpected packet received by server interface')
+
+ def __repr__(self):
+ if self._transport:
+ peername = self._transport.get_extra_info('peername')
+ else:
+ peername = 'not connected'
+ return '<Interface {} {}>'.format(self.__interface__, peername)
+
+ async def pending_up_impl(self):
+ try:
+ self._server = await self._create_socket()
+ except Exception as e:
+ await self._set_state(InterfaceState.Error)
+ self._error = str(e)
+
+ # Only the server interface is up once the socket has been created and
+ # is listening...
+ await self._set_state(InterfaceState.Up)
+
+class SocketClient:
+ def __init__(self, *args, **kwargs):
+ # For a client connection, this is a tuple
+ # (_SelectorSocketTransport, protocol)
+ self._transport = None
+ self._protocol = None
+
+ def send_impl(self, packet):
+ self._protocol.send(packet)
+
+ async def pending_up_impl(self):
+ try:
+ self._transport, self._protocol = await self._create_socket()
+ except Exception as e:
+ await self._set_state(InterfaceState.Error)
+ self._error = str(e)
+
+ def pending_down_impl(self):
+ if self._socket:
+ self._transport.close()
+
+ def __repr__(self):
+ if self._socket:
+ peername = self._transport.get_extra_info('peername')
+ else:
+ peername = 'not connected'
+ return '<Interface {} {}>'.format(self.__interface__, peername)
+
+
diff --git a/netmodel/interfaces/socket/tcp.py b/netmodel/interfaces/socket/tcp.py
new file mode 100644
index 00000000..5b886d9a
--- /dev/null
+++ b/netmodel/interfaces/socket/tcp.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+
+DEFAULT_ADDRESS = '127.0.0.1'
+DEFAULT_PORT = 7000
+
+class TCPProtocol:
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.write(msg)
+
+ # asyncio.Protocol
+
+ def data_received(self, data):
+ """
+ Called when some data is received.
+ The argument is a bytes object.
+ """
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+class TCPServerProtocol(TCPProtocol, ServerProtocol, Interface):
+ __interface__ = 'tcp'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class TCPClientProtocol(TCPProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class TCPServerInterface(SocketServer, Interface):
+ __interface__ = 'tcpserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def new_protocol(self):
+ p = TcpServerProtocol(callback = self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ return loop.create_server(self.new_protocol, self._address, self._port)
+
+class TCPClientInterface(SocketClient, Interface):
+ __interface__ = 'tcpclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : TCPClientProtocol(self)
+ return loop.create_connection(protocol, self._address, self._port)
diff --git a/netmodel/interfaces/socket/udp.py b/netmodel/interfaces/socket/udp.py
new file mode 100644
index 00000000..d3fdb696
--- /dev/null
+++ b/netmodel/interfaces/socket/udp.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+
+DEFAULT_ADDRESS = '127.0.0.1'
+DEFAULT_PORT = 7000
+
+class UDPProtocol:
+
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.sendto(msg)
+
+ # asyncio.Protocol
+
+ def datagram_received(self, data, addr):
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+ def error_received(self, exc):
+ print('Error received:', exc)
+
+class UDPServerProtocol(UDPProtocol, ServerProtocol, Interface):
+ __interface__ = 'udp'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class UDPClientProtocol(UDPProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class UDPServerInterface(SocketServer, Interface):
+ __interface__ = 'udpserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def new_protocol(self):
+ p = UdpServerProtocol(callback = self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ return loop.create_datagram_endpoint(self.new_protocol,
+ local_addr=(self._address, self._port))
+
+class UDPClientInterface(SocketClient, Interface):
+ __interface__ = 'udpclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : UDPClientProtocol(self)
+ return loop.create_datagram_endpoint(protocol,
+ remote_addr=(self._address, self._port))
diff --git a/netmodel/interfaces/socket/unix.py b/netmodel/interfaces/socket/unix.py
new file mode 100644
index 00000000..eec3d680
--- /dev/null
+++ b/netmodel/interfaces/socket/unix.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+from netmodel.util.misc import silentremove
+
+DEFAULT_PATH = '/tmp/unix_interface'
+
+class UnixProtocol:
+
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.write(msg)
+
+ def data_received(self, data):
+ """
+ Called when some data is received.
+ The argument is a bytes object.
+ """
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+class UnixServerProtocol(UnixProtocol, ServerProtocol, Interface):
+ __interface__ = 'unix'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class UnixClientProtocol(UnixProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class UnixServerInterface(SocketServer, Interface):
+ __interface__ = 'unixserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._path = kwargs.pop('path', DEFAULT_PATH)
+ Interface.__init__(self, *args, **kwargs)
+
+ def terminate(self):
+ silentremove(self._path)
+
+ def new_protocol(self):
+ p = UnixServerProtocol(callback=self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ silentremove(self._path)
+ return loop.create_unix_server(self.new_protocol, self._path)
+
+class UnixClientInterface(SocketClient, Interface):
+ __interface__ = 'unixclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._path = kwargs.pop('path', DEFAULT_PATH)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : UnixClientProtocol(self)
+ return loop.create_unix_connection(protocol, self._path)
diff --git a/netmodel/interfaces/vicn.py b/netmodel/interfaces/vicn.py
new file mode 100644
index 00000000..9ec9672e
--- /dev/null
+++ b/netmodel/interfaces/vicn.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from vicn.core.task import BashTask
+from netmodel.model.object import Object
+from netmodel.model.attribute import Attribute
+from netmodel.model.query import Query, ACTION_INSERT
+from netmodel.model.type import String
+from netmodel.network.interface import Interface, InterfaceState
+from netmodel.network.packet import Packet
+from netmodel.network.prefix import Prefix
+from netmodel.util.misc import lookahead
+
+class VICNBaseResource(Object):
+ __type__ = 'vicn/'
+
+ @classmethod
+ def get(cls, query, interface):
+ cb = interface._callback
+
+ if query.object_name == 'script':
+ predicates = query.filter.to_list()
+ assert len(predicates) == 1
+ _, _, name = predicates[0]
+ script = '{}/{}'.format(interface._manager._base, name)
+
+ task = BashTask(None, script)
+ interface._manager.schedule(task)
+ return
+
+ elif query.object_name == 'gui':
+ interface._manager._broadcast(query)
+ return
+
+ elif query.object_name == 'resource':
+ resources = interface._manager.get_resources()
+ else:
+ resources = interface._manager.by_type_str(query.object_name)
+
+ for resource, last in lookahead(resources):
+ params = resource.get_attribute_dict(aggregates = True)
+ params['id'] = resource._state.uuid._uuid
+ params['type'] = resource.get_types()
+ params['state'] = resource._state.state
+ params['log'] = resource._state.log
+ reply = Query(ACTION_INSERT, query.object_name, params = params)
+ reply.last = last
+ packet = Packet.from_query(reply, reply = True)
+ cb(packet, ingress_interface = interface)
+
+class L2Graph(Object):
+ __type__ = 'vicn/l2graph'
+
+ @classmethod
+ def get(cls, query, interface):
+ cb = interface._callback
+
+ from vicn.resource.central import _get_l2_graph
+ G = _get_l2_graph(interface._manager, with_managed=True)
+
+ nodes = G.nodes()
+ edges = G.edges()
+ params = {'nodes': nodes, 'edges': edges}
+ reply = Query(ACTION_INSERT, query.object_name, params = params)
+ reply.last = True
+ packet = Packet.from_query(reply, reply = True)
+ cb(packet, ingress_interface = interface)
+
+class VICNInterface(Interface):
+ __interface__ = 'vicn'
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._manager = kwargs.pop('manager')
+
+ # Resources
+ resources = list()
+ resources.extend(self._manager.get_resource_type_names())
+ resources.append('resource')
+ for resource in resources:
+ class VICNResource(VICNBaseResource):
+ __type__ = '{}'.format(resource.lower())
+ self.register_object(VICNResource)
+
+ self.register_object(L2Graph)
diff --git a/netmodel/interfaces/websocket/__init__.py b/netmodel/interfaces/websocket/__init__.py
new file mode 100644
index 00000000..cb79fc39
--- /dev/null
+++ b/netmodel/interfaces/websocket/__init__.py
@@ -0,0 +1,358 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+import logging
+import json
+
+from netmodel.network.interface import Interface, InterfaceState
+from netmodel.network.packet import Packet
+from netmodel.model.query import Query
+from netmodel.model.query import ACTION_INSERT, ACTION_SELECT
+from netmodel.model.query import ACTION_UPDATE, ACTION_DELETE
+from netmodel.model.query import ACTION_EXECUTE
+
+from autobahn.asyncio.websocket import WebSocketClientProtocol, \
+ WebSocketClientFactory
+from autobahn.asyncio.websocket import WebSocketServerProtocol, \
+ WebSocketServerFactory
+
+log = logging.getLogger(__name__)
+
+DEFAULT_ADDRESS = '0.0.0.0'
+DEFAULT_CLIENT_ADDRESS = '127.0.0.1'
+DEFAULT_PORT = 9000
+DEFAULT_TIMEOUT = 2
+
+#------------------------------------------------------------------------------
+
+from json import JSONEncoder
+class DictEncoder(JSONEncoder):
+ """Default JSON encoder
+
+ Because some classes are not JSON serializable, we define here our own
+ encoder which is based on the member variables of the object.
+
+ The ideal solution would be to make all objects JSON serializable, but this
+ encoder is useful for user-defined classes that would otherwise make the
+ whole program to fail. It might though raise a warning to incitate
+ developers to make their class conformant.
+
+ Reference:
+ http://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable
+ """
+ def default(self, o):
+ try:
+ return vars(o)
+ except:
+ return {}
+
+#------------------------------------------------------------------------------
+
+class ClientProtocol(WebSocketClientProtocol):
+ """
+ Default WebSocket client protocol.
+
+ This protocol is mainly used to relay events to the Interface, which is
+ pointer to by the factory.
+ """
+
+ #--------------------------------------------------------------------------
+ # Internal methods
+ #--------------------------------------------------------------------------
+
+ def send_impl(self, packet):
+ msg = json.dumps(packet.to_query().to_dict())
+ self.sendMessage(msg.encode(), False)
+
+ #--------------------------------------------------------------------------
+ # WebSocket events
+ #--------------------------------------------------------------------------
+
+ # Websocket events
+
+ def onConnect(self, response):
+ """
+ Websocket opening handshake is started by the client.
+ """
+ self.factory.interface._on_client_connect(self, response)
+
+ def onOpen(self):
+ """
+ Websocket opening handshake has completed.
+ """
+ self.factory.interface._on_client_open(self)
+
+ def onMessage(self, payload, isBinary):
+ self.factory.interface._on_client_message(self, payload, isBinary)
+
+ def onClose(self, wasClean, code, reason):
+ self.factory.interface._on_client_close(self, wasClean, code, reason)
+
+#------------------------------------------------------------------------------
+
+class WebSocketClientInterface(Interface):
+ """
+ All messages are exchanged using text (non-binary) mode.
+ """
+ __interface__ = 'websocketclient'
+
+ def __init__(self, *args, **kwargs):
+ """
+ Constructor.
+
+ Args:
+ address (str) : Address of the remote websocket server. Defaults to
+ 127.0.0.1 (localhost).
+ port (int) : Port of the remote websocket server. Defaults to 9999.
+
+ This constructor triggers the initialization of a WebSocket client
+ factory, which is associated a ClientProtocol, as well as a reference
+ to the current interface.
+
+ PendingUp --- connect --- Up ...disconnect... Down
+ A |
+ +-----+
+ retry
+
+ All messages are exchanged using text (non-binary) mode.
+ """
+
+ self._address = kwargs.pop('address', DEFAULT_CLIENT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ self._timeout = kwargs.pop('timeout', DEFAULT_TIMEOUT)
+
+ super().__init__(*args, **kwargs)
+
+ self._factory = WebSocketClientFactory("ws://{}:{}".format(
+ self._address, self._port))
+ self._factory.protocol = ClientProtocol
+ self._factory.interface = self
+
+ self._instance = None
+
+ # Holds the instance of the connect client protocol
+ self._client = None
+
+ #--------------------------------------------------------------------------
+ # Interface API
+ #--------------------------------------------------------------------------
+
+ async def pending_up_impl(self):
+ await self._connect()
+
+ def send_impl(self, packet):
+ if not self._client:
+ log.error('interface is up but has no client')
+ self._client.send_impl(packet)
+
+ #--------------------------------------------------------------------------
+ # Internal methods
+ #--------------------------------------------------------------------------
+
+ async def _connect(self):
+ loop = asyncio.get_event_loop()
+ try:
+ self._instance = await loop.create_connection(self._factory,
+ self._address, self._port)
+ except Exception as e:
+ log.warning('Connect failed : {}'.format(e))
+ self._instance = None
+ # don't await for retry, since it cause an infinite recursion...
+ asyncio.ensure_future(self._retry())
+
+ async def _retry(self):
+ """
+ Timer: retry connection after timeout.
+ """
+ log.info('Reconnecting in {} seconds...'.format(self._timeout))
+ await asyncio.sleep(self._timeout)
+ log.info('Reconnecting...')
+ await self._connect()
+
+ # WebSocket events (from the underlying protocol)
+
+ def _on_client_connect(self, client, response):
+ self._client = client
+
+ def _on_client_open(self, client):
+ self.set_state(InterfaceState.Up)
+
+ def _on_client_message(self, client, payload, isBinary):
+ """
+ Event: a message is received by the WebSocket client connection.
+ """
+
+ assert not isBinary
+
+ args = json.loads(payload.decode('utf-8'))
+ query, record = None, None
+ if len(args) == 2:
+ query, record = args
+ else:
+ query = args
+
+ if isinstance(query, dict):
+ query = Query.from_dict(query)
+ else:
+ query = Query(ACTION_SELECT, query)
+
+ packet = Packet.from_query(query)
+
+ self.receive(packet)
+
+ def _on_client_close(self, client, wasClean, code, reason):
+ self._client = None
+ self._instance = None
+
+ self.set_state(InterfaceState.Down)
+
+ # Schedule reconnection
+ asyncio.ensure_future(self._retry())
+
+#------------------------------------------------------------------------------
+
+class ServerProtocol(WebSocketServerProtocol, Interface):
+ """
+ Default WebSocket server protocol.
+
+ This protocol is used for every server-side accepted WebSocket connection.
+ As such it is an Interface on its own, and should handle the Interface state
+ machinery.
+
+ We would better triggering code directly
+ """
+ __interface__ = 'websocket'
+
+ def __init__(self, callback, hook):
+ """
+ Constructor.
+
+ Args:
+ callback (Function[ -> ]) :
+ hook (Function[->]) : Hook method to be called for every packet to
+ be sent on the interface. Processing continues with the packet
+ returned by this function, or is interrupted in case of a None
+ value. Defaults to None = no hook.
+ """
+ WebSocketServerProtocol.__init__(self)
+ Interface.__init__(self, callback=callback, hook=hook)
+
+ #--------------------------------------------------------------------------
+ # Interface API
+ #--------------------------------------------------------------------------
+
+ async def pending_up_impl(self):
+ await self._set_state(InterfaceState.Up)
+
+ def send_impl(self, packet):
+ # We assume we only send records...
+ msg = json.dumps(packet.to_query().to_dict(), cls=DictEncoder)
+ self.sendMessage(msg.encode(), False)
+
+ #--------------------------------------------------------------------------
+ # Internal methods
+ #--------------------------------------------------------------------------
+
+ # Websocket events
+
+ def onConnect(self, request):
+ self.factory._instances.append(self)
+ self.set_state(InterfaceState.Up)
+
+ def onOpen(self):
+ #print("WebSocket connection open.")
+ pass
+
+ def onMessage(self, payload, isBinary):
+ assert not isBinary, "Binary message received: {0} bytes".format(
+ len(payload))
+ query_dict = json.loads(payload.decode('utf8'))
+ query = Query.from_dict(query_dict)
+ packet = Packet.from_query(query)
+ self.receive(packet)
+
+ def onClose(self, wasClean, code, reason):
+ self.set_state(InterfaceState.Down)
+ try:
+ self.factory._instances.remove(self)
+ except: pass
+
+ self.delete_interface(self)
+
+#------------------------------------------------------------------------------
+
+class WebSocketServerInterface(Interface):
+ """
+ This virtual interface only listens for incoming connections in order to
+ dynamically instanciate new interfaces upon client connection.
+
+ It is also used to broadcast packets to all connected clients.
+
+ All messages are exchanged using text (non-binary) mode.
+ """
+
+ __interface__ = 'websocketserver'
+
+ def __init__(self, *args, **kwargs):
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+
+ super().__init__(*args, **kwargs)
+
+ def new_server_protocol():
+ p = ServerProtocol(self._callback, self._hook)
+ self.spawn_interface(p)
+ return p
+
+ ws_url = u"ws://{}:{}".format(self._address, self._port)
+ self._factory = WebSocketServerFactory(ws_url)
+ # see comment in MyWebSocketServerFactory
+ self._factory.protocol = new_server_protocol
+ self._factory._callback = self._callback
+ self._factory._interface = self
+
+ # A list of all connected instances (= interfaces), used to broadcast
+ # packets.
+ self._factory._instances = list()
+
+ #--------------------------------------------------------------------------
+ # Interface API
+ #--------------------------------------------------------------------------
+
+ async def pending_up_impl(self):
+ """
+ As we have no feedback for when the server is actually started, we mark
+ the interface up as soon as the create_server method returns.
+ """
+ loop = asyncio.get_event_loop()
+ # Websocket server
+ log.info('WebSocket server started')
+ self._server = await loop.create_server(self._factory, self._address,
+ self._port)
+ await self._set_state(InterfaceState.Up)
+
+ async def pending_down_impl(self):
+ raise NotImplementedError
+
+ def send_impl(self, packet):
+ """
+ Sends a packet to all connected clients (broadcast).
+ """
+ for instance in self._factory._instances:
+ instance.send(packet)