bug - XDSL adslAtucCurrOutputPwr exception (Cisco CSCvj53634) (#15614)

* Cisco CSCvj53634

tests

change RRD minimas to accept negative dbm output power

unnecessary tests

* unsignedAsSigned function

* use new function Number::unsignedAsSigned

* negative power for Vdsl as well

* style

* style

* Add types

* simplify and exception if over max value

* Add tests

* Style fixes

* Tell phpstan to shut up

---------

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
PipoCanaja
2023-12-15 17:06:52 +01:00
committed by GitHub
parent 45b3e0f624
commit 648a5ea7d3
3 changed files with 43 additions and 4 deletions

View File

@@ -137,6 +137,10 @@ class Xdsl implements Module
// Values are 1/10
foreach ($this->adslTenthValues as $oid) {
if (isset($data[$oid])) {
if ($oid == 'adslAtucCurrOutputPwr') {
// workaround Cisco Bug CSCvj53634
$data[$oid] = Number::unsignedAsSigned($data[$oid]);
}
$data[$oid] = $data[$oid] / 10;
}
}
@@ -212,12 +216,12 @@ class Xdsl implements Module
$rrd_def = RrdDefinition::make()
->addDataset('AtucCurrSnrMgn', 'GAUGE', 0, 635)
->addDataset('AtucCurrAtn', 'GAUGE', 0, 635)
->addDataset('AtucCurrOutputPwr', 'GAUGE', 0, 635)
->addDataset('AtucCurrOutputPwr', 'GAUGE', -100, 635)
->addDataset('AtucCurrAttainableR', 'GAUGE', 0)
->addDataset('AtucChanCurrTxRate', 'GAUGE', 0)
->addDataset('AturCurrSnrMgn', 'GAUGE', 0, 635)
->addDataset('AturCurrAtn', 'GAUGE', 0, 635)
->addDataset('AturCurrOutputPwr', 'GAUGE', 0, 635)
->addDataset('AturCurrOutputPwr', 'GAUGE', -100, 635)
->addDataset('AturCurrAttainableR', 'GAUGE', 0)
->addDataset('AturChanCurrTxRate', 'GAUGE', 0)
->addDataset('AtucPerfLofs', 'COUNTER', null, 100000000000)
@@ -298,8 +302,8 @@ class Xdsl implements Module
'ifName' => $os->ifIndexToName($ifIndex),
'rrd_name' => Rrd::portName($port->port_id, 'xdsl2LineStatusActAtp'),
'rrd_def' => RrdDefinition::make()
->addDataset('ds', 'GAUGE', 0)
->addDataset('us', 'GAUGE', 0),
->addDataset('ds', 'GAUGE', -100)
->addDataset('us', 'GAUGE', -100),
], [
'ds' => $data['xdsl2LineStatusActAtpDs'] ?? null,
'us' => $data['xdsl2LineStatusActAtpUs'] ?? null,

View File

@@ -175,4 +175,22 @@ class Number
return (int) ($number * (1024 ** $exponent));
}
public static function unsignedAsSigned(int $unsignedValue, int $bitLength = 32): int
{
// Maximum representable value for signed integer
$maxSignedValue = pow(2, $bitLength - 1) - 1;
// Check if the unsigned value is greater than the maximum representable unsigned value
// If so, convert it to its two's complement representation
if ($unsignedValue > $maxSignedValue) {
if ($unsignedValue > 2 ** $bitLength - 1) {
throw new \InvalidArgumentException('Unsigned value exceeds the maximum representable value of the give bit length: ' . $bitLength);
}
return $unsignedValue - ($maxSignedValue + 1) * 2;
}
return $unsignedValue;
}
}

View File

@@ -126,4 +126,21 @@ class FunctionsTest extends TestCase
$this->assertSame(1, Number::cast(1.0));
$this->assertSame(2, Number::cast('2.000'));
}
public function testNumberAsUnsigned()
{
$this->assertSame(42, Number::unsignedAsSigned('42')); /** @phpstan-ignore-line */
$this->assertSame(2147483647, Number::unsignedAsSigned(2147483647));
$this->assertSame(-2147483648, Number::unsignedAsSigned(2147483648));
$this->assertSame(-2147483647, Number::unsignedAsSigned(2147483649));
$this->assertSame(-1, Number::unsignedAsSigned(4294967295));
}
public function testNumberAsUnsignedValueExceedsMaxUnsignedValue()
{
$this->expectException(\InvalidArgumentException::class);
// Exceeds the maximum representable value for a 32-bit unsigned integer
Number::unsignedAsSigned(4294967296, 32);
}
}