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:
print " Exception GET at '"+URLrequest+"' " + str(e)
return -1
+
def of_install(args):
line_number=1
try:
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]
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"
#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)