Skip to content
Snippets Groups Projects
Commit c1ad15c1 authored by Auke Klazema's avatar Auke Klazema
Browse files

L2SS-873: Refactor convertion using isinstance checks

parent 09f68ca3
Branches
Tags
1 merge request!398Resolve L2SS-873
......@@ -183,52 +183,64 @@ class snmp_attribute:
"""
get all the values in a list, make sure to convert specific types that dont want to play nicely
"""
vals = []
values = []
varBinds = var_binds[0]
for var_bind in varBinds:
value = self._convert_var_bind(var_bind)
vals.append(value)
value = self._convert_var_bind_value(var_bind[1])
values.append(value)
if self.is_scalar:
vals = vals[0]
values = values[0]
return vals
return values
def _convert_var_bind(self, var_bind):
# Some MIB's used custom types, some dont. Custom types are merely wrapped base types.
varbind_types = var_bind[1].__class__.__bases__ + (type(var_bind[1]),)
def _convert_var_bind_value(self, value):
def is_an_hlapi_integer(value):
return isinstance(value, (hlapi.Integer32, hlapi.Integer))
snmp_type = None
def is_an_hlapi_number_type(value):
return isinstance(value, (hlapi.TimeTicks, hlapi.Counter32, hlapi.Gauge32,
hlapi.Integer32, hlapi.Integer))
# find if one of the base types is present.
for i in varbind_types:
if i in snmp_to_numpy_dict.keys():
snmp_type = i
def is_an_hlapi_string_type(value):
return isinstance(value, (hlapi.OctetString, hlapi.ObjectIdentity))
if snmp_type is None:
raise TypeError(f"Error: did not find a valid snmp type. Got: {varbind_types}, expected one of: '{snmp_to_numpy_dict.keys()}'")
def needs_conversion_from_integer_to_str(value):
return is_an_hlapi_integer(value) and self.dtype == str
if snmp_type is hlapi.IpAddress:
# IpAddress values get printed as their raw value but in hex (7F 20 20 01 for 127.0.0.1 for example)
value = var_bind[1].prettyPrint()
def needs_conversion_from_ipaddress_to_str(value):
return isinstance(value, hlapi.IpAddress) and self.dtype == str
elif (snmp_type is hlapi.Integer32 or snmp_type is hlapi.Integer) and self.dtype == str:
# Integers can have 'named values', Where a value can be translated to a specific name. A dict basically
# Example: {1: "other", 2: "invalid", 3: "dynamic", 4: "static",}
def needs_conversion_from_number_to_int64(value):
return is_an_hlapi_number_type(value) and self.dtype == numpy.int64
if var_bind[1].namedValues == {}:
# An empty dict {} means no namedValue's are present.
value = snmp_to_numpy_dict[snmp_type](var_bind[1])
def needs_conversion_from_string_to_str(value):
return is_an_hlapi_string_type(value) and self.dtype == str
def convert_integer_to_str(value):
if value.namedValues:
result = value.prettyPrint()
else:
# append the named values string instead of the raw number.
value = var_bind[1].prettyPrint()
result = numpy.int64(value)
return result
if needs_conversion_from_ipaddress_to_str(value):
result = value.prettyPrint()
elif needs_conversion_from_integer_to_str(value):
result = convert_integer_to_str(value)
elif needs_conversion_from_number_to_int64(value):
result = numpy.int64(value) * self.scaling_factor
elif needs_conversion_from_string_to_str(value):
result = str(value)
else:
# convert from the funky pysnmp types to numpy types and then append
value = snmp_to_numpy_dict[snmp_type](var_bind[1]) * self.scaling_factor
raise TypeError(f"Error: did not find a valid snmp type. Got: {type(value)}, expected one of: '{snmp_to_numpy_dict.keys()}'")
return result
return value
class mib_loader:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment