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