Test Failed
Pull Request — master (#96)
by Jose
02:07
created

build.main.Main._add_flow_mod_sent()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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