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