blob: 53c010e2feb4c6e6c08da9cf5e2e7435470c4ab4 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Anusha Dathatri1cd34b52019-08-13 04:43:12 -05002
George Keishinge635ddc2022-12-08 07:38:02 -06003import json
4import ssl
Patrick Williams20f38712022-12-08 06:18:26 -06005
George Keishinge635ddc2022-12-08 07:38:02 -06006import gen_print as gp
Patrick Williams20f38712022-12-08 06:18:26 -06007import gen_valid as gv
8import requests
9import websocket
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050010
11
Patrick Williams20f38712022-12-08 06:18:26 -060012class event_notification:
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050013 r"""
14 Main class to subscribe and receive event notifications.
15 """
16
17 def __init__(self, host, username, password):
18 r"""
19 Initialize instance variables.
20
21 Description of argument(s):
22 host The IP or host name of the system to subscribe to.
23 username The username for the host system.
24 password The password for the host system.
25 """
26 self.__host = host
27 self.__user = username
28 self.__password = password
29
30 def __del__(self):
31 try:
32 self.__websocket.close()
33 except AttributeError:
34 pass
35
36 def login(self):
37 r"""
38 Login and return session object.
39 """
Patrick Williams20f38712022-12-08 06:18:26 -060040 http_header = {"Content-Type": "application/json"}
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050041 session = requests.session()
Patrick Williams20f38712022-12-08 06:18:26 -060042 response = session.post(
43 "https://" + self.__host + "/login",
44 headers=http_header,
45 json={"data": [self.__user, self.__password]},
46 verify=False,
47 timeout=30,
48 )
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050049 gv.valid_value(response.status_code, valid_values=[200])
50 login_response = json.loads(response.text)
51 gp.qprint_var(login_response)
Patrick Williams20f38712022-12-08 06:18:26 -060052 gv.valid_value(login_response["status"], valid_values=["ok"])
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050053 return session
54
55 def subscribe(self, dbus_path, enable_trace=False):
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050056 r"""
57 Subscribe to the given path and return a list of event notifications.
58
59 For more details on "subscribe" and "events" go to
60 https://github.com/openbmc/docs/blob/master/rest-api.md#event-subscription-protocol
61
62 Example robot code:
63 ${event_notifications}= Subscribe /xyz/openbmc_project/sensors
64 Rprint Vars event_notifications
65
66 Example output:
67 event_notifications:
68 [0]:
69 [interface]: xyz.openbmc_project.Sensor.Value
70 [path]: /xyz/openbmc_project/sensors/temperature/ambient
71 [event]: PropertiesChanged
72 [properties]:
73 [Value]: 23813
74
75 Description of argument(s):
George Keishingcfffea22019-10-18 13:12:20 -050076 dbus_path The subscribing event's path (e.g.
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050077 "/xyz/openbmc_project/sensors").
78 enable_trace Enable or disable trace.
79 """
80
81 session = self.login()
82 cookies = session.cookies.get_dict()
83 # Convert from dictionary to a string of the following format:
84 # key=value;key=value...
Patrick Williams20f38712022-12-08 06:18:26 -060085 cookies = gp.sprint_var(
86 cookies,
87 fmt=gp.no_header() | gp.strip_brackets(),
88 col1_width=0,
89 trailing_char="",
90 delim="=",
91 ).replace("\n", ";")
Anusha Dathatri1cd34b52019-08-13 04:43:12 -050092
93 websocket.enableTrace(enable_trace)
Patrick Williams20f38712022-12-08 06:18:26 -060094 self.__websocket = websocket.create_connection(
95 "wss://{host}/subscribe".format(host=self.__host),
96 sslopt={"cert_reqs": ssl.CERT_NONE},
97 cookie=cookies,
98 )
99 dbus_path = [path.strip() for path in dbus_path.split(",")]
Anusha Dathatri1cd34b52019-08-13 04:43:12 -0500100 dbus_path = {"paths": dbus_path}
101
102 self.__websocket.send(json.dumps(dbus_path))
103 event_notifications = json.loads(self.__websocket.recv())
104 self.__websocket.close()
105 return event_notifications