aboutsummaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/vnf/ims/ixia/utils/IxChassisUtils.py
blob: 973e0264d7f00d61b87211f45860e85b5ac2ba9a (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
#!/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

import httplib2
import json
import logging


okStates = [200, 201, 202]
states = [
    'Queued',
    'In Progress',
    'Manual Step Required',
    'Error',
    'Finished',
    'Aborted',
    'Retried',
    'IRebooting',
    'Force Continue',
    'Pending',
    ]
notStartedState = 'Not_Started'
errorStates = ['Error', 'Aborted', 'Force Continue']
finishedStates = ['Manual Step Required', 'Finished']

logger = logging.getLogger(__name__)


class TestFailedError(Exception):
    pass


class ChassisRestAPI:
    @staticmethod
    def postWithPayload(loginUrl, payload=None):
        urlHeadersJson = {'content-type': 'application/json'}
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            if payload is None:
                logger.debug('POST: ' + loginUrl)
                (response, content) = h.request(loginUrl, 'POST', '',
                                                urlHeadersJson)
                logger.debug(content)
            else:
                logger.debug('POST: ' + loginUrl + ' <- Data: ' + str(payload))
                (response, content) = h.request(loginUrl, 'POST',
                                                body=payload,
                                                headers=urlHeadersJson)
                logger.debug(response)
                logger.debug(content)
        except Exception, e:
            raise Exception('Got an error code: ', e)
        return content

    @staticmethod
    def postWithPayloadAndHeaders(loginUrl, urlHeadersJson,
                                  payload=None):
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            if payload is None:
                logger.debug('POST: ' + loginUrl)
                (response, content) = h.request(loginUrl, 'POST', '',
                                                urlHeadersJson)
            else:
                logger.debug('POST: ' + loginUrl + ' <- Data: ' + str(payload))
                (response, content) = h.request(loginUrl, 'POST',
                                                body=payload,
                                                headers=urlHeadersJson)
        except Exception, e:
            raise Exception('Got an error code: ', e)
        return content

    @staticmethod
    def postOperation(url, apiKey, payload=''):
        urlHeadersJson = {'content-type': 'application/json',
                          'X-Api-Key': '%s' % str(apiKey)}
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            if payload is None:
                logger.debug('POST: ' + url)
                (response, content) = h.request(url, 'POST',
                                                json.dumps(payload),
                                                urlHeadersJson)
            else:
                logger.debug('POST: ' + url + ' <- Data: ' + str(payload))
                (response, content) = h.request(url, 'POST',
                                                json.dumps(payload),
                                                headers=urlHeadersJson)
        except Exception, e:
            raise Exception('Got an error code: ', e)
        return content

    @staticmethod
    def patch(url, payload, apiKey):
        urlHeadersJson = {'content-type': 'application/json',
                          'X-Api-Key': '%s' % str(apiKey)}
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            logger.debug('PATCH: ' + url + ' <-- Attribute: ' +
                         str(payload))
            (response, content) = h.request(url, 'PATCH',
                                            json.dumps(payload),
                                            urlHeadersJson)
        except Exception, e:

            # print (response, content)

            raise Exception('Got an error code: ', e)
        return content

    @staticmethod
    def delete(url, apiKey):
        urlHeadersJson = {'content-type': 'application/json',
                          'X-Api-Key': '%s' % str(apiKey)}
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            (response, content) = h.request(url, 'DELETE', '', urlHeadersJson)
            logger.debug('DELETE: ' + url)
        except Exception, e:
            raise Exception('Got an error code: ', e)
        if response.status not in okStates:
            raise TestFailedError(json.loads(content)['error'])
        return json.loads(content)

    @staticmethod
    def getWithHeaders(url, apiKey):
        urlHeadersJson = {'content-type': 'application/json',
                          'X-Api-Key': '%s' % str(apiKey)}
        try:
            h = httplib2.Http('.cache',
                              disable_ssl_certificate_validation=True)
            logger.debug('GET: ' + url)
            (response, content) = h.request(url, 'GET', '', urlHeadersJson)
        except Exception, e:
            raise Exception('Got an error code: ', e)
        if response.status not in okStates:
            raise TestFailedError(json.loads(content)['error'])
        output = json.loads(content)
        return output