Repository navigation
Expand file tree
/
Copy path__ws_wrapper.py
More file actions
625 lines (557 loc) · 24.2 KB
/
Copy path__ws_wrapper.py
File metadata and controls
625 lines (557 loc) · 24.2 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import json
import queue
import re
import threading
from collections.abc import Callable, Generator
from enum import Enum
from mistapi import APISession
from mistapi.__api_response import APIResponse as _APIResponse
from mistapi.__logger import logger as LOGGER
# Matches ANSI CSI sequences, OSC sequences, and character set designations
_ANSI_ESCAPE_RE = re.compile(
r"\x1b\[[0-9;]*[a-zA-Z]|\x1b\][^\x07]*\x07|\x1b[()][A-B0-2]"
)
# Detects VT100 cursor positioning / clear-screen (triggers screen-buffer mode)
_SCREEN_MODE_RE = re.compile(r"\x1b\[[\d;]*H|\x1b\[2J")
class _VT100Screen:
"""Minimal VT100 terminal emulator for rendering screen-based output.
Handles the subset of VT100 sequences used by Junos ``top`` and
``monitor interface`` commands: cursor positioning, screen/line
clearing, and cursor movement. SGR (colors), scroll regions, and
mode changes are silently ignored.
"""
def __init__(self, rows: int = 24, cols: int = 80) -> None:
self.rows = rows
self.cols = cols
self.cursor_row = 0
self.cursor_col = 0
self.grid: list[list[str]] = [[" "] * cols for _ in range(rows)]
def feed(self, text: str) -> None:
"""Process *text* (may contain VT100 sequences) into the screen buffer."""
i = 0
n = len(text)
while i < n:
ch = text[i]
if ch == "\x1b" and i + 1 < n:
nxt = text[i + 1]
if nxt == "[":
# CSI sequence: \x1b[ <params> <cmd>
j = i + 2
params = ""
while j < n and text[j] in "0123456789;":
params += text[j]
j += 1
if j < n:
self._handle_csi(params, text[j])
i = j + 1
else:
i = j
continue
if nxt in "()":
# Character-set designation – skip 3 bytes
i += 3 if i + 2 < n else n
continue
if nxt == "]":
# OSC sequence – skip until BEL
j = i + 2
while j < n and text[j] != "\x07":
j += 1
i = j + 1
continue
# Unknown escape – skip \x1b and the next char
i += 2
continue
if ch == "\r":
self.cursor_col = 0
i += 1
continue
if ch == "\n":
self.cursor_row += 1
self.cursor_col = 0
if self.cursor_row >= self.rows:
self.grid.pop(0)
self.grid.append([" "] * self.cols)
self.cursor_row = self.rows - 1
i += 1
continue
if ch == "\x00":
i += 1
continue
# Printable character
if 0 <= self.cursor_row < self.rows and 0 <= self.cursor_col < self.cols:
self.grid[self.cursor_row][self.cursor_col] = ch
self.cursor_col += 1
i += 1
# ------------------------------------------------------------------
def _handle_csi(self, params: str, cmd: str) -> None:
nums = []
for p in params.split(";") if params else []:
try:
nums.append(int(p))
except ValueError:
nums.append(0)
if cmd in ("H", "f"): # Cursor position
row = (nums[0] - 1) if nums else 0
col = (nums[1] - 1) if len(nums) > 1 else 0
self.cursor_row = max(0, min(row, self.rows - 1))
self.cursor_col = max(0, min(col, self.cols - 1))
elif cmd == "A": # Cursor up
self.cursor_row = max(0, self.cursor_row - (nums[0] if nums else 1))
elif cmd == "B": # Cursor down
self.cursor_row = min(
self.rows - 1, self.cursor_row + (nums[0] if nums else 1)
)
elif cmd == "C": # Cursor forward
self.cursor_col = min(
self.cols - 1, self.cursor_col + (nums[0] if nums else 1)
)
elif cmd == "D": # Cursor back
self.cursor_col = max(0, self.cursor_col - (nums[0] if nums else 1))
elif cmd == "J": # Erase in display
n = nums[0] if nums else 0
if n == 2:
self.grid = [[" "] * self.cols for _ in range(self.rows)]
self.cursor_row = 0
self.cursor_col = 0
elif n == 0:
for c in range(self.cursor_col, self.cols):
self.grid[self.cursor_row][c] = " "
for r in range(self.cursor_row + 1, self.rows):
self.grid[r] = [" "] * self.cols
elif cmd == "K": # Erase in line
n = nums[0] if nums else 0
if n == 0:
for c in range(self.cursor_col, self.cols):
self.grid[self.cursor_row][c] = " "
elif n == 1:
for c in range(self.cursor_col + 1):
self.grid[self.cursor_row][c] = " "
elif n == 2:
self.grid[self.cursor_row] = [" "] * self.cols
# SGR (m), scroll region (r), mode set/reset (l, h) – ignore
# ------------------------------------------------------------------
def render(self) -> str:
"""Return screen content as text with trailing whitespace trimmed."""
lines = ["".join(row).rstrip() for row in self.grid]
while lines and not lines[-1]:
lines.pop()
return "\n".join(lines)
class TimerAction(Enum):
"""
TimerAction Enum for managing timer actions in WebSocketWrapper.
"""
START = "start"
STOP = "stop"
RESET = "reset"
class Timer(Enum):
"""
Timer Enum for specifying different timer types in WebSocketWrapper.
"""
TIMEOUT = "timeout"
FIRST_MESSAGE_TIMEOUT = "first_message_timeout"
MAX_DURATION = "max_duration"
class UtilResponse:
"""
Encapsulates the response from device utility functions.
Returned immediately by tool functions. When a WebSocket stream is
involved, data is collected in the background. Use ``receive()``,
``wait()``, or the ``on_message`` callback to consume results.
USAGE PATTERNS
-----------
Callback style (on_message passed at call time)::
response = ex.ping(session, site_id, device_id, host="8.8.8.8",
on_message=lambda msg: print(msg))
do_other_work()
response.wait()
print(response.ws_data)
Generator style::
response = ex.ping(session, site_id, device_id, host="8.8.8.8")
for msg in response.receive():
print(msg)
Context manager::
with ex.ping(session, site_id, device_id, host="8.8.8.8") as response:
for msg in response.receive():
print(msg)
Async await::
response = ex.ping(session, site_id, device_id, host="8.8.8.8")
await response
print(response.ws_data)
"""
def __init__(
self,
api_response: _APIResponse | None = None,
) -> None:
self.trigger_api_response = api_response
self.ws_required: bool = False
self.ws_data: list[str] = []
self.ws_raw_events: list[str] = []
# Set when the WebSocket leg did NOT complete cleanly (transport error,
# abnormal close, or failure to start). Stays None on a clean completion or
# a trigger-only command, so `ws_error is not None` is a reliable
# "did not cleanly confirm" signal for consumers. ws_close_code records the
# WebSocket close status code when one was received.
self.ws_error: str | None = None
self.ws_close_code: int | None = None
self._queue: queue.Queue[str | None] = queue.Queue()
self._closed = threading.Event()
self._await_timeout: float | None = None
if api_response is not None:
# api_response provided → done immediately, no WS needed
self._closed.set()
# api_response is None → _closed stays unset (in-progress, waiting for WS)
self._disconnect_fn: Callable[[], None] | None = None
@property
def done(self) -> bool:
"""True if data collection is complete (or no WS was needed)."""
return self._closed.is_set()
def wait(self, timeout: float | None = None) -> "UtilResponse":
"""Block until data collection is complete. Returns self."""
self._closed.wait(timeout=timeout)
return self
def receive(self) -> Generator[str, None, None]:
"""
Blocking generator that yields each processed message as it arrives.
Mirrors ``_MistWebsocket.receive()``. Exits cleanly when the
WebSocket connection closes or ``disconnect()`` is called.
"""
while True:
try:
item = self._queue.get(timeout=0.1)
except queue.Empty:
if self._closed.is_set() and self._queue.empty():
break
continue
if item is None:
break
yield item
def disconnect(self) -> None:
"""Stop the WebSocket connection early."""
fn = self._disconnect_fn
if fn is not None:
fn()
def __enter__(self) -> "UtilResponse":
return self
def __exit__(self, *args) -> None:
self.disconnect()
def __await__(self):
"""Allow ``result = await response`` in async contexts."""
import asyncio
async def _await_impl():
timed_out = not await asyncio.to_thread(
self._closed.wait, self._await_timeout
)
if timed_out:
LOGGER.warning("await timed out after %s seconds", self._await_timeout)
return self
return _await_impl().__await__()
class WebSocketWrapper:
"""
A wrapper class for managing WebSocket connections and events.
This class provides a simplified interface for connecting to WebSocket channels,
handling messages, and managing connection timeouts.
"""
def __init__(
self,
apisession: APISession,
util_response: UtilResponse,
timeout: int = 10,
max_duration: int = 60,
on_message: Callable[[dict], None] | None = None,
) -> None:
self.apisession = apisession
self.util_response = util_response
self.timers = {
Timer.TIMEOUT.value: {
"thread": None,
"duration": timeout,
},
Timer.FIRST_MESSAGE_TIMEOUT.value: {
"thread": None,
"duration": 30,
},
Timer.MAX_DURATION.value: {
"thread": None,
"duration": max_duration,
},
}
self.received_messages = 0
self.data = []
self.raw_events = []
self.ws = None
self.session_id: str | None = None
self.capture_id: str | None = None
self._on_message_cb = on_message
self._screen: _VT100Screen | None = None
self._screen_mode: bool = False
self._extract_trigger_ids()
def _extract_trigger_ids(self):
"""Extract session_id and capture_id from the trigger API response."""
if not self.util_response.trigger_api_response:
return
LOGGER.debug(
"trigger response: %s", self.util_response.trigger_api_response.data
)
if self.util_response.trigger_api_response.data and isinstance(
self.util_response.trigger_api_response.data, dict
):
self.session_id = self.util_response.trigger_api_response.data.get(
"session", None
)
self.capture_id = self.util_response.trigger_api_response.data.get(
"id", None
)
LOGGER.debug("Extracted session_id: %s", self.session_id)
LOGGER.debug("Extracted capture_id: %s", self.capture_id)
def _on_open(self):
LOGGER.info("WebSocket connection opened")
# Start the max duration timer
self._timeout_handler(Timer.MAX_DURATION, TimerAction.START)
def _on_error(self, error):
LOGGER.error("Error: %s", error)
# Record the first WebSocket transport error so a consumer can tell an
# errored stream from a clean one (otherwise the error is only logged).
if self.util_response.ws_error is None:
self.util_response.ws_error = str(error)
def _on_close(self, code, msg):
LOGGER.info("WebSocket closed: %s - %s", code, msg)
self.util_response.ws_close_code = code
# An abnormal close (anything other than a normal 1000 / no-status close)
# is recorded as an error so a clean completion is distinguishable.
if code is not None and code != 1000 and self.util_response.ws_error is None:
self.util_response.ws_error = (
f"WebSocket closed abnormally (code {code}): {msg}"
)
self._stop_all_timers()
self.util_response._queue.put(None) # sentinel for receive()
self.util_response._closed.set() # signal completion
##########################################################################
## Helper methods for managing timers
def _timeout_handler(self, timer_type: Timer, action: TimerAction):
duration = self.timers[timer_type.value]["duration"]
if action == TimerAction.STOP or action == TimerAction.RESET:
if self.timers[timer_type.value]["thread"]:
LOGGER.debug("Stopping %s timer", timer_type.value)
self.timers[timer_type.value]["thread"].cancel()
self.timers[timer_type.value]["thread"] = None
elif action == TimerAction.STOP:
# Only warn when explicitly stopping (not resetting) a non-active timer
LOGGER.warning("%s timer is not active to stop", timer_type.value)
if action == TimerAction.START or action == TimerAction.RESET:
if self.ws:
LOGGER.debug(
"Starting %s timer with duration: %s seconds",
timer_type.value,
duration,
)
self.timers[timer_type.value]["thread"] = threading.Timer(
duration, self.ws.disconnect
)
self.timers[timer_type.value]["thread"].start()
else:
LOGGER.warning(
"WebSocket is not available to start %s timer", timer_type.value
)
def _stop_all_timers(self):
for timer_info in self.timers.values():
if timer_info["thread"]:
timer_info["thread"].cancel()
timer_info["thread"] = None
##########################################################################
## WebSocket event handlers
def _handle_message(self, msg):
if isinstance(msg, dict) and msg.get("event") == "channel_subscribed":
LOGGER.debug("channel_subscribed: %s", msg)
# Start the first message timeout timer when the channel is successfully subscribed
self._timeout_handler(Timer.FIRST_MESSAGE_TIMEOUT, TimerAction.START)
elif self._extract_session_id(msg):
# Stop the first message timeout timer on receiving the first message
if self.timers[Timer.FIRST_MESSAGE_TIMEOUT.value]["thread"]:
self._timeout_handler(Timer.FIRST_MESSAGE_TIMEOUT, TimerAction.STOP)
LOGGER.debug("data: %s", msg)
raw = self._extract_raw(msg)
if raw:
self.data.append(raw)
self.util_response._queue.put(raw) # feed receive() generator
if self._on_message_cb:
self._on_message_cb(raw)
self._timeout_handler(Timer.TIMEOUT, TimerAction.RESET)
##########################################################################
## Message processing and WebSocket connection management
def _extract_session_id(self, message) -> bool:
"""
Extracts the session_id from the message and compares it to the expected session_id.
This method is designed to handle messages that may have the session_id nested at
different levels.
If the expected session_id is None, it will accept all messages.
"""
if not self.session_id and not self.capture_id:
LOGGER.debug("No session_id or capture_id provided, accepting all messages")
return True
if isinstance(message, str):
LOGGER.debug("Trying to decode message: %s", message)
try:
message = json.loads(message)
except json.JSONDecodeError:
LOGGER.warning("Failed to decode message as JSON: %s", message)
return False
if isinstance(message, dict):
if message.get("event") == "data" and message.get("data"):
LOGGER.debug(
"Checking nested data for session_id or capture_id: %s",
message["data"],
)
return self._extract_session_id(message["data"])
if message.get("session") == self.session_id:
LOGGER.info(
"Message session_id matches expected session_id: %s",
self.session_id,
)
return True
if message.get("capture_id") == self.capture_id:
LOGGER.info(
"Message capture_id matches expected capture_id: %s",
self.capture_id,
)
return True
return False
def _extract_raw(self, message, root: bool = True):
"""
Extracts the raw message from the given message.
This method is designed to handle messages that may have the raw message nested at
different levels.
Handles both command events (with "raw" field) and pcap events (with "pcap_dict" field).
"""
if root:
self.raw_events.append(message)
event = message
if isinstance(event, str):
try:
event = json.loads(event)
except json.JSONDecodeError:
LOGGER.warning("Failed to decode message as JSON: %s", message)
return None
if isinstance(event, dict):
if event.get("event") == "data" and event.get("data"):
return self._extract_raw(event["data"], root=False)
if "raw" in event:
self.received_messages += 1
raw_value = event["raw"]
if isinstance(raw_value, str):
# Detect screen-mode (cursor positioning / clear-screen)
if not self._screen_mode and _SCREEN_MODE_RE.search(raw_value):
self._screen_mode = True
self._screen = _VT100Screen()
if self._screen_mode and self._screen is not None:
self._screen.feed(raw_value)
raw_value = self._screen.render()
else:
raw_value = _ANSI_ESCAPE_RE.sub("", raw_value)
LOGGER.debug("Extracted raw message: %s", raw_value)
return raw_value
if "pcap_dict" in event:
self.received_messages += 1
LOGGER.debug("Extracted pcap data: %s", event["pcap_dict"])
return event["pcap_dict"]
return None
##########################################################################
## WebSocket connection management
def start(self, ws) -> UtilResponse:
"""
Start the WS connection in the background and return immediately.
The returned ``UtilResponse`` collects data as it streams in. Use
``response.receive()``, ``response.wait()``, or the ``on_message``
callback to consume results.
PARAMS
-----------
ws : _MistWebsocket
An already-constructed WebSocket channel object.
"""
self.ws = ws
ws.on_message(self._handle_message)
ws.on_error(self._on_error)
ws.on_close(self._on_close)
ws.on_open(self._on_open)
# Wire up UtilResponse before starting WS
# _closed is already unset (in-progress) from UtilResponse.__init__
self.util_response.ws_required = True
self.util_response.ws_data = self.data # live list reference
self.util_response.ws_raw_events = self.raw_events
self.util_response._await_timeout = (
self.timers[Timer.MAX_DURATION.value]["duration"] + 10
)
self.util_response._disconnect_fn = ws.disconnect
ws.connect(run_in_background=True) # non-blocking
return self.util_response
def start_with_trigger(
self,
trigger_fn: Callable,
ws_factory_fn: Callable | None = None,
) -> UtilResponse:
"""
Run the trigger API call and optionally start a WebSocket stream.
If ``ws_factory_fn`` is provided, the trigger and WebSocket setup
run in a background thread (non-blocking). If ``ws_factory_fn`` is
``None``, the trigger runs synchronously so that
``trigger_api_response`` is immediately available on the returned
``UtilResponse``.
PARAMS
-----------
trigger_fn : Callable
A zero-argument callable that performs the REST API trigger and
returns an ``APIResponse``.
ws_factory_fn : Callable, optional
A one-argument callable that receives the trigger ``APIResponse``
and returns a WebSocket channel object (e.g. ``DeviceCmdEvents``).
If ``None``, no WebSocket is started and the ``UtilResponse``
completes as soon as the trigger finishes.
"""
if ws_factory_fn is None:
return self._trigger_only(trigger_fn)
def _run():
try:
trigger = trigger_fn()
self.util_response.trigger_api_response = trigger
if trigger.status_code == 200:
LOGGER.info("Trigger succeeded: %s", trigger.data)
self._extract_trigger_ids()
ws = ws_factory_fn(trigger)
if ws:
self.start(ws)
return # start() / _on_close manages _closed
LOGGER.error("WS factory returned None")
# The WebSocket leg was expected but never started; record it so
# consumers don't mistake this for a clean trigger-only completion.
self.util_response.ws_error = "WebSocket factory returned None"
else:
LOGGER.error(
"Failed to trigger command: %s - %s",
trigger.status_code,
trigger.data,
)
except Exception as e:
LOGGER.error("Error during trigger: %s", e)
if self.util_response.ws_error is None:
self.util_response.ws_error = f"WebSocket setup error: {e}"
# Mark done (failure or WS factory returned None)
self.util_response._queue.put(None)
self.util_response._closed.set()
threading.Thread(target=_run, daemon=True).start()
return self.util_response
def _trigger_only(self, trigger_fn: Callable) -> UtilResponse:
"""Run a trigger-only command synchronously (no WebSocket needed)."""
try:
trigger = trigger_fn()
self.util_response.trigger_api_response = trigger
if trigger.status_code == 200:
LOGGER.info("Trigger succeeded: %s", trigger.data)
else:
LOGGER.error(
"Failed to trigger command: %s - %s",
trigger.status_code,
trigger.data,
)
except Exception as e:
LOGGER.error("Error during trigger: %s", e)
self.util_response._queue.put(None)
self.util_response._closed.set()
return self.util_response