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 d776aeff110afecd90727e3a4c9d1659163725c5..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));
@@ -50,8 +94,39 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
     }
 
     @Override
-    public void rowUpdated(Node node, String tableName, String uuid, Table<?> row) {
-        this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, SouthboundEvent.Action.UPDATE));
+    public void rowUpdated(Node node, String tableName, String uuid, Table<?> oldRow, Table<?> newRow) {
+        if (this.isUpdateOfInterest(oldRow, newRow)) {
+            this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, SouthboundEvent.Action.UPDATE));
+        }
+    }
+
+    /*
+     * Ignore unneccesary updates to be even considered for processing.
+     * (Especially stats update are fast and furious).
+     */
+
+    private boolean isUpdateOfInterest(Table<?> oldRow, Table<?> newRow) {
+        if (oldRow == null) return true;
+        if (newRow.getTableName().equals(Interface.NAME)) {
+            // We are NOT interested in Stats only updates
+            Interface oldIntf = (Interface)oldRow;
+            if (oldIntf.getName() == null && oldIntf.getExternal_ids() == null && oldIntf.getMac() == null &&
+                oldIntf.getOfport() == null && oldIntf.getOptions() == null && oldIntf.getOther_config() == null &&
+                oldIntf.getType() == null) {
+                logger.trace("IGNORING Interface Update : "+newRow.toString());
+                return false;
+            }
+        } else if (newRow.getTableName().equals(Port.NAME)) {
+            // We are NOT interested in Stats only updates
+            Port oldPort = (Port)oldRow;
+            if (oldPort.getName() == null && oldPort.getExternal_ids() == null && oldPort.getMac() == null &&
+                oldPort.getInterfaces() == null && oldPort.getTag() == null && oldPort.getTrunks() == null) {
+                logger.trace("IGNORING Port Update : "+newRow.toString());
+                return false;
+            }
+        }
+
+        return true;
     }
 
     @Override
@@ -63,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);
 
@@ -112,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) {
@@ -120,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());
@@ -139,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);
             }
         }
     }