Add openflow-port-mapping CLI command
[osm/openvim.git] / onos.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 ##
5 # Copyright 2016, I2T Research Group (UPV/EHU)
6 # This file is part of openvim
7 # All Rights Reserved.
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 #
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: alaitz.mendiola@ehu.eus or alaitz.mendiola@gmail.com
23 ##
24
25 '''
26 ImplementS the pluging for the Open Network Operating System (ONOS) openflow
27 controller. It creates the class OF_conn to create dataplane connections
28 with static rules based on packet destination MAC address
29 '''
30
31 __author__="Alaitz Mendiola"
32 __date__ ="$22-nov-2016$"
33
34
35 import json
36 import requests
37 import base64
38 import logging
39 import openflow_conn
40
41
42 class OF_conn(openflow_conn.OpenflowConn):
43 """
44 ONOS connector. No MAC learning is used
45 """
46 def __init__(self, params):
47 """ Constructor.
48 Params: dictionary with the following keys:
49 of_dpid: DPID to use for this controller ?? Does a controller have a dpid?
50 of_ip: controller IP address
51 of_port: controller TCP port
52 of_user: user credentials, can be missing or None
53 of_password: password credentials
54 of_debug: debug level for logging. Default to ERROR
55 other keys are ignored
56 Raise an exception if same parameter is missing or wrong
57 """
58
59 openflow_conn.OpenflowConn.__init__(self, params)
60
61 # check params
62 if "of_ip" not in params or params["of_ip"]==None or "of_port" not in params or params["of_port"]==None:
63 raise ValueError("IP address and port must be provided")
64 #internal variables
65 self.name = "onos"
66 self.headers = {'content-type':'application/json','accept':'application/json',}
67
68 self.auth="None"
69 self.pp2ofi={} # From Physical Port to OpenFlow Index
70 self.ofi2pp={} # From OpenFlow Index to Physical Port
71
72 self.dpid = str(params["of_dpid"])
73 self.id = 'of:'+str(self.dpid.replace(':', ''))
74 self.url = "http://%s:%s/onos/v1/" %( str(params["of_ip"]), str(params["of_port"] ) )
75
76 # TODO This may not be straightforward
77 if "of_user" in params and params["of_user"]!=None:
78 if not params.get("of_password"):
79 of_password=""
80 else:
81 of_password=str(params["of_password"])
82 self.auth = base64.b64encode(str(params["of_user"])+":"+of_password)
83 self.headers['authorization'] = 'Basic ' + self.auth
84
85 self.logger = logging.getLogger('vim.OF.onos')
86 self.logger.setLevel( getattr(logging, params.get("of_debug", "ERROR")) )
87 self.ip_address = None
88
89 def get_of_switches(self):
90 """
91 Obtain a a list of switches or DPID detected by this controller
92 :return: list where each element a tuple pair (DPID, IP address)
93 Raise a openflowconnUnexpectedResponse expection in case of failure
94 """
95 try:
96 self.headers['content-type'] = 'text/plain'
97 of_response = requests.get(self.url + "devices", headers=self.headers)
98 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
99 if of_response.status_code != 200:
100 self.logger.warning("get_of_switches " + error_text)
101 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
102
103 self.logger.debug("get_of_switches " + error_text)
104 info = of_response.json()
105
106 if type(info) != dict:
107 self.logger.error("get_of_switches. Unexpected response, not a dict: %s", str(info))
108 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, not a dict. Wrong version?")
109
110 node_list = info.get('devices')
111
112 if type(node_list) is not list:
113 self.logger.error(
114 "get_of_switches. Unexpected response, at 'devices', not found or not a list: %s",
115 str(type(node_list)))
116 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, at 'devices', not found "
117 "or not a list. Wrong version?")
118
119 switch_list = []
120 for node in node_list:
121 node_id = node.get('id')
122 if node_id is None:
123 self.logger.error("get_of_switches. Unexpected response at 'device':'id', not found: %s",
124 str(node))
125 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'device':'id', "
126 "not found . Wrong version?")
127
128 node_ip_address = node.get('annotations').get('managementAddress')
129 if node_ip_address is None:
130 self.logger.error(
131 "get_of_switches. Unexpected response at 'device':'managementAddress', not found: %s",
132 str(node))
133 raise openflow_conn.OpenflowconnUnexpectedResponse(
134 "Unexpected response at 'device':'managementAddress', not found. Wrong version?")
135
136 node_id_hex = hex(int(node_id.split(':')[1])).split('x')[1].zfill(16)
137
138 switch_list.append(
139 (':'.join(a + b for a, b in zip(node_id_hex[::2], node_id_hex[1::2])), node_ip_address))
140 raise switch_list
141
142 except requests.exceptions.RequestException as e:
143 error_text = type(e).__name__ + ": " + str(e)
144 self.logger.error("get_of_switches " + error_text)
145 raise openflow_conn.OpenflowconnConnectionException(error_text)
146 except ValueError as e:
147 # ValueError in the case that JSON can not be decoded
148 error_text = type(e).__name__ + ": " + str(e)
149 self.logger.error("get_of_switches " + error_text)
150 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
151
152 def obtain_port_correspondence(self):
153 """
154 Obtain the correspondence between physical and openflow port names
155 :return: dictionary with physical name as key, openflow name as value
156 Raise a openflowconnUnexpectedResponse expection in case of failure
157 """
158 try:
159 self.headers['content-type'] = 'text/plain'
160 of_response = requests.get(self.url + "devices/" + self.id + "/ports", headers=self.headers)
161 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
162 if of_response.status_code != 200:
163 self.logger.warning("obtain_port_correspondence " + error_text)
164 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
165
166 self.logger.debug("obtain_port_correspondence " + error_text)
167 info = of_response.json()
168
169 node_connector_list = info.get('ports')
170 if type(node_connector_list) is not list:
171 self.logger.error(
172 "obtain_port_correspondence. Unexpected response at 'ports', not found or not a list: %s",
173 str(node_connector_list))
174 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'ports', not found or not "
175 "a list. Wrong version?")
176
177 for node_connector in node_connector_list:
178 if node_connector['port'] != "local":
179 self.pp2ofi[str(node_connector['annotations']['portName'])] = str(node_connector['port'])
180 self.ofi2pp[str(node_connector['port'])] = str(node_connector['annotations']['portName'])
181
182 node_ip_address = info['annotations']['managementAddress']
183 if node_ip_address is None:
184 self.logger.error(
185 "obtain_port_correspondence. Unexpected response at 'managementAddress', not found: %s",
186 str(self.id))
187 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'managementAddress', "
188 "not found. Wrong version?")
189 self.ip_address = node_ip_address
190
191 # print self.name, ": obtain_port_correspondence ports:", self.pp2ofi
192 return self.pp2ofi
193 except requests.exceptions.RequestException as e:
194 error_text = type(e).__name__ + ": " + str(e)
195 self.logger.error("obtain_port_correspondence " + error_text)
196 raise openflow_conn.OpenflowconnConnectionException(error_text)
197 except ValueError as e:
198 # ValueError in the case that JSON can not be decoded
199 error_text = type(e).__name__ + ": " + str(e)
200 self.logger.error("obtain_port_correspondence " + error_text)
201 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
202
203 def get_of_rules(self, translate_of_ports=True):
204 """
205 Obtain the rules inserted at openflow controller
206 :param translate_of_ports: if True it translates ports from openflow index to physical switch name
207 :return: dict if ok: with the rule name as key and value is another dictionary with the following content:
208 priority: rule priority
209 name: rule name (present also as the master dict key)
210 ingress_port: match input port of the rule
211 dst_mac: match destination mac address of the rule, can be missing or None if not apply
212 vlan_id: match vlan tag of the rule, can be missing or None if not apply
213 actions: list of actions, composed by a pair tuples:
214 (vlan, None/int): for stripping/setting a vlan tag
215 (out, port): send to this port
216 switch: DPID, all
217 Raise a openflowconnUnexpectedResponse expection in case of failure
218 """
219
220 try:
221
222 if len(self.ofi2pp) == 0:
223 self.obtain_port_correspondence()
224
225 # get rules
226 self.headers['content-type'] = 'text/plain'
227 of_response = requests.get(self.url + "flows/" + self.id, headers=self.headers)
228 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
229
230 # The configured page does not exist if there are no rules installed. In that case we return an empty dict
231 if of_response.status_code == 404:
232 return {}
233
234 elif of_response.status_code != 200:
235 self.logger.warning("get_of_rules " + error_text)
236 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
237 self.logger.debug("get_of_rules " + error_text)
238
239 info = of_response.json()
240
241 if type(info) != dict:
242 self.logger.error("get_of_rules. Unexpected response, not a dict: %s", str(info))
243 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected openflow response, not a dict. "
244 "Wrong version?")
245
246 flow_list = info.get('flows')
247
248 if flow_list is None:
249 return {}
250
251 if type(flow_list) is not list:
252 self.logger.error(
253 "get_of_rules. Unexpected response at 'flows', not a list: %s",
254 str(type(flow_list)))
255 raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'flows', not a list. "
256 "Wrong version?")
257
258 rules = dict() # Response dictionary
259
260 for flow in flow_list:
261 if not ('id' in flow and 'selector' in flow and 'treatment' in flow and \
262 'instructions' in flow['treatment'] and 'criteria' in \
263 flow['selector']):
264 raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow response, one or more "
265 "elements are missing. Wrong version?")
266
267 rule = dict()
268 rule['switch'] = self.dpid
269 rule['priority'] = flow.get('priority')
270 rule['name'] = flow['id']
271
272 for criteria in flow['selector']['criteria']:
273 if criteria['type'] == 'IN_PORT':
274 in_port = str(criteria['port'])
275 if in_port != "CONTROLLER":
276 if not in_port in self.ofi2pp:
277 raise openflow_conn.OpenflowconnUnexpectedResponse("Error: Ingress port {} is not "
278 "in switch port list".format(in_port))
279 if translate_of_ports:
280 in_port = self.ofi2pp[in_port]
281 rule['ingress_port'] = in_port
282
283 elif criteria['type'] == 'VLAN_VID':
284 rule['vlan_id'] = criteria['vlanId']
285
286 elif criteria['type'] == 'ETH_DST':
287 rule['dst_mac'] = str(criteria['mac']).lower()
288
289 actions = []
290 for instruction in flow['treatment']['instructions']:
291 if instruction['type'] == "OUTPUT":
292 out_port = str(instruction['port'])
293 if out_port != "CONTROLLER":
294 if not out_port in self.ofi2pp:
295 raise openflow_conn.OpenflowconnUnexpectedResponse("Error: Output port {} is not in "
296 "switch port list".format(out_port))
297
298 if translate_of_ports:
299 out_port = self.ofi2pp[out_port]
300
301 actions.append( ('out', out_port) )
302
303 if instruction['type'] == "L2MODIFICATION" and instruction['subtype'] == "VLAN_POP":
304 actions.append( ('vlan', 'None') )
305 if instruction['type'] == "L2MODIFICATION" and instruction['subtype'] == "VLAN_ID":
306 actions.append( ('vlan', instruction['vlanId']) )
307
308 rule['actions'] = actions
309 rules[flow['id']] = dict(rule)
310 return rules
311
312 except requests.exceptions.RequestException as e:
313 # ValueError in the case that JSON can not be decoded
314 error_text = type(e).__name__ + ": " + str(e)
315 self.logger.error("get_of_rules " + error_text)
316 raise openflow_conn.OpenflowconnConnectionException(error_text)
317 except ValueError as e:
318 # ValueError in the case that JSON can not be decoded
319 error_text = type(e).__name__ + ": " + str(e)
320 self.logger.error("get_of_rules " + error_text)
321 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
322
323 def del_flow(self, flow_name):
324 """
325 Delete an existing rule
326 :param flow_name:
327 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
328 """
329
330 try:
331 self.headers['content-type'] = None
332 of_response = requests.delete(self.url + "flows/" + self.id + "/" + flow_name, headers=self.headers)
333 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
334
335 if of_response.status_code != 204:
336 self.logger.warning("del_flow " + error_text)
337 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
338
339 self.logger.debug("del_flow OK " + error_text)
340 return None
341
342 except requests.exceptions.RequestException as e:
343 error_text = type(e).__name__ + ": " + str(e)
344 self.logger.error("del_flow " + error_text)
345 raise openflow_conn.OpenflowconnConnectionException(error_text)
346
347 def new_flow(self, data):
348 """
349 Insert a new static rule
350 :param data: dictionary with the following content:
351 priority: rule priority
352 name: rule name
353 ingress_port: match input port of the rule
354 dst_mac: match destination mac address of the rule, missing or None if not apply
355 vlan_id: match vlan tag of the rule, missing or None if not apply
356 actions: list of actions, composed by a pair tuples with these posibilities:
357 ('vlan', None/int): for stripping/setting a vlan tag
358 ('out', port): send to this port
359 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
360 """
361 try:
362
363 if len(self.pp2ofi) == 0:
364 self.obtain_port_correspondence()
365
366 # Build the dictionary with the flow rule information for ONOS
367 flow = dict()
368 #flow['id'] = data['name']
369 flow['tableId'] = 0
370 flow['priority'] = data.get('priority')
371 flow['timeout'] = 0
372 flow['isPermanent'] = "true"
373 flow['appId'] = 10 # FIXME We should create an appId for OSM
374 flow['selector'] = dict()
375 flow['selector']['criteria'] = list()
376
377 # Flow rule matching criteria
378 if not data['ingress_port'] in self.pp2ofi:
379 error_text = 'Error. Port ' + data['ingress_port'] + ' is not present in the switch'
380 self.logger.warning("new_flow " + error_text)
381 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
382
383 ingress_port_criteria = dict()
384 ingress_port_criteria['type'] = "IN_PORT"
385 ingress_port_criteria['port'] = self.pp2ofi[data['ingress_port']]
386 flow['selector']['criteria'].append(ingress_port_criteria)
387
388 if 'dst_mac' in data:
389 dst_mac_criteria = dict()
390 dst_mac_criteria["type"] = "ETH_DST"
391 dst_mac_criteria["mac"] = data['dst_mac']
392 flow['selector']['criteria'].append(dst_mac_criteria)
393
394 if data.get('vlan_id'):
395 vlan_criteria = dict()
396 vlan_criteria["type"] = "VLAN_VID"
397 vlan_criteria["vlanId"] = int(data['vlan_id'])
398 flow['selector']['criteria'].append(vlan_criteria)
399
400 # Flow rule treatment
401 flow['treatment'] = dict()
402 flow['treatment']['instructions'] = list()
403 flow['treatment']['deferred'] = list()
404
405 for action in data['actions']:
406 new_action = dict()
407 if action[0] == "vlan":
408 new_action['type'] = "L2MODIFICATION"
409 if action[1] == None:
410 new_action['subtype'] = "VLAN_POP"
411 else:
412 new_action['subtype'] = "VLAN_ID"
413 new_action['vlanId'] = int(action[1])
414 elif action[0] == 'out':
415 new_action['type'] = "OUTPUT"
416 if not action[1] in self.pp2ofi:
417 error_msj = 'Port '+ action[1] + ' is not present in the switch'
418 raise openflow_conn.OpenflowconnUnexpectedResponse(error_msj)
419 new_action['port'] = self.pp2ofi[action[1]]
420 else:
421 error_msj = "Unknown item '%s' in action list" % action[0]
422 self.logger.error("new_flow " + error_msj)
423 raise openflow_conn.OpenflowconnUnexpectedResponse(error_msj)
424
425 flow['treatment']['instructions'].append(new_action)
426
427 self.headers['content-type'] = 'application/json'
428 path = self.url + "flows/" + self.id
429 of_response = requests.post(path, headers=self.headers, data=json.dumps(flow) )
430
431 error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
432 if of_response.status_code != 201:
433 self.logger.warning("new_flow " + error_text)
434 raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
435
436 flowId = of_response.headers['location'][path.__len__() + 1:]
437
438 data['name'] = flowId
439
440 self.logger.debug("new_flow OK " + error_text)
441 return None
442
443 except requests.exceptions.RequestException as e:
444 error_text = type(e).__name__ + ": " + str(e)
445 self.logger.error("new_flow " + error_text)
446 raise openflow_conn.OpenflowconnConnectionException(error_text)
447
448 def clear_all_flows(self):
449 """
450 Delete all existing rules
451 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
452 """
453 try:
454 rules = self.get_of_rules(True)
455
456 for rule in rules:
457 self.del_flow(rule)
458
459 self.logger.debug("clear_all_flows OK ")
460 return None
461
462 except requests.exceptions.RequestException as e:
463 error_text = type(e).__name__ + ": " + str(e)
464 self.logger.error("clear_all_flows " + error_text)
465 raise openflow_conn.OpenflowconnConnectionException(error_text)
466
467
468
469
470