Bug 5212 - Neutron-mapper has dependency on Ofoverlay renderer
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / node / SwitchManager.java
index b72e95471021d7d3b311061b2c9d921ec33e5a94..66a69d8afb0e4c1aa98eefc8977ef7b240ab99fd 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.getOfPortNum;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -22,11 +23,13 @@ import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.annotation.Nullable;
 
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayConfig.EncapsulationFormat;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.ExternalInterfaces;
@@ -40,6 +43,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlanGpe;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -47,6 +51,7 @@ import org.slf4j.LoggerFactory;
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
 import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -101,10 +106,26 @@ public class SwitchManager implements AutoCloseable {
             LOG.error("No SwitchState for {} in deactivatingSwitch. This should not happen.",nodeId);
             return;
         }
-        state.setHasEndpoints(false);;
+        state.setHasEndpoints(false);
         state.updateStatus();
     }
 
+    public synchronized InstanceIdentifier<NodeConnector> getNodeConnectorIidForPortName(Name portName) {
+        for (SwitchState sw : switches.values()) {
+            if (sw.fcncByNcIid == null) {
+                continue;
+            }
+            for (Entry<InstanceIdentifier<NodeConnector>, FlowCapableNodeConnector> fcncByNcIidEntry : sw.fcncByNcIid
+                .entrySet()) {
+                FlowCapableNodeConnector fcnc = fcncByNcIidEntry.getValue();
+                if (portName.getValue().equals(fcnc.getName())) {
+                    return fcncByNcIidEntry.getKey();
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * Get the collection of switches that are in the "ready" state. Note
      * that the collection is immutable.
@@ -134,12 +155,44 @@ public class SwitchManager implements AutoCloseable {
 
     public synchronized Set<NodeConnectorId> getExternalPorts(NodeId nodeId) {
         SwitchState state = switches.get(nodeId);
-        if (state == null)
+        if (state == null) {
             return Collections.emptySet();
+        }
         return ImmutableSet.copyOf(state.externalPorts);
     }
 
-    public static synchronized NodeConnectorId getTunnelPort(NodeId nodeId, Class<? extends TunnelTypeBase> tunnelType) {
+    public Set<Long> getExternalPortNumbers(NodeId nodeId) {
+        Set<Long> extPortNumbers = new HashSet<>();
+        for(NodeConnectorId nc : getExternalPorts(nodeId)) {
+            long portNum;
+            try {
+                portNum = getOfPortNum(nc);
+            } catch (NumberFormatException ex) {
+                LOG.warn("Could not parse port number {}", nc, ex);
+                return null;
+            }
+            extPortNumbers.add(portNum);
+        }
+        return extPortNumbers;
+    }
+
+    public synchronized Collection<NodeConnectorId> getTunnelPorts(NodeId nodeId) {
+        Collection<NodeConnectorId> ncIds = new HashSet<>();
+        SwitchState state = switches.get(nodeId);
+        if (state == null ) {
+            return Collections.emptySet();
+        }
+        ncIds = Collections2.transform(state.tunnelBuilderByType.values(),new Function<TunnelBuilder, NodeConnectorId>() {
+
+            @Override
+            public NodeConnectorId apply(TunnelBuilder input) {
+                return input.getNodeConnectorId();
+            }
+        });
+
+        return ncIds;
+    }
+    public synchronized NodeConnectorId getTunnelPort(NodeId nodeId, Class<? extends TunnelTypeBase> tunnelType) {
         SwitchState state = switches.get(nodeId);
         if (state == null) {
             return null;
@@ -151,7 +204,7 @@ public class SwitchManager implements AutoCloseable {
         return tunnel.getNodeConnectorId();
     }
 
-    public static synchronized IpAddress getTunnelIP(NodeId nodeId, Class<? extends TunnelTypeBase> tunnelType) {
+    public synchronized IpAddress getTunnelIP(NodeId nodeId, Class<? extends TunnelTypeBase> tunnelType) {
         SwitchState state = switches.get(nodeId);
         if (state == null) {
             return null;
@@ -290,6 +343,11 @@ public class SwitchManager implements AutoCloseable {
 
         /**
          * Constructor used for tests
+         *
+         * @param node the node id
+         * @param tunnelPort the tunnel port
+         * @param externalPorts the set of expternal ports
+         * @param nodeConfig the ofoverlay node config
          */
         public SwitchState(NodeId node, NodeConnectorId tunnelPort, Set<NodeConnectorId> externalPorts,
                 OfOverlayNodeConfig nodeConfig) {
@@ -302,29 +360,9 @@ public class SwitchManager implements AutoCloseable {
         private void update() {
             tunnelBuilderByType = new HashMap<>();
             externalPorts = new HashSet<>();
-            for (Entry<InstanceIdentifier<NodeConnector>, FlowCapableNodeConnector> fcncByNcIidEntry : fcncByNcIid.entrySet()) {
-                FlowCapableNodeConnector fcnc = fcncByNcIidEntry.getValue();
-                if (fcnc.getName() == null) {
-                    continue;
-                }
-                InstanceIdentifier<NodeConnector> ncIid = fcncByNcIidEntry.getKey();
-                NodeConnectorId ncId = ncIid.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId();
-                if (fcnc.getName().matches(".*(vxlan).*")) {
-                    TunnelBuilder tunnelBuilder = tunnelBuilderByType.get(TunnelTypeVxlan.class);
-                    if (tunnelBuilder == null) {
-                        tunnelBuilder = new TunnelBuilder().setTunnelType(TunnelTypeVxlan.class);
-                        tunnelBuilderByType.put(TunnelTypeVxlan.class, tunnelBuilder);
-                    }
-                    tunnelBuilder.setNodeConnectorId(ncId);
-                }
-                if (nodeConfig != null && nodeConfig.getExternalInterfaces() != null) {
-                    for (ExternalInterfaces extIf : nodeConfig.getExternalInterfaces()) {
-                        if (extIf.getNodeConnectorId() != null
-                                && ncId.equals(extIf.getNodeConnectorId())) {
-                            externalPorts.add(ncId);
-                            break;
-                        }
-                    }
+            if (nodeConfig != null && nodeConfig.getExternalInterfaces() != null) {
+                for (ExternalInterfaces nc : nodeConfig.getExternalInterfaces()) {
+                    externalPorts.add(nc.getNodeConnectorId());
                 }
             }
             if (nodeConfig != null && nodeConfig.getTunnel() != null) {
@@ -345,6 +383,29 @@ public class SwitchManager implements AutoCloseable {
                     }
                 }
             }
+            for (Entry<InstanceIdentifier<NodeConnector>, FlowCapableNodeConnector> fcncByNcIidEntry : fcncByNcIid.entrySet()) {
+                FlowCapableNodeConnector fcnc = fcncByNcIidEntry.getValue();
+                if (fcnc.getName() == null) {
+                    continue;
+                }
+                InstanceIdentifier<NodeConnector> ncIid = fcncByNcIidEntry.getKey();
+                NodeConnectorId ncId = ncIid.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId();
+                if (fcnc.getName().matches(".*(vxlan-).*")) {
+                    TunnelBuilder tunnelBuilder = tunnelBuilderByType.get(TunnelTypeVxlan.class);
+                    if (tunnelBuilder == null) {
+                        tunnelBuilder = new TunnelBuilder().setTunnelType(TunnelTypeVxlan.class);
+                        tunnelBuilderByType.put(TunnelTypeVxlan.class, tunnelBuilder);
+                    }
+                    tunnelBuilder.setNodeConnectorId(ncId);
+                } else if (fcnc.getName().matches(".*(vxlangpe-).*")) {
+                    TunnelBuilder tunnelBuilder = tunnelBuilderByType.get(TunnelTypeVxlanGpe.class);
+                    if (tunnelBuilder == null) {
+                        tunnelBuilder = new TunnelBuilder().setTunnelType(TunnelTypeVxlanGpe.class);
+                        tunnelBuilderByType.put(TunnelTypeVxlanGpe.class, tunnelBuilder);
+                    }
+                    tunnelBuilder.setNodeConnectorId(ncId);
+                }
+            }
         }
 
         private void updateStatus() {
@@ -376,26 +437,25 @@ public class SwitchManager implements AutoCloseable {
             LOG.trace("Iterating over tunnel till tunnel with IP and node-connector is not found.");
             for (TunnelBuilder tb : tunnelBuilderByType.values()) {
                 if (tb.getIp() != null && tb.getNodeConnectorId() != null) {
-//                    LOG.trace("Tunnel found. Type: {} IP: {} Port: {} Node-connector: {}", tb.getTunnelType()
-//                        .getSimpleName(), tb.getIp(), tb.getPort(), tb.getNodeConnectorId());
-                    LOG.trace("Tunnel found.");
+                    LOG.trace("Tunnel {} found.",tb.toString());
                     return true;
                 } else {
-//                    LOG.trace("Tunnel which is not completed: Type: {} IP: {} Port: {} Node-connector: {}",
-//                            tb.getTunnelType().getSimpleName(), tb.getIp(), tb.getPort(), tb.getNodeConnectorId());
-                    LOG.trace("Tunnel which is not completed");
+                    LOG.trace("Tunnel is not complete for node: {}", nodeId.getValue());
                 }
             }
             return false;
         }
 
         public boolean isConfigurationEmpty() {
-            if (fcNode != null)
+            if (fcNode != null) {
                 return false;
-            if (nodeConfig != null)
+            }
+            if (nodeConfig != null) {
                 return false;
-            if (!fcncByNcIid.isEmpty())
+            }
+            if (!fcncByNcIid.isEmpty()) {
                 return false;
+            }
             return true;
         }