Update odltools
[netvirt.git] / resources / tools / odltools / odltools / common / odl_client.py
1 # Copyright 2018 Red Hat, Inc. and others. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 from requests import sessions
17
18 logger = logging.getLogger('common.odl_client')
19
20
21 class OpenDaylightRestClient(object):
22
23     def __init__(self, url, username, password, timeout):
24         super(OpenDaylightRestClient, self).__init__()
25         self.url = url
26         self.timeout = timeout
27         self.session = sessions.Session()
28         self.session.auth = (username, password)
29
30     def get(self, urlpath='', data=None):
31         return self.request('get', urlpath, data)
32
33     def put(self, urlpath='', data=None):
34         return self.request('put', urlpath, data)
35
36     def delete(self, urlpath='', data=None):
37         return self.request('delete', urlpath, data)
38
39     def request(self, method, urlpath='', data=None):
40         headers = {'Content-Type': 'application/json'}
41         url = '/'.join([self.url, urlpath])
42         logging.debug(
43             "Sending METHOD (%(method)s) URL (%(url)s) JSON (%(data)s)",
44             {'method': method, 'url': url, 'data': data})
45         return self.session.request(
46             method, url=url, headers=headers, data=data, timeout=self.timeout)