Search in sources :

Example 1 with SnmpMessage

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);
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) AttributeRef(org.openremote.model.attribute.AttributeRef) HashMap(java.util.HashMap) ValueUtil(org.openremote.model.util.ValueUtil) Logger(java.util.logging.Logger) PDU(org.snmp4j.PDU) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) SnmpMessage(org.apache.camel.component.snmp.SnmpMessage) RouteBuilder(org.apache.camel.builder.RouteBuilder) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) Map(java.util.Map) SyslogCategory(org.openremote.model.syslog.SyslogCategory) PDU(org.snmp4j.PDU) AttributeState(org.openremote.model.attribute.AttributeState) RouteBuilder(org.apache.camel.builder.RouteBuilder) AttributeRef(org.openremote.model.attribute.AttributeRef) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SnmpMessage(org.apache.camel.component.snmp.SnmpMessage)

Example 2 with SnmpMessage

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();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) PDU(org.snmp4j.PDU) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) SnmpMessage(org.apache.camel.component.snmp.SnmpMessage) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)2 SnmpMessage (org.apache.camel.component.snmp.SnmpMessage)2 PDU (org.snmp4j.PDU)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Logger (java.util.logging.Logger)1 CamelContext (org.apache.camel.CamelContext)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 Test (org.junit.Test)1 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)1 Container (org.openremote.model.Container)1 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)1 Attribute (org.openremote.model.attribute.Attribute)1 AttributeEvent (org.openremote.model.attribute.AttributeEvent)1 AttributeRef (org.openremote.model.attribute.AttributeRef)1 AttributeState (org.openremote.model.attribute.AttributeState)1 SyslogCategory (org.openremote.model.syslog.SyslogCategory)1 PROTOCOL (org.openremote.model.syslog.SyslogCategory.PROTOCOL)1