George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Shaheena Begum Mohammed | f0b9122 | 2023-02-10 02:53:38 -0600 | [diff] [blame] | 2 | r""" |
| 3 | This is an extended user library to support Robot Selenium code. |
| 4 | The class contains functions which the robot framework will use |
| 5 | and import as a user-defined keyword. |
| 6 | """ |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import json |
| 8 | import ssl |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 9 | |
Shaheena Begum Mohammed | f0b9122 | 2023-02-10 02:53:38 -0600 | [diff] [blame] | 10 | import gen_print as gp # NOQA |
| 11 | import gen_valid as gv # NOQA |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 12 | import requests |
| 13 | import websocket |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 14 | |
| 15 | |
Shaheena Begum Mohammed | f0b9122 | 2023-02-10 02:53:38 -0600 | [diff] [blame] | 16 | class event_notification: # NOQA |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 17 | r""" |
| 18 | Main class to subscribe and receive event notifications. |
| 19 | """ |
| 20 | |
| 21 | def __init__(self, host, username, password): |
| 22 | r""" |
| 23 | Initialize instance variables. |
| 24 | |
| 25 | Description of argument(s): |
| 26 | host The IP or host name of the system to subscribe to. |
| 27 | username The username for the host system. |
| 28 | password The password for the host system. |
| 29 | """ |
| 30 | self.__host = host |
| 31 | self.__user = username |
| 32 | self.__password = password |
Shaheena Begum Mohammed | f0b9122 | 2023-02-10 02:53:38 -0600 | [diff] [blame] | 33 | self.__websocket = None |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 34 | |
| 35 | def __del__(self): |
| 36 | try: |
| 37 | self.__websocket.close() |
| 38 | except AttributeError: |
| 39 | pass |
| 40 | |
| 41 | def login(self): |
| 42 | r""" |
| 43 | Login and return session object. |
| 44 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 45 | http_header = {"Content-Type": "application/json"} |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 46 | session = requests.session() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 47 | response = session.post( |
| 48 | "https://" + self.__host + "/login", |
| 49 | headers=http_header, |
| 50 | json={"data": [self.__user, self.__password]}, |
| 51 | verify=False, |
| 52 | timeout=30, |
| 53 | ) |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 54 | gv.valid_value(response.status_code, valid_values=[200]) |
| 55 | login_response = json.loads(response.text) |
| 56 | gp.qprint_var(login_response) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 57 | gv.valid_value(login_response["status"], valid_values=["ok"]) |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 58 | return session |
| 59 | |
| 60 | def subscribe(self, dbus_path, enable_trace=False): |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 61 | r""" |
| 62 | Subscribe to the given path and return a list of event notifications. |
| 63 | |
| 64 | For more details on "subscribe" and "events" go to |
| 65 | https://github.com/openbmc/docs/blob/master/rest-api.md#event-subscription-protocol |
| 66 | |
| 67 | Example robot code: |
| 68 | ${event_notifications}= Subscribe /xyz/openbmc_project/sensors |
| 69 | Rprint Vars event_notifications |
| 70 | |
| 71 | Example output: |
| 72 | event_notifications: |
| 73 | [0]: |
| 74 | [interface]: xyz.openbmc_project.Sensor.Value |
| 75 | [path]: /xyz/openbmc_project/sensors/temperature/ambient |
| 76 | [event]: PropertiesChanged |
| 77 | [properties]: |
| 78 | [Value]: 23813 |
| 79 | |
| 80 | Description of argument(s): |
George Keishing | cfffea2 | 2019-10-18 13:12:20 -0500 | [diff] [blame] | 81 | dbus_path The subscribing event's path (e.g. |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 82 | "/xyz/openbmc_project/sensors"). |
| 83 | enable_trace Enable or disable trace. |
| 84 | """ |
| 85 | |
| 86 | session = self.login() |
| 87 | cookies = session.cookies.get_dict() |
| 88 | # Convert from dictionary to a string of the following format: |
| 89 | # key=value;key=value... |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 90 | cookies = gp.sprint_var( |
| 91 | cookies, |
| 92 | fmt=gp.no_header() | gp.strip_brackets(), |
| 93 | col1_width=0, |
| 94 | trailing_char="", |
| 95 | delim="=", |
| 96 | ).replace("\n", ";") |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 97 | |
| 98 | websocket.enableTrace(enable_trace) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 99 | self.__websocket = websocket.create_connection( |
Shaheena Begum Mohammed | f0b9122 | 2023-02-10 02:53:38 -0600 | [diff] [blame] | 100 | f"wss://{self.__host}/subscribe", |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 101 | sslopt={"cert_reqs": ssl.CERT_NONE}, |
| 102 | cookie=cookies, |
| 103 | ) |
| 104 | dbus_path = [path.strip() for path in dbus_path.split(",")] |
Anusha Dathatri | 1cd34b5 | 2019-08-13 04:43:12 -0500 | [diff] [blame] | 105 | dbus_path = {"paths": dbus_path} |
| 106 | |
| 107 | self.__websocket.send(json.dumps(dbus_path)) |
| 108 | event_notifications = json.loads(self.__websocket.recv()) |
| 109 | self.__websocket.close() |
| 110 | return event_notifications |