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.of_core.flow import FlowFactory |
9
|
|
|
|
10
|
1 |
|
from .exceptions import InvalidCommandError |
11
|
1 |
|
from .settings import FLOWS_DICT_MAX_SIZE |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class Main(KytosNApp): |
15
|
|
|
"""Main class to be used by Kytos controller.""" |
16
|
|
|
|
17
|
1 |
|
def setup(self): |
18
|
|
|
"""Replace the 'init' method for the KytosApp subclass. |
19
|
|
|
|
20
|
|
|
The setup method is automatically called by the run method. |
21
|
|
|
Users shouldn't call this method directly. |
22
|
|
|
""" |
23
|
1 |
|
log.debug("flow-manager starting") |
24
|
1 |
|
self._flow_mods_sent = OrderedDict() |
25
|
1 |
|
self._flow_mods_sent_max_size = FLOWS_DICT_MAX_SIZE |
26
|
|
|
|
27
|
1 |
|
def execute(self): |
28
|
|
|
"""Run once on NApp 'start' or in a loop. |
29
|
|
|
|
30
|
|
|
The execute method is called by the run method of KytosNApp class. |
31
|
|
|
Users shouldn't call this method directly. |
32
|
|
|
""" |
33
|
|
|
|
34
|
1 |
|
def shutdown(self): |
35
|
|
|
"""Shutdown routine of the NApp.""" |
36
|
|
|
log.debug("flow-manager stopping") |
37
|
|
|
|
38
|
1 |
|
@rest('v2/flows') |
39
|
1 |
|
@rest('v2/flows/<dpid>') |
40
|
1 |
|
def list(self, dpid=None): |
41
|
|
|
"""Retrieve all flows from a switch identified by dpid. |
42
|
|
|
|
43
|
|
|
If no dpid is specified, return all flows from all switches. |
44
|
|
|
""" |
45
|
1 |
|
if dpid is None: |
46
|
1 |
|
switches = self.controller.switches.values() |
47
|
|
|
else: |
48
|
1 |
|
switches = [self.controller.get_switch_by_dpid(dpid)] |
49
|
|
|
|
50
|
1 |
|
switch_flows = {} |
51
|
|
|
|
52
|
1 |
|
for switch in switches: |
53
|
1 |
|
flows_dict = [flow.as_dict() for flow in switch.flows] |
54
|
1 |
|
switch_flows[switch.dpid] = {'flows': flows_dict} |
55
|
|
|
|
56
|
1 |
|
return jsonify(switch_flows) |
57
|
|
|
|
58
|
1 |
|
@rest('v2/flows', methods=['POST']) |
59
|
1 |
|
@rest('v2/flows/<dpid>', methods=['POST']) |
60
|
1 |
|
def add(self, dpid=None): |
61
|
|
|
"""Install new flows in the switch identified by dpid. |
62
|
|
|
|
63
|
|
|
If no dpid is specified, install flows in all switches. |
64
|
|
|
""" |
65
|
1 |
|
return self._send_flow_mods_from_request(dpid, "add") |
66
|
|
|
|
67
|
1 |
|
@rest('v2/delete', methods=['POST']) |
68
|
1 |
|
@rest('v2/delete/<dpid>', methods=['POST']) |
69
|
1 |
|
@rest('v2/flows', methods=['DELETE']) |
70
|
1 |
|
@rest('v2/flows/<dpid>', methods=['DELETE']) |
71
|
1 |
|
def delete(self, dpid=None): |
72
|
|
|
"""Delete existing flows in the switch identified by dpid. |
73
|
|
|
|
74
|
|
|
If no dpid is specified, delete flows from all switches. |
75
|
|
|
""" |
76
|
1 |
|
return self._send_flow_mods_from_request(dpid, "delete") |
77
|
|
|
|
78
|
1 |
|
def _get_all_switches_enabled(self): |
79
|
|
|
"""Get a list of all switches enabled.""" |
80
|
1 |
|
switches = self.controller.switches.values() |
81
|
1 |
|
return [switch for switch in switches if switch.is_enabled()] |
82
|
|
|
|
83
|
1 |
|
def _send_flow_mods_from_request(self, dpid, command): |
84
|
|
|
"""Install FlowsMods from request.""" |
85
|
1 |
|
flows_dict = request.get_json() |
86
|
|
|
|
87
|
1 |
|
if flows_dict is None: |
88
|
1 |
|
return jsonify({"response": 'flows dict is none.'}), 404 |
89
|
|
|
|
90
|
1 |
|
if dpid: |
91
|
1 |
|
switch = self.controller.get_switch_by_dpid(dpid) |
92
|
1 |
|
if not switch: |
93
|
1 |
|
return jsonify({"response": 'dpid not found.'}), 404 |
94
|
1 |
|
elif switch.is_enabled() is False: |
95
|
1 |
|
return jsonify({"response": 'switch is disabled.'}), 404 |
96
|
|
|
else: |
97
|
1 |
|
self._install_flows(command, flows_dict, [switch]) |
98
|
|
|
else: |
99
|
1 |
|
self._install_flows(command, flows_dict, |
100
|
|
|
self._get_all_switches_enabled()) |
101
|
|
|
|
102
|
1 |
|
return jsonify({"response": "FlowMod Messages Sent"}) |
103
|
|
|
|
104
|
1 |
|
def _install_flows(self, command, flows_dict, switches=[]): |
105
|
|
|
"""Execute all procedures to install flows in the switches. |
106
|
|
|
|
107
|
|
|
Args: |
108
|
|
|
command: Flow command to be installed |
109
|
|
|
flows_dict: Dictionary with flows to be installed in the switches. |
110
|
|
|
switches: A list of switches |
111
|
|
|
""" |
112
|
1 |
|
for switch in switches: |
113
|
1 |
|
serializer = FlowFactory.get_class(switch) |
114
|
1 |
|
flows = flows_dict.get('flows', []) |
115
|
1 |
|
for flow_dict in flows: |
116
|
1 |
|
flow = serializer.from_dict(flow_dict, switch) |
117
|
1 |
|
if command == "delete": |
118
|
|
|
flow_mod = flow.as_of_delete_flow_mod() |
119
|
1 |
|
elif command == "add": |
120
|
1 |
|
flow_mod = flow.as_of_add_flow_mod() |
121
|
|
|
else: |
122
|
|
|
raise InvalidCommandError |
123
|
1 |
|
self._send_flow_mod(flow.switch, flow_mod) |
124
|
1 |
|
self._add_flow_mod_sent(flow_mod.header.xid, flow) |
125
|
|
|
|
126
|
1 |
|
self._send_napp_event(switch, flow, command) |
127
|
|
|
|
128
|
1 |
|
def _add_flow_mod_sent(self, xid, flow): |
129
|
|
|
"""Add the flow mod to the list of flow mods sent.""" |
130
|
1 |
|
if len(self._flow_mods_sent) >= self._flow_mods_sent_max_size: |
131
|
|
|
self._flow_mods_sent.popitem(last=False) |
132
|
1 |
|
self._flow_mods_sent[xid] = flow |
133
|
|
|
|
134
|
1 |
|
def _send_flow_mod(self, switch, flow_mod): |
135
|
1 |
|
event_name = 'kytos/flow_manager.messages.out.ofpt_flow_mod' |
136
|
|
|
|
137
|
1 |
|
content = {'destination': switch.connection, |
138
|
|
|
'message': flow_mod} |
139
|
|
|
|
140
|
1 |
|
event = KytosEvent(name=event_name, content=content) |
141
|
1 |
|
self.controller.buffers.msg_out.put(event) |
142
|
|
|
|
143
|
1 |
|
def _send_napp_event(self, switch, flow, command, **kwargs): |
144
|
|
|
"""Send an Event to other apps informing about a FlowMod.""" |
145
|
1 |
|
if command == 'add': |
146
|
1 |
|
name = 'kytos/flow_manager.flow.added' |
147
|
1 |
|
elif command == 'delete': |
148
|
1 |
|
name = 'kytos/flow_manager.flow.removed' |
149
|
1 |
|
elif command == 'error': |
150
|
1 |
|
name = 'kytos/flow_manager.flow.error' |
151
|
|
|
else: |
152
|
|
|
raise InvalidCommandError |
153
|
1 |
|
content = {'datapath': switch, |
154
|
|
|
'flow': flow} |
155
|
1 |
|
content.update(kwargs) |
156
|
1 |
|
event_app = KytosEvent(name, content) |
157
|
1 |
|
self.controller.buffers.app.put(event_app) |
158
|
|
|
|
159
|
1 |
|
@listen_to('.*.of_core.*.ofpt_error') |
160
|
|
|
def handle_errors(self, event): |
161
|
|
|
"""Receive OpenFlow error and send a event. |
162
|
|
|
|
163
|
|
|
The event is sent only if the error is related to a request made |
164
|
|
|
by flow_manager. |
165
|
|
|
""" |
166
|
1 |
|
xid = event.content["message"].header.xid.value |
167
|
1 |
|
error_type = event.content["message"].error_type |
168
|
1 |
|
error_code = event.content["message"].code |
169
|
1 |
|
try: |
170
|
1 |
|
flow = self._flow_mods_sent[xid] |
171
|
|
|
except KeyError: |
172
|
|
|
pass |
173
|
|
|
else: |
174
|
1 |
|
self._send_napp_event(flow.switch, flow, 'error', |
175
|
|
|
error_type=error_type, error_code=error_code) |
176
|
|
|
|