Test Failed
Pull Request — master (#117)
by Carlos
63:01 queued 14:44
created

build.main   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 392
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
eloc 242
dl 0
loc 392
ccs 182
cts 221
cp 0.8235
rs 2.96
c 0
b 0
f 0
wmc 68

19 Methods

Rating   Name   Duplication   Size   Complexity  
A Main._load_flows() 0 11 4
A Main.resend_stored_flows() 0 17 4
A Main.shutdown() 0 3 1
A Main.setup() 0 21 2
A Main.check_switch_consistency() 0 27 5
A Main.execute() 0 10 2
A Main.consistency_check() 0 11 4
A Main.check_storehouse_consistency() 0 26 4
A Main.list() 0 19 3
B Main._send_flow_mods_from_request() 0 23 7
A Main._send_flow_mod() 0 8 1
A Main.delete() 0 10 1
A Main._get_all_switches_enabled() 0 4 1
A Main._send_napp_event() 0 15 4
C Main._store_changed_flows() 0 64 9
A Main._install_flows() 0 24 5
B Main.handle_errors() 0 45 8
A Main.add() 0 8 1
A Main._add_flow_mod_sent() 0 5 2

How to fix   Complexity   

Complexity

Complex classes like build.main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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