Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

First version for SNMPClientProtocol. It's possible to receive SNMP trap messages and link them to attributes

Michael Rademakerea41f32d9d1a
diff --git a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientAgent.java b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientAgent.java
index 9314abdb9..072c30f6e 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientAgent.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientAgent.java
@@ -1,5 +1,6 @@
package org.openremote.agent.protocol.snmp;
+import com.fasterxml.jackson.annotation.JsonValue;
import org.openremote.agent.protocol.knx.KNXAgent;
import org.openremote.model.asset.agent.Agent;
import org.openremote.model.asset.agent.AgentDescriptor;
@@ -12,31 +13,20 @@ import javax.validation.constraints.NotNull;
import java.util.Optional;
@Entity
-public class SNMPClientAgent extends Agent<SNMPClientAgent, SNMPClientProtocol, AgentLink.Default> {
+public class SNMPClientAgent extends Agent<SNMPClientAgent, SNMPClientProtocol, SNMPClientAgent.SNMPAgentLink> {
public static class SNMPAgentLink extends AgentLink<SNMPClientAgent.SNMPAgentLink> {
- @NotNull
- protected String community;
@NotNull
protected String oid;
// For Hydrators
protected SNMPAgentLink() {}
- public SNMPAgentLink(String id, String community, String oid) {
+ public SNMPAgentLink(String id, String oid) {
super(id);
- this.community = community;
- this.oid = oid;
- }
-
- public Optional<String> getCommunity() {
- return Optional.ofNullable(community);
- }
- public SNMPClientAgent.SNMPAgentLink setCommunity(String community) {
- this.community = community;
- return this;
+ this.oid = oid;
}
public Optional<String> getOID() {
@@ -49,20 +39,37 @@ public class SNMPClientAgent extends Agent<SNMPClientAgent, SNMPClientProtocol,
}
}
- public enum SNMP_VERSION
+ public enum SNMPVersion
{
- V1,
- V2c,
- V3
+ V1(0),
+ V2c(1),
+ V3(3);
+
+ private final int version;
+
+ SNMPVersion(int version) {
+ this.version = version;
+ }
+
+ public int getVersion() {
+ return version;
+ }
+
+ @JsonValue
+ public String getValue() {
+ return toString();
+ }
}
- public static final ValueDescriptor<SNMP_VERSION> VALUE_SNMP_VERSION = new ValueDescriptor<>("sNMPVersion", SNMP_VERSION.class);
+ public static final ValueDescriptor<SNMPVersion> VALUE_SNMP_VERSION = new ValueDescriptor<>("sNMPVersion", SNMPVersion.class);
- public static final AttributeDescriptor<SNMP_VERSION> SNMP_VERSION = new AttributeDescriptor<>("sNMPVersionValue", VALUE_SNMP_VERSION);
+ public static final AttributeDescriptor<SNMPVersion> SNMP_VERSION = new AttributeDescriptor<>("sNMPVersionValue", VALUE_SNMP_VERSION);
+ public static final AttributeDescriptor<String> SNMP_HOST = HOST.withOptional(false);
+ public static final AttributeDescriptor<Integer> SNMP_PORT = PORT.withOptional(false);
- public static final AgentDescriptor<SNMPClientAgent, SNMPClientProtocol, AgentLink.Default> DESCRIPTOR = new AgentDescriptor<>(
- SNMPClientAgent.class, SNMPClientProtocol.class, AgentLink.Default.class
+ public static final AgentDescriptor<SNMPClientAgent, SNMPClientProtocol, SNMPAgentLink> DESCRIPTOR = new AgentDescriptor<>(
+ SNMPClientAgent.class, SNMPClientProtocol.class, SNMPAgentLink.class
);
/**
@@ -79,4 +86,13 @@ public class SNMPClientAgent extends Agent<SNMPClientAgent, SNMPClientProtocol,
public SNMPClientProtocol getProtocolInstance() {
return new SNMPClientProtocol(this);
}
+
+ public Optional<SNMPVersion> getSNMPVersion() {
+ return getAttributes().getValue(SNMP_VERSION);
+ }
+
+ public SNMPClientAgent setSNMPVersion(SNMPVersion version) {
+ getAttributes().getOrCreate(SNMP_VERSION).setValue(version);
+ return this;
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientProtocol.java
index 4a4c633cf..ecf67e083 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPClientProtocol.java
@@ -4,21 +4,30 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.snmp.SnmpMessage;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.Container;
-import org.openremote.model.asset.agent.AgentLink;
+import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.AttributeState;
import org.openremote.model.syslog.SyslogCategory;
import org.snmp4j.PDU;
-import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
import java.util.logging.Logger;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
-public class SNMPClientProtocol extends AbstractProtocol<SNMPClientAgent, AgentLink.Default> {
+/**
+ * This is a SNMP client protocol for receiving SNMP traps.
+ * <p>
+ * To use this protocol create a {@link SNMPClientAgent}.
+ */
+public class SNMPClientProtocol extends AbstractProtocol<SNMPClientAgent, SNMPClientAgent.SNMPAgentLink> {
public static final String PROTOCOL_DISPLAY_NAME = "SNMP Client";
protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, SNMPClientProtocol.class);
+ protected final Map<String, AttributeRef> oidMap = new HashMap<>();
public SNMPClientProtocol(SNMPClientAgent agent) {
super(agent);
@@ -29,6 +38,14 @@ public class SNMPClientProtocol extends AbstractProtocol<SNMPClientAgent, AgentL
return PROTOCOL_DISPLAY_NAME;
}
+ @Override
+ public String getProtocolInstanceUri() {
+ return String.format("snmp:%s:%d?protocol=udp&type=TRAP&snmpVersion=%d",
+ agent.getHost().orElse(""),
+ agent.getPort().orElse(162),
+ agent.getSNMPVersion().orElse(SNMPClientAgent.SNMPVersion.V2c).getVersion());
+ }
+
@Override
protected void doStart(Container container) throws Exception {
@@ -39,8 +56,8 @@ public class SNMPClientProtocol extends AbstractProtocol<SNMPClientAgent, AgentL
});
Integer snmpPort = agent.getPort().orElse(162);
-
- String snmpUri = String.format("snmp:%s:%d?protocol=udp&type=TRAP", snmpHost, snmpPort);
+ SNMPClientAgent.SNMPVersion snmpVersion = agent.getSNMPVersion().orElse(SNMPClientAgent.SNMPVersion.V2c);
+ String snmpUri = String.format("snmp:%s:%d?protocol=udp&type=TRAP&snmpVersion=%d", snmpHost, snmpPort, snmpVersion.getVersion());
messageBrokerContext.addRoutes(new RouteBuilder() {
@Override
@@ -48,38 +65,45 @@ public class SNMPClientProtocol extends AbstractProtocol<SNMPClientAgent, AgentL
from(snmpUri)
.routeId(getProtocolName() + getAgent().getId())
.process(exchange -> {
-
// Since we are using Snmp, we get SnmpMessage object, we need to typecast from Message
SnmpMessage msg = (SnmpMessage) exchange.getIn();
PDU pdu = msg.getSnmpMessage();
- pdu.getVariableBindings().forEach(variableBinding -> LOG.info(variableBinding.toValueString()));
+ pdu.getVariableBindings().forEach(variableBinding -> {
+ AttributeRef attributeRef = oidMap.get(variableBinding.getOid().format());
+ if (attributeRef != null) {
+ updateLinkedAttribute(new AttributeState(attributeRef, variableBinding.toValueString()));
+ }
+ });
});
}
});
- }
-
- @Override
- protected void doStop(Container container) throws Exception {
+ setConnectionStatus(ConnectionStatus.CONNECTED);
}
@Override
- protected void doLinkAttribute(String assetId, Attribute<?> attribute, AgentLink.Default agentLink) throws RuntimeException {
-
+ protected void doStop(Container container) throws Exception {
+ setConnectionStatus(ConnectionStatus.STOPPED);
}
@Override
- protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, AgentLink.Default agentLink) {
+ protected void doLinkAttribute(String assetId, Attribute<?> attribute, SNMPClientAgent.SNMPAgentLink agentLink) throws RuntimeException {
+ String oid = agentLink.getOID().orElseThrow(() -> {
+ String msg = "No OID provided for protocol: " + this;
+ LOG.info(msg);
+ return new IllegalArgumentException(msg);
+ });
+ oidMap.put(oid, new AttributeRef(assetId, attribute.getName()));
}
@Override
- protected void doLinkedAttributeWrite(Attribute<?> attribute, AgentLink.Default agentLink, AttributeEvent event, Object processedValue) {
-
+ protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, SNMPClientAgent.SNMPAgentLink agentLink) {
+ agentLink.getOID().ifPresent(oidMap::remove);
}
@Override
- public String getProtocolInstanceUri() {
- return null;
+ protected void doLinkedAttributeWrite(Attribute<?> attribute, SNMPClientAgent.SNMPAgentLink agentLink, AttributeEvent event, Object processedValue) {
+ // Nothing to do here
}
}