mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[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:
parent
aa58d58009
commit
37d25ee55f
@ -13,20 +13,27 @@
|
|||||||
package org.openhab.binding.snmp.internal;
|
package org.openhab.binding.snmp.internal;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.binding.snmp.internal.config.SnmpServiceConfiguration;
|
import org.openhab.binding.snmp.internal.config.SnmpServiceConfiguration;
|
||||||
import org.openhab.core.config.core.Configuration;
|
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.Activate;
|
||||||
import org.osgi.service.component.annotations.Component;
|
import org.osgi.service.component.annotations.Component;
|
||||||
import org.osgi.service.component.annotations.Deactivate;
|
import org.osgi.service.component.annotations.Deactivate;
|
||||||
import org.osgi.service.component.annotations.Modified;
|
import org.osgi.service.component.annotations.Modified;
|
||||||
|
import org.osgi.service.component.annotations.Reference;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.snmp4j.CommandResponder;
|
import org.snmp4j.CommandResponder;
|
||||||
@ -64,27 +71,31 @@ import org.snmp4j.transport.DefaultUdpTransportMapping;
|
|||||||
|
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(configurationPid = "binding.snmp", service = SnmpService.class)
|
@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 final Logger logger = LoggerFactory.getLogger(SnmpServiceImpl.class);
|
||||||
|
|
||||||
private @Nullable Snmp snmp;
|
private @Nullable Snmp snmp;
|
||||||
private @Nullable DefaultUdpTransportMapping transport;
|
private @Nullable DefaultUdpTransportMapping transport;
|
||||||
|
private final NetworkAddressService networkAddressService;
|
||||||
|
|
||||||
private final List<CommandResponder> listeners = new ArrayList<>();
|
private final List<CommandResponder> listeners = new ArrayList<>();
|
||||||
private final Set<UserEntry> userEntries = new HashSet<>();
|
private final Set<UserEntry> userEntries = new HashSet<>();
|
||||||
|
private Map<String, Object> config = new HashMap<>();
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public SnmpServiceImpl(Map<String, Object> config) {
|
public SnmpServiceImpl(Map<String, Object> config, @Reference NetworkAddressService networkAddressService) {
|
||||||
addProtocols();
|
addProtocols();
|
||||||
OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
|
OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
|
||||||
USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0);
|
USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0);
|
||||||
SecurityModels.getInstance().addSecurityModel(usm);
|
SecurityModels.getInstance().addSecurityModel(usm);
|
||||||
|
this.networkAddressService = networkAddressService;
|
||||||
|
networkAddressService.addNetworkAddressChangeListener(this);
|
||||||
modified(config);
|
modified(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Modified
|
@Modified
|
||||||
protected void modified(Map<String, Object> config) {
|
protected void modified(Map<String, Object> config) {
|
||||||
|
this.config = config;
|
||||||
SnmpServiceConfiguration snmpCfg = new Configuration(config).as(SnmpServiceConfiguration.class);
|
SnmpServiceConfiguration snmpCfg = new Configuration(config).as(SnmpServiceConfiguration.class);
|
||||||
try {
|
try {
|
||||||
shutdownSnmp();
|
shutdownSnmp();
|
||||||
@ -92,7 +103,10 @@ public class SnmpServiceImpl implements SnmpService {
|
|||||||
final DefaultUdpTransportMapping transport;
|
final DefaultUdpTransportMapping transport;
|
||||||
|
|
||||||
if (snmpCfg.port > 0) {
|
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 {
|
} else {
|
||||||
transport = new DefaultUdpTransportMapping();
|
transport = new DefaultUdpTransportMapping();
|
||||||
}
|
}
|
||||||
@ -109,7 +123,7 @@ public class SnmpServiceImpl implements SnmpService {
|
|||||||
this.snmp = snmp;
|
this.snmp = snmp;
|
||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
|
|
||||||
logger.debug("initialized SNMP at {}", transport.getAddress());
|
logger.info("Initialized SNMP service at {}", transport.getAddress());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warn("could not open SNMP instance on port {}: {}", snmpCfg.port, e.getMessage());
|
logger.warn("could not open SNMP instance on port {}: {}", snmpCfg.port, e.getMessage());
|
||||||
}
|
}
|
||||||
@ -121,7 +135,7 @@ public class SnmpServiceImpl implements SnmpService {
|
|||||||
try {
|
try {
|
||||||
shutdownSnmp();
|
shutdownSnmp();
|
||||||
} catch (IOException e) {
|
} 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;
|
this.user = user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged(List<CidrAddress> added, List<CidrAddress> removed) {
|
||||||
|
logger.trace("SNMP reacting on network interface changes.");
|
||||||
|
modified(this.config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user