Merge "Added the missing Copyright headers to most of the java files."
[netvirt.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / SouthboundHandler.java
index 2aac3213c67c845a9baa32b929b928aa303833d8..9f410e5d33d1ecc656e94f822420e58469a64782 100644 (file)
@@ -1,8 +1,19 @@
+/*
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Madhu Venugopal, Brent Salisbury
+ */
 package org.opendaylight.ovsdb.neutron;
 
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 
 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
@@ -19,21 +30,54 @@ import org.slf4j.LoggerFactory;
 
 public class SouthboundHandler extends BaseHandler implements OVSDBInventoryListener {
     static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
-    private Thread eventThread;
+    //private Thread eventThread;
+    private ExecutorService eventHandler;
     private BlockingQueue<SouthboundEvent> events;
 
     void init() {
-        eventThread = new Thread(new EventHandler(), "SouthBound Event Thread");
+        eventHandler = Executors.newSingleThreadExecutor();
         this.events = new LinkedBlockingQueue<SouthboundEvent>();
     }
 
     void start() {
-        eventThread.start();
+        eventHandler.submit(new Runnable()  {
+            @Override
+            public void run() {
+                while (true) {
+                    SouthboundEvent ev;
+                    try {
+                        ev = events.take();
+                    } catch (InterruptedException e) {
+                        logger.info("The event handler thread was interrupted, shutting down", e);
+                        return;
+                    }
+                    switch (ev.getType()) {
+                    case NODE:
+                        try {
+                            processNodeUpdate(ev.getNode(), ev.getAction());
+                        } catch (Exception e) {
+                            logger.error("Exception caught in ProcessNodeUpdate for node " + ev.getNode(), e);
+                        }
+                        break;
+                    case ROW:
+                        try {
+                            processRowUpdate(ev.getNode(), ev.getTableName(), ev.getUuid(), ev.getRow(), ev.getAction());
+                        } catch (Exception e) {
+                            logger.error("Exception caught in ProcessRowUpdate for node " + ev.getNode(), e);
+                        }
+                        break;
+                    default:
+                        logger.warn("Unable to process action " + ev.getAction() + " for node " + ev.getNode());
+                    }
+                }
+            }
+        });
     }
 
     void stop() {
-        eventThread.interrupt();
+        eventHandler.shutdownNow();
     }
+
     @Override
     public void nodeAdded(Node node) {
         this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.ADD));
@@ -94,45 +138,26 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
         try {
             events.put(event);
         } catch (InterruptedException e) {
-            e.printStackTrace();
+            logger.error("Thread was interrupted while trying to enqueue event ", e);
         }
 
     }
-    private class EventHandler implements Runnable {
-        @Override
-        public void run() {
-            while (true) {
-                try {
-                    SouthboundEvent ev = events.take();
-                    switch (ev.getType()) {
-                    case NODE:
-                        ProcessNodeUpdate(ev.getNode(), ev.getAction());
-                    case ROW:
-                        ProcessRowUpdate(ev.getNode(), ev.getTableName(), ev.getUuid(), ev.getRow(), ev.getAction());
-                        break;
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
 
-    public void ProcessNodeUpdate(Node node, SouthboundEvent.Action action) {
+    public void processNodeUpdate(Node node, SouthboundEvent.Action action) {
         if (action == SouthboundEvent.Action.DELETE) return;
         logger.trace("Process Node added {}", node);
         InternalNetworkManager.getManager().prepareInternalNetwork(node);
     }
 
-    private void ProcessRowUpdate(Node node, String tableName, String uuid, Table<?> row,
+    private void processRowUpdate(Node node, String tableName, String uuid, Table<?> row,
                                   SouthboundEvent.Action action) {
         if (action == SouthboundEvent.Action.DELETE) return;
 
         if (Interface.NAME.getName().equalsIgnoreCase(tableName)) {
-            logger.debug("trace {} Added / Updated {} , {}, {}", tableName, node, uuid, row);
+            logger.debug("{} Added / Updated {} , {}, {}", tableName, node, uuid, row);
             Interface intf = (Interface)row;
             NeutronNetwork network = TenantNetworkManager.getManager().getTenantNetworkForInterface(intf);
-            if (network != null) {
+            if (network != null && !network.getRouterExternal()) {
                 int vlan = TenantNetworkManager.getManager().networkCreated(network.getID());
                 logger.trace("Neutron Network {} Created with Internal Vlan : {}", network.toString(), vlan);
 
@@ -143,7 +168,7 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
                 this.createTunnels(node, uuid, intf);
             }
         } else if (Port.NAME.getName().equalsIgnoreCase(tableName)) {
-            logger.debug("trace {} Added / Updated {} , {}, {}", tableName, node, uuid, row);
+            logger.debug("{} Added / Updated {} , {}, {}", tableName, node, uuid, row);
             Port port = (Port)row;
             Set<UUID> interfaceUUIDs = port.getInterfaces();
             for (UUID intfUUID : interfaceUUIDs) {
@@ -151,15 +176,15 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
                 try {
                     Interface intf = (Interface)this.ovsdbConfigService.getRow(node, Interface.NAME.getName(), intfUUID.toString());
                     NeutronNetwork network = TenantNetworkManager.getManager().getTenantNetworkForInterface(intf);
-                    if (network != null) {
+                    if (network != null && !network.getRouterExternal()) {
                         TenantNetworkManager.getManager().programTenantNetworkInternalVlan(node, uuid, network);
                     }
                 } catch (Exception e) {
-                    e.printStackTrace();
+                    logger.error("Failed to process row update", e);
                 }
             }
         } else if (Open_vSwitch.NAME.getName().equalsIgnoreCase(tableName)) {
-            logger.debug("trace {} Added / Updated {} , {}, {}", tableName, node, uuid, row);
+            logger.debug("{} Added / Updated {} , {}, {}", tableName, node, uuid, row);
             AdminConfigManager.getManager().populateTunnelEndpoint(node);
             try {
                 Map<String, Table<?>> interfaces = this.ovsdbConfigService.getRows(node, Interface.NAME.getName());
@@ -170,7 +195,7 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
                     }
                 }
             } catch (Exception e) {
-                logger.error("Error fetching Interface Rows for node {}", node);
+                logger.error("Error fetching Interface Rows for node " + node, e);
             }
         }
     }