CAMEL-10319, SNMP Producer (fix onStart/onStop)

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/406b97ec
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/406b97ec
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/406b97ec

Branch: refs/heads/master
Commit: 406b97ecfa939b2599b66bb813048379e4f31298
Parents: a2486d2
Author: Fabrizio Spataro <fabrizio.spat...@bizmate.it>
Authored: Thu Sep 15 10:40:01 2016 +0200
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Thu Sep 15 10:52:14 2016 +0200

----------------------------------------------------------------------
 .../camel/component/snmp/SnmpProducer.java      | 61 +++++++++++---------
 .../camel/component/snmp/ProducerTest.java      | 14 +----
 2 files changed, 36 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/406b97ec/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
index 9f69ff9..6b6d2c8 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
@@ -53,32 +53,54 @@ public class SnmpProducer extends DefaultProducer {
     
     private Address targetAddress;
     private USM usm;
-
+    private CommunityTarget target;
+    private PDU pdu;
+    
     public SnmpProducer(SnmpEndpoint endpoint) {
         super(endpoint);
         this.endpoint = endpoint;
     }
     
     @Override
-    public void start() throws Exception {
-        super.start();
+    protected void doStart() throws Exception {
+        super.doStart();
 
-        this.targetAddress = GenericAddress.parse(endpoint.getAddress());
+        this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
         LOG.debug("targetAddress: {}", targetAddress);
 
         this.usm = new USM(SecurityProtocols.getInstance(), new 
OctetString(MPv3.createLocalEngineID()), 0);
         SecurityModels.getInstance().addSecurityModel(this.usm);
+        
+        // setting up target
+        this.target = new CommunityTarget();
+        this.target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
+        this.target.setAddress(this.targetAddress);
+        this.target.setRetries(this.endpoint.getRetries());
+        this.target.setTimeout(this.endpoint.getTimeout());
+        this.target.setVersion(this.endpoint.getSnmpVersion());
+
+        this.pdu = new PDU();
+        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);
+        
     }
     
     @Override
-    public void stop() throws Exception {
-        super.stop();
+    protected void doStop() throws Exception {
+        super.doStop();
 
         try {
             SecurityModels.getInstance().removeSecurityModel(new 
Integer32(this.usm.getID()));
         } finally {
             this.targetAddress = null;
             this.usm = null;
+            this.target = null;
+            this.pdu = null;
         }
     }
     
@@ -89,40 +111,23 @@ public class SnmpProducer extends DefaultProducer {
         TransportMapping<? extends Address> transport = null;
 
         try {
-            LOG.debug("Starting SNMP producer on {}", endpoint.getAddress());
+            LOG.debug("Starting SNMP producer on {}", 
this.endpoint.getAddress());
             
             // either tcp or udp
-            if ("tcp".equals(endpoint.getProtocol())) {
+            if ("tcp".equals(this.endpoint.getProtocol())) {
                 transport = new DefaultTcpTransportMapping();
-            } else if ("udp".equals(endpoint.getProtocol())) {
+            } else if ("udp".equals(this.endpoint.getProtocol())) {
                 transport = new DefaultUdpTransportMapping();
             } else {
-                throw new IllegalArgumentException("Unknown protocol: {} " + 
endpoint.getProtocol());
+                throw new IllegalArgumentException("Unknown protocol: {} " + 
this.endpoint.getProtocol());
             }
     
             snmp = new Snmp(transport);
             
-            // setting up target
-            CommunityTarget 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 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);
+            ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
             
             LOG.debug("Snmp: sended");
     

http://git-wip-us.apache.org/repos/asf/camel/blob/406b97ec/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
index 45d8ff4..2532eac 100644
--- 
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
@@ -18,29 +18,21 @@ package org.apache.camel.component.snmp;
 
 import java.util.concurrent.TimeUnit;
 
-import org.apache.camel.CamelExecutionException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.Rule;
+import org.junit.Ignore;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 
+@Ignore("CAMEL-10319: Set host, port and oids to test snmp producer.")
 public class ProducerTest extends CamelTestSupport {
 
-    @Rule
-    public ExpectedException thrown = ExpectedException.none();
-
-
     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";
-    private Integer timeout = 5; // 5ms
     
     @Test
     public void testSnmpProducer() throws Exception {
-        thrown.expect(CamelExecutionException.class);
-
         template.sendBody("direct:in", "");
         
         MockEndpoint mock = getMockEndpoint("mock:result");
@@ -54,7 +46,7 @@ public class ProducerTest extends CamelTestSupport {
         return new RouteBuilder() {
             public void configure() {
                 from("direct:in")
-                        .to("snmp://" + host + ":" + port + "?oids=" + oids + 
"&timeout=" + timeout)
+                        .to("snmp://" + host + ":" + port + "?oids=" + oids)
                         .log("${body}")
                         .to("mock:result");
             }

Reply via email to