Merge "getNode fails when id contains |"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / SouthboundHandler.java
index ef19966e673234aacedf00e4e098ab658cff31d4..9d6123507ab74cd6e33dfdc8902a01be24b13a67 100644 (file)
@@ -9,7 +9,7 @@
  */
 package org.opendaylight.ovsdb.openstack.netvirt;
 
-import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
+import org.opendaylight.neutron.spi.NeutronNetwork;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.core.Property;
@@ -17,13 +17,15 @@ import org.opendaylight.controller.sal.core.UpdateType;
 import org.opendaylight.controller.switchmanager.IInventoryListener;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.notation.UUID;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
-import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
-import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
-import org.opendaylight.ovsdb.plugin.OvsdbInventoryListener;
+import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
+import org.opendaylight.ovsdb.compatibility.plugin.api.OvsdbConfigurationService;
+import org.opendaylight.ovsdb.compatibility.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.compatibility.plugin.api.OvsdbInventoryListener;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
 import org.opendaylight.ovsdb.schema.openvswitch.Port;
@@ -32,20 +34,16 @@ import com.google.common.collect.Lists;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.net.InetAddress;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.LinkedBlockingQueue;
 
-public class SouthboundHandler extends AbstractHandler implements OvsdbInventoryListener, IInventoryListener {
+public class SouthboundHandler extends AbstractHandler implements OvsdbInventoryListener,
+                                                                  IInventoryListener {
     static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
     //private Thread eventThread;
-    private ExecutorService eventHandler;
-    private BlockingQueue<SouthboundEvent> events;
     List<Node> nodeCache;
 
     // The implementation for each of these services is resolved by the OSGi Service Manager
@@ -53,104 +51,67 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
     private volatile BridgeConfigurationManager bridgeConfigurationManager;
     private volatile TenantNetworkManager tenantNetworkManager;
     private volatile NetworkingProviderManager networkingProviderManager;
-    private volatile OvsdbConfigService ovsdbConfigService;
-    private volatile IConnectionServiceInternal connectionService;
+    private volatile OvsdbConfigurationService ovsdbConfigurationService;
+    private volatile OvsdbConnectionService connectionService;
+    private volatile NeutronL3Adapter neutronL3Adapter;
 
     void init() {
-        eventHandler = Executors.newSingleThreadExecutor();
-        this.events = new LinkedBlockingQueue<>();
         nodeCache = Lists.newArrayList();
     }
 
     void 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.getContext(),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());
-                    }
-                }
-            }
-        });
         this.triggerUpdates();
     }
 
-    void stop() {
-        eventHandler.shutdownNow();
-    }
-
     @Override
-    public void nodeAdded(Node node) {
-        this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.ADD));
+    public void nodeAdded(Node node, InetAddress address, int port) {
+        logger.info("nodeAdded: {}", node);
+        this.enqueueEvent(new SouthboundEvent(node, Action.ADD));
     }
 
     @Override
     public void nodeRemoved(Node node) {
-        this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.DELETE));
+        this.enqueueEvent(new SouthboundEvent(node, Action.DELETE));
     }
 
     @Override
     public void rowAdded(Node node, String tableName, String uuid, Row row) {
-        this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, SouthboundEvent.Action.ADD));
+        this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, Action.ADD));
     }
 
     @Override
     public void rowUpdated(Node node, String tableName, String uuid, Row oldRow, Row newRow) {
         if (this.isUpdateOfInterest(node, oldRow, newRow)) {
-            this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, SouthboundEvent.Action.UPDATE));
+            this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, Action.UPDATE));
         }
     }
 
     /*
-     * Ignore unneccesary updates to be even considered for processing.
+     * Ignore unnecessary updates to be even considered for processing.
      * (Especially stats update are fast and furious).
      */
 
     private boolean isUpdateOfInterest(Node node, Row oldRow, Row newRow) {
         if (oldRow == null) return true;
-        if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, Interface.class))) {
+        if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Interface.class))) {
             // We are NOT interested in Stats only updates
-            Interface oldIntf = ovsdbConfigService.getTypedRow(node, Interface.class, oldRow);
+            Interface oldIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, oldRow);
             if (oldIntf.getName() == null && oldIntf.getExternalIdsColumn() == null && oldIntf.getMacColumn() == null &&
                 oldIntf.getOpenFlowPortColumn() == null && oldIntf.getOptionsColumn() == null && oldIntf.getOtherConfigColumn() == null &&
                 oldIntf.getTypeColumn() == null) {
                 logger.trace("IGNORING Interface Update: node {}, row: {}", node, newRow);
                 return false;
             }
-        } else if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, Port.class))) {
+        } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Port.class))) {
             // We are NOT interested in Stats only updates
-            Port oldPort = ovsdbConfigService.getTypedRow(node, Port.class, oldRow);
+            Port oldPort = ovsdbConfigurationService.getTypedRow(node, Port.class, oldRow);
             if (oldPort.getName() == null && oldPort.getExternalIdsColumn() == null && oldPort.getMacColumn() == null &&
                 oldPort.getInterfacesColumn() == null && oldPort.getTagColumn() == null && oldPort.getTrunksColumn() == null) {
                 logger.trace("IGNORING Port Update: node {}, row: {}", node, newRow);
                 return false;
             }
-        } else if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, OpenVSwitch.class))) {
-            OpenVSwitch oldOpenvSwitch = ovsdbConfigService.getTypedRow(node, OpenVSwitch.class, oldRow);
+        } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
+            OpenVSwitch oldOpenvSwitch = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, oldRow);
             if (oldOpenvSwitch.getOtherConfigColumn()== null) {
                 /* we are only interested in other_config field change */
                 return false;
@@ -161,29 +122,21 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
 
     @Override
     public void rowRemoved(Node node, String tableName, String uuid, Row row, Object context) {
-        this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, context, SouthboundEvent.Action.DELETE));
-    }
-
-    private void enqueueEvent (SouthboundEvent event) {
-        try {
-            events.put(event);
-        } catch (InterruptedException e) {
-            logger.error("Thread was interrupted while trying to enqueue event ", e);
-        }
+        this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, context, Action.DELETE));
     }
 
-    public void processNodeUpdate(Node node, SouthboundEvent.Action action) {
-        if (action == SouthboundEvent.Action.DELETE) return;
+    public void processNodeUpdate(Node node, Action action) {
+        if (action == Action.DELETE) return;
         logger.trace("Process Node added {}", node);
         bridgeConfigurationManager.prepareNode(node);
     }
 
     private void processRowUpdate(Node node, String tableName, String uuid, Row row,
-                                  Object context, SouthboundEvent.Action action) {
-        if (action == SouthboundEvent.Action.DELETE) {
-            if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Interface.class))) {
+                                  Object context, Action action) {
+        if (action == Action.DELETE) {
+            if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
                 logger.debug("Processing update of {}. Deleted node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
-                Interface deletedIntf = ovsdbConfigService.getTypedRow(node, Interface.class, row);
+                Interface deletedIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
                 NeutronNetwork network = null;
                 if (context == null) {
                     network = tenantNetworkManager.getTenantNetwork(deletedIntf);
@@ -202,12 +155,13 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
                     logger.debug("Processing update of {}:{} node {} intf {} network {}",
                             tableName, action, node, uuid, network.getNetworkUUID());
                     try {
-                        ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
+                        ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
+                                .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
                         if (interfaces != null) {
                             boolean isLastInstanceOnNode = true;
                             for (String intfUUID : interfaces.keySet()) {
                                 if (intfUUID.equals(uuid)) continue;
-                                Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
+                                Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
                                 NeutronNetwork neutronNetwork = tenantNetworkManager.getTenantNetwork(intf);
                                 if (neutronNetwork != null && neutronNetwork.equals(network)) isLastInstanceOnNode = false;
                             }
@@ -219,10 +173,10 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
                 }
             }
         }
-        else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Interface.class))) {
+        else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
             logger.debug("Processing update of {}:{} node: {}, interface uuid: {}, row: {}",
                     tableName, action, node, uuid, row);
-            Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, row);
+            Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
             NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
             if (network != null && !network.getRouterExternal()) {
                 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
@@ -239,15 +193,18 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
                 }
                 this.handleInterfaceUpdate(node, uuid, intf);
             }
-        } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Port.class))) {
+
+        } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Port.class))) {
             logger.debug("Processing update of {}:{} node: {}, port uuid: {}, row: {}", tableName, action, node, uuid, row);
-            Port port = this.ovsdbConfigService.getTypedRow(node, Port.class, row);
+            Port port = this.ovsdbConfigurationService.getTypedRow(node, Port.class, row);
             Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
             for (UUID intfUUID : interfaceUUIDs) {
                 logger.trace("Scanning interface "+intfUUID);
                 try {
-                    Row intfRow = this.ovsdbConfigService.getRow(node, ovsdbConfigService.getTableName(node, Interface.class), intfUUID.toString());
-                    Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, intfRow);
+                    Row intfRow = this.ovsdbConfigurationService
+                            .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
+                                    intfUUID.toString());
+                    Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, intfRow);
                     NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
                     if (network != null && !network.getRouterExternal()) {
                          logger.debug("Processing update of {}:{} node {} intf {} network {}",
@@ -262,13 +219,14 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
                     logger.error("Failed to process row update", e);
                 }
             }
-        } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, OpenVSwitch.class))) {
+        } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
             logger.debug("Processing update of {}:{} node: {}, ovs uuid: {}, row: {}", tableName, action, node, uuid, row);
             try {
-                ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
+                ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
+                        .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
                 if (interfaces != null) {
                     for (String intfUUID : interfaces.keySet()) {
-                        Interface intf = ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
+                        Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
                         this.handleInterfaceUpdate(node, intfUUID, intf);
                     }
                 }
@@ -282,6 +240,7 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
         logger.trace("Interface update of node: {}, uuid: {}", node, uuid);
         NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
         if (network != null) {
+            neutronL3Adapter.handleInterfaceEvent(node, intf, network, Action.UPDATE);
             if (bridgeConfigurationManager.createLocalNetwork(node, network))
                 networkingProviderManager.getProvider(node).handleInterfaceUpdate(network, node, intf);
         } else {
@@ -294,6 +253,7 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
         logger.debug("handleInterfaceDelete: node: {}, uuid: {}, isLastInstanceOnNode: {}, interface: {}",
                 node, uuid, isLastInstanceOnNode, intf);
 
+        neutronL3Adapter.handleInterfaceEvent(node, intf, network, Action.DELETE);
         List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
         if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
             intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
@@ -316,10 +276,10 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
 
     private String getPortIdForInterface (Node node, String uuid, Interface intf) {
         try {
-            Map<String, Row> ports = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Port.class));
+            Map<String, Row> ports = this.ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
             if (ports == null) return null;
             for (String portUUID : ports.keySet()) {
-                Port port = ovsdbConfigService.getTypedRow(node, Port.class, ports.get(portUUID));
+                Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, ports.get(portUUID));
                 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
                 logger.trace("Scanning Port {} to identify interface : {} ",port, uuid);
                 for (UUID intfUUID : interfaceUUIDs) {
@@ -330,7 +290,7 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
                 }
             }
         } catch (Exception e) {
-            logger.debug("Failed to get Port tag for for Intf {}:{}", intf, e);
+            logger.debug("Failed to get Port tag for for Intf " + intf, e);
         }
         return null;
     }
@@ -358,10 +318,10 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
         if (nodes == null) return;
         for (Node node : nodes) {
             try {
-                List<String> tableNames = ovsdbConfigService.getTables(node);
+                List<String> tableNames = ovsdbConfigurationService.getTables(node);
                 if (tableNames == null) continue;
                 for (String tableName : tableNames) {
-                    Map<String, Row> rows = ovsdbConfigService.getRows(node, tableName);
+                    Map<String, Row> rows = ovsdbConfigurationService.getRows(node, tableName);
                     if (rows == null) continue;
                     for (String uuid : rows.keySet()) {
                         Row row = rows.get(uuid);
@@ -373,4 +333,40 @@ public class SouthboundHandler extends AbstractHandler implements OvsdbInventory
             }
         }
     }
+
+    /**
+     * Process the event.
+     *
+     * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
+     * @see EventDispatcher
+     */
+    @Override
+    public void processEvent(AbstractEvent abstractEvent) {
+        if (!(abstractEvent instanceof SouthboundEvent)) {
+            logger.error("Unable to process abstract event " + abstractEvent);
+            return;
+        }
+        SouthboundEvent ev = (SouthboundEvent) abstractEvent;
+        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.getContext(),ev.getAction());
+                } catch (Exception e) {
+                    logger.error("Exception caught in ProcessRowUpdate for node " + ev.getNode(), e);
+                }
+                break;
+            default:
+                logger.warn("Unable to process type " + ev.getType() +
+                            " action " + ev.getAction() + " for node " + ev.getNode());
+                break;
+        }
+    }
 }