1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
#!/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 enum
import inspect
import logging
import pkgutil
import sys
import traceback
import netmodel.interfaces as interfaces
from netmodel.network.packet import Packet
from netmodel.network.prefix import Prefix
# Tag identifying an interface name
INTERFACE_NAME_PROPERTY = '__interface__'
RECONNECTION_DELAY = 10
log = logging.getLogger(__name__)
class InterfaceState(enum.Enum):
Up = 'up'
Down = 'down'
PendingUp = 'up (pending)'
PendingDown = 'down (pending)'
Error = 'error'
def register_interfaces():
Interface._factory = dict()
for loader, module_name, is_pkg in pkgutil.walk_packages(interfaces.__path__,
interfaces.__name__ + '.'):
# Note: we cannot skip files using is_pkg otherwise it will ignore
# interfaces defined outside of __init__.py
#if not is_pkg:
# continue
try:
module = loader.find_module(module_name).load_module(module_name)
for _, obj in inspect.getmembers(module):
if not inspect.isclass(obj):
continue
if not issubclass(obj, Interface):
continue
if obj is Interface:
continue
if not hasattr(obj, INTERFACE_NAME_PROPERTY):
log.warning('Ignored interface' + module_name + \
'with no ' + INTERFACE_NAME_PROPERTY + ' property')
continue
name = getattr(obj, INTERFACE_NAME_PROPERTY)
Interface._factory[name] = obj
except ImportError as e:
log.warning('Interface {} automatically disabled. ' \
'Please install dependencies if you wish to use it: {}'\
.format(module_name, e))
except Exception as e:
log.warning('Failed to load interface {}: {}'.format(
module_name, e))
#------------------------------------------------------------------------------
class Interface:
@classmethod
def get(cls, name):
if not hasattr(cls, '_factory'):
register_interfaces()
return cls._factory.get(name, None)
STATE_DOWN = 0
STATE_PENDING_UP = 1
STATE_UP = 2
STATE_PENDING_DOWN = 3
def __init__(self, *args, **kwargs):
self._callback = kwargs.pop('callback', None)
self._hook = kwargs.pop('hook', None)
self._tx_buffer = list()
self._state = InterfaceState.Down
self._error = None
self._reconnecting = True
self._reconnection_delay = RECONNECTION_DELAY
self._registered_objects = dict()
# Callbacks
self._up_callbacks = list()
self._down_callbacks = list()
self._spawn_callbacks = list()
self._delete_callbacks = list()
# Set upon registration
self._name = None
def terminate(self):
self.set_state(InterfaceState.PendingDown)
def __repr__(self):
return "<Interface %s>" % (self.__class__.__name__)
def __hash__(self):
return hash(self._name)
#---------------------------------------------------------------------------
def register_object(self, obj):
self._registered_objects[obj.__type__] = obj
def get_prefixes(self):
return [ Prefix(v.__type__) for v in self._registered_objects.values() ]
#---------------------------------------------------------------------------
# State management, callbacks
#---------------------------------------------------------------------------
def set_state(self, state):
asyncio.ensure_future(self._set_state(state))
async def _set_state(self, state):
self._state = state
if state == InterfaceState.PendingUp:
await self.pending_up_impl()
elif state == InterfaceState.PendingDown:
await self.pending_down_impl()
elif state == InterfaceState.Error:
pass
elif state == InterfaceState.Up:
log.info("Interface {} : new state UP.".format(self.__interface__,))
if self._tx_buffer:
log.info("Platform %s: sending %d buffered packets." %
(self.__interface__, len(self._tx_buffer)))
while self._tx_buffer:
packet = self._tx_buffer.pop()
self.send_impl(packet)
# Trigger callbacks to inform interface is up
for cb, args, kwargs in self._up_callbacks:
cb(self, *args, **kwargs)
elif state == InterfaceState.Down:
log.info("Interface %s: new state DOWN." % (self.__interface__,))
self._state = self.STATE_DOWN
# Trigger callbacks to inform interface is down
for cb, args, kwargs in self._down_callbacks:
cb(self, *args, **kwargs)
def spawn_interface(self, interface):
#print('spawn interface', interface)
for cb, args, kwargs in self._spawn_callbacks:
cb(interface, *args, **kwargs)
def delete_interface(self, interface):
for cb, args, kwargs in self._delete_callbacks:
cb(interface, *args, **kwargs)
#--------------------------------------------------------------------------
def set_reconnecting(self, reconnecting):
self._reconnecting = reconnecting
def get_interface_type(self):
return self.__interface__
def get_description(self):
return str(self)
def get_status(self):
return 'UP' if self.is_up() else 'ERROR' if self.is_error() else 'DOWN'
def is_up(self):
return self._state == InterfaceState.Up
def is_down(self):
return not self.is_up()
def is_error(self):
return self.is_down() and self._error is not None
def reinit_impl(self):
pass
def reinit(self, **platform_config):
self.set_down()
if platform_config:
self.reconnect_impl(self, **platform_config)
self.set_up()
#--------------------------------------------------------------------------
# Callback management
#--------------------------------------------------------------------------
def add_up_callback(self, callback, *args, **kwargs):
cb_tuple = (callback, args, kwargs)
self._up_callbacks.append(cb_tuple)
def del_up_callback(self, callback):
self._up_callbacks = [cb for cb in self._up_callbacks \
if cb[0] == callback]
def add_down_callback(self, callback, *args, **kwargs):
cb_tuple = (callback, args, kwargs)
self._down_callbacks.append(cb_tuple)
def del_down_callback(self, callback):
self._down_callbacks = [cb for cb in self._down_callbacks \
if cb[0] == callback]
def add_spawn_callback(self, callback, *args, **kwargs):
cb_tuple = (callback, args, kwargs)
self._spawn_callbacks.append(cb_tuple)
def del_spawn_callback(self, callback):
self._spawn_callbacks = [cb for cb in self._spawn_callbacks \
if cb[0] == callback]
def add_delete_callback(self, callback, *args, **kwargs):
cb_tuple = (callback, args, kwargs)
self._delete_callbacks.append(cb_tuple)
def del_delete_callback(self, callback):
self._delete_callbacks = [cb for cb in self._delete_callbacks \
if cb[0] == callback]
#--------------------------------------------------------------------------
# Interface API
#--------------------------------------------------------------------------
async def pending_up_impl(self):
self.set_state(InterfaceState.Up)
def send_impl(self, packet):
query = packet.to_query()
obj = self._registered_objects.get(query.object_name)
obj.get(query, self)
def receive_impl(self, packet):
ingress_interface = self
cb = self._callback
if self._hook:
new_packet = self._hook(packet)
if cb is not None and new_packet is not None:
cb(new_packet, ingress_interface=ingress_interface)
return
if cb is None:
return
cb(packet, ingress_interface=ingress_interface)
#--------------------------------------------------------------------------
def send(self, packet):
if self.is_up():
self.send_impl(packet)
else:
self._tx_buffer.append(packet)
def receive(self, packet):
"""
For packets received from outside (eg. a remote server).
"""
self.receive_impl(packet)
def execute(self, query):
self.send(Packet.from_query(query))
|