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
|
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from typing import Any, Dict, Tuple
from pysnmp.proto.api import v2c
from pysnmp.smi import builder, view
from pysnmp.smi.rfc1902 import NotificationType, ObjectIdentity, ObjectType
__all__ = ["CommandGeneratorVarBinds", "NotificationOriginatorVarBinds"]
def is_end_of_mib(var_binds): # noqa: N816
"""
Check if the given variable bindings indicate the end of the MIB.
Parameters:
var_binds (list): A list of variable bindings.
Returns:
bool: True if it is the end of the MIB, False otherwise.
"""
return not v2c.apiPDU.get_next_varbinds(var_binds)[1]
class MibViewControllerManager:
@staticmethod
def get_mib_view_controller(userCache):
try:
mibViewController = userCache["mibViewController"]
except KeyError:
mibViewController = view.MibViewController(builder.MibBuilder())
userCache["mibViewController"] = mibViewController
return mibViewController
class CommandGeneratorVarBinds(MibViewControllerManager):
"""Var-binds processor for Command Generator."""
def make_varbinds(
self, userCache: Dict[str, Any], varBinds: Tuple[ObjectType, ...]
) -> Tuple[ObjectType, ...]:
"""Return a tuple of ObjectType instances."""
mibViewController = self.get_mib_view_controller(userCache)
resolvedVarBinds = []
for varBind in varBinds:
if isinstance(varBind, ObjectType):
pass
elif isinstance(varBind[0], ObjectIdentity):
varBind = ObjectType(*varBind)
elif isinstance(varBind[0][0], tuple): # legacy
varBind = ObjectType(
ObjectIdentity(varBind[0][0][0], varBind[0][0][1], *varBind[0][1:]),
varBind[1],
)
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
resolvedVarBinds.append(
varBind.resolve_with_mib(mibViewController, ignoreErrors=False)
)
return tuple(resolvedVarBinds)
def unmake_varbinds(
self,
userCache: Dict[str, Any],
varBinds: Tuple[ObjectType, ...],
lookupMib=True,
) -> Tuple[ObjectType, ...]:
"""Return a tuple of ObjectType instances."""
if lookupMib:
mibViewController = self.get_mib_view_controller(userCache)
varBinds = tuple(
ObjectType(ObjectIdentity(x[0]), x[1]).resolve_with_mib(
mibViewController
)
for x in varBinds
)
return varBinds
class NotificationOriginatorVarBinds(MibViewControllerManager):
"""Var-binds processor for Notification Originator."""
def make_varbinds(
self, userCache: Dict[str, Any], varBinds: "tuple[NotificationType, ...]"
) -> "tuple[ObjectType, ...]":
"""Return a tuple of ObjectType instances."""
mibViewController = self.get_mib_view_controller(userCache)
# TODO: this shouldn't be needed
if isinstance(varBinds, NotificationType):
return varBinds.resolve_with_mib(
mibViewController, ignoreErrors=False
).to_varbinds()
resolvedVarBinds: "list[ObjectType]" = []
for varBind in varBinds:
if isinstance(varBind, NotificationType):
resolvedVarBinds.extend(
varBind.resolve_with_mib(
mibViewController, ignoreErrors=False
).to_varbinds()
)
continue
if isinstance(varBind, ObjectType):
pass
elif isinstance(varBind[0], ObjectIdentity):
varBind = ObjectType(*varBind)
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
resolvedVarBinds.append(
varBind.resolve_with_mib(mibViewController, ignoreErrors=False)
)
return tuple(resolvedVarBinds)
def unmake_varbinds(
self,
userCache: Dict[str, Any],
varBinds: "tuple[ObjectType, ...]",
lookupMib=False,
) -> "tuple[ObjectType, ...]":
"""Return a tuple of ObjectType instances."""
if lookupMib:
mibViewController = self.get_mib_view_controller(userCache)
varBinds = tuple(
ObjectType(ObjectIdentity(x[0]), x[1]).resolve_with_mib(
mibViewController
)
for x in varBinds
)
return varBinds
|