CSIT-190 Add honeycomb interface management test
[csit.git] / resources / libraries / python / honeycomb / Notifications.py
1 # Copyright (c) 2016 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, time_out=10):
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         :param time_out: Timeout value for each read operation in seconds.
36         :type subscription: str
37         :type time_out: int
38         :raises HoneycombError: If subscription to notifications fails.
39         """
40
41         logger.debug(subscription)
42         self.send(subscription)
43
44         reply = self.get_response(
45             time_out=time_out,
46             err="Timeout on notifications subscription."
47         )
48
49         if "<ok/>" not in reply:
50             raise HoneycombError("Notifications subscription failed with"
51                                  " message: {0}".format(reply))
52
53         logger.debug("Notifications subscription successful.")
54
55     def get_notification(self, time_out=10):
56         """Read and return the next notification message.
57
58         :param time_out: Timeout value for the read operation in seconds.
59         :type time_out: int
60         :return: Data received from buffer.
61         :rtype: str
62         """
63
64         logger.debug("Getting notification. Timeout set to {0} seconds."
65                      .format(time_out))
66
67         reply = self.get_response(
68             time_out=time_out,
69             err="Timeout on getting notification."
70         )
71
72         return reply