- Add stop() method in IMessageReadWrite for proper clean up when connection is closed. 45/145/1
authorJason Ye <yisye@cisco.com>
Fri, 12 Apr 2013 23:08:25 +0000 (16:08 -0700)
committerJason Ye <yisye@cisco.com>
Fri, 12 Apr 2013 23:08:25 +0000 (16:08 -0700)
- Be able to handle whitespace in TLS config lines
Signed-off-by: Jason Ye <yisye@cisco.com>
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/IMessageReadWrite.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/MessageReadWriteService.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/SecureMessageReadWriteService.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/SwitchHandler.java
opendaylight/topologymanager/src/main/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImpl.java

index 301159d6d3fbf8520ff6bf912210a0fab29fd600..8fb9a6acb8a2c498533ce15db5161af7a28d6562 100644 (file)
@@ -42,4 +42,12 @@ public interface IMessageReadWrite {
         * @throws Exception
         */
     public List<OFMessage> readMessages() throws Exception;
         * @throws Exception
         */
     public List<OFMessage> readMessages() throws Exception;
+    
+       /**
+        * Proper clean up when the switch connection is closed
+        * 
+        * @return
+        * @throws Exception
+        */
+    public void stop() throws Exception;
 }
 }
index f183b516de5fb4de0b60a79618664c58108209e1..f3004acaba5a3cb6c9ffb287bd968599283f1001 100644 (file)
@@ -246,6 +246,7 @@ public class Controller implements IController, CommandProvider {
             }
         }
         ((SwitchHandler) sw).stop();
             }
         }
         ((SwitchHandler) sw).stop();
+        sw = null;
     }
 
     private void notifySwitchAdded(ISwitch sw) {
     }
 
     private void notifySwitchAdded(ISwitch sw) {
@@ -264,8 +265,7 @@ public class Controller implements IController, CommandProvider {
         try {
             this.switchEvents.put(event);
         } catch (InterruptedException e) {
         try {
             this.switchEvents.put(event);
         } catch (InterruptedException e) {
-            e.printStackTrace();
-            logger.error("Interrupt Exception " + e.toString());
+            logger.debug("SwitchEvent caught Interrupt Exception");
         }
     }
 
         }
     }
 
index d20bf1e0a0fe07fff82596be1f96f78a20f117b7..fb34b0f06371538930abc6fed698e4f6df12a735 100644 (file)
@@ -139,4 +139,10 @@ public class MessageReadWriteService implements IMessageReadWrite {
         }
         return msgs;
     }
         }
         return msgs;
     }
+
+       @Override
+       public void stop() {
+               inBuffer = null;
+               outBuffer = null;
+       }
 }
 }
index b41156147f7d3ea04066903e4794820437ab9099..ddc87bc530a719fc3b6c3439ad6851bdeebf78bc 100644 (file)
@@ -10,6 +10,7 @@
 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
 
 import java.io.FileInputStream;
 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
 
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SelectionKey;
 import java.nio.ByteBuffer;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SelectionKey;
@@ -50,14 +51,20 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
     private ByteBuffer myNetData;                      // encrypted message to be sent
     private ByteBuffer peerAppData;                            // clear text message received from the switch
     private ByteBuffer peerNetData;                    // encrypted message from the switch
     private ByteBuffer myNetData;                      // encrypted message to be sent
     private ByteBuffer peerAppData;                            // clear text message received from the switch
     private ByteBuffer peerNetData;                    // encrypted message from the switch
+    private FileInputStream kfd = null, tfd = null;
 
     public SecureMessageReadWriteService(SocketChannel socket, Selector selector) throws Exception {
        this.socket = socket;
        this.selector = selector;
        this.factory = new BasicFactory();
 
 
     public SecureMessageReadWriteService(SocketChannel socket, Selector selector) throws Exception {
        this.socket = socket;
        this.selector = selector;
        this.factory = new BasicFactory();
 
-       createSecureChannel(socket);
-       createBuffers(sslEngine);
+       try {
+               createSecureChannel(socket);
+               createBuffers(sslEngine);
+       } catch (Exception e) {
+               stop();
+               throw e;
+       }
     }
 
        /**
     }
 
        /**
@@ -67,17 +74,19 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
         * @throws Exception
         */
     private void createSecureChannel(SocketChannel socket) throws Exception {
         * @throws Exception
         */
     private void createSecureChannel(SocketChannel socket) throws Exception {
-       String keyStoreFile = System.getProperty("controllerKeyStore");
-       String keyStorePassword = System.getProperty("controllerKeyStorePassword");
-       String trustStoreFile = System.getProperty("controllerTrustStore");
-       String trustStorePassword = System.getProperty("controllerTrustStorePassword");
+       String keyStoreFile = System.getProperty("controllerKeyStore").trim();
+       String keyStorePassword = System.getProperty("controllerKeyStorePassword").trim();
+       String trustStoreFile = System.getProperty("controllerTrustStore").trim();
+       String trustStorePassword = System.getProperty("controllerTrustStorePassword").trim();
 
         KeyStore ks = KeyStore.getInstance("JKS");
         KeyStore ts = KeyStore.getInstance("JKS");
         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
 
         KeyStore ks = KeyStore.getInstance("JKS");
         KeyStore ts = KeyStore.getInstance("JKS");
         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
         TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
-        ks.load(new FileInputStream(keyStoreFile), keyStorePassword.toCharArray());
-        ts.load(new FileInputStream(trustStoreFile), trustStorePassword.toCharArray());
+        kfd = new FileInputStream(keyStoreFile);
+        tfd = new FileInputStream(trustStoreFile);
+        ks.load(kfd, keyStorePassword.toCharArray());
+        ts.load(tfd, trustStorePassword.toCharArray());
         kmf.init(ks, keyStorePassword.toCharArray());
         tmf.init(ts);
 
         kmf.init(ks, keyStorePassword.toCharArray());
         tmf.init(ts);
 
@@ -344,4 +353,23 @@ public class SecureMessageReadWriteService implements IMessageReadWrite {
        this.myNetData = ByteBuffer.allocate(session.getPacketBufferSize());
        this.peerNetData = ByteBuffer.allocate(session.getPacketBufferSize());
     }
        this.myNetData = ByteBuffer.allocate(session.getPacketBufferSize());
        this.peerNetData = ByteBuffer.allocate(session.getPacketBufferSize());
     }
+
+       @Override
+       public void stop() throws IOException {
+               this.sslEngine = null;
+               this.sslEngineResult = null;
+               this.myAppData = null;
+               this.myNetData = null;
+               this.peerAppData = null;
+               this.peerNetData = null;
+           
+           if (this.kfd != null) {
+               this.kfd.close();
+               this.kfd = null;
+               }
+               if (this.tfd != null) {
+                       this.tfd.close();
+                       this.tfd = null;
+               }
+       }
 }
 }
index b87b785e3c1aa8f0d855c12208bba8088a421cf1..78ab3275a4ac674333f8b4cb26464b61488d2cda 100644 (file)
@@ -9,6 +9,7 @@
 
 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
 
 
 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
 
+import java.io.IOException;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
@@ -186,17 +187,33 @@ public class SwitchHandler implements ISwitch {
     }
 
     public void stop() {
     }
 
     public void stop() {
-        try {
-            running = false;
-            selector.wakeup();
-            cancelSwitchTimer();
-            this.selector.close();
-            this.socket.close();
-            executor.shutdown();
-        } catch (Exception e) {
-               // do nothing since we are shutting down.
-               return;
-        }
+       running = false;
+       cancelSwitchTimer();
+       try {
+               selector.wakeup();
+               selector.close();
+               } catch (Exception e) {
+               }
+       try {
+                       socket.close();
+               } catch (Exception e) {
+               }
+       try {
+                       msgReadWriteService.stop();
+               } catch (Exception e) {
+               }
+       executor.shutdown();
+       
+       selector = null;
+       socket = null;
+               msgReadWriteService = null;
+               
+               if (switchHandlerThread != null) {
+                       switchHandlerThread.interrupt();
+               }
+               if (transmitThread != null) {
+                       transmitThread.interrupt();
+               }
     }
 
     @Override
     }
 
     @Override
@@ -713,7 +730,8 @@ public class SwitchHandler implements ISwitch {
         */
     class PriorityMessageTransmit implements Runnable {
         public void run() {
         */
     class PriorityMessageTransmit implements Runnable {
         public void run() {
-            while (true) {
+            running = true;
+            while (running) {
                try {
                        if (!transmitQ.isEmpty()) {
                                PriorityMessage pmsg = transmitQ.poll();
                try {
                        if (!transmitQ.isEmpty()) {
                                PriorityMessage pmsg = transmitQ.poll();
@@ -725,6 +743,7 @@ public class SwitchHandler implements ISwitch {
                        reportError(e);
                }
             }
                        reportError(e);
                }
             }
+               transmitQ = null;
         }
     }
 
         }
     }
 
@@ -762,7 +781,7 @@ public class SwitchHandler implements ISwitch {
     }
     
     private IMessageReadWrite getMessageReadWriteService() throws Exception {
     }
     
     private IMessageReadWrite getMessageReadWriteService() throws Exception {
-       String str = System.getProperty("secureChannelEnabled");
+       String str = System.getProperty("secureChannelEnabled").trim();
         return ((str != null) && (str.equalsIgnoreCase("true"))) ? 
                        new SecureMessageReadWriteService(socket, selector) : 
                        new MessageReadWriteService(socket, selector);
         return ((str != null) && (str.equalsIgnoreCase("true"))) ? 
                        new SecureMessageReadWriteService(socket, selector) : 
                        new MessageReadWriteService(socket, selector);
index 25058a3e1386c58b2d349fc64893f60b4e281b0e..227df54944b115107079a93058c4845824110713 100644 (file)
@@ -733,7 +733,7 @@ public class TopologyManagerImpl implements ITopologyManager,
 
         String dpid = ci.nextArgument();
         if (dpid == null) {
 
         String dpid = ci.nextArgument();
         if (dpid == null) {
-            ci.println("Null source ndoe id");
+            ci.println("Null source node id");
             return;
         }
 
             return;
         }