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 java.net.URI;
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.ExchangePattern;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.impl.DefaultExchange;
026    import org.apache.camel.impl.DefaultPollingEndpoint;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.snmp4j.PDU;
030    import org.snmp4j.mp.SnmpConstants;
031    
032    public class SnmpEndpoint extends DefaultPollingEndpoint {
033    
034        public static final String DEFAULT_COMMUNITY = "public";
035        public static final int DEFAULT_SNMP_VERSION = SnmpConstants.version1;
036        public static final int DEFAULT_SNMP_RETRIES = 2;
037        public static final int DEFAULT_SNMP_TIMEOUT = 1500;
038    
039        private static final Log LOG = LogFactory.getLog(SnmpEndpoint.class);
040    
041        private OIDList oids = new OIDList();
042        private String address;
043        private String protocol;
044        private int retries = DEFAULT_SNMP_RETRIES;
045        private int timeout = DEFAULT_SNMP_TIMEOUT;
046        private int snmpVersion = DEFAULT_SNMP_VERSION;
047        private String snmpCommunity = DEFAULT_COMMUNITY;
048        private SnmpActionType type;
049        private int delay = 60;
050    
051        /**
052         * creates a snmp endpoint
053         *
054         * @param uri       the endpoint uri
055         * @param component the component
056         */
057        public SnmpEndpoint(String uri, SnmpComponent component) {
058            super(uri, component);
059        }
060    
061        public Consumer createConsumer(Processor processor) throws Exception {
062            if (this.type == SnmpActionType.TRAP) {
063                return new SnmpTrapConsumer(this, processor);
064            } else if (this.type == SnmpActionType.POLL) {
065                return new SnmpOIDPoller(this, processor);
066            } else {
067                throw new IllegalArgumentException("The type '" + this.type + "' is not valid!");
068            }
069        }
070    
071        public Producer createProducer() throws Exception {
072            throw new UnsupportedOperationException("SnmpProducer is not implemented");
073        }
074    
075        public boolean isSingleton() {
076            return true;
077        }
078    
079        /**
080         * creates an exchange for the given message
081         *
082         * @param pdu the pdu
083         * @return an exchange
084         */
085        public Exchange createExchange(PDU pdu) {
086            return createExchange(getExchangePattern(), pdu);
087        }
088    
089        /**
090         * creates an exchange for the given pattern and message
091         *
092         * @param pattern the message exchange pattern
093         * @param pdu     the pdu
094         * @return the exchange
095         */
096        private Exchange createExchange(ExchangePattern pattern, PDU pdu) {
097            Exchange exchange = new DefaultExchange(this, pattern);
098            exchange.setIn(new SnmpMessage(pdu));
099            return exchange;
100        }
101    
102        /**
103         * creates and configures the endpoint
104         *
105         * @throws Exception if unable to setup connection
106         */
107        public void initiate() throws Exception {
108            URI uri = URI.create(getEndpointUri());
109            String host = uri.getHost();
110            int port = uri.getPort();
111            if (host == null || host.trim().length() < 1) {
112                host = "127.0.0.1";
113            }
114            if (port == -1) {
115                if (getType() == SnmpActionType.POLL) {
116                    port = 161; // default snmp poll port
117                } else {
118                    port = 162; // default trap port
119                }
120            }
121    
122    
123            // set the address
124            String address = String.format("%s:%s/%d", getProtocol(), host, port);
125            if (LOG.isDebugEnabled()) {
126                LOG.debug("Using snmp address " + address);
127            }
128            setAddress(address);
129        }
130    
131        public int getDelay() {
132            return delay;
133        }
134    
135        /**
136         * Sets update rate in seconds
137         *
138         * @param updateEvery the update rate in seconds
139         */
140        public void setDelay(int updateEvery) {
141            this.delay = updateEvery;
142        }
143    
144        public SnmpActionType getType() {
145            return this.type;
146        }
147    
148        public void setType(SnmpActionType type) {
149            this.type = type;
150        }
151    
152        public OIDList getOids() {
153            return this.oids;
154        }
155    
156        public void setOids(OIDList oids) {
157            this.oids = oids;
158        }
159    
160        public String getAddress() {
161            return this.address;
162        }
163    
164        public void setAddress(String address) {
165            this.address = address;
166        }
167    
168        public int getRetries() {
169            return this.retries;
170        }
171    
172        public void setRetries(int retries) {
173            this.retries = retries;
174        }
175    
176        public int getTimeout() {
177            return this.timeout;
178        }
179    
180        public void setTimeout(int timeout) {
181            this.timeout = timeout;
182        }
183    
184        public int getSnmpVersion() {
185            return this.snmpVersion;
186        }
187    
188        public void setSnmpVersion(int snmpVersion) {
189            this.snmpVersion = snmpVersion;
190        }
191    
192        public String getSnmpCommunity() {
193            return this.snmpCommunity;
194        }
195    
196        public void setSnmpCommunity(String snmpCommunity) {
197            this.snmpCommunity = snmpCommunity;
198        }
199    
200        public String getProtocol() {
201            return this.protocol;
202        }
203    
204        public void setProtocol(String protocol) {
205            this.protocol = protocol;
206        }
207    }