[snmp] nullness improvements (#9038)

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-11-16 02:24:02 +01:00 committed by GitHub
parent 1e5aaf948c
commit 09b0e3fb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 39 deletions

View File

@ -100,18 +100,21 @@ public class SnmpServiceImpl implements SnmpService {
} }
private void shutdownSnmp() throws IOException { private void shutdownSnmp() throws IOException {
DefaultUdpTransportMapping transport = this.transport;
if (transport != null) { if (transport != null) {
transport.close(); transport.close();
transport = null; this.transport = null;
} }
Snmp snmp = this.snmp;
if (snmp != null) { if (snmp != null) {
snmp.close(); snmp.close();
snmp = null; this.snmp = null;
} }
} }
@Override @Override
public void addCommandResponder(CommandResponder listener) { public void addCommandResponder(CommandResponder listener) {
Snmp snmp = this.snmp;
if (snmp != null) { if (snmp != null) {
snmp.addCommandResponder(listener); snmp.addCommandResponder(listener);
} }
@ -120,6 +123,7 @@ public class SnmpServiceImpl implements SnmpService {
@Override @Override
public void removeCommandResponder(CommandResponder listener) { public void removeCommandResponder(CommandResponder listener) {
Snmp snmp = this.snmp;
if (snmp != null) { if (snmp != null) {
snmp.removeCommandResponder(listener); snmp.removeCommandResponder(listener);
} }
@ -129,6 +133,7 @@ public class SnmpServiceImpl implements SnmpService {
@Override @Override
public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener) public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener)
throws IOException { throws IOException {
Snmp snmp = this.snmp;
if (snmp != null) { if (snmp != null) {
snmp.send(pdu, target, userHandle, listener); snmp.send(pdu, target, userHandle, listener);
logger.trace("send {} to {}", pdu, target); logger.trace("send {} to {}", pdu, target);

View File

@ -207,9 +207,9 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe
logger.trace("{} received {}", thing.getUID(), response); logger.trace("{} received {}", thing.getUID(), response);
response.getVariableBindings().forEach(variable -> { response.getVariableBindings().forEach(variable -> {
OID oid = variable.getOid(); if (variable != null) {
Variable value = variable.getVariable(); updateChannels(variable.getOid(), variable.getVariable(), readChannelSet);
updateChannels(oid, value, 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) if ((pdu.getType() == PDU.TRAP || pdu.getType() == PDU.V1TRAP) && config.community.equals(community)
&& targetAddressString.equals(address)) { && targetAddressString.equals(address)) {
pdu.getVariableBindings().forEach(variable -> { pdu.getVariableBindings().forEach(variable -> {
OID oid = variable.getOid(); if (variable != null) {
Variable value = variable.getVariable(); updateChannels(variable.getOid(), variable.getVariable(), trapChannelSet);
updateChannels(oid, value, trapChannelSet); }
}); });
} }
} }
@ -247,60 +247,65 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe
private @Nullable SnmpInternalChannelConfiguration getChannelConfigFromChannel(Channel channel) { private @Nullable SnmpInternalChannelConfiguration getChannelConfigFromChannel(Channel channel) {
SnmpChannelConfiguration config = channel.getConfiguration().as(SnmpChannelConfiguration.class); 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 onValue = null;
Variable offValue = null; Variable offValue = null;
State exceptionValue = UnDefType.UNDEF; State exceptionValue = UnDefType.UNDEF;
if (CHANNEL_TYPE_UID_NUMBER.equals(channel.getChannelTypeUID())) { if (CHANNEL_TYPE_UID_NUMBER.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) { if (datatype == null) {
datatype = SnmpDatatype.INT32; datatype = SnmpDatatype.INT32;
} else if (config.datatype == SnmpDatatype.IPADDRESS || config.datatype == SnmpDatatype.STRING) { } else if (datatype == SnmpDatatype.IPADDRESS || datatype == SnmpDatatype.STRING) {
return null; return null;
} else {
datatype = config.datatype;
} }
if (config.exceptionValue != null) { String configExceptionValue = config.exceptionValue;
exceptionValue = DecimalType.valueOf(config.exceptionValue); if (configExceptionValue != null) {
exceptionValue = DecimalType.valueOf(configExceptionValue);
} }
} else if (CHANNEL_TYPE_UID_STRING.equals(channel.getChannelTypeUID())) { } else if (CHANNEL_TYPE_UID_STRING.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) { if (datatype == null) {
datatype = SnmpDatatype.STRING; datatype = SnmpDatatype.STRING;
} else if (config.datatype != SnmpDatatype.IPADDRESS && config.datatype != SnmpDatatype.STRING } else if (datatype != SnmpDatatype.IPADDRESS && datatype != SnmpDatatype.STRING
&& config.datatype != SnmpDatatype.HEXSTRING) { && datatype != SnmpDatatype.HEXSTRING) {
return null; return null;
} else {
datatype = config.datatype;
} }
if (config.exceptionValue != null) { String configExceptionValue = config.exceptionValue;
exceptionValue = StringType.valueOf(config.exceptionValue); if (configExceptionValue != null) {
exceptionValue = StringType.valueOf(configExceptionValue);
} }
} else if (CHANNEL_TYPE_UID_SWITCH.equals(channel.getChannelTypeUID())) { } else if (CHANNEL_TYPE_UID_SWITCH.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) { if (datatype == null) {
datatype = SnmpDatatype.UINT32; datatype = SnmpDatatype.UINT32;
} else {
datatype = config.datatype;
} }
try { try {
if (config.onvalue != null) { final String configOnValue = config.onvalue;
onValue = convertDatatype(new StringType(config.onvalue), config.datatype); if (configOnValue != null) {
onValue = convertDatatype(new StringType(configOnValue), datatype);
} }
if (config.offvalue != null) { final String configOffValue = config.offvalue;
offValue = convertDatatype(new StringType(config.offvalue), config.datatype); if (configOffValue != null) {
offValue = convertDatatype(new StringType(configOffValue), datatype);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
logger.warn("illegal value configuration for channel {}", channel.getUID()); logger.warn("illegal value configuration for channel {}", channel.getUID());
return null; return null;
} }
if (config.exceptionValue != null) { String configExceptionValue = config.exceptionValue;
exceptionValue = OnOffType.from(config.exceptionValue); if (configExceptionValue != null) {
exceptionValue = OnOffType.from(configExceptionValue);
} }
} else { } else {
logger.warn("unknown channel type found for channel {}", channel.getUID()); logger.warn("unknown channel type found for channel {}", channel.getUID());
return null; return null;
} }
return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(config.oid), config.mode, datatype, return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(oid), config.mode, datatype, onValue,
onValue, offValue, exceptionValue, config.doNotLogException); offValue, exceptionValue, config.doNotLogException);
} }
private void generateChannelConfigs() { private void generateChannelConfigs() {

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.binding.snmp.internal.config; 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.SnmpChannelMode;
import org.openhab.binding.snmp.internal.SnmpDatatype; import org.openhab.binding.snmp.internal.SnmpDatatype;
@ -20,14 +22,15 @@ import org.openhab.binding.snmp.internal.SnmpDatatype;
* *
* @author Jan N. Klug - Initial contribution * @author Jan N. Klug - Initial contribution
*/ */
@NonNullByDefault
public class SnmpChannelConfiguration { public class SnmpChannelConfiguration {
public String oid; public @Nullable String oid;
public SnmpChannelMode mode = SnmpChannelMode.READ; public SnmpChannelMode mode = SnmpChannelMode.READ;
public SnmpDatatype datatype; public @Nullable SnmpDatatype datatype;
public String onvalue; public @Nullable String onvalue;
public String offvalue; public @Nullable String offvalue;
public String exceptionValue; public @Nullable String exceptionValue;
public boolean doNotLogException = false; public boolean doNotLogException = false;
} }

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.binding.snmp.internal.config; package org.openhab.binding.snmp.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* The {@link SnmpServiceConfiguration} class contains fields mapping binding configuration parameters. * The {@link SnmpServiceConfiguration} class contains fields mapping binding configuration parameters.
* *
* @author Jan N. Klug - Initial contribution * @author Jan N. Klug - Initial contribution
*/ */
@NonNullByDefault
public class SnmpServiceConfiguration { public class SnmpServiceConfiguration {
public int port = 0; public int port = 0;
} }

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.binding.snmp.internal.config; 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; import org.openhab.binding.snmp.internal.SnmpProtocolVersion;
/** /**
@ -19,8 +21,9 @@ import org.openhab.binding.snmp.internal.SnmpProtocolVersion;
* *
* @author Jan N. Klug - Initial contribution * @author Jan N. Klug - Initial contribution
*/ */
@NonNullByDefault
public class SnmpTargetConfiguration { public class SnmpTargetConfiguration {
public String hostname; public @Nullable String hostname;
public int port = 161; public int port = 161;
public String community = "public"; public String community = "public";
public int refresh = 60; public int refresh = 60;