This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new 14ad219f871d [backport camel-4.22.x] CAMEL-27224: camel-snmp - the
trap consumer receives only the traps of its snmpVersion, and for SNMPv3 of its
user and security level (#30408)
14ad219f871d is described below
commit 14ad219f871d05c1d2ef22b773203f1eff7eee12
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 14:12:02 2026 +0100
[backport camel-4.22.x] CAMEL-27224: camel-snmp - the trap consumer
receives only the traps of its snmpVersion, and for SNMPv3 of its user and
security level (#30408)
The trap consumer receives only the traps of the snmpVersion of its
endpoint. With snmpVersion 0 (default) or 1 it receives SNMPv1 and SNMPv2c
traps and drops SNMPv3 traps. With snmpVersion 3 it receives only the
SNMPv3 traps of the user in securityName, checked with the authentication
and privacy options of the endpoint and with at least securityLevel, using
its own message dispatcher and USM instead of the JVM-wide SecurityModels
of SNMP4J. Dropped traps are logged at DEBUG. Upgrade guide entry for 4.24.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4dc8c2d888ae3cae8feff1fed38c7467aacf97c6)
---
.../apache/camel/catalog/docs/snmp-component.adoc | 15 ++
.../camel-snmp/src/main/docs/snmp-component.adoc | 15 ++
.../apache/camel/component/snmp/SnmpHelper.java | 12 +-
.../camel/component/snmp/SnmpTrapConsumer.java | 66 ++++-
.../component/snmp/TrapConsumerVersionTest.java | 285 +++++++++++++++++++++
5 files changed, 390 insertions(+), 3 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc
index 2f0eda76b8e8..dd3453998109 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc
@@ -127,6 +127,21 @@ snmp:127.0.0.1:162?protocol=udp&type=TRAP
You can get the community of SNMP TRAP with message
header `securityName`, peer address of the SNMP TRAP with message header
`peerAddress`.
+The trap receiver receives only the traps of its `snmpVersion`:
+
+* With `snmpVersion` 0 (the default) or 1, it receives SNMPv1 and SNMPv2c
traps, whatever their community. The route
+can check the community in the `securityName` header.
+* With `snmpVersion` 3, it receives only the SNMPv3 traps of the user set in
`securityName`, with at least the
+`securityLevel` of the endpoint (the default is 3, authentication and
privacy). The traps are checked and decrypted with
+the `authenticationProtocol`, `authenticationPassphrase`, `privacyProtocol`
and `privacyPassphrase` of the endpoint.
+These protocols must be registered in the
`org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with
+`SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA())`.
+
+------------------------------------------------------------------------------------------------------------------
+snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=camel&securityLevel=3
+
&authenticationProtocol=SHA1&authenticationPassphrase=RAW(...)&privacyProtocol=AES128&privacyPassphrase=RAW(...)
+------------------------------------------------------------------------------------------------------------------
+
Routing example in Java: (converts the SNMP PDU to XML String)
[tabs]
diff --git a/components/camel-snmp/src/main/docs/snmp-component.adoc
b/components/camel-snmp/src/main/docs/snmp-component.adoc
index 2f0eda76b8e8..dd3453998109 100644
--- a/components/camel-snmp/src/main/docs/snmp-component.adoc
+++ b/components/camel-snmp/src/main/docs/snmp-component.adoc
@@ -127,6 +127,21 @@ snmp:127.0.0.1:162?protocol=udp&type=TRAP
You can get the community of SNMP TRAP with message
header `securityName`, peer address of the SNMP TRAP with message header
`peerAddress`.
+The trap receiver receives only the traps of its `snmpVersion`:
+
+* With `snmpVersion` 0 (the default) or 1, it receives SNMPv1 and SNMPv2c
traps, whatever their community. The route
+can check the community in the `securityName` header.
+* With `snmpVersion` 3, it receives only the SNMPv3 traps of the user set in
`securityName`, with at least the
+`securityLevel` of the endpoint (the default is 3, authentication and
privacy). The traps are checked and decrypted with
+the `authenticationProtocol`, `authenticationPassphrase`, `privacyProtocol`
and `privacyPassphrase` of the endpoint.
+These protocols must be registered in the
`org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with
+`SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA())`.
+
+------------------------------------------------------------------------------------------------------------------
+snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=camel&securityLevel=3
+
&authenticationProtocol=SHA1&authenticationPassphrase=RAW(...)&privacyProtocol=AES128&privacyPassphrase=RAW(...)
+------------------------------------------------------------------------------------------------------------------
+
Routing example in Java: (converts the SNMP PDU to XML String)
[tabs]
diff --git
a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java
b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java
index ee2636af0dda..7521c611047e 100644
---
a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java
+++
b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java
@@ -77,12 +77,20 @@ final class SnmpHelper {
}
private static USM internalCreateAndSetUSM(SnmpEndpoint endpoint) {
+ USM usm = createUSM(endpoint);
+ SecurityModels.getInstance().addSecurityModel(usm);
+ return usm;
+ }
+
+ /**
+ * Creates a USM with the user of the endpoint, without registering it in
the JVM-wide {@link SecurityModels}.
+ */
+ static USM createUSM(SnmpEndpoint endpoint) {
if (endpoint.getSecurityName() == null) {
- throw new IllegalArgumentException("SecurityNme is required for
SNMP v3");
+ throw new IllegalArgumentException("SecurityName is required for
SNMP v3");
}
USM usm = new USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()), 0);
- SecurityModels.getInstance().addSecurityModel(usm);
OID authProtocol =
convertAuthenticationProtocol(endpoint.getAuthenticationProtocol());
diff --git
a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java
b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java
index 849401607b10..4899cdedb3a6 100644
---
a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java
+++
b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.snmp;
+import java.util.Arrays;
+
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;
@@ -24,14 +26,22 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.MessageException;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
+import org.snmp4j.mp.MPv1;
+import org.snmp4j.mp.MPv2c;
+import org.snmp4j.mp.MPv3;
+import org.snmp4j.mp.MessageProcessingModel;
+import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.mp.StateReference;
import org.snmp4j.mp.StatusInformation;
+import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
@@ -71,7 +81,7 @@ public class SnmpTrapConsumer extends DefaultConsumer
implements CommandResponde
}
// NOTE: the object is going to be closed by stop() lifecycle.
- this.snmp = new Snmp(transport); // NOSONAR
+ this.snmp = new Snmp(createMessageDispatcher(), transport); // NOSONAR
snmp.addCommandResponder(this);
// listen to the transport
@@ -84,6 +94,57 @@ public class SnmpTrapConsumer extends DefaultConsumer
implements CommandResponde
}
}
+ /**
+ * Creates a message dispatcher with only the message processing models of
the configured snmpVersion: SNMPv1 and
+ * SNMPv2c for the versions 0 and 1, and SNMPv3 with a USM of its own that
holds only the user of the endpoint for
+ * the version 3.
+ */
+ private MessageDispatcherImpl createMessageDispatcher() {
+ MessageDispatcherImpl dispatcher = new MessageDispatcherImpl();
+ int version = endpoint.getSnmpVersion();
+ if (version == SnmpConstants.version1 || version ==
SnmpConstants.version2c) {
+ dispatcher.addMessageProcessingModel(new MPv1());
+ dispatcher.addMessageProcessingModel(new MPv2c());
+ } else if (version == SnmpConstants.version3) {
+ USM usm = SnmpHelper.createUSM(endpoint);
+ dispatcher.addMessageProcessingModel(new MPv3(usm));
+ } else {
+ throw new IllegalArgumentException("Unknown snmpVersion: " +
version);
+ }
+ return dispatcher;
+ }
+
+ /**
+ * Whether the event matches the configured snmpVersion and, for SNMPv3,
the security name and the minimum security
+ * level of the endpoint.
+ */
+ private boolean isAccepted(CommandResponderEvent event) {
+ int version = endpoint.getSnmpVersion();
+ int model = event.getMessageProcessingModel();
+ if (version == SnmpConstants.version3) {
+ if (model != MessageProcessingModel.MPv3) {
+ LOG.debug("Dropping trap from {}: message processing model {}
is not SNMPv3", event.getPeerAddress(), model);
+ return false;
+ }
+ if (!Arrays.equals(new
OctetString(endpoint.getSecurityName()).getValue(), event.getSecurityName())) {
+ LOG.debug("Dropping trap from {}: security name is not the one
of the endpoint", event.getPeerAddress());
+ return false;
+ }
+ if (event.getSecurityLevel() < endpoint.getSecurityLevel()) {
+ LOG.debug("Dropping trap from {}: security level {} is below
the security level {} of the endpoint",
+ event.getPeerAddress(), event.getSecurityLevel(),
endpoint.getSecurityLevel());
+ return false;
+ }
+ return true;
+ }
+ if (model != MessageProcessingModel.MPv1 && model !=
MessageProcessingModel.MPv2c) {
+ LOG.debug("Dropping trap from {}: message processing model {} is
not SNMPv1 or SNMPv2c", event.getPeerAddress(),
+ model);
+ return false;
+ }
+ return true;
+ }
+
@Override
protected void doStop() throws Exception {
// stop listening to the transport
@@ -101,6 +162,9 @@ public class SnmpTrapConsumer extends DefaultConsumer
implements CommandResponde
@Override
public void processPdu(CommandResponderEvent event) {
+ if (!isAccepted(event)) {
+ return;
+ }
PDU pdu = event.getPDU();
// check PDU not null
if (pdu != null) {
diff --git
a/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TrapConsumerVersionTest.java
b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TrapConsumerVersionTest.java
new file mode 100644
index 000000000000..d2e3c58effef
--- /dev/null
+++
b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TrapConsumerVersionTest.java
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.snmp;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.snmp4j.CommunityTarget;
+import org.snmp4j.MessageDispatcherImpl;
+import org.snmp4j.PDU;
+import org.snmp4j.PDUv1;
+import org.snmp4j.ScopedPDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.UserTarget;
+import org.snmp4j.mp.MPv1;
+import org.snmp4j.mp.MPv2c;
+import org.snmp4j.mp.MPv3;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.security.AuthSHA;
+import org.snmp4j.security.PrivAES128;
+import org.snmp4j.security.SecurityLevel;
+import org.snmp4j.security.SecurityModel;
+import org.snmp4j.security.SecurityModels;
+import org.snmp4j.security.SecurityProtocols;
+import org.snmp4j.security.USM;
+import org.snmp4j.security.UsmUser;
+import org.snmp4j.smi.Integer32;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.TimeTicks;
+import org.snmp4j.smi.UdpAddress;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The trap consumer receives only the traps of its snmpVersion and, for
SNMPv3, of its user and security level. The
+ * traps are sent by a plain SNMP4J sender, and the consumer is the only snmp
endpoint of the CamelContext.
+ */
+public class TrapConsumerVersionTest extends CamelTestSupport {
+
+ private static final String USER = "camel";
+ private static final String AUTH_PASSPHRASE = "authpass12";
+ private static final String PRIV_PASSPHRASE = "privpass12";
+ private static final String V3_AUTH_PRIV = "snmpVersion=3&securityName=" +
USER + "&securityLevel=3"
+ +
"&authenticationProtocol=SHA1&authenticationPassphrase=" + AUTH_PASSPHRASE
+ +
"&privacyProtocol=AES128&privacyPassphrase=" + PRIV_PASSPHRASE;
+
+ @RegisterExtension
+ AvailablePortFinder.Port trapPort = AvailablePortFinder.find();
+
+ private boolean addedAuthSha;
+ private boolean addedPrivAes128;
+
+ @BeforeEach
+ void registerSecurityProtocols() {
+ // the authentication and privacy protocols of SNMP4J are JVM-wide,
and none is registered by default
+ SecurityProtocols protocols = SecurityProtocols.getInstance();
+ if (protocols.getAuthenticationProtocol(AuthSHA.ID) == null) {
+ protocols.addAuthenticationProtocol(new AuthSHA());
+ addedAuthSha = true;
+ }
+ if (protocols.getPrivacyProtocol(PrivAES128.ID) == null) {
+ protocols.addPrivacyProtocol(new PrivAES128());
+ addedPrivAes128 = true;
+ }
+ }
+
+ @AfterEach
+ void removeSecurityProtocols() {
+ if (addedAuthSha) {
+
SecurityProtocols.getInstance().removeAuthenticationProtocol(AuthSHA.ID);
+ }
+ if (addedPrivAes128) {
+
SecurityProtocols.getInstance().removePrivacyProtocol(PrivAES128.ID);
+ }
+ }
+
+ @Test
+ public void v3ConsumerReceivesV3AuthPrivTrap() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ // the consumer does not register its user in the JVM-wide
SecurityModels of SNMP4J
+ assertNull(SecurityModels.getInstance().getSecurityModel(new
Integer32(SecurityModel.SECURITY_MODEL_USM)));
+ }
+
+ @Test
+ public void v3ConsumerDropsV2cTrap() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendCommunityTrap(SnmpConstants.version2c, "public", "dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v3ConsumerDropsV1Trap() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendCommunityTrap(SnmpConstants.version1, "public", "dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v3ConsumerDropsTrapBelowSecurityLevel() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.NOAUTH_NOPRIV,
"dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_NOPRIV,
"dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v3ConsumerDropsTrapOfAnotherUser() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendV3Trap("other", AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV,
"dropped");
+ sendV3Trap("other", AUTH_PASSPHRASE, SecurityLevel.NOAUTH_NOPRIV,
"dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v3ConsumerDropsTrapWithAnotherPassphrase() throws Exception {
+ MockEndpoint mock = startConsumer(V3_AUTH_PRIV);
+
+ sendV3Trap(USER, "otherpass12", SecurityLevel.AUTH_PRIV, "dropped");
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.AUTH_PRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v3NoAuthNoPrivConsumerReceivesV3Trap() throws Exception {
+ MockEndpoint mock = startConsumer("snmpVersion=3&securityName=" + USER
+ "&securityLevel=1");
+
+ sendV3Trap(USER, null, SecurityLevel.NOAUTH_NOPRIV, "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ @Test
+ public void v2cConsumerReceivesV1AndV2cTrapsAndDropsV3Trap() throws
Exception {
+ MockEndpoint mock = startConsumer("snmpVersion=1");
+ mock.expectedMessageCount(2);
+
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.NOAUTH_NOPRIV,
"dropped");
+ sendCommunityTrap(SnmpConstants.version1, "public", "first");
+ sendCommunityTrap(SnmpConstants.version2c, "public", "second");
+
+ MockEndpoint.assertIsSatisfied(context);
+
assertTrue(mock.getReceivedExchanges().get(0).getMessage().getBody(String.class).contains("first"));
+
assertTrue(mock.getReceivedExchanges().get(1).getMessage().getBody(String.class).contains("second"));
+ }
+
+ @Test
+ public void v1ConsumerDropsV3Trap() throws Exception {
+ MockEndpoint mock = startConsumer("snmpVersion=0");
+
+ sendV3Trap(USER, AUTH_PASSPHRASE, SecurityLevel.NOAUTH_NOPRIV,
"dropped");
+ sendCommunityTrap(SnmpConstants.version1, "public", "accepted");
+
+ assertReceivedOnly(mock, "accepted");
+ }
+
+ private MockEndpoint startConsumer(String options) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("snmp:127.0.0.1:" + trapPort.getPort() +
"?protocol=udp&type=TRAP&" + options).to("mock:trap");
+ }
+ });
+ return getMockEndpoint("mock:trap");
+ }
+
+ /**
+ * The traps are sent one after the other to the single listener thread of
the consumer, so a dropped trap sent
+ * before the accepted one would have been received first.
+ */
+ private void assertReceivedOnly(MockEndpoint mock, String marker) throws
Exception {
+ mock.expectedMessageCount(1);
+ MockEndpoint.assertIsSatisfied(context);
+ String body =
mock.getReceivedExchanges().get(0).getMessage().getBody(String.class);
+ assertTrue(body.contains(marker), "Expected the trap " + marker + "
but got: " + body);
+ assertEquals(1, mock.getReceivedCounter());
+ }
+
+ private void sendCommunityTrap(int version, String community, String
marker) throws Exception {
+ CommunityTarget<UdpAddress> target = new
CommunityTarget<>(trapAddress(), new OctetString(community));
+ target.setVersion(version);
+ PDU pdu;
+ if (version == SnmpConstants.version1) {
+ PDUv1 v1 = new PDUv1();
+ v1.setType(PDU.V1TRAP);
+ v1.setEnterprise(new OID("1.2.3.4"));
+ v1.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
+ v1.setSpecificTrap(1);
+ pdu = v1;
+ } else {
+ pdu = new PDU();
+ pdu.setType(PDU.TRAP);
+ addTrapBindings(pdu);
+ }
+ pdu.add(new VariableBinding(new OID("1.2.3.4.5.1"), new
OctetString(marker)));
+
+ MessageDispatcherImpl dispatcher = new MessageDispatcherImpl();
+ dispatcher.addMessageProcessingModel(new MPv1());
+ dispatcher.addMessageProcessingModel(new MPv2c());
+ send(dispatcher, pdu, target);
+ }
+
+ private void sendV3Trap(String user, String authPassphrase, int
securityLevel, String marker) throws Exception {
+ USM usm = new USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()), 0);
+ if (authPassphrase != null) {
+ usm.addUser(new OctetString(user), new UsmUser(
+ new OctetString(user), AuthSHA.ID, new
OctetString(authPassphrase), PrivAES128.ID,
+ new OctetString(PRIV_PASSPHRASE)));
+ } else {
+ usm.addUser(new OctetString(user), new UsmUser(new
OctetString(user), null, null, null, null));
+ }
+ MessageDispatcherImpl dispatcher = new MessageDispatcherImpl();
+ dispatcher.addMessageProcessingModel(new MPv3(usm));
+
+ UserTarget<UdpAddress> target = new UserTarget<>();
+ target.setAddress(trapAddress());
+ target.setVersion(SnmpConstants.version3);
+ target.setSecurityName(new OctetString(user));
+ target.setSecurityLevel(securityLevel);
+
+ ScopedPDU pdu = new ScopedPDU();
+ pdu.setType(PDU.TRAP);
+ addTrapBindings(pdu);
+ pdu.add(new VariableBinding(new OID("1.2.3.4.5.1"), new
OctetString(marker)));
+ send(dispatcher, pdu, target);
+ }
+
+ private static void addTrapBindings(PDU pdu) {
+ pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new
TimeTicks(5000)));
+ pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new
OID("1.2.3.4.5")));
+ }
+
+ private UdpAddress trapAddress() {
+ return new UdpAddress("127.0.0.1/" + trapPort.getPort());
+ }
+
+ private static void send(MessageDispatcherImpl dispatcher, PDU pdu,
Target<UdpAddress> target) throws Exception {
+ DefaultUdpTransportMapping transport = new
DefaultUdpTransportMapping();
+ try (Snmp snmp = new Snmp(dispatcher, transport)) {
+ transport.listen();
+ snmp.send(pdu, target);
+ }
+ }
+}