Test Failed
Pull Request — master (#127)
by Carlos
03:17
created

build.main.cast_fields()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

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