]> git.basschouten.com Git - openhab-addons.git/commitdiff
[snmp] nullness improvements (#9038)
authorJ-N-K <J-N-K@users.noreply.github.com>
Mon, 16 Nov 2020 01:24:02 +0000 (02:24 +0100)
committerGitHub <noreply@github.com>
Mon, 16 Nov 2020 01:24:02 +0000 (17:24 -0800)
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpServiceImpl.java
bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/SnmpTargetHandler.java
bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpChannelConfiguration.java
bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpServiceConfiguration.java
bundles/org.openhab.binding.snmp/src/main/java/org/openhab/binding/snmp/internal/config/SnmpTargetConfiguration.java

index ef9360b28fd53f0c93f3367a958128b5d29d4ab6..f61edefe44649860b73c97564c6704b8142649ba 100644 (file)
@@ -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);
index 1e35357fff0a82e853b4dc39057f525fa0dac2ce..81e32c6a23f443a715ac5c2a6592a27cfaab2882 100644 (file)
@@ -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() {
index 02d4179739e1ad336bcf4386237215c53d329f49..d826bfe50f3b15afadf4c942492e3d50b7fec2df 100644 (file)
@@ -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;
 }
index e58a7d6c9d9a8cc08b5b815e9be017e779197711..c1e0829be0538eec55fda4f6e5c38ed4da01e14d 100644 (file)
  */
 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;
 }
index d0e8d4e4c7e238652725055e588b889d80495334..e9f0a67787b40555c2edee1d39f16038d6379dd5 100644 (file)
@@ -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;