3 Authors: Denghui Huang@IBM, Baohua Yang@IBM
12 DEFAULT_CONTROLLER_IP = '127.0.0.1'
13 # DEFAULT_CONTROLLER_IP = '9.186.105.113' #just for temp test
15 DEFAULT_PREFIX = 'http://' + DEFAULT_CONTROLLER_IP + ':' + DEFAULT_PORT
16 DEFAULT_CONTAINER = 'default'
17 DEFAULT_USER = 'admin'
19 MODULES_DIR = 'modules'
27 def do_post_request(url, content_type, payload=None, user=DEFAULT_USER, password=DEFAULT_PWD):
30 if content_type == 'json':
31 headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
32 if payload is not None:
33 data = json.dumps(payload)
34 elif content_type == 'xml':
35 headers = {'Content-type': 'application/xml', 'Accept': 'application/xml'}
37 print 'unsupported content-type'
39 r = requests.post(url, data, headers=headers, auth=(user, password), timeout=TIMEOUTS)
41 except (requests.exceptions.HTTPError, requests.exceptions.Timeout):
47 def do_get_request_with_status_code(url, content_type, user=DEFAULT_USER, password=DEFAULT_PWD):
50 @return The status code.
54 r = requests.get(url, auth=(user, password), timeout=TIMEOUTS)
56 except (requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
63 def do_put_request(url, content_type, payload=None, user=DEFAULT_USER, password=DEFAULT_PWD):
66 @return The status code.
70 if content_type == 'json':
71 headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
72 if payload is not None:
73 data = json.dumps(payload)
74 elif content_type == 'xml':
75 headers = {'Content-type': 'application/xml', 'Accept': 'application/xml'}
77 print 'unsupported content-type'
79 r = requests.put(url, data, headers=headers, auth=(user, password), timeout=TIMEOUTS)
81 except (requests.exceptions.HTTPError, requests.exceptions.Timeout):
87 def do_delete_request(url, user=DEFAULT_USER, password=DEFAULT_PWD):
89 Send a DELETE request.
90 @return The status code.
94 r = requests.delete(url, auth=(user, password), timeout=TIMEOUTS)
96 except (requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
103 def convert_result_to_list(result):
105 Convert the result content to list.
109 content = result.values()
110 for list1 in content:
111 list2 = [dict1.values() for dict1 in list1]
115 for element in list4:
116 list3.append(element)
121 def do_get_request_with_response_content(url, content_type, user=DEFAULT_USER, password=DEFAULT_PWD,
122 convert_to_list=False):
124 Send a GET request and get the response.
125 @return response content as list.
128 r = requests.get(url, auth=(user, password), timeout=TIMEOUTS)
130 except (requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
135 if content_type == 'json':
137 return convert_result_to_list(content) if convert_to_list else content
138 elif content_type == 'xml': # TODO: add parser to xml
142 if __name__ == '__main__':
144 # Note: in json body, all field name and value (if it is string type) must be enclosed in double quotes.
145 # This constraint maybe cause by json parser.
146 body = {"status": "Success", "dstNodeConnector": "OF|1@OF|00:00:00:00:00:00:00:01", "name": "link3",
147 "srcNodeConnector": "OF|1@OF|00:00:00:00:00:00:00:03"}
148 url = 'http://127.0.0.1:8080/controller/nb/v2/topology/default/userLink/link3'
149 content_type = 'json'
150 print do_put_request(url, content_type, body)