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.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
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 Logger LOG = LoggerFactory.getLogger(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 {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
076            }
077            this.transport.listen();
078            if (LOG.isInfoEnabled()) {
079                LOG.info("Started trap consumer on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
080            }
081        }
082    
083        @Override
084        protected void doStop() throws Exception {
085            // stop listening to the transport
086            if (this.transport != null && this.transport.isListening()) {
087                if (LOG.isDebugEnabled()) {
088                    LOG.debug("Stopping trap consumer on {}", this.endpoint.getAddress());
089                }
090                this.transport.close();
091                LOG.info("Stopped trap consumer on " + this.endpoint.getAddress());
092            }
093            
094            super.doStop();
095        }
096    
097        public void processPdu(CommandResponderEvent event) {
098            PDU pdu = event.getPDU();
099            // check PDU not null
100            if (pdu != null) {
101                processPDU(pdu);
102            } else {
103                LOG.debug("Received invalid trap PDU: " + pdu);
104            }
105        }
106        
107        public void processPDU(PDU pdu) {
108            if (LOG.isDebugEnabled()) {
109                LOG.debug("Received trap event for {} : {}", this.endpoint.getAddress(), pdu);
110            }
111            Exchange exchange = endpoint.createExchange(pdu);
112            try {
113                getProcessor().process(exchange);
114            } catch (Exception e) {
115                getExceptionHandler().handleException(e);
116            }
117        }
118    }