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.UdpAddress;
032    import org.snmp4j.transport.DefaultUdpTransportMapping;
033    
034    public class SnmpTrapConsumer extends DefaultConsumer implements CommandResponder {
035    
036        private static final Log LOG = LogFactory.getLog(SnmpTrapConsumer.class);
037        
038        private SnmpEndpoint endpoint;
039        private Address listenGenericAddress;
040        private Snmp snmp;
041        private TransportMapping transport;
042    
043        public SnmpTrapConsumer(SnmpEndpoint endpoint, Processor processor) {
044            super(endpoint, processor);
045            this.endpoint = endpoint;
046        }
047        
048        @Override
049        protected void doStart() throws Exception {
050            super.doStart();
051    
052            // load connection data only if the endpoint is enabled
053            if (LOG.isInfoEnabled()) {
054                LOG.info("Starting trap consumer on " + this.endpoint.getAddress());
055            }
056            this.listenGenericAddress = GenericAddress.parse(this.endpoint.getAddress());
057            this.transport = new DefaultUdpTransportMapping((UdpAddress)this.listenGenericAddress);
058            this.snmp = new Snmp(transport);
059            this.snmp.addCommandResponder(this);
060            
061            // listen to the transport
062            this.transport.listen();
063        }
064    
065        @Override
066        protected void doStop() throws Exception {
067            // stop listening to the transport
068            if (this.transport != null && this.transport.isListening()) {
069                if (LOG.isInfoEnabled()) {
070                    LOG.info("Stopping trap consumer on " + this.endpoint.getAddress());
071                }
072                this.transport.close();
073            }
074            
075            super.doStop();
076        }
077    
078        public void processPdu(CommandResponderEvent event) {
079            PDU pdu = event.getPDU();
080            // check PDU not null
081            if (pdu != null) {
082                processPDU(pdu);
083            } else {
084                LOG.debug("Received invalid trap PDU: " + pdu);
085            }
086        }
087        
088        public void processPDU(PDU pdu) {
089            if (LOG.isDebugEnabled()) {
090                LOG.debug("Received trap event for " + this.endpoint.getAddress() + " : " + pdu);
091            }
092            Exchange exchange = endpoint.createExchange(pdu);
093            try {
094                getProcessor().process(exchange);
095            } catch (Exception ex) {
096                exchange.setException(ex);
097            }
098        }
099    }