Fix PyLint errors
[csit.git] / resources / libraries / python / honeycomb / Notifications.py
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Implementation of keywords for managing Honeycomb notifications."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
19 from resources.libraries.python.honeycomb.Netconf import Netconf
20
21
22 class Notifications(Netconf):
23     """Implements keywords for receiving Honeycomb notifications.
24
25     The keywords implemented in this class make it possible to:
26     - receive notifications from Honeycomb
27     - read received notifications
28     """
29
30     def add_notification_listener(self, subscription):
31         """Open a new channel on the SSH session, connect to Netconf subsystem
32         and subscribe to receive Honeycomb notifications.
33
34         :param subscription: RPC for subscription to notifications.
35         :type subscription: str
36         :raises HoneycombError: If subscription to notifications fails.
37         """
38
39         logger.debug(subscription)
40         self.send(subscription)
41
42         reply = self.get_response(
43             err="Timeout on notifications subscription."
44         )
45
46         if "<ok/>" not in reply:
47             raise HoneycombError("Notifications subscription failed with"
48                                  " message: {0}".format(reply))
49
50         logger.debug("Notifications subscription successful.")
51
52     def get_notification(self, time_out=10):
53         """Read and return the next notification message.
54
55         :param time_out: Timeout value for the read operation in seconds.
56         :type time_out: int
57         :returns: Data received from buffer.
58         :rtype: str
59         """
60
61         logger.debug("Getting notification. Timeout set to {0} seconds."
62                      .format(time_out))
63
64         reply = self.get_response(
65             err="Timeout on getting notification."
66         )
67
68         return reply