diff --git a/tangostationcontrol/tangostationcontrol/clients/snmp_client.py b/tangostationcontrol/tangostationcontrol/clients/snmp_client.py index ecb84074cc8c3c8487af2e27600456f14a833e4b..4826bbe599b3ac335cc9b5b689726fbab334015d 100644 --- a/tangostationcontrol/tangostationcontrol/clients/snmp_client.py +++ b/tangostationcontrol/tangostationcontrol/clients/snmp_client.py @@ -149,12 +149,34 @@ class snmp_attribute: self.name = name self.idx = idx self.dtype = dtype - self.dim_x = dim_x - self.dim_y = dim_y - self.is_scalar = (self.dim_x + self.dim_y) == 1 + + self.is_scalar = (dim_x + dim_y) == 1 + self.len = self.get_len(dim_x, dim_y) self.objID = self.create_objID() + + + def get_len(self, dim_x, dim_y): + """""Small helper function to not clutter the __init__""" + + if dim_x == 0: + dim_x = 1 + if dim_y == 0: + dim_y = 1 + return dim_x * dim_y + + def create_objID(self): + + # only scalars can be used at the present time. + if not self.is_scalar: + objID = tuple(hlapi.ObjectIdentity(self.mib, self.name, self.idx + i) for i in range(self.len)) + + else: + objID = hlapi.ObjectIdentity(self.mib, self.name, self.idx) + + return objID + def next_wrap(self, cmd): """ This function exists to allow the next(cmd) call to be mocked for unit testing. As the @@ -165,8 +187,6 @@ class snmp_attribute: """ Read function we give to the attribute wrapper """ - - # must be recreated for each read it seems self.objs = tuple(hlapi.ObjectType(i) for i in self.objID) # get the thingy to get the values @@ -193,6 +213,7 @@ class snmp_attribute: write_obj = tuple(hlapi.ObjectType(self.objID[i], value[i]) for i in range(len(self.objID))) set_cmd = hlapi.setCmd(self.client.engine, self.client.community, self.client.trasport, self.client.ctx_data, *write_obj) + errorIndication, errorStatus, errorIndex, *varBinds = self.next_wrap(set_cmd) def convert(self, varBinds): @@ -221,16 +242,4 @@ class snmp_attribute: return vals - def create_objID(self): - - # only scalars can be used at the present time. - if not self.is_scalar: - # tuple(hlapi.ObjectIdentity(mib, name, idx) for i in range(len(oids))) - - raise ValueError(f"MIB + name type attributes can only be scalars, got dimensions of: ({x}, {y})") - else: - objID = hlapi.ObjectIdentity(self.mib, self.name, self.idx) - - return objID - diff --git a/tangostationcontrol/tangostationcontrol/test/clients/test_snmp_client.py b/tangostationcontrol/tangostationcontrol/test/clients/test_snmp_client.py index be185cdd3bc098625c44957ba2756d18b5d6acc4..1e7854bf2c2123ce90b15c0be0982b711e30e04b 100644 --- a/tangostationcontrol/tangostationcontrol/test/clients/test_snmp_client.py +++ b/tangostationcontrol/tangostationcontrol/test/clients/test_snmp_client.py @@ -22,8 +22,7 @@ class server_imitator: # shortcut for testing dimensionality dim_list = { "scalar": (1, 0), - #NOTE: spectrum not supported currently - #"spectrum": (4, 0), + "spectrum": (4, 0), } def get_return_val(self, snmp_type : type, dims : tuple): @@ -132,9 +131,10 @@ class TestSNMP(base.TestCase): self.assertEqual(checkval, val, f"Expected: {checkval}, got: {val}") @mock.patch('pysnmp.hlapi.ObjectIdentity') + @mock.patch('pysnmp.hlapi.ObjectType') @mock.patch('pysnmp.hlapi.setCmd') @mock.patch('tangostationcontrol.clients.snmp_client.snmp_attribute.next_wrap') - def test_snmp_obj_set(self, m_next, m_nextCmd, m_obj_i): + def test_snmp_obj_set(self, m_next, m_nextCmd, m_obj_T, m_obj_ID): """ Attempts to write a value to an SNMP server, but instead intercepts it and compared whether the values is as expected. """