3 from threading import Thread
4 from functools import wraps
5 #from multiprocessing import Process
7 __all__ = ['configure_flows', 'wait_until', 'deconfigure_flows']
9 #class KeyWord(Process):
10 class KeyWord(Thread):
11 def __init__(self, *args, **kwargs):
12 super(KeyWord, self).__init__(*args, **kwargs)
14 self._kw_result = None
20 return self._kw_result
23 """Taken from http://code.activestate.com/recipes/576684-simple-threading-decorator/
27 def async_func(*args, **kwargs):
28 func_hl = KeyWord(target = func, args = args, kwargs = kwargs)
29 func_hl._Thread__args = (func_hl,) + func_hl._Thread__args
30 #func_hl._args = (func_hl,) + func_hl._args
37 def wait_until(*tasks, **kwargs):
41 if 'timeout' in kwargs:
42 timeout = int(kwargs['timeout'])
45 while time.time() < (timeout+tstart):
48 if t.is_alive() == False:
53 return (time.time()-tstart)
56 if t.is_alive() == True:
61 return (time.time()-tstart)
64 def Example_of_robot_keyword(self, a, b, c):
65 """be carefull, when calling this kw from robot,
66 do not count on self, it is a thread object itself
67 injected by decorator. The purpose is to make
68 possibility to exit from thread on demand by
69 wait until keywork which makes thread.stop()
70 if needed. In your fw you should use self._stop
75 ${thread}= Example Of Robot Keyword a b c
78 if self._stop == True:
84 def configure_flows(self, host, port, switchid, tableid, minid, maxid):
85 flow_template = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
86 <flow xmlns="urn:opendaylight:flow:inventory">
87 <strict>false</strict>
99 <table_id>{}</table_id>
101 <cookie_mask>255</cookie_mask>
102 <installHw>false</installHw>
109 <ipv4-destination>10.0.1.0/24</ipv4-destination>
112 <flow-name>FooXf{}</flow-name>
113 <priority>{}</priority>
114 <barrier>false</barrier>
119 ses = requests.Session()
121 for i in range(int(minid),int(maxid)+1):
122 if self._stop == True:
125 flow = flow_template.format(tableid,fid,fid,fid)
126 url = 'http://{}:{}/restconf/config/opendaylight-inventory:nodes/node/openflow:{}/table/{}/flow/{}'.format(host,
127 port, switchid, tableid, fid)
130 rsp = ses.put(url, headers={'Content-Type':'application/xml'}, data=flow, timeout=3)
131 if rsp.status_code == 200:
134 except Exception as e:
139 def deconfigure_flows(self, host, port, switchid, tableid, minid, maxid):
140 """Result will be the number of status code 200 returned"""
142 ses = requests.Session()
144 for fid in range(int(minid),int(maxid)):
145 if self._stop == True:
147 url = 'http://{}:{}/restconf/config/opendaylight-inventory:nodes/node/openflow:{}/table/{}/flow/{}'.format(host,
148 port, switchid, tableid, fid)
151 rsp = ses.delete(url, headers={'Content-Type':'application/xml'}, timeout=3)
152 if rsp.status_code == 200:
154 except Exception as e: