[snmp] make use of openHAB network configuration (#17699)

* Network

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
lsiepel 2024-11-07 19:37:19 +01:00 committed by Ciprian Pascu
parent aa58d58009
commit 37d25ee55f

View File

@ -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<CommandResponder> listeners = new ArrayList<>();
private final Set<UserEntry> userEntries = new HashSet<>();
private Map<String, Object> config = new HashMap<>();
@Activate
public SnmpServiceImpl(Map<String, Object> config) {
public SnmpServiceImpl(Map<String, Object> 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<String, Object> 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<CidrAddress> added, List<CidrAddress> removed) {
logger.trace("SNMP reacting on network interface changes.");
modified(this.config);
}
}