Skip to content

Commit

Permalink
resouce: ethernetport: convert to asyncio
Browse files Browse the repository at this point in the history
Follow the sync API deprecation and use an internal asyncio loop if no
external loop can be retrieved.

Signed-off-by: Rouven Czerwinski <[email protected]>
  • Loading branch information
Emantor committed Sep 24, 2024
1 parent d66d93b commit 717f1e8
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions labgrid/resource/ethernetport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ class SNMPSwitch:
hostname = attr.ib(validator=attr.validators.instance_of(str))

def __attrs_post_init__(self):

try:
# if called from async code, try to get current's thread loop
self.loop = asyncio.get_running_loop()
except RuntimeError:
# no previous, external or running loop found, create a new one
self.loop = asyncio.new_event_loop()

self.logger = logging.getLogger(f"{self}")
self.ports = {}
self.fdb = {}
self.macs_by_port = {}
self.transport = self.loop.run_until_complete(hlapi.UdpTransportTarget.create((self.hostname, 161)))
self._autodetect()

def _autodetect(self):
from pysnmp import hlapi
from pysnmp.hlapi.v3arch import asyncio as hlapi

for (errorIndication, errorStatus, _, varBindTable) in hlapi.getCmd(
for (errorIndication, errorStatus, _, varBindTable) in self.loop.run_until_complete(hlapi.getCmd(
hlapi.SnmpEngine(),
hlapi.CommunityData('public'),
hlapi.UdpTransportTarget((self.hostname, 161)),
self.transport,
hlapi.ContextData(),
hlapi.ObjectType(hlapi.ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))):
hlapi.ObjectType(hlapi.ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))):
if errorIndication:
raise Exception(f"snmp error {errorIndication}")
elif errorStatus:
Expand All @@ -51,7 +60,7 @@ def _get_ports(self):
Returns:
Dict[Dict[]]: ports and their values
"""
from pysnmp import hlapi
from pysnmp.hlapi.v3arch import asyncio as hlapi

variables = [
(hlapi.ObjectType(hlapi.ObjectIdentity('IF-MIB', 'ifIndex')), 'index'),
Expand All @@ -64,14 +73,14 @@ def _get_ports(self):
]
ports = {}

for (errorIndication, errorStatus, _, varBindTable) in hlapi.bulkCmd(
for (errorIndication, errorStatus, _, varBindTable) in self.loop.run_until_complete(hlapi.bulkCmd(
hlapi.SnmpEngine(),
hlapi.CommunityData('public'),
hlapi.UdpTransportTarget((self.hostname, 161)),
self.transport,
hlapi.ContextData(),
0, 20,
*[x[0] for x in variables],
lexicographicMode=False):
lexicographicMode=False)):
if errorIndication:
raise Exception(f"snmp error {errorIndication}")
elif errorStatus:
Expand All @@ -93,18 +102,18 @@ def _get_fdb_dot1d(self):
Returns:
Dict[List[str]]: ports and their values
"""
from pysnmp import hlapi
from pysnmp.hlapi.v3arch import asyncio as hlapi

ports = {}

for (errorIndication, errorStatus, _, varBindTable) in hlapi.bulkCmd(
for (errorIndication, errorStatus, _, varBindTable) in self.loop.run_until_complete(hlapi.bulkCmd(
hlapi.SnmpEngine(),
hlapi.CommunityData('public'),
hlapi.UdpTransportTarget((self.hostname, 161)),
self.transport,
hlapi.ContextData(),
0, 50,
hlapi.ObjectType(hlapi.ObjectIdentity('BRIDGE-MIB', 'dot1dTpFdbPort')),
lexicographicMode=False):
lexicographicMode=False)):
if errorIndication:
raise Exception(f"snmp error {errorIndication}")
elif errorStatus:
Expand All @@ -126,18 +135,18 @@ def _get_fdb_dot1q(self):
Returns:
Dict[List[str]]: ports and their values
"""
from pysnmp import hlapi
from pysnmp.hlapi.v3arch import asyncio as hlapi

ports = {}

for (errorIndication, errorStatus, _, varBindTable) in hlapi.bulkCmd(
for (errorIndication, errorStatus, _, varBindTable) in self.loop.run_until_complete(hlapi.bulkCmd(
hlapi.SnmpEngine(),
hlapi.CommunityData('public'),
hlapi.UdpTransportTarget((self.hostname, 161)),
self.transport,
hlapi.ContextData(),
0, 50,
hlapi.ObjectType(hlapi.ObjectIdentity('Q-BRIDGE-MIB', 'dot1qTpFdbPort')),
lexicographicMode=False):
lexicographicMode=False)):
if errorIndication:
raise Exception(f"snmp error {errorIndication}")
elif errorStatus:
Expand Down

0 comments on commit 717f1e8

Please sign in to comment.