From 09b0e3fb1bda0e1b9e421e1e8adf4cc9523cb3b6 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Mon, 16 Nov 2020 02:24:02 +0100 Subject: [PATCH] [snmp] nullness improvements (#9038) Signed-off-by: Jan N. Klug --- .../snmp/internal/SnmpServiceImpl.java | 9 ++- .../snmp/internal/SnmpTargetHandler.java | 67 ++++++++++--------- .../config/SnmpChannelConfiguration.java | 13 ++-- .../config/SnmpServiceConfiguration.java | 3 + .../config/SnmpTargetConfiguration.java | 5 +- 5 files changed, 58 insertions(+), 39 deletions(-) diff --git a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpServiceImpl.java b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpServiceImpl.java index ef9360b28..f61edefe4 100644 --- a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpServiceImpl.java +++ b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpServiceImpl.java @@ -100,18 +100,21 @@ public class SnmpServiceImpl implements SnmpService { } private void shutdownSnmp() throws IOException { + DefaultUdpTransportMapping transport = this.transport; if (transport != null) { transport.close(); - transport = null; + this.transport = null; } + Snmp snmp = this.snmp; if (snmp != null) { snmp.close(); - snmp = null; + this.snmp = null; } } @Override public void addCommandResponder(CommandResponder listener) { + Snmp snmp = this.snmp; if (snmp != null) { snmp.addCommandResponder(listener); } @@ -120,6 +123,7 @@ public class SnmpServiceImpl implements SnmpService { @Override public void removeCommandResponder(CommandResponder listener) { + Snmp snmp = this.snmp; if (snmp != null) { snmp.removeCommandResponder(listener); } @@ -129,6 +133,7 @@ public class SnmpServiceImpl implements SnmpService { @Override public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener) throws IOException { + Snmp snmp = this.snmp; if (snmp != null) { snmp.send(pdu, target, userHandle, listener); logger.trace("send {} to {}", pdu, target); diff --git a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpTargetHandler.java b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpTargetHandler.java index 1e35357ff..81e32c6a2 100644 --- a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpTargetHandler.java +++ b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpTargetHandler.java @@ -207,9 +207,9 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe logger.trace("{} received {}", thing.getUID(), response); response.getVariableBindings().forEach(variable -> { - OID oid = variable.getOid(); - Variable value = variable.getVariable(); - updateChannels(oid, value, readChannelSet); + if (variable != null) { + updateChannels(variable.getOid(), variable.getVariable(), readChannelSet); + } }); } @@ -237,9 +237,9 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe if ((pdu.getType() == PDU.TRAP || pdu.getType() == PDU.V1TRAP) && config.community.equals(community) && targetAddressString.equals(address)) { pdu.getVariableBindings().forEach(variable -> { - OID oid = variable.getOid(); - Variable value = variable.getVariable(); - updateChannels(oid, value, trapChannelSet); + if (variable != null) { + updateChannels(variable.getOid(), variable.getVariable(), trapChannelSet); + } }); } } @@ -247,60 +247,65 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe private @Nullable SnmpInternalChannelConfiguration getChannelConfigFromChannel(Channel channel) { SnmpChannelConfiguration config = channel.getConfiguration().as(SnmpChannelConfiguration.class); - SnmpDatatype datatype; + String oid = config.oid; + if (oid == null) { + logger.warn("oid must not be null"); + return null; + } + + SnmpDatatype datatype = config.datatype; // maybe null, override later Variable onValue = null; Variable offValue = null; State exceptionValue = UnDefType.UNDEF; if (CHANNEL_TYPE_UID_NUMBER.equals(channel.getChannelTypeUID())) { - if (config.datatype == null) { + if (datatype == null) { datatype = SnmpDatatype.INT32; - } else if (config.datatype == SnmpDatatype.IPADDRESS || config.datatype == SnmpDatatype.STRING) { + } else if (datatype == SnmpDatatype.IPADDRESS || datatype == SnmpDatatype.STRING) { return null; - } else { - datatype = config.datatype; } - if (config.exceptionValue != null) { - exceptionValue = DecimalType.valueOf(config.exceptionValue); + String configExceptionValue = config.exceptionValue; + if (configExceptionValue != null) { + exceptionValue = DecimalType.valueOf(configExceptionValue); } } else if (CHANNEL_TYPE_UID_STRING.equals(channel.getChannelTypeUID())) { - if (config.datatype == null) { + if (datatype == null) { datatype = SnmpDatatype.STRING; - } else if (config.datatype != SnmpDatatype.IPADDRESS && config.datatype != SnmpDatatype.STRING - && config.datatype != SnmpDatatype.HEXSTRING) { + } else if (datatype != SnmpDatatype.IPADDRESS && datatype != SnmpDatatype.STRING + && datatype != SnmpDatatype.HEXSTRING) { return null; - } else { - datatype = config.datatype; } - if (config.exceptionValue != null) { - exceptionValue = StringType.valueOf(config.exceptionValue); + String configExceptionValue = config.exceptionValue; + if (configExceptionValue != null) { + exceptionValue = StringType.valueOf(configExceptionValue); } } else if (CHANNEL_TYPE_UID_SWITCH.equals(channel.getChannelTypeUID())) { - if (config.datatype == null) { + if (datatype == null) { datatype = SnmpDatatype.UINT32; - } else { - datatype = config.datatype; } try { - if (config.onvalue != null) { - onValue = convertDatatype(new StringType(config.onvalue), config.datatype); + final String configOnValue = config.onvalue; + if (configOnValue != null) { + onValue = convertDatatype(new StringType(configOnValue), datatype); } - if (config.offvalue != null) { - offValue = convertDatatype(new StringType(config.offvalue), config.datatype); + final String configOffValue = config.offvalue; + if (configOffValue != null) { + offValue = convertDatatype(new StringType(configOffValue), datatype); } } catch (IllegalArgumentException e) { logger.warn("illegal value configuration for channel {}", channel.getUID()); return null; } - if (config.exceptionValue != null) { - exceptionValue = OnOffType.from(config.exceptionValue); + String configExceptionValue = config.exceptionValue; + if (configExceptionValue != null) { + exceptionValue = OnOffType.from(configExceptionValue); } } else { logger.warn("unknown channel type found for channel {}", channel.getUID()); return null; } - return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(config.oid), config.mode, datatype, - onValue, offValue, exceptionValue, config.doNotLogException); + return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(oid), config.mode, datatype, onValue, + offValue, exceptionValue, config.doNotLogException); } private void generateChannelConfigs() { diff --git a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpChannelConfiguration.java b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpChannelConfiguration.java index 02d417973..d826bfe50 100644 --- a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpChannelConfiguration.java +++ b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpChannelConfiguration.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.snmp.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.snmp.internal.SnmpChannelMode; import org.openhab.binding.snmp.internal.SnmpDatatype; @@ -20,14 +22,15 @@ import org.openhab.binding.snmp.internal.SnmpDatatype; * * @author Jan N. Klug - Initial contribution */ +@NonNullByDefault public class SnmpChannelConfiguration { - public String oid; + public @Nullable String oid; public SnmpChannelMode mode = SnmpChannelMode.READ; - public SnmpDatatype datatype; + public @Nullable SnmpDatatype datatype; - public String onvalue; - public String offvalue; - public String exceptionValue; + public @Nullable String onvalue; + public @Nullable String offvalue; + public @Nullable String exceptionValue; public boolean doNotLogException = false; } diff --git a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpServiceConfiguration.java b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpServiceConfiguration.java index e58a7d6c9..c1e0829be 100644 --- a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpServiceConfiguration.java +++ b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpServiceConfiguration.java @@ -12,11 +12,14 @@ */ package org.openhab.binding.snmp.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * The {@link SnmpServiceConfiguration} class contains fields mapping binding configuration parameters. * * @author Jan N. Klug - Initial contribution */ +@NonNullByDefault public class SnmpServiceConfiguration { public int port = 0; } diff --git a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpTargetConfiguration.java b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpTargetConfiguration.java index d0e8d4e4c..e9f0a6778 100644 --- a/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpTargetConfiguration.java +++ b/bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpTargetConfiguration.java @@ -12,6 +12,8 @@ */ package org.openhab.binding.snmp.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.snmp.internal.SnmpProtocolVersion; /** @@ -19,8 +21,9 @@ import org.openhab.binding.snmp.internal.SnmpProtocolVersion; * * @author Jan N. Klug - Initial contribution */ +@NonNullByDefault public class SnmpTargetConfiguration { - public String hostname; + public @Nullable String hostname; public int port = 161; public String community = "public"; public int refresh = 60;