Passed
Pull Request — master (#112)
by Carlos
02:26
created

build.main.Main.is_ignored()   B

Complexity

Conditions 7

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
nop 2
dl 0
loc 19
rs 8
c 0
b 0
f 0
ccs 12
cts 12
cp 1
crap 7
1
"""kytos/flow_manager NApp installs, lists and deletes switch flows."""
2 1
from collections import OrderedDict
3
4 1
from flask import jsonify, request
5 1
from pyof.v0x01.asynchronous.error_msg import BadActionCode
6 1
from pyof.v0x01.common.phy_port import PortConfig
7
8 1
from kytos.core import KytosEvent, KytosNApp, log, rest
9 1
from kytos.core.helpers import listen_to
10 1
from napps.kytos.flow_manager.storehouse import StoreHouse
11 1
from napps.kytos.of_core.flow import FlowFactory
12
13 1
from .exceptions import InvalidCommandError
14 1
from .settings import (CONSISTENCY_COOKIE_EXCEPTION_RANGE,
15
                       CONSISTENCY_INTERVAL,
16
                       CONSISTENCY_TABLE_ID_EXCEPTION_RANGE,
17
                       FLOWS_DICT_MAX_SIZE)
18
19
20 1
class Main(KytosNApp):
21
    """Main class to be used by Kytos controller."""
22
23 1
    def setup(self):
24
        """Replace the 'init' method for the KytosApp subclass.
25
26
        The setup method is automatically called by the run method.
27
        Users shouldn't call this method directly.
28
        """
29 1
        log.debug("flow-manager starting")
30 1
        self._flow_mods_sent = OrderedDict()
31 1
        self._flow_mods_sent_max_size = FLOWS_DICT_MAX_SIZE
32 1
        self.cookies_exception_range = CONSISTENCY_COOKIE_EXCEPTION_RANGE
33 1
        self.table_ids_exception_range = CONSISTENCY_TABLE_ID_EXCEPTION_RANGE
34
35
        # Storehouse client to save and restore flow data:
36 1
        self.storehouse = StoreHouse(self.controller)
37
38
        # Format of stored flow data:
39
        # {'flow_persistence': {'dpid_str': {'flow_list': [
40
        #                                     {'command': '<add|delete>',
41
        #                                      'flow': {flow_dict}}]}}}
42 1
        self.stored_flows = {}
43 1
        self.resent_flows = set()
44 1
        if CONSISTENCY_INTERVAL > 0:
45 1
            self.execute_as_loop(CONSISTENCY_INTERVAL)
46
47 1
    def execute(self):
48
        """Run once on NApp 'start' or in a loop.
49
50
        The execute method is called by the run method of KytosNApp class.
51
        Users shouldn't call this method directly.
52
        """
53
        self._load_flows()
54
55
        if CONSISTENCY_INTERVAL > 0:
56
            self.consistency_check()
57
58 1
    def shutdown(self):
59
        """Shutdown routine of the NApp."""
60
        log.debug("flow-manager stopping")
61
62 1
    @listen_to('kytos/of_core.handshake.completed')
63
    def resend_stored_flows(self, event):
64
        """Resend stored Flows."""
65 1
        switch = event.content['switch']
66 1
        dpid = str(switch.dpid)
67
        # This can be a problem because this code is running a thread
68 1
        if dpid in self.resent_flows:
69
            log.debug(f'Flow already resent to the switch {dpid}')
70
            return
71 1
        if dpid in self.stored_flows:
72 1
            flow_list = self.stored_flows[dpid]['flow_list']
73 1
            for flow in flow_list:
74 1
                command = flow['command']
75 1
                flows_dict = {"flows": [flow['flow']]}
76 1
                self._install_flows(command, flows_dict, [switch])
77 1
            self.resent_flows.add(dpid)
78 1
            log.info(f'Flows resent to Switch {dpid}')
79
80 1
    def is_ignored(self, flow):
81
        """Verify if the flow is in the exception range.
82
83
        Check by `cookie` range and `table_id` range.
84
        """
85
        # Check by cookie
86 1
        if len(self.cookies_exception_range) == 2:
87 1
            begin_cookie = self.cookies_exception_range[0]
88 1
            end_cookie = self.cookies_exception_range[1]
89 1
            if flow.cookie >= begin_cookie and flow.cookie <= end_cookie:
90 1
                return True
91
92
        # Check by `table_id`
93 1
        if len(self.table_ids_exception_range) == 2:
94 1
            begin_tab_id = self.table_ids_exception_range[0]
95 1
            end_tab_id = self.table_ids_exception_range[1]
96 1
            if flow.table_id >= begin_tab_id and flow.table_id <= end_tab_id:
97 1
                return True
98 1
        return False
99
100 1
    def consistency_check(self):
101
        """Check the consistency of flows in each switch."""
102
        switches = self.controller.switches.values()
103
104
        for switch in switches:
105
            # Check if a dpid is a key in 'stored_flows' dictionary
106
            if switch.is_enabled():
107
                self.check_storehouse_consistency(switch)
108
109
                if switch.dpid in self.stored_flows:
110
                    self.check_switch_consistency(switch)
111
112 1
    def check_switch_consistency(self, switch):
113
        """Check consistency of installed flows for a specific switch."""
114 1
        dpid = switch.dpid
115
116
        # Flows stored in storehouse
117 1
        stored_flows = self.stored_flows[dpid]['flow_list']
118
119 1
        serializer = FlowFactory.get_class(switch)
120
121 1
        for stored_flow in stored_flows:
122 1
            command = stored_flow['command']
123 1
            stored_flow_obj = serializer.from_dict(stored_flow['flow'], switch)
124
125 1
            flow = {'flows': [stored_flow['flow']]}
126
127 1
            if stored_flow_obj not in switch.flows:
128 1
                if command == 'add':
129 1
                    log.info('A consistency problem was detected in '
130
                             f'switch {dpid}.')
131 1
                    self._install_flows(command, flow, [switch])
132 1
                    log.info(f'Flow forwarded to switch {dpid} to be '
133
                             'installed.')
134 1
            elif command == 'delete':
135 1
                log.info('A consistency problem was detected in '
136
                         f'switch {dpid}.')
137 1
                self._install_flows(command, flow, [switch])
138 1
                log.info(f'Flow forwarded to switch {dpid} to be deleted.')
139
140 1
    def check_storehouse_consistency(self, switch):
141
        """Check consistency of installed flows for a specific switch."""
142 1
        dpid = switch.dpid
143
144 1
        for installed_flow in switch.flows:
145
146
            # Check if the flow are in the excluded flow list
147 1
            if self.is_ignored(installed_flow):
148 1
                continue
149
150 1
            if dpid not in self.stored_flows:
151
                log.info('A consistency problem was detected in '
152
                         f'switch {dpid}.')
153
                flow = {'flows': [installed_flow.as_dict()]}
154
                command = 'delete'
155
                self._install_flows(command, flow, [switch])
156
                log.info(f'Flow forwarded to switch {dpid} to be deleted.')
157
            else:
158 1
                serializer = FlowFactory.get_class(switch)
159 1
                stored_flows = self.stored_flows[dpid]['flow_list']
160 1
                stored_flows_list = [serializer.from_dict(stored_flow['flow'],
161
                                                          switch)
162
                                     for stored_flow in stored_flows]
163
164 1
                if installed_flow not in stored_flows_list:
165 1
                    log.info('A consistency problem was detected in '
166
                             f'switch {dpid}.')
167 1
                    flow = {'flows': [installed_flow.as_dict()]}
168 1
                    command = 'delete'
169 1
                    self._install_flows(command, flow, [switch])
170 1
                    log.info(f'Flow forwarded to switch {dpid} to be deleted.')
171
172
    # pylint: disable=attribute-defined-outside-init
173 1
    def _load_flows(self):
174
        """Load stored flows."""
175 1
        try:
176 1
            data = self.storehouse.get_data()['flow_persistence']
177 1
            if 'id' in data:
178
                del data['id']
179 1
            self.stored_flows = data
180
        except (KeyError, FileNotFoundError) as error:
181
            log.debug(f'There are no flows to load: {error}')
182
        else:
183 1
            log.info('Flows loaded.')
184
185 1
    def _store_changed_flows(self, command, flow, switch):
186
        """Store changed flows.
187
188
        Args:
189
            command: Flow command to be installed
190
            flow: Flows to be stored
191
            switch: Switch target
192
        """
193 1
        stored_flows_box = self.stored_flows.copy()
194
        # if the flow has a destination dpid it can be stored.
195 1
        if not switch:
196
            log.info('The Flow cannot be stored, the destination switch '
197
                     f'have not been specified: {switch}')
198
            return
199 1
        installed_flow = {}
200 1
        flow_list = []
201 1
        installed_flow['command'] = command
202 1
        installed_flow['flow'] = flow
203
204 1
        serializer = FlowFactory.get_class(switch)
205 1
        installed_flow_obj = serializer.from_dict(flow, switch)
206
207 1
        if switch.id not in stored_flows_box:
208
            # Switch not stored, add to box.
209 1
            flow_list.append(installed_flow)
210 1
            stored_flows_box[switch.id] = {'flow_list': flow_list}
211
        else:
212 1
            stored_flows = stored_flows_box[switch.id].get('flow_list', [])
213
            # Check if flow already stored
214 1
            for stored_flow in stored_flows:
215 1
                stored_flow_obj = serializer.from_dict(stored_flow['flow'],
216
                                                       switch)
217 1
                if installed_flow_obj == stored_flow_obj:
218 1
                    if stored_flow['command'] == installed_flow['command']:
219
                        log.debug('Data already stored.')
220
                        return
221
                    # Flow with inconsistency in "command" fields : Remove the
222
                    # old instruction. This happens when there is a stored
223
                    # instruction to install the flow, but the new instruction
224
                    # is to remove it. In this case, the old instruction is
225
                    # removed and the new one is stored.
226 1
                    stored_flow['command'] = installed_flow.get('command')
227 1
                    stored_flows.remove(stored_flow)
228 1
                    break
229
230 1
            stored_flows.append(installed_flow)
231 1
            stored_flows_box[switch.id]['flow_list'] = stored_flows
232
233 1
        stored_flows_box['id'] = 'flow_persistence'
234 1
        self.storehouse.save_flow(stored_flows_box)
235 1
        del stored_flows_box['id']
236 1
        self.stored_flows = stored_flows_box.copy()
237
238 1
    @rest('v2/flows')
239 1
    @rest('v2/flows/<dpid>')
240 1
    def list(self, dpid=None):
241
        """Retrieve all flows from a switch identified by dpid.
242
243
        If no dpid is specified, return all flows from all switches.
244
        """
245 1
        if dpid is None:
246 1
            switches = self.controller.switches.values()
247
        else:
248 1
            switches = [self.controller.get_switch_by_dpid(dpid)]
249
250 1
        switch_flows = {}
251
252 1
        for switch in switches:
253 1
            flows_dict = [flow.as_dict() for flow in switch.flows]
254 1
            switch_flows[switch.dpid] = {'flows': flows_dict}
255
256 1
        return jsonify(switch_flows)
257
258 1
    @rest('v2/flows', methods=['POST'])
259 1
    @rest('v2/flows/<dpid>', methods=['POST'])
260 1
    def add(self, dpid=None):
261
        """Install new flows in the switch identified by dpid.
262
263
        If no dpid is specified, install flows in all switches.
264
        """
265 1
        return self._send_flow_mods_from_request(dpid, "add")
266
267 1
    @rest('v2/delete', methods=['POST'])
268 1
    @rest('v2/delete/<dpid>', methods=['POST'])
269 1
    @rest('v2/flows', methods=['DELETE'])
270 1
    @rest('v2/flows/<dpid>', methods=['DELETE'])
271 1
    def delete(self, dpid=None):
272
        """Delete existing flows in the switch identified by dpid.
273
274
        If no dpid is specified, delete flows from all switches.
275
        """
276 1
        return self._send_flow_mods_from_request(dpid, "delete")
277
278 1
    def _get_all_switches_enabled(self):
279
        """Get a list of all switches enabled."""
280 1
        switches = self.controller.switches.values()
281 1
        return [switch for switch in switches if switch.is_enabled()]
282
283 1
    def _send_flow_mods_from_request(self, dpid, command, flows_dict=None):
284
        """Install FlowsMods from request."""
285 1
        if flows_dict is None:
286 1
            flows_dict = request.get_json()
287 1
            if flows_dict is None:
288 1
                return jsonify({"response": 'flows dict is none.'}), 404
289
290 1
        if dpid:
291 1
            switch = self.controller.get_switch_by_dpid(dpid)
292 1
            if not switch:
293 1
                return jsonify({"response": 'dpid not found.'}), 404
294 1
            elif switch.is_enabled() is False:
295 1
                if command == "delete":
296 1
                    self._install_flows(command, flows_dict, [switch])
297
                else:
298 1
                    return jsonify({"response": 'switch is disabled.'}), 404
299
            else:
300 1
                self._install_flows(command, flows_dict, [switch])
301
        else:
302 1
            self._install_flows(command, flows_dict,
303
                                self._get_all_switches_enabled())
304
305 1
        return jsonify({"response": "FlowMod Messages Sent"})
306
307 1
    def _install_flows(self, command, flows_dict, switches=[]):
308
        """Execute all procedures to install flows in the switches.
309
310
        Args:
311
            command: Flow command to be installed
312
            flows_dict: Dictionary with flows to be installed in the switches.
313
            switches: A list of switches
314
        """
315 1
        for switch in switches:
316 1
            serializer = FlowFactory.get_class(switch)
317 1
            flows = flows_dict.get('flows', [])
318 1
            for flow_dict in flows:
319 1
                flow = serializer.from_dict(flow_dict, switch)
320 1
                if command == "delete":
321
                    flow_mod = flow.as_of_delete_flow_mod()
322 1
                elif command == "add":
323 1
                    flow_mod = flow.as_of_add_flow_mod()
324
                else:
325
                    raise InvalidCommandError
326 1
                self._send_flow_mod(flow.switch, flow_mod)
327 1
                self._add_flow_mod_sent(flow_mod.header.xid, flow, command)
328
329 1
                self._send_napp_event(switch, flow, command)
330 1
                self._store_changed_flows(command, flow_dict, switch)
331
332 1
    def _add_flow_mod_sent(self, xid, flow, command):
333
        """Add the flow mod to the list of flow mods sent."""
334 1
        if len(self._flow_mods_sent) >= self._flow_mods_sent_max_size:
335
            self._flow_mods_sent.popitem(last=False)
336 1
        self._flow_mods_sent[xid] = (flow, command)
337
338 1
    def _send_flow_mod(self, switch, flow_mod):
339 1
        event_name = 'kytos/flow_manager.messages.out.ofpt_flow_mod'
340
341 1
        content = {'destination': switch.connection,
342
                   'message': flow_mod}
343
344 1
        event = KytosEvent(name=event_name, content=content)
345 1
        self.controller.buffers.msg_out.put(event)
346
347 1
    def _send_napp_event(self, switch, flow, command, **kwargs):
348
        """Send an Event to other apps informing about a FlowMod."""
349 1
        if command == 'add':
350 1
            name = 'kytos/flow_manager.flow.added'
351 1
        elif command == 'delete':
352 1
            name = 'kytos/flow_manager.flow.removed'
353 1
        elif command == 'error':
354 1
            name = 'kytos/flow_manager.flow.error'
355
        else:
356
            raise InvalidCommandError
357 1
        content = {'datapath': switch,
358
                   'flow': flow}
359 1
        content.update(kwargs)
360 1
        event_app = KytosEvent(name, content)
361 1
        self.controller.buffers.app.put(event_app)
362
363 1
    @listen_to('.*.of_core.*.ofpt_error')
364
    def handle_errors(self, event):
365
        """Receive OpenFlow error and send a event.
366
367
        The event is sent only if the error is related to a request made
368
        by flow_manager.
369
        """
370 1
        message = event.content["message"]
371
372 1
        connection = event.source
373 1
        switch = connection.switch
374
375 1
        xid = message.header.xid.value
376 1
        error_type = message.error_type
377 1
        error_code = message.code
378 1
        error_data = message.data.pack()
379
380
        # Get the packet responsible for the error
381 1
        error_packet = connection.protocol.unpack(error_data)
382
383 1
        if message.code == BadActionCode.OFPBAC_BAD_OUT_PORT:
384
            actions = []
385
            if hasattr(error_packet, 'actions'):
386
                # Get actions from the flow mod (OF 1.0)
387
                actions = error_packet.actions
388
            else:
389
                # Get actions from the list of flow mod instructions (OF 1.3)
390
                for instruction in error_packet.instructions:
391
                    actions.extend(instruction.actions)
392
393
            for action in actions:
394
                iface = switch.get_interface_by_port_no(action.port)
395
396
                # Set interface to drop packets forwarded to it
397
                if iface:
398
                    iface.config = PortConfig.OFPPC_NO_FWD
399
400 1
        try:
401 1
            flow, error_command = self._flow_mods_sent[xid]
402
        except KeyError:
403
            pass
404
        else:
405 1
            self._send_napp_event(flow.switch, flow, 'error',
406
                                  error_command=error_command,
407
                                  error_type=error_type, error_code=error_code)
408