use of org.apache.camel.component.snmp.SnmpMessage in project openremote by openremote.
the class SNMPProtocol method doStart.
@Override
protected void doStart(Container container) throws Exception {
String snmpBindHost = agent.getBindHost().orElseThrow(() -> {
String msg = "No SNMP bind host provided for protocol: " + this;
LOG.info(msg);
return new IllegalArgumentException(msg);
});
Integer snmpBindPort = agent.getBindPort().orElse(162);
SNMPAgent.SNMPVersion snmpVersion = agent.getSNMPVersion().orElse(SNMPAgent.SNMPVersion.V2c);
String snmpUri = String.format("snmp:%s:%d?protocol=udp&type=TRAP&snmpVersion=%d", snmpBindHost, snmpBindPort, snmpVersion.getVersion());
messageBrokerContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from(snmpUri).routeId(getProtocolName() + getAgent().getId()).process(exchange -> {
SnmpMessage msg = exchange.getIn(SnmpMessage.class);
LOG.fine(String.format("Message received: %s", msg));
PDU pdu = msg.getSnmpMessage();
AttributeRef wildCardAttributeRef;
if ((wildCardAttributeRef = oidMap.get("*")) != null) {
ObjectNode wildCardValue = ValueUtil.createJsonObject();
pdu.getVariableBindings().forEach(variableBinding -> {
wildCardValue.put(variableBinding.getOid().format(), variableBinding.toValueString());
});
updateLinkedAttribute(new AttributeState(wildCardAttributeRef, wildCardValue));
}
pdu.getVariableBindings().forEach(variableBinding -> {
AttributeRef attributeRef = oidMap.get(variableBinding.getOid().format());
if (attributeRef != null) {
updateLinkedAttribute(new AttributeState(attributeRef, variableBinding.toValueString()));
}
});
});
}
});
setConnectionStatus(ConnectionStatus.CONNECTED);
}
use of org.apache.camel.component.snmp.SnmpMessage in project wildfly-camel by wildfly-extras.
the class SNMPIntegrationTest method testSnmpEndpoint.
@Test
public void testSnmpEndpoint() throws Exception {
int port = AvailablePortFinder.getNextAvailable();
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
fromF("snmp://localhost:%d?protocol=tcp&type=TRAP", port).to("mock:result");
}
});
camelctx.start();
try {
String uri = String.format("snmp://localhost:%d?oids=%s&protocol=tcp", port, SNMP_OIDS);
ProducerTemplate template = camelctx.createProducerTemplate();
SnmpMessage message = template.requestBody(uri, null, SnmpMessage.class);
PDU snmpMessage = message.getSnmpMessage();
Assert.assertNotNull(snmpMessage);
Assert.assertEquals(0, snmpMessage.getErrorStatus());
} finally {
camelctx.close();
}
}
Aggregations