Test Failed
Pull Request — master (#90)
by Carlos
02:25
created

build.main.Main._store_changed_flows()   B

Complexity

Conditions 6

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.064

Importance

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