Passed
Pull Request — master (#129)
by Carlos
02:23
created

build.main.cast_fields()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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