Passed
Push — master ( 07a00f...bd5303 )
by Humberto
02:55 queued 31s
created

build.main.Main.check_storehouse_consistency()   A

Complexity

Conditions 4

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.343

Importance

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