Bug 4160 - null pointer exception in getDHCPServerPort() (re-do)
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandler.java
index cb78649a779e6fa94f5bddca79c191f7cd057cba..40f2643858d72f89f6df82220a9e8a1b7abac91e 100644 (file)
@@ -1,52 +1,42 @@
 /*
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
  *
  * 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.openstack.netvirt;
 
+import java.net.HttpURLConnection;
+import java.util.List;
+
 import org.opendaylight.neutron.spi.INeutronPortAware;
 import org.opendaylight.neutron.spi.NeutronPort;
-import org.opendaylight.controller.sal.core.Node;
-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.Constants;
+import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
+import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
-import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
-import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
-import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryListener;
-import org.opendaylight.ovsdb.schema.openvswitch.Interface;
-import org.opendaylight.ovsdb.schema.openvswitch.Port;
-
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.net.HttpURLConnection;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentMap;
-
 /**
  * Handle requests for Neutron Port.
  */
-public class PortHandler extends AbstractHandler
-                         implements INeutronPortAware {
-
-    /**
-     * Logger instance.
-     */
-    static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
+public class PortHandler extends AbstractHandler implements INeutronPortAware, ConfigInterface {
+    private static final Logger LOG = LoggerFactory.getLogger(PortHandler.class);
 
     // The implementation for each of these services is resolved by the OSGi Service Manager
-    private volatile OvsdbConfigurationService ovsdbConfigurationService;
-    private volatile OvsdbConnectionService connectionService;
-    private volatile OvsdbInventoryListener ovsdbInventoryListener;
+    private volatile NodeCacheManager nodeCacheManager;
     private volatile NeutronL3Adapter neutronL3Adapter;
+    private volatile Southbound southbound;
 
     /**
      * Invoked when a port creation is requested
@@ -57,27 +47,20 @@ public class PortHandler extends AbstractHandler
      */
     @Override
     public int canCreatePort(NeutronPort port) {
-        return HttpURLConnection.HTTP_CREATED;
+        return HttpURLConnection.HTTP_OK;
     }
 
     /**
      * Invoked to take action after a port has been created.
      *
-     * @param port An instance of new Neutron Port object.
+     * @param neutronPort An instance of new Neutron Port object.
      */
     @Override
     public void neutronPortCreated(NeutronPort neutronPort) {
-        int result = canCreatePort(neutronPort);
-        if (result != HttpURLConnection.HTTP_CREATED) {
-            logger.error(" Port create validation failed result - {} ", result);
-            return;
-        }
-
         enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
     }
     private void doNeutronPortCreated(NeutronPort neutronPort) {
-        logger.debug(" Port-ADD successful for tenant-id - {}," +
-                     " network-id - {}, port-id - {}",
+        LOG.debug(" Port-ADD successful for tenant-id - {}, network-id - {}, port-id - {}",
                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
                      neutronPort.getID());
         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
@@ -96,29 +79,20 @@ public class PortHandler extends AbstractHandler
     @Override
     public int canUpdatePort(NeutronPort delta,
                              NeutronPort original) {
-        int result = HttpURLConnection.HTTP_OK;
-        /**
-         * To basic validation of the request
-         */
-
-        if ((original == null) || (delta == null)) {
-            logger.error("port object not specified");
-            return HttpURLConnection.HTTP_BAD_REQUEST;
-        }
-        return result;
+        return HttpURLConnection.HTTP_OK;
     }
 
     /**
      * Invoked to take action after a port has been updated.
      *
-     * @param port An instance of modified Neutron Port object.
+     * @param neutronPort An instance of modified Neutron Port object.
      */
     @Override
     public void neutronPortUpdated(NeutronPort neutronPort) {
         enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
     }
     private void doNeutronPortUpdated(NeutronPort neutronPort) {
-        logger.debug("Handling neutron update port " + neutronPort);
+        LOG.debug("Handling neutron update port {}", neutronPort);
         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
     }
 
@@ -141,78 +115,44 @@ public class PortHandler extends AbstractHandler
      */
     @Override
     public void neutronPortDeleted(NeutronPort neutronPort) {
-
-        int result = canDeletePort(neutronPort);
-        if  (result != HttpURLConnection.HTTP_OK) {
-            logger.error(" deletePort validation failed - result {} ", result);
-            return;
-        }
-
         enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
     }
     private void doNeutronPortDeleted(NeutronPort neutronPort) {
-        logger.debug("Handling neutron delete port " + neutronPort);
+        LOG.debug("Handling neutron delete port {}", neutronPort);
         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
-
-        List<Node> nodes = connectionService.getNodes();
+        //TODO: Need to implement getNodes
+        List<Node> nodes = nodeCacheManager.getNodes();
         for (Node node : nodes) {
             try {
-                ConcurrentMap<String, Row> portRows =
-                        this.ovsdbConfigurationService.getRows(node,
-                                                        ovsdbConfigurationService.getTableName(node, Port.class));
-                if (portRows != null) {
-                    for (Row portRow : portRows.values()) {
-                        Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
-                        for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
-                            Row ifaceRow = ovsdbConfigurationService
-                                    .getRow(node,
-                                            ovsdbConfigurationService.getTableName(node, Interface.class),
-                                            interfaceUuid.toString());
-                            Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
-                            Map<String, String> externalIds = iface.getExternalIdsColumn().getData();
-
-                            if (externalIds == null) {
-                                logger.trace("No external_ids seen in {}", iface.getName());
-                                continue;
-                            }
-
-                            /* Compare Neutron port uuid */
-                            String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
-                            if (neutronPortId == null) {
-                                continue;
-                            }
-
-                            if (neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
-                                logger.trace("neutronPortDeleted: Delete interface {}", iface.getName());
-                                ovsdbConfigurationService.deleteRow(node,
-                                                             ovsdbConfigurationService.getTableName(node, Port.class),
-                                                             port.getUuid().toString());
-                                break;
-                            }
-                        }
+                List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
+                for (OvsdbTerminationPointAugmentation port : ports) {
+                    String neutronPortId =
+                            southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
+                    if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
+                        LOG.trace("neutronPortDeleted: Delete interface {}", port.getName());
+                        southbound.deleteTerminationPoint(node, port.getName());
+                        break;
                     }
                 }
             } catch (Exception e) {
-                logger.error("Exception during handlingNeutron port delete", e);
+                LOG.error("Exception during handlingNeutron port delete", e);
             }
         }
-        logger.debug(" PORT delete successful for tenant-id - {}, " +
-                     " network-id - {}, port-id - {}",
+        LOG.debug(" PORT delete successful for tenant-id - {}, network-id - {}, port-id - {}",
                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
                      neutronPort.getID());
-
     }
 
     /**
      * Process the event.
      *
      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
-     * @see EventDispatcher
+     * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
      */
     @Override
     public void processEvent(AbstractEvent abstractEvent) {
         if (!(abstractEvent instanceof NorthboundEvent)) {
-            logger.error("Unable to process abstract event " + abstractEvent);
+            LOG.error("Unable to process abstract event {}", abstractEvent);
             return;
         }
         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
@@ -227,8 +167,25 @@ public class PortHandler extends AbstractHandler
                 doNeutronPortUpdated(ev.getPort());
                 break;
             default:
-                logger.warn("Unable to process event action " + ev.getAction());
+                LOG.warn("Unable to process event action {}", ev.getAction());
                 break;
         }
     }
+
+    @Override
+    public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
+        nodeCacheManager =
+                (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
+        neutronL3Adapter =
+                (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
+        southbound =
+                (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
+        eventDispatcher =
+                (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
+        eventDispatcher.eventHandlerAdded(
+                bundleContext.getServiceReference(INeutronPortAware.class.getName()), this);
+    }
+
+    @Override
+    public void setDependencies(Object impl) {}
 }