summaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/vnf/ims/ixia/utils/IxRestUtils.py
blob: 41c23dc53a0f3b55180d5bb80647bb58522678f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python

# Copyright (c) 2017 IXIA and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

from urlparse import urljoin
import requests


def getConnection(server, port):
    """
    Gets a Connection instance, that will be used to
    make the HTTP requests to the application
    """
    connectionUrl = 'http://%s:%s/' % (server, port)

    conn = Connection(connectionUrl, 'v0')
    return conn


def formatDictToJSONPayload(dictionary):
    """
    Converts a given python dict instance to a string
    JSON payload that can be sent to a REST API.
    """
    jsonPayload = '{'
    optionsList = []
    for (key, val) in dictionary.items():
        valStr = str(val)
        if type(val) is str:
            valStr = '"%s"' % val
        if type(val) is bool:
            valStr = valStr.lower()
        optionsList.append('"%s":%s' % (key, valStr))

    jsonPayload += ','.join(optionsList)
    jsonPayload += '}'

    return jsonPayload


class Connection(object):

    """
    Class that executes the HTTP requests to the application instance.
    It handles creating the HTTP session and executing HTTP methods.
    """

    kHeaderContentType = 'content-type'
    kContentJson = 'application/json'

    def __init__(self, siteUrl, apiVersion):
        self.httpSession = None

        self.url = Connection.urljoin(siteUrl, 'api')
        self.url = Connection.urljoin(self.url, apiVersion)

    def _getHttpSession(self):
        """
        This is a lazy initializer for the HTTP session.
        It does not need to be active until it is required.
        """

        if self.httpSession is None:
            self.httpSession = requests.Session()
        return self.httpSession

    @classmethod
    def urljoin(cls, base, end):
        """ Join two URLs. If the second URL is absolute, the base is ignored.

        Use this instead of urlparse.urljoin directly so that we can customize
        its behavior if necessary.
        Currently differs in that it

            1. appends a / to base if not present.
            2. casts end to a str as a convenience
        """

        if base and not base.endswith('/'):
            base = base + '/'
        return urljoin(base, str(end))

    def httpRequest(self, method, url='', data='', params={}, headers={}):
        """
        Method for making a HTTP request.
        The method type (GET, POST, PATCH, DELETE) will be sent as a parameter.
        Along with the url and request data. The HTTP response is returned
        """

        headers[Connection.kHeaderContentType] = Connection.kContentJson

        absUrl = Connection.urljoin(self.url, url)
        result = self._getHttpSession().request(method,
                                                absUrl,
                                                data=str(data),
                                                params=params,
                                                headers=headers)
        return result

    def httpGet(self, url='', data='', params={}, headers={}):
        """
        Method for calling HTTP GET.
        This will return a WebObject that has the fields returned
        in JSON format by the GET operation.
        """

        reply = self.httpRequest('GET', url, data, params, headers)
        return _WebObject(reply.json())

    def httpPost(self, url='', data='', params={}, headers={}):
        """
        Method for calling HTTP POST. Will return the HTTP reply.
        """

        return self.httpRequest('POST', url, data, params, headers)

    def httpPatch(self, url='', data='', params={}, headers={}):
        """
        Method for calling HTTP PATCH. Will return the HTTP reply.
        """

        return self.httpRequest('PATCH', url, data, params, headers)

    def httpDelete(self, url='', data='', params={}, headers={}):
        """
        Method for calling HTTP DELETE. Will return the HTTP reply.
        """

        return self.httpRequest('DELETE', url, data, params, headers)


def _WebObject(value):
    """
    Method used for creating a wrapper object corresponding to the JSON string
    received on a GET request.
    """

    if isinstance(value, dict):
        result = WebObject(**value)
    elif isinstance(value, list):
        result = WebList(entries=value)
    else:
        result = value
    return result


class WebList(list):

    """
    Using this class a JSON list will be transformed
    in a list of WebObject instances.
    """

    def __init__(self, entries=[]):
        """
        Create a WebList from a list of items that
        are processed by the _WebObject function
        """

        for item in entries:
            self.append(_WebObject(item))


class WebObject(object):

    """
    A WebObject instance will have its fields set to correspond to
    the JSON format received on a GET request.
    """

    def __init__(self, **entries):
        """
        Create a WebObject instance by providing a dict having a
        property - value structure.
        """

        self.jsonOptions = {}
        for (key, value) in entries.iteritems():
            webObj = _WebObject(value)
            self.jsonOptions[key] = webObj
            self.__dict__[key] = webObj

    def getOptions(self):
        '''
        Get the JSON dictionary which represents the WebObject Instance
        '''

        return self.jsonOptions