Merge "Wait to process datastore until providers are ready"
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / LBaaSPoolHandler.java
index 7dffc7f5bb2f0f8ec5fcaf6daa5bcc1be306299d..ee349b235a161403781634dea74f2c7feb7e795c 100755 (executable)
 
 package org.opendaylight.ovsdb.openstack.netvirt;
 
-import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerCRUD;
-import org.opendaylight.controller.networkconfig.neutron.INeutronLoadBalancerPoolAware;
-import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
-import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
-import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
-import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancer;
-import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPool;
-import org.opendaylight.controller.networkconfig.neutron.NeutronLoadBalancerPoolMember;
-import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.switchmanager.ISwitchManager;
+import org.opendaylight.neutron.spi.INeutronLoadBalancerCRUD;
+import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolAware;
+import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
+import org.opendaylight.neutron.spi.INeutronPortCRUD;
+import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
+import org.opendaylight.neutron.spi.NeutronLoadBalancer;
+import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
+import org.opendaylight.neutron.spi.NeutronLoadBalancerPoolMember;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
+import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerConfiguration;
 import org.opendaylight.ovsdb.openstack.netvirt.api.LoadBalancerProvider;
+import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+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;
 
@@ -40,17 +44,18 @@ import java.util.Map;
  */
 
 public class LBaaSPoolHandler extends AbstractHandler
-        implements INeutronLoadBalancerPoolAware {
+        implements INeutronLoadBalancerPoolAware, ConfigInterface {
 
     private static final Logger logger = LoggerFactory.getLogger(LBaaSPoolHandler.class);
 
     // The implementation for each of these services is resolved by the OSGi Service Manager
     private volatile INeutronLoadBalancerCRUD neutronLBCache;
-    private volatile INeutronPortCRUD neutronPortsCache;
+    private volatile INeutronPortCRUD neutronPortCache;
     private volatile INeutronNetworkCRUD neutronNetworkCache;
     private volatile INeutronSubnetCRUD neutronSubnetCache;
     private volatile LoadBalancerProvider loadBalancerProvider;
-    private volatile ISwitchManager switchManager;
+    private volatile NodeCacheManager nodeCacheManager;
+    private volatile EventDispatcher eventDispatcher;
 
     @Override
     public int canCreateNeutronLoadBalancerPool(NeutronLoadBalancerPool neutronLBPool) {
@@ -80,11 +85,12 @@ public class LBaaSPoolHandler extends AbstractHandler
     private void doNeutronLoadBalancerPoolCreate(NeutronLoadBalancerPool neutronLBPool) {
         Preconditions.checkNotNull(loadBalancerProvider);
         List<LoadBalancerConfiguration> lbConfigList = extractLBConfiguration(neutronLBPool);
+        final List<Node> nodes = nodeCacheManager.getBridgeNodes();
         if (lbConfigList == null) {
             logger.debug("Neutron LB configuration invalid for pool {} ", neutronLBPool.getLoadBalancerPoolID());
         } else if (lbConfigList.size() == 0) {
             logger.debug("No Neutron LB VIP not created yet for pool {} ", neutronLBPool.getLoadBalancerPoolID());
-        } else if (this.switchManager.getNodes().size() == 0) {
+        } else if (nodes.isEmpty()) {
             logger.debug("Noop with LB pool {} creation because no nodes available.", neutronLBPool.getLoadBalancerPoolID());
         } else {
             for (LoadBalancerConfiguration lbConfig: lbConfigList) {
@@ -92,7 +98,7 @@ public class LBaaSPoolHandler extends AbstractHandler
                     logger.debug("Neutron LB pool configuration invalid for {} ", lbConfig.getName());
                     continue;
                 } else {
-                    for (Node node: this.switchManager.getNodes()) {
+                    for (Node node : nodes) {
                         loadBalancerProvider.programLoadBalancerRules(node, lbConfig, Action.ADD);
                     }
                 }
@@ -135,11 +141,12 @@ public class LBaaSPoolHandler extends AbstractHandler
         Preconditions.checkNotNull(loadBalancerProvider);
 
         List<LoadBalancerConfiguration> lbConfigList = extractLBConfiguration(neutronLBPool);
+        final List<Node> nodes = nodeCacheManager.getBridgeNodes();
         if (lbConfigList == null) {
             logger.debug("Neutron LB configuration invalid for pool {} ", neutronLBPool.getLoadBalancerPoolID());
         } else if (lbConfigList.size() == 0) {
             logger.debug("No Neutron LB VIP not created yet for pool {} ", neutronLBPool.getLoadBalancerPoolID());
-        } else if (this.switchManager.getNodes().size() == 0) {
+        } else if (nodes.isEmpty()) {
             logger.debug("Noop with LB pool {} deletion because no nodes available.", neutronLBPool.getLoadBalancerPoolID());
         } else {
             for (LoadBalancerConfiguration lbConfig: lbConfigList) {
@@ -147,7 +154,7 @@ public class LBaaSPoolHandler extends AbstractHandler
                     logger.debug("Neutron LB pool configuration invalid for {} ", lbConfig.getName());
                     continue;
                 } else {
-                    for (Node node: this.switchManager.getNodes()) {
+                    for (Node node : nodes) {
                         loadBalancerProvider.programLoadBalancerRules(node, lbConfig, Action.DELETE);
                     }
                 }
@@ -225,7 +232,7 @@ public class LBaaSPoolHandler extends AbstractHandler
                 lbConfig.setProviderNetworkType(providerInfo.getKey());
                 lbConfig.setProviderSegmentationId(providerInfo.getValue());
             }
-            lbConfig.setVmac(NeutronCacheUtils.getMacAddress(neutronPortsCache, loadBalancerSubnetID, loadBalancerVip));
+            lbConfig.setVmac(NeutronCacheUtils.getMacAddress(neutronPortCache, loadBalancerSubnetID, loadBalancerVip));
 
             /* Iterate over all the members in this pool and find those in same
              * subnet as the VIP. Those will be included in the lbConfigList
@@ -246,7 +253,7 @@ public class LBaaSPoolHandler extends AbstractHandler
                         logger.debug("Neutron LB pool member details incomplete: {}", neutronLBPoolMember);
                         continue;
                     }
-                    memberMAC = NeutronCacheUtils.getMacAddress(neutronPortsCache, memberSubnetID, memberIP);
+                    memberMAC = NeutronCacheUtils.getMacAddress(neutronPortCache, memberSubnetID, memberIP);
                     if (memberMAC == null) {
                         continue;
                     }
@@ -260,4 +267,32 @@ public class LBaaSPoolHandler extends AbstractHandler
 
         return lbConfigList;
     }
+
+    @Override
+    public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
+        loadBalancerProvider =
+                (LoadBalancerProvider) ServiceHelper.getGlobalInstance(LoadBalancerProvider.class, this);
+        nodeCacheManager =
+                (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
+        eventDispatcher =
+                (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
+        eventDispatcher.eventHandlerAdded(
+                bundleContext.getServiceReference(INeutronLoadBalancerPoolAware.class.getName()), this);
+        super.setDispatcher(eventDispatcher);
+    }
+
+    @Override
+    public void setDependencies(Object impl) {
+        if (impl instanceof INeutronNetworkCRUD) {
+            neutronNetworkCache = (INeutronNetworkCRUD)impl;
+        } else if (impl instanceof INeutronPortCRUD) {
+            neutronPortCache = (INeutronPortCRUD)impl;
+        } else if (impl instanceof INeutronSubnetCRUD) {
+            neutronSubnetCache = (INeutronSubnetCRUD)impl;
+        } else if (impl instanceof INeutronLoadBalancerCRUD) {
+            neutronLBCache = (INeutronLoadBalancerCRUD)impl;
+        } else if (impl instanceof LoadBalancerProvider) {
+            loadBalancerProvider = (LoadBalancerProvider)impl;
+        }
+    }
 }