X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=openflow;h=cac14de6cd53f3fc973e60807fc21d697828eee2;hb=9cead2a0262deabadc900d580ef312fbc468efc0;hp=80cf624a826d4350b0d118231846bb6e19fe6cb1;hpb=c1d1d47cee5bd382fd9e2ca4d829aef0f545a0d1;p=osm%2Fopenvim.git diff --git a/openflow b/openflow index 80cf624..cac14de 100755 --- a/openflow +++ b/openflow @@ -3,7 +3,7 @@ # PYTHON_ARGCOMPLETE_OK ## -# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U. # This file is part of openmano # All Rights Reserved. # @@ -40,64 +40,76 @@ import imp import yaml import requests import logging -from openflow_thread import change_db2of, FlowBadFormat +import osm_openvim.openflow_conn as openflow_conn +from osm_openvim.openflow_thread import change_db2of, FlowBadFormat + def of_switches(args): - r,c = ofconnector.get_of_switches() - if r<0: - print c - return r - else: + try: + c = ofconnector.get_of_switches() + for s in c: print " %s %s" % (s[0], s[1]) - return 0 + return 0 + except openflow_conn.OpenflowconnException as e: + print ("OF get switch error {}".format(str(e))) + return -1 + def of_list(args): - r,c = ofconnector.get_of_rules(not args.no_translate) - if r<0: - print c - return r - if args.verbose > 0: - print yaml.safe_dump(c, indent=4, default_flow_style=False) + try: + c = ofconnector.get_of_rules(not args.no_translate) + + if args.verbose > 0: + print yaml.safe_dump(c, indent=4, default_flow_style=False) + return 0 + + print " switch priority name ingress_port " \ + "dst_mac vlan_id actions" + for name, rule in c.iteritems(): + action_list = [] + for action in rule["actions"]: + action_list.append(action[0] + "=" + str(action[1])) + if "vlan_id" in rule: + vlan = str(rule["vlan_id"]) + else: + vlan = "any" + print "%s %s %s %s %s %s %s" % \ + (rule["switch"], str(rule["priority"]).ljust(6), name.ljust(40), rule["ingress_port"].ljust(8), + rule.get("dst_mac", "any").ljust(18), vlan.ljust(4), ",".join(action_list)) return 0 - print " switch priority name ingress_port dst_mac vlan_id actions" - for name,rule in c.iteritems(): - action_list=[] - for action in rule["actions"]: - action_list.append(action[0]+"="+str(action[1])) - if "vlan_id" in rule: - vlan=str(rule["vlan_id"]) - else: - vlan="any" - print "%s %s %s %s %s %s %s" % \ - (rule["switch"], str(rule["priority"]).ljust(6), name.ljust(40), rule["ingress_port"].ljust(8), \ - rule.get("dst_mac","any").ljust(18), vlan.ljust(4), ",".join(action_list) ) - return 0 + except openflow_conn.OpenflowconnException as e: + print("OF get list error {}".format(str(e))) + return -1 + def of_clear(args): - if not args.force: - r = raw_input("Clear all Openflow rules (y/N)? ") - if not (len(r)>0 and r[0].lower()=="y"): - return 0 - r,c = ofconnector.clear_all_flows() - if r<0: - print c - return r - return 0 + try: + if not args.force: + r = raw_input("Clear all Openflow rules (y/N)? ") + if not (len(r) > 0 and r[0].lower() == "y"): + return 0 + c = ofconnector.clear_all_flows() + return 0 + except openflow_conn.OpenflowconnException as e: + print ("OF error {}".format(str(e))) + return -1 + def of_port_list(args): - r,c = ofconnector.obtain_port_correspondence() - if r<0: - print c - return r - yaml.safe_dump({"ports": c}, sys.stdout, indent=2, default_flow_style=False) - -#def of_dump(args): -# args.verbose = 3 -# args.no_translate=False -# of_list(args) - return 0 + try: + c = ofconnector.obtain_port_correspondence() + yaml.safe_dump({"ports": c}, sys.stdout, indent=2, default_flow_style=False) + # def of_dump(args): + # args.verbose = 3 + # args.no_translate=False + # of_list(args) + return len(c) + except openflow_conn.OpenflowconnException as e: + print("OF error {}".format(str(e))) + return -1 + def of_reinstall(args): try: @@ -110,6 +122,7 @@ def of_reinstall(args): print " Exception GET at '"+URLrequest+"' " + str(e) return -1 + def of_install(args): line_number=1 try: @@ -138,14 +151,17 @@ def of_install(args): except FlowBadFormat as e: print "Format error at line %d: %s" % (line_number, str(e)) continue - r,c = ofconnector.new_flow(rule) - if r<0: - error="ERROR: "+c - else: - error="OK" - print "%s %s %s input=%s dst_mac=%s vlan_id=%s %s" % \ - (rule["switch"], str(rule.get("priority")).ljust(6), rule["name"].ljust(20), rule["ingress_port"].ljust(3), \ - rule.get("dst_mac","any").ljust(18), rule.get("vlan_id","any").ljust(4), error ) + try: + ofconnector.new_flow(rule) + error = "OK" + except openflow_conn.OpenflowconnException as e: + error = "ERROR: " + str(e) + print "%s %s %s input=%s dst_mac=%s vlan_id=%s %s" % (rule["switch"], + str(rule.get("priority")).ljust(6), + rule["name"].ljust(20), + rule["ingress_port"].ljust(3), + rule.get("dst_mac", "any").ljust(18), + rule.get("vlan_id", "any").ljust(4), error) return 0 except IOError as e: print " Error opening file '" + args.file + "': " + e.args[1] @@ -158,6 +174,7 @@ def of_install(args): print " Error yaml/json format error at " + error_pos return -1 + def of_add(args): if args.act==None and args.actions==None: print "openflow add: error: one of the arguments --actions or [--setvlan,--stripvlan],--out is required" @@ -201,24 +218,29 @@ def of_add(args): #print rule #return - r,c = ofconnector.new_flow(rule) - if r<0: - print c + try: + c = ofconnector.new_flow(rule) + if args.print_id: + print rule["name"] + return 0 + + except openflow_conn.OpenflowconnException as e: + print("OF error {}".format(str(e))) return -1 - if args.print_id: - print rule["name"] - return 0 + def of_delete(args): if not args.force: r = raw_input("Clear rule %s (y/N)? " %(args.name)) - if not (len(r)>0 and r[0].lower()=="y"): + if not (len(r) >0 and r[0].lower() == "y"): return 0 - r,c = ofconnector.del_flow(args.name) - if r<0: - print c + try: + ofconnector.del_flow(args.name) + return 0 + except openflow_conn.OpenflowconnException as e: + print("OF error {}".format(str(e))) return -1 - return 0 + def config(args): print "OPENVIM_HOST: %s" %(vim_host) @@ -257,7 +279,7 @@ if __name__=="__main__": of_controller_password = os.getenv('OF_CONTROLLER_PASSWORD',None) #of_controller_version = os.getenv('OF_CONTROLLER_VERSION',"0.90") of_controller_ip = os.getenv('OF_CONTROLLER_IP',"localhost") - of_controller_port = os.getenv('OF_CONTROLLER_PORT',"7070") + of_controller_port = os.getenv('OF_CONTROLLER_PORT',"8080") of_controller_dpid = os.getenv('OF_CONTROLLER_DPID','00:01:02:03:e4:05:e6:07') of_controller_module = os.getenv('OF_CONTROLLER_MODULE',None) @@ -353,10 +375,10 @@ if __name__=="__main__": module = of_controller_module else: module = of_controller_type - module_info = imp.find_module(module) - - of_conn = imp.load_module("of_conn", *module_info) + try: + pkg = __import__("osm_openvim." + module) + of_conn = getattr(pkg, module) ofconnector = of_conn.OF_conn(params) except Exception as e: print "Cannot open the Openflow controller '%s': %s" % (type(e).__name__, str(e))