You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/09/15 08:54:11 UTC
[3/6] camel git commit: CAMEL-10319, SNMP Producer
CAMEL-10319, SNMP Producer
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/64441b9f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/64441b9f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/64441b9f
Branch: refs/heads/master
Commit: 64441b9fc067be1f22e315474fb4f5cbe50d863f
Parents: 404fae8
Author: Fabrizio Spataro <fa...@bizmate.it>
Authored: Wed Sep 14 17:34:53 2016 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Thu Sep 15 10:52:13 2016 +0200
----------------------------------------------------------------------
.../src/main/docs/snmp-component.adoc | 13 +-
.../camel/component/snmp/SnmpEndpoint.java | 2 +-
.../camel/component/snmp/SnmpProducer.java | 125 +++++++++++++++++++
.../camel/component/snmp/ProducerTest.java | 54 ++++++++
4 files changed, 191 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/64441b9f/components/camel-snmp/src/main/docs/snmp-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-snmp/src/main/docs/snmp-component.adoc b/components/camel-snmp/src/main/docs/snmp-component.adoc
index e6d24d3..ba6edbd 100644
--- a/components/camel-snmp/src/main/docs/snmp-component.adoc
+++ b/components/camel-snmp/src/main/docs/snmp-component.adoc
@@ -5,7 +5,7 @@ SNMP Component
*Available as of Camel 2.1*
The *snmp:* component gives you the ability to poll SNMP capable devices
-or receiving traps.
+or receiving traps
Maven users will need to add the following dependency to their `pom.xml`
for this component:
@@ -35,6 +35,16 @@ and receiving traps.
You can append query options to the URI in the following format,
`?option=value&option=value&...`
+[[SNMP-Producer]]
+Snmp Producer
+^^^^^^^^^^^^^
+
+*Available from 2.18 release*
+
+It can also be used to request information using GET method.
+
+The response body type is org.apache.camel.component.snmp.SnmpMessage
+
[[SNMP-Options]]
Options
^^^^^^^
@@ -93,7 +103,6 @@ The SNMP component supports 36 endpoint options which are listed below:
{% endraw %}
// endpoint options: END
-
[[SNMP-Theresultofapoll]]
The result of a poll
^^^^^^^^^^^^^^^^^^^^
http://git-wip-us.apache.org/repos/asf/camel/blob/64441b9f/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java
index ebb2f63..005e635 100644
--- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java
+++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java
@@ -111,7 +111,7 @@ public class SnmpEndpoint extends DefaultPollingEndpoint {
}
public Producer createProducer() throws Exception {
- throw new UnsupportedOperationException("SnmpProducer is not implemented");
+ return new SnmpProducer(this);
}
public boolean isSingleton() {
http://git-wip-us.apache.org/repos/asf/camel/blob/64441b9f/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java
new file mode 100644
index 0000000..1db6696
--- /dev/null
+++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.snmp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultProducer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.snmp4j.CommunityTarget;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.mp.MPv3;
+import org.snmp4j.security.SecurityModels;
+import org.snmp4j.security.SecurityProtocols;
+import org.snmp4j.security.USM;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.transport.DefaultTcpTransportMapping;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+/**
+ * A snmp producer
+ *
+ *
+ */
+public class SnmpProducer extends DefaultProducer {
+
+ private static final Logger LOG = LoggerFactory.getLogger(SnmpProducer.class);
+
+ private SnmpEndpoint endpoint;
+
+ public SnmpProducer(SnmpEndpoint endpoint) {
+ super(endpoint);
+ this.endpoint = endpoint;
+ }
+
+ @Override
+ public void process(final Exchange exchange) throws Exception {
+ // load connection data only if the endpoint is enabled
+ Snmp snmp = null;
+ USM usm;
+ TransportMapping<? extends Address> transport = null;
+ CommunityTarget target = null;
+ PDU pdu = null;
+ Address targetAddress = null;
+
+ try {
+ LOG.debug("Starting SNMP producer on {}", endpoint.getAddress());
+
+ targetAddress = GenericAddress.parse(endpoint.getAddress());
+
+ LOG.debug("targetAddress: " + targetAddress);
+
+ // either tcp or udp
+ if ("tcp".equals(endpoint.getProtocol())) {
+ transport = new DefaultTcpTransportMapping();
+ } else if ("udp".equals(endpoint.getProtocol())) {
+ transport = new DefaultUdpTransportMapping();
+ } else {
+ throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
+ }
+
+ snmp = new Snmp(transport);
+ usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
+
+ SecurityModels.getInstance().addSecurityModel(usm);
+
+ // setting up target
+ target = new CommunityTarget();
+ target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
+ target.setAddress(targetAddress);
+ target.setRetries(this.endpoint.getRetries());
+ target.setTimeout(this.endpoint.getTimeout());
+ target.setVersion(this.endpoint.getSnmpVersion());
+
+ pdu = new PDU();
+ for (OID oid : endpoint.getOids()) {
+ pdu.add(new VariableBinding(oid));
+ }
+ pdu.setErrorIndex(0);
+ pdu.setErrorStatus(0);
+ pdu.setMaxRepetitions(0);
+ pdu.setType(PDU.GET);
+
+ LOG.debug("Snmp: i am sending");
+
+ snmp.listen();
+ ResponseEvent responseEvent = snmp.send(pdu, target);
+
+ LOG.debug("Snmp: sended");
+
+ if (responseEvent.getResponse() != null) {
+ exchange.getIn().setBody(new SnmpMessage(responseEvent.getResponse()));
+ } else {
+ throw new Exception("SNMP Producer Timeout");
+ }
+ } finally {
+ try {
+ transport.close();
+ } catch (Exception e) { }
+ try {
+ snmp.close();
+ } catch (Exception e) { }
+ }
+ } //end process
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/64441b9f/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java
new file mode 100644
index 0000000..d667f36
--- /dev/null
+++ b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.snmp;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class ProducerTest extends CamelTestSupport {
+
+ private String host = "192.168.0.254";
+ private String port = "161";
+ private String oids = "1.3.6.1.2.1.1.3.0,1.3.6.1.2.1.25.3.2.1.5.1,1.3.6.1.2.1.25.3.5.1.1.1,1.3.6.1.2.1.43.5.1.1.11.1";
+
+ @Test
+ public void testSnmpProducer() throws Exception {
+ template.sendBody("direct:in", "");
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMinimumMessageCount(1);
+
+ assertMockEndpointsSatisfied(60, TimeUnit.SECONDS);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:in")
+ .to("snmp://" + host + ":" + port + "?oids=" + oids)
+ .log("${body}")
+ .to("mock:result");
+ }
+ };
+ }
+}
+