You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/09/06 15:19:12 UTC

[camel] 02/03: Update SnmpProducer.java

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0e72d3026e172be4d27bd21761899902e2bf92c7
Author: cumtwwei <19...@qq.com>
AuthorDate: Fri Sep 6 10:30:49 2019 +0800

    Update SnmpProducer.java
    
    add support to 'snmp walk', use the snmp4j 'GET_NEXT'
---
 .../apache/camel/component/snmp/SnmpProducer.java  | 69 ++++++++++++++++++----
 1 file changed, 57 insertions(+), 12 deletions(-)

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
index 3cffe4a..cdcada8 100644
--- 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
@@ -74,13 +74,20 @@ public class SnmpProducer extends DefaultProducer {
         this.target.setVersion(this.endpoint.getSnmpVersion());
 
         this.pdu = new PDU();
-        for (OID oid : this.endpoint.getOids()) {
-            this.pdu.add(new VariableBinding(oid));
+        // in here,only POLL do set the oids
+        if (this.actionType == SnmpActionType.POLL) {
+            for (OID oid : this.endpoint.getOids()) {
+                this.pdu.add(new VariableBinding(oid));
+            }
         }
         this.pdu.setErrorIndex(0);
         this.pdu.setErrorStatus(0);
         this.pdu.setMaxRepetitions(0);
-        this.pdu.setType(PDU.GET);
+        // support POLL and GET_NEXT
+        if (this.actionType == SnmpActionType.GET_NEXT)
+          this.pdu.setType(PDU.GETNEXT);
+        else
+          this.pdu.setType(PDU.GET); 
         
     }
     
@@ -121,15 +128,53 @@ public class SnmpProducer extends DefaultProducer {
             log.debug("Snmp: i am sending");
     
             snmp.listen();
-            ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
             
-            log.debug("Snmp: sended");
-    
-            if (responseEvent.getResponse() != null) {
-                exchange.getIn().setBody(new SnmpMessage(getEndpoint().getCamelContext(), responseEvent.getResponse()));
-            } else {
-                throw new TimeoutException("SNMP Producer Timeout");
-            }
+            if (this.actionType == SnmpActionType.GET_NEXT) {
+                // snmp walk
+                List<SnmpMessage> smLst = new ArrayList<>();
+                for (OID oid : this.endpoint.getOids()) {
+                    this.pdu.clear();
+                    this.pdu.add(new VariableBinding(oid));
+
+                    boolean matched = true;
+                    while (matched) {
+                        ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
+                        if (responseEvent == null || responseEvent.getResponse() == null) {
+                            break;
+                        }
+                        PDU response = responseEvent.getResponse();
+                        String nextOid = null;
+                        Vector<? extends VariableBinding> variableBindings = response.getVariableBindings();
+                        for (int i = 0; i < variableBindings.size(); i++) {
+                            VariableBinding variableBinding = variableBindings.elementAt(i);
+                            Variable variable = variableBinding.getVariable();
+                            nextOid = variableBinding.getOid().toDottedString();
+                            if (!nextOid.startsWith(oid.toDottedString())) {
+                                matched = false;
+                                break;
+                            }
+                        }
+                        if (!matched) {
+                            break;
+                        }
+                        this.pdu.clear();
+                        pdu.add(new VariableBinding(new OID(nextOid)));
+                        smLst.add(new SnmpMessage(getEndpoint().getCamelContext(), response));
+                    }
+                }
+                exchange.getIn().setBody(smLst);
+              } else {
+                // snmp get
+                ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
+
+                log.debug("Snmp: sended");
+
+                if (responseEvent.getResponse() != null) {
+                    exchange.getIn().setBody(new SnmpMessage(getEndpoint().getCamelContext(), responseEvent.getResponse()));
+                } else {
+                    throw new TimeoutException("SNMP Producer Timeout");
+                }
+              }
         } finally {
             try {
                 transport.close(); 
@@ -139,4 +184,4 @@ public class SnmpProducer extends DefaultProducer {
             } catch (Exception e) { }
         }
     } //end process
-}
\ No newline at end of file
+}