3 from threading import Thread
4 from functools import wraps
5 # from multiprocessing import Process
7 __all__ = ['configure_flows', 'wait_until', 'deconfigure_flows']
10 # class KeyWord(Process):
11 class KeyWord(Thread):
12 def __init__(self, *args, **kwargs):
13 super(KeyWord, self).__init__(*args, **kwargs)
15 self._kw_result = None
21 return self._kw_result
25 """Taken from http://code.activestate.com/recipes/576684-simple-threading-decorator/
29 def async_func(*args, **kwargs):
30 func_hl = KeyWord(target=func, args=args, kwargs=kwargs)
31 func_hl._Thread__args = (func_hl,) + func_hl._Thread__args
32 # func_hl._args = (func_hl,) + func_hl._args
39 def wait_until(*tasks, **kwargs):
43 if 'timeout' in kwargs:
44 timeout = int(kwargs['timeout'])
47 while time.time() < (timeout + tstart):
50 if t.is_alive() is False:
55 return (time.time()-tstart)
58 if t.is_alive() is True:
63 return (time.time()-tstart)
67 def Example_of_robot_keyword(self, a, b, c):
68 """be carefull, when calling this kw from robot,
69 do not count on self, it is a thread object itself
70 injected by decorator. The purpose is to make
71 possibility to exit from thread on demand by
72 wait until keywork which makes thread.stop()
73 if needed. In your fw you should use self._stop
78 ${thread}= Example Of Robot Keyword a b c
81 if self._stop is True:
86 def configure_flows(self, host, port, switchid, tableid, minid, maxid):
87 flow_template = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
88 <flow xmlns="urn:opendaylight:flow:inventory">
89 <strict>false</strict>
101 <table_id>{}</table_id>
103 <cookie_mask>255</cookie_mask>
104 <installHw>false</installHw>
111 <ipv4-destination>10.0.1.0/24</ipv4-destination>
114 <flow-name>FooXf{}</flow-name>
115 <priority>{}</priority>
116 <barrier>false</barrier>
121 ses = requests.Session()
123 for i in range(int(minid), int(maxid) + 1):
124 if self._stop is True:
127 flow = flow_template.format(tableid, fid, fid, fid)
128 url = 'http://{}:{}/restconf/config/opendaylight-inventory:nodes/node/openflow:{}/table/{}/flow/{}'.format(
129 host, port, switchid, tableid, fid)
132 rsp = ses.put(url, headers={'Content-Type': 'application/xml'}, data=flow, timeout=3)
133 if rsp.status_code == 200:
141 def deconfigure_flows(self, host, port, switchid, tableid, minid, maxid):
142 """Result will be the number of status code 200 returned"""
144 ses = requests.Session()
146 for fid in range(int(minid), int(maxid)):
147 if self._stop is True:
149 url = 'http://{}:{}/restconf/config/opendaylight-inventory:nodes/node/openflow:{}/table/{}/flow/{}'.format(
150 host, port, switchid, tableid, fid)
153 rsp = ses.delete(url, headers={'Content-Type': 'application/xml'}, timeout=3)
154 if rsp.status_code == 200: