From 37d25ee55f2522eaae7874fc2bd41611e91767d8 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Thu, 7 Nov 2024 19:37:19 +0100 Subject: [PATCH] [snmp] make use of openHAB network configuration (#17699) * Network Signed-off-by: Leo Siepel Signed-off-by: Ciprian Pascu --- .../snmp/internal/SnmpServiceImpl.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 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 5182e7093af..b787c55bb09 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 @@ -13,20 +13,27 @@ package org.openhab.binding.snmp.internal; import java.io.IOException; +import java.net.InetAddress; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.snmp.internal.config.SnmpServiceConfiguration; import org.openhab.core.config.core.Configuration; +import org.openhab.core.net.CidrAddress; +import org.openhab.core.net.NetworkAddressChangeListener; +import org.openhab.core.net.NetworkAddressService; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Modified; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.snmp4j.CommandResponder; @@ -64,27 +71,31 @@ import org.snmp4j.transport.DefaultUdpTransportMapping; @NonNullByDefault @Component(configurationPid = "binding.snmp", service = SnmpService.class) -public class SnmpServiceImpl implements SnmpService { +public class SnmpServiceImpl implements SnmpService, NetworkAddressChangeListener { private final Logger logger = LoggerFactory.getLogger(SnmpServiceImpl.class); private @Nullable Snmp snmp; private @Nullable DefaultUdpTransportMapping transport; + private final NetworkAddressService networkAddressService; private final List listeners = new ArrayList<>(); private final Set userEntries = new HashSet<>(); + private Map config = new HashMap<>(); @Activate - public SnmpServiceImpl(Map config) { + public SnmpServiceImpl(Map config, @Reference NetworkAddressService networkAddressService) { addProtocols(); OctetString localEngineId = new OctetString(MPv3.createLocalEngineID()); USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0); SecurityModels.getInstance().addSecurityModel(usm); - + this.networkAddressService = networkAddressService; + networkAddressService.addNetworkAddressChangeListener(this); modified(config); } @Modified protected void modified(Map config) { + this.config = config; SnmpServiceConfiguration snmpCfg = new Configuration(config).as(SnmpServiceConfiguration.class); try { shutdownSnmp(); @@ -92,7 +103,10 @@ public class SnmpServiceImpl implements SnmpService { final DefaultUdpTransportMapping transport; if (snmpCfg.port > 0) { - transport = new DefaultUdpTransportMapping(new UdpAddress(snmpCfg.port), true); + InetAddress inetAddress = Objects.requireNonNullElse( + InetAddress.getByName(networkAddressService.getPrimaryIpv4HostAddress()), + InetAddress.getLocalHost()); + transport = new DefaultUdpTransportMapping(new UdpAddress(inetAddress, snmpCfg.port), true); } else { transport = new DefaultUdpTransportMapping(); } @@ -109,7 +123,7 @@ public class SnmpServiceImpl implements SnmpService { this.snmp = snmp; this.transport = transport; - logger.debug("initialized SNMP at {}", transport.getAddress()); + logger.info("Initialized SNMP service at {}", transport.getAddress()); } catch (IOException e) { logger.warn("could not open SNMP instance on port {}: {}", snmpCfg.port, e.getMessage()); } @@ -121,7 +135,7 @@ public class SnmpServiceImpl implements SnmpService { try { shutdownSnmp(); } catch (IOException e) { - logger.info("could not end SNMP: {}", e.getMessage()); + logger.warn("Could not end SNMP service: {}", e.getMessage()); } } @@ -222,4 +236,10 @@ public class SnmpServiceImpl implements SnmpService { this.user = user; } } + + @Override + public void onChanged(List added, List removed) { + logger.trace("SNMP reacting on network interface changes."); + modified(this.config); + } }