001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.snmp;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.Processor;
021 import org.apache.camel.impl.DefaultConsumer;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.snmp4j.CommandResponder;
025 import org.snmp4j.CommandResponderEvent;
026 import org.snmp4j.PDU;
027 import org.snmp4j.Snmp;
028 import org.snmp4j.TransportMapping;
029 import org.snmp4j.smi.Address;
030 import org.snmp4j.smi.GenericAddress;
031 import org.snmp4j.smi.TcpAddress;
032 import org.snmp4j.smi.UdpAddress;
033 import org.snmp4j.transport.DefaultTcpTransportMapping;
034 import org.snmp4j.transport.DefaultUdpTransportMapping;
035
036 public class SnmpTrapConsumer extends DefaultConsumer implements CommandResponder {
037
038 private static final Log LOG = LogFactory.getLog(SnmpTrapConsumer.class);
039
040 private SnmpEndpoint endpoint;
041 private Address listenGenericAddress;
042 private Snmp snmp;
043 private TransportMapping transport;
044
045 public SnmpTrapConsumer(SnmpEndpoint endpoint, Processor processor) {
046 super(endpoint, processor);
047 this.endpoint = endpoint;
048 }
049
050 @Override
051 protected void doStart() throws Exception {
052 super.doStart();
053
054 // load connection data only if the endpoint is enabled
055 if (LOG.isInfoEnabled()) {
056 LOG.info("Starting trap consumer on " + this.endpoint.getAddress());
057 }
058
059 this.listenGenericAddress = GenericAddress.parse(this.endpoint.getAddress());
060
061 // either tcp or udp
062 if ("tcp".equals(endpoint.getProtocol())) {
063 this.transport = new DefaultTcpTransportMapping((TcpAddress)this.listenGenericAddress);
064 } else if ("udp".equals(endpoint.getProtocol())) {
065 this.transport = new DefaultUdpTransportMapping((UdpAddress)this.listenGenericAddress);
066 } else {
067 throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
068 }
069
070 this.snmp = new Snmp(transport);
071 this.snmp.addCommandResponder(this);
072
073 // listen to the transport
074 if (LOG.isDebugEnabled()) {
075 LOG.debug("Starting trap consumer on " + endpoint.getAddress() + " using " + endpoint.getProtocol() + " protocol");
076 }
077 this.transport.listen();
078 LOG.info("Started trap consumer on " + endpoint.getAddress() + " using " + endpoint.getProtocol() + " protocol");
079 }
080
081 @Override
082 protected void doStop() throws Exception {
083 // stop listening to the transport
084 if (this.transport != null && this.transport.isListening()) {
085 if (LOG.isDebugEnabled()) {
086 LOG.debug("Stopping trap consumer on " + this.endpoint.getAddress());
087 }
088 this.transport.close();
089 LOG.info("Stopped trap consumer on " + this.endpoint.getAddress());
090 }
091
092 super.doStop();
093 }
094
095 public void processPdu(CommandResponderEvent event) {
096 PDU pdu = event.getPDU();
097 // check PDU not null
098 if (pdu != null) {
099 processPDU(pdu);
100 } else {
101 LOG.debug("Received invalid trap PDU: " + pdu);
102 }
103 }
104
105 public void processPDU(PDU pdu) {
106 if (LOG.isDebugEnabled()) {
107 LOG.debug("Received trap event for " + this.endpoint.getAddress() + " : " + pdu);
108 }
109 Exchange exchange = endpoint.createExchange(pdu);
110 try {
111 getProcessor().process(exchange);
112 } catch (Exception e) {
113 getExceptionHandler().handleException(e);
114 }
115 }
116 }