Use Felix DM for OVSDB Neutron Services
[ovsdb.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / NodeConfiguration.java
index e0d76771ec149381194eed5f0a56eab667f662e7..ae9914384e20d8392a5d33a22d1a1f46f223f1a8 100644 (file)
 
 package org.opendaylight.ovsdb.neutron;
 
+import java.math.BigInteger;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.ovsdb.lib.notation.UUID;
@@ -21,29 +27,23 @@ import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.math.BigInteger;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ConcurrentHashMap;
-
 public class NodeConfiguration {
     static final Logger logger = LoggerFactory.getLogger(NodeConfiguration.class);
     private static final int MAX_VLAN = 4096;
     private java.util.Queue<Integer> internalVlans = new LinkedList<>();
     private ConcurrentMap<String, Integer> tenantVlanMap = new ConcurrentHashMap<>();
+    private ITenantNetworkManager tenantNetworkManager;
 
-    public NodeConfiguration(Node node) {
+    public NodeConfiguration(Node node, ITenantNetworkManager tenantNetworkManager) {
         for (int i = 1; i < MAX_VLAN ; i++) {
             internalVlans.add(i);
         }
-
+        setTenantNetworkManager(tenantNetworkManager);
         initializeNodeConfiguration(node);
     }
 
 
     private void initializeNodeConfiguration(Node node) {
-
         int vlan = 0;
         String networkId = new String();
         OVSDBConfigService ovsdbTable = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
@@ -66,7 +66,7 @@ public class NodeConfiguration {
                     vlan = tags[0].intValue();
                 }
                 else {
-                   logger.debug("This port has more {} interfaces", tags.length);
+                   logger.debug("This port ({}) has {} tags", port.getName(), tags.length);
                    continue;
                 }
 
@@ -78,7 +78,7 @@ public class NodeConfiguration {
                         continue;
                     }
 
-                    networkId = TenantNetworkManager.getManager().getTenantNetworkForInterface(iface).getNetworkUUID();
+                    networkId = tenantNetworkManager.getTenantNetworkForInterface(iface).getNetworkUUID();
 
                     if (networkId != null) break;
                 }
@@ -88,6 +88,8 @@ public class NodeConfiguration {
                     this.internalVlanInUse(vlan);
                     this.tenantVlanMap.put(networkId, vlan);
 
+                } else {
+                    logger.debug("Node: {} initialized without a vlan", node);
                 }
             }
         }
@@ -96,6 +98,10 @@ public class NodeConfiguration {
         }
     }
 
+    /*
+     * Return the currently mapped internal vlan or get the next
+     * free internal vlan from the available pool and map it to the networkId.
+     */
     public int assignInternalVlan (String networkId) {
         Integer mappedVlan = tenantVlanMap.get(networkId);
         if (mappedVlan != null) return mappedVlan;
@@ -104,14 +110,36 @@ public class NodeConfiguration {
         return mappedVlan;
     }
 
+    /*
+     * Return the mapped internal vlan to the available pool.
+     */
+    public int reclaimInternalVlan (String networkId) {
+        Integer mappedVlan = tenantVlanMap.get(networkId);
+        if (mappedVlan != null) {
+            tenantVlanMap.remove(mappedVlan);
+            internalVlans.add(mappedVlan);
+            return mappedVlan;
+        }
+        return 0;
+    }
+
+    /*
+     * Remove the internal vlan from the available pool.
+     */
     public void internalVlanInUse (int vlan) {
         internalVlans.remove(vlan);
     }
 
+    /*
+     * Return a vlan from the mapped pool keyed by the networkId.
+     */
     public int getInternalVlan (String networkId) {
         Integer vlan = tenantVlanMap.get(networkId);
         if (vlan == null) return 0;
         return vlan.intValue();
     }
 
+    public void setTenantNetworkManager(ITenantNetworkManager tenantNetworkManager) {
+        this.tenantNetworkManager = tenantNetworkManager;
+    }
 }