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