aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/honeycomb/HoneycombUtil.py
blob: 24f81af7b357c5c81864c25a35c6b39cab3ff961 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of low level functionality used in communication with
Honeycomb.

Exception HoneycombError is used in all methods and in all modules with
Honeycomb keywords.

Class HoneycombUtil implements methods used by Honeycomb keywords. They must not
be used directly in tests. Use keywords implemented in the module
HoneycombAPIKeywords instead.
"""

from json import loads, dumps
from enum import Enum, unique

from robot.api import logger

from resources.libraries.python.ssh import SSH
from resources.libraries.python.HTTPRequest import HTTPRequest
from resources.libraries.python.constants import Constants as Const


@unique
class DataRepresentation(Enum):
    """Representation of data sent by PUT and POST requests."""
    NO_DATA = 0
    JSON = 1
    XML = 2
    TXT = 3


# Headers used in requests. Key - content representation, value - header.
HEADERS = {DataRepresentation.NO_DATA:
               {},  # It must be empty dictionary.
           DataRepresentation.JSON:
               {"Content-Type": "application/json",
                "Accept": "text/plain"},
           DataRepresentation.XML:
               {"Content-Type": "application/xml",
                "Accept": "text/plain"},
           DataRepresentation.TXT:
               {"Content-Type": "text/plain",
                "Accept": "text/plain"}
          }


class HoneycombError(Exception):

    """Exception(s) raised by methods working with Honeycomb.

    When raising this exception, put this information to the message in this
    order:
     - short description of the encountered problem (parameter msg),
     - relevant messages if there are any collected, e.g., from caught
       exception (optional parameter details),
     - relevant data if there are any collected (optional parameter details).
     The logging is performed on two levels: 1. error - short description of the
     problem; 2. debug - detailed information.
    """

    def __init__(self, msg, details='', enable_logging=True):
        """Sets the exception message and enables / disables logging.

        It is not wanted to log errors when using these keywords together
        with keywords like "Wait until keyword succeeds". So you can disable
        logging by setting enable_logging to False.

        :param msg: Message to be displayed and logged.
        :param enable_logging: When True, logging is enabled, otherwise
        logging is disabled.
        :type msg: str
        :type enable_logging: bool
        """
        super(HoneycombError, self).__init__()
        self._msg = "{0}: {1}".format(self.__class__.__name__, msg)
        self._details = details
        if enable_logging:
            logger.debug(self._details)

    def __repr__(self):
        return repr(self._msg)

    def __str__(self):
        return str(self._msg)


class HoneycombUtil(object):
    """Implements low level functionality used in communication with Honeycomb.

    There are implemented methods to get, put and delete data to/from Honeycomb.
    They are based on functionality implemented in the module HTTPRequests which
    uses HTTP requests GET, PUT, POST and DELETE to communicate with Honeycomb.

    It is possible to PUT the data represented as XML or JSON structures or as
    plain text.
    Data received in the response of GET are always represented as a JSON
    structure.

    There are also two supportive methods implemented:
    - read_path_from_url_file which reads URL file and returns a path (see
      docs/honeycomb_url_files.rst).
    - parse_json_response which parses data from response in JSON representation
      according to given path.
    """

    def __init__(self):
        pass

    @staticmethod
    def read_path_from_url_file(url_file):
        """Read path from *.url file.

        For more information about *.url file see docs/honeycomb_url_files.rst
        :param url_file: URL file. The argument contains only the name of file
        without extension, not the full path.
        :type url_file: str
        :returns: Requested path.
        :rtype: str
        """

        url_file = "{0}/{1}.url".format(Const.RESOURCES_TPL_HC, url_file)
        with open(url_file) as template:
            path = template.readline()
        return path

    @staticmethod
    def find_item(data, path):
        """Find a data item (single leaf or sub-tree) in data received from
        Honeycomb REST API.

        Path format:
        The path is a tuple with items navigating to requested data. The items
        can be strings or tuples:
        - string item represents a dictionary key in data,
        - tuple item represents list item in data.

        Example:
        data = \
        {
            "interfaces": {
                "interface": [
                    {
                        "name": "GigabitEthernet0/8/0",
                        "enabled": "true",
                        "type": "iana-if-type:ethernetCsmacd",
                    },
                    {
                        "name": "local0",
                        "enabled": "false",
                        "type": "iana-if-type:ethernetCsmacd",
                    }
                ]
            }
        }

        path = ("interfaces", ("interface", "name", "local0"), "enabled")
        This path points to "false".

        The tuple ("interface", "name", "local0") consists of:
        index 0 - dictionary key pointing to a list,
        index 1 - key which identifies an item in the list, it is also marked as
                  the key in corresponding yang file.
        index 2 - key value.

        :param data: Data received from Honeycomb REST API.
        :param path: Path to data we want to find.
        :type data: dict
        :type path: tuple
        :returns: Data represented by path.
        :rtype: str, dict, or list
        :raises HoneycombError: If the data has not been found.
        """

        for path_item in path:
            try:
                if isinstance(path_item, str):
                    data = data[path_item]
                elif isinstance(path_item, tuple):
                    for data_item in data[path_item[0]]:
                        if data_item[path_item[1]] == path_item[2]:
                            data = data_item
            except KeyError as err:
                raise HoneycombError("Data not found: {0}".format(err))

        return data

    @staticmethod
    def remove_item(data, path):
        """Remove a data item (single leaf or sub-tree) in data received from
        Honeycomb REST API.

        :param data: Data received from Honeycomb REST API.
        :param path: Path to data we want to remove.
        :type data: dict
        :type path: tuple
        :returns: Original data without removed part.
        :rtype: dict
        """

        origin_data = previous_data = data
        try:
            for path_item in path:
                previous_data = data
                if isinstance(path_item, str):
                    data = data[path_item]
                elif isinstance(path_item, tuple):
                    for data_item in data[path_item[0]]:
                        if data_item[path_item[1]] == path_item[2]:
                            data = data_item
        except KeyError as err:
            logger.debug("Data not found: {0}".format(err))
            return origin_data

        if isinstance(path[-1], str):
            previous_data.pop(path[-1])
        elif isinstance(path[-1], tuple):
            previous_data[path[-1][0]].remove(data)
            if not previous_data[path[-1][0]]:
                previous_data.pop(path[-1][0])

        return origin_data

    @staticmethod
    def set_item_value(data, path, new_value):
        """Set or change the value (single leaf or sub-tree) in data received
        from Honeycomb REST API.

        If the item is not present in the data structure, it is created.

        :param data: Data received from Honeycomb REST API.
        :param path: Path to data we want to change or create.
        :param new_value: The value to be set.
        :type data: dict
        :type path: tuple
        :type new_value: str, dict or list
        :returns: Original data with the new value.
        :rtype: dict
        """

        origin_data = data
        for path_item in path[:-1]:
            if isinstance(path_item, str):
                try:
                    data = data[path_item]
                except KeyError:
                    data[path_item] = {}
                    data = data[path_item]
            elif isinstance(path_item, tuple):
                try:
                    flag = False
                    index = 0
                    for data_item in data[path_item[0]]:
                        if data_item[path_item[1]] == path_item[2]:
                            data = data[path_item[0]][index]
                            flag = True
                            break
                        index += 1
                    if not flag:
                        data[path_item[0]].append({path_item[1]: path_item[2]})
                        data = data[path_item[0]][-1]
                except KeyError:
                    data[path_item] = []

        if not path[-1] in data.keys():
            data[path[-1]] = {}

        if isinstance(new_value, list) and isinstance(data[path[-1]], list):
            for value in new_value:
                data[path[-1]].append(value)
        else:
            data[path[-1]] = new_value

        return origin_data

    @staticmethod
    def get_honeycomb_data(node, url_file, path=""):
        """Retrieve data from Honeycomb according to given URL.

        :param node: Honeycomb node.
        :param url_file: URL file. The argument contains only the name of file
        without extension, not the full path.
        :param path: Path which is added to the base path to identify the data.
        :type node: dict
        :type url_file: str
        :type path: str
        :returns: Status code and content of response.
        :rtype tuple
        """

        base_path = HoneycombUtil.read_path_from_url_file(url_file)
        path = base_path + path
        status_code, resp = HTTPRequest.get(node, path)
        return status_code, loads(resp)

    @staticmethod
    def put_honeycomb_data(node, url_file, data, path="",
                           data_representation=DataRepresentation.JSON):
        """Send configuration data using PUT request and return the status code
        and response content.

        :param node: Honeycomb node.
        :param url_file: URL file. The argument contains only the name of file
        without extension, not the full path.
        :param data: Configuration data to be sent to Honeycomb.
        :param path: Path which is added to the base path to identify the data.
        :param data_representation: How the data is represented.
        :type node: dict
        :type url_file: str
        :type data: dict, str
        :type path: str
        :type data_representation: DataRepresentation
        :returns: Status code and content of response.
        :rtype: tuple
        :raises HoneycombError: If the given data representation is not defined
        in HEADERS.
        """

        try:
            header = HEADERS[data_representation]
        except AttributeError as err:
            raise HoneycombError("Wrong data representation: {0}.".
                                 format(data_representation), repr(err))
        if data_representation == DataRepresentation.JSON:
            data = dumps(data)

        logger.trace(data)

        base_path = HoneycombUtil.read_path_from_url_file(url_file)
        path = base_path + path
        logger.trace(path)
        return HTTPRequest.put(
            node=node, path=path, headers=header, payload=data)

    @staticmethod
    def post_honeycomb_data(node, url_file, data=None,
                            data_representation=DataRepresentation.JSON,
                            timeout=10):
        """Send a POST request and return the status code and response content.

        :param node: Honeycomb node.
        :param url_file: URL file. The argument contains only the name of file
        without extension, not the full path.
        :param data: Configuration data to be sent to Honeycomb.
        :param data_representation: How the data is represented.
        :param timeout: How long to wait for the server to send data before
        giving up.
        :type node: dict
        :type url_file: str
        :type data: dict, str
        :type data_representation: DataRepresentation
        :type timeout: int
        :returns: Status code and content of response.
        :rtype: tuple
        :raises HoneycombError: If the given data representation is not defined
        in HEADERS.
        """

        try:
            header = HEADERS[data_representation]
        except AttributeError as err:
            raise HoneycombError("Wrong data representation: {0}.".
                                 format(data_representation), repr(err))
        if data_representation == DataRepresentation.JSON:
            data = dumps(data)

        path = HoneycombUtil.read_path_from_url_file(url_file)
        return HTTPRequest.post(
            node=node, path=path, headers=header, payload=data, timeout=timeout)

    @staticmethod
    def delete_honeycomb_data(node, url_file, path=""):
        """Delete data from Honeycomb according to given URL.

        :param node: Honeycomb node.
        :param url_file: URL file. The argument contains only the name of file
        without extension, not the full path.
        :param path: Path which is added to the base path to identify the data.
        :type node: dict
        :type url_file: str
        :type path: str
        :returns: Status code and content of response.
        :rtype tuple
        """

        base_path = HoneycombUtil.read_path_from_url_file(url_file)
        path = base_path + path
        return HTTPRequest.delete(node, path)

    @staticmethod
    def archive_honeycomb_log(node, perf=False):
        """Copy honeycomb log file from DUT node to VIRL for archiving.

        :param node: Honeycomb node.
        :param perf: Alternate handling, for use with performance test topology.
        :type node: dict
        :type perf: bool
        """

        ssh = SSH()
        ssh.connect(node)

        if not perf:
            cmd = "cp /var/log/honeycomb/honeycomb.log /scratch/"
            ssh.exec_command_sudo(cmd)
        else:
            ssh.scp(
                ".",
                "/var/log/honeycomb/honeycomb.log",
                get=True)