|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Name: transceiver_event.py, version: 1.0 |
| 4 | +# |
| 5 | + |
| 6 | +try: |
| 7 | + import time |
| 8 | + import socket |
| 9 | + import re |
| 10 | + import os |
| 11 | + from collections import OrderedDict |
| 12 | +except ImportError as e: |
| 13 | + raise ImportError("%s - required module not found" % str(e)) |
| 14 | + |
| 15 | + |
| 16 | +class NetlinkEventMonitor(object): |
| 17 | + __instance = None |
| 18 | + |
| 19 | + def __new__(cls, *args, **kwargs): |
| 20 | + if not cls.__instance: |
| 21 | + # print(cls) |
| 22 | + cls.__instance = super(NetlinkEventMonitor, cls).__new__(cls) |
| 23 | + cls.__instance.__recieved_events = OrderedDict() |
| 24 | + return cls.__instance |
| 25 | + |
| 26 | + def __init__(self, timeout): |
| 27 | + # print('__init__', self) |
| 28 | + NETLINK_KOBJECT_UEVENT = 15 |
| 29 | + self.__socket = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, NETLINK_KOBJECT_UEVENT) |
| 30 | + self.__timeout = timeout |
| 31 | + |
| 32 | + def start(self): |
| 33 | + # print('start', self.__timeout) |
| 34 | + self.__socket.bind((os.getpid(), -1)) |
| 35 | + if 0 == self.__timeout: |
| 36 | + self.__socket.settimeout(None) |
| 37 | + else: |
| 38 | + self.__socket.settimeout(self.__timeout/1000.0) |
| 39 | + |
| 40 | + def stop(self): |
| 41 | + self.__socket.close() |
| 42 | + |
| 43 | + def __enter__(self): |
| 44 | + # print('__enter__', self) |
| 45 | + self.start() |
| 46 | + return self |
| 47 | + |
| 48 | + def __exit__(self, exc_type, exc_value, traceback): |
| 49 | + # print('__exit__', self) |
| 50 | + self.stop() |
| 51 | + |
| 52 | + def __iter__(self): |
| 53 | + # print('__iter__', self) |
| 54 | + while True: |
| 55 | + for item in self.next_events(): |
| 56 | + yield item |
| 57 | + |
| 58 | + def next_events(self): |
| 59 | + try: |
| 60 | + data = self.__socket.recv(16384) |
| 61 | + event = {} |
| 62 | + for item in data.split(b'\x00'): |
| 63 | + if not item: |
| 64 | + # check if we have an event and if we already received it |
| 65 | + if event and 'SEQNUM' in event: |
| 66 | + event_seqnum = event['SEQNUM'] |
| 67 | + if event_seqnum in self.__recieved_events: |
| 68 | + pass |
| 69 | + else: |
| 70 | + # print("=", event_seqnum) |
| 71 | + self.__recieved_events[event_seqnum] = event |
| 72 | + length = len(self.__recieved_events) |
| 73 | + # print("=", length) |
| 74 | + if (length > 100): |
| 75 | + self.__recieved_events.popitem(last=False) |
| 76 | + yield event |
| 77 | + event = {} |
| 78 | + else: |
| 79 | + try: |
| 80 | + k, v = item.split(b'=', 1) |
| 81 | + event[k.decode('ascii')] = v.decode('ascii') |
| 82 | + # print("=",k,v) |
| 83 | + except ValueError: |
| 84 | + pass |
| 85 | + except Exception: |
| 86 | + yield {} |
| 87 | + |
| 88 | +class TransceiverEvent(object): |
| 89 | + |
| 90 | + def __init__(self): |
| 91 | + pass |
| 92 | + |
| 93 | + def get_transceiver_change_event(self, timeout=0): |
| 94 | + port_dict = {} |
| 95 | + with NetlinkEventMonitor(timeout) as netlink_monitor: |
| 96 | + for event in netlink_monitor: |
| 97 | + if event and 'SUBSYSTEM' in event: |
| 98 | + if event['SUBSYSTEM'] == 'swps': |
| 99 | + #print('SWPS event. From %s, ACTION %s, IF_TYPE %s, IF_LANE %s' % (event['DEVPATH'], event['ACTION'], event['IF_TYPE'], event['IF_LANE'])) |
| 100 | + portname = event['DEVPATH'].split("/")[-1] |
| 101 | + rc = re.match(r"port(?P<num>\d+)",portname) |
| 102 | + if rc is not None: |
| 103 | + if event['ACTION'] == "remove": |
| 104 | + remove_num = int(rc.group("num")) |
| 105 | + port_dict[remove_num] = "0" |
| 106 | + elif event['ACTION'] == "add": |
| 107 | + add_num = int(rc.group("num")) |
| 108 | + port_dict[add_num] = "1" |
| 109 | + return True, port_dict |
| 110 | + else: |
| 111 | + return False, {} |
| 112 | + else: |
| 113 | + pass |
| 114 | + else: |
| 115 | + return True, {} |
| 116 | + |
0 commit comments