Bug5427: Added JavaDoc for OfOverlay rendeder (flow description)
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / SourceMapper.java
old mode 100644 (file)
new mode 100755 (executable)
index f7d22ac..dc5f626
@@ -19,25 +19,23 @@ import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtil
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 
+import org.opendaylight.groupbasedpolicy.dto.EgKey;
+import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
-import org.opendaylight.groupbasedpolicy.resolver.EgKey;
-import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
-import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTable;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -53,8 +51,43 @@ import org.slf4j.LoggerFactory;
 import com.google.common.collect.Sets;
 
 /**
- * Manage the table that assigns source endpoint group, bridge domain, and
- * router domain to registers to be used by other tables.
+ * <h1>Manage the table that assigns source endpoint group, bridge domain, and
+ * router domain to registers to be used by other tables</h1>
+ *
+ * <i>Remote tunnel flow:</i><br>
+ * Priority = 150<br>
+ * Matches:<br>
+ *      - in_port (should be tunnel port), {@link NodeConnectorId}
+ *      - tunnel ID match {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxTunId}<br>
+ * Actions:<br>
+ *      - loadReg1 fixed value 0xffffff {@link NxmNxReg1}<br>
+ *      - loadReg4 {@link NxmNxReg4}<br>
+ *      - loadReg5 {@link NxmNxReg5}<br>
+ *      - loadReg6 {@link NxmNxReg6}<br>
+ *      - {@link GoToTable} DESTINATION MAPPER table
+ * <p>
+ * <i>Remote broadcast flow:</i><br>
+ * Priority = 150<br>
+ * Matches:<br>
+ *      - in_port (should be tunnel port), {@link NodeConnectorId}
+ *      - tunnel ID match {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxTunId}<br>
+ * Actions:<br>
+ *      - loadReg5 {@link NxmNxReg5}<br>
+ *      - {@link GoToTable} DESTINATION MAPPER table
+ * <p>
+ * <i>Local EP flow:</i><br>
+ * Priority = 100<br>
+ * Matches:<br>
+ *      - dl_src (source mac address) {@link org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress}<br>
+ *      - in_port (node connector ID) {@link NodeConnectorId}<br>
+ * Actions:<br>
+ *      - loadReg0 {@link NxmNxReg0}<br>
+ *      - loadReg1 {@link NxmNxReg1}<br>
+ *      - loadReg4 {@link NxmNxReg4}<br>
+ *      - loadReg5 {@link NxmNxReg5}<br>
+ *      - loadReg6 {@link NxmNxReg6}<br>
+ *      - loadTunnelId<br>
+ *      - {@link GoToTable} DESTINATION MAPPER table
  */
 public class SourceMapper extends FlowTable {
 
@@ -65,7 +98,7 @@ public class SourceMapper extends FlowTable {
 
     public SourceMapper(OfContext ctx, short tableId) {
         super(ctx);
-        this.TABLE_ID=tableId;
+        TABLE_ID = tableId;
     }
 
     @Override
@@ -74,48 +107,40 @@ public class SourceMapper extends FlowTable {
     }
 
     @Override
-    public void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception {
+    public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
 
-        flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null));
+        ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
 
         // Handle case where packets from from External
-        Map<EndpointKey, EndpointL3> l3EpWithNatByL2Key = ctx.getEndpointManager().getL3EpWithNatByL2Key();
         for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
-            OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
-
-            IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
+            IndexedTenant tenant = ctx.getTenant(ep.getTenant());
             if (tenant == null)
                 continue;
 
-            EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
-            EgKey sepg = new EgKey(ep.getTenant(), ep.getEndpointGroup());
+            EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep);
+            if (epFwdCtxOrds == null) {
+                LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", ep);
+                continue;
+            }
 
-            createRemoteTunnels(flowMap, nodeId, ep, policyInfo, epFwdCtxOrds);
+            createRemoteTunnels(ofWriter, nodeId, ep, epFwdCtxOrds);
 
             if (ep.getTenant() == null || (ep.getEndpointGroup() == null && ep.getEndpointGroups() == null)) {
                 continue;
             }
 
+            OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
             if (ofc != null && ofc.getNodeConnectorId() != null
-                    && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))) {
-                /**
-                 * Sync the local EP information.
-                 */
-                syncEP(flowMap, nodeId, ep, ofc.getNodeConnectorId(), epFwdCtxOrds);
+                    && (EndpointManager.isInternal(ep, ctx.getTenant(ep.getTenant()).getExternalImplicitGroups()))) {
 
+                // Sync the local EP information
+                syncEP(ofWriter, nodeId, ep, ofc.getNodeConnectorId(), epFwdCtxOrds);
             }
-//            if (l3EpWithNatByL2Key.containsKey(ep.getKey())) {
-//                Set<NodeConnectorId> external = ctx.getSwitchManager().getExternalPorts(nodeId);
-//                for (NodeConnectorId ncId : external) {
-//                    // TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
-//                    syncEP(flowMap, nodeId, ep, ncId, epFwdCtxOrds);
-//                }
-//            }
         }
     }
 
-    private void createRemoteTunnels(FlowMap flowMap, NodeId nodeId, Endpoint ep, PolicyInfo policyInfo,
-            EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
+    private void createRemoteTunnels(OfWriter ofWriter, NodeId nodeId, Endpoint ep, EndpointFwdCtxOrdinals epFwdCtxOrds)
+            throws Exception {
         Set<EgKey> epgs = new HashSet<>();
 
         // Get EPGs and add to Set to remove duplicates
@@ -131,7 +156,7 @@ public class SourceMapper extends FlowTable {
 
         // Create tunnels on remote Nodes that may talk to us.
         for (EgKey epg : epgs) {
-            Set<EgKey> peers = Sets.union(Collections.singleton(epg), policyInfo.getPeers(epg));
+            Set<EgKey> peers = Sets.union(Collections.singleton(epg), ctx.getCurrentPolicy().getPeers(epg));
             for (EgKey peer : peers) {
                 for (NodeId remoteNodeId : ctx.getEndpointManager().getNodesForGroup(peer)) {
 
@@ -143,8 +168,8 @@ public class SourceMapper extends FlowTable {
                                 nodeId.getValue(), remoteNodeId.getValue());
                         continue;
                     }
-                    flowMap.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
-                    flowMap.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
+                    ofWriter.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
+                    ofWriter.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
                 }
             }
         }
@@ -154,12 +179,6 @@ public class SourceMapper extends FlowTable {
 
         int fdId = epFwdCtxOrds.getFdId();
 
-        FlowId flowid = new FlowId(new StringBuilder().append(tunPort.getValue())
-            .append("|tunnel|")
-            .append("|")
-            .append(fdId)
-            .toString());
-
         MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
         addNxTunIdMatch(mb, fdId);
 
@@ -176,9 +195,11 @@ public class SourceMapper extends FlowTable {
 
         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
 
+        Match match = mb.build();
+        FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnelFdId", match);
         FlowBuilder flowb = base().setId(flowid)
             .setPriority(Integer.valueOf(150))
-            .setMatch(mb.build())
+            .setMatch(match)
             .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
         return flowb.build();
     }
@@ -192,19 +213,6 @@ public class SourceMapper extends FlowTable {
         int l3Id = epFwdCtxOrds.getL3Id();
         int tunnelId = epFwdCtxOrds.getTunnelId();
 
-        FlowId flowid = new FlowId(new StringBuilder().append(tunPort.getValue())
-            .append("|tunnel|")
-            .append(egId)
-            .append("|")
-            .append(bdId)
-            .append("|")
-            .append(fdId)
-            .append("|")
-            .append(l3Id)
-            .append("|")
-            .append(tunnelId)
-            .toString());
-
         MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
         addNxTunIdMatch(mb, tunnelId);
 
@@ -223,16 +231,18 @@ public class SourceMapper extends FlowTable {
         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
+        Match match = mb.build();
+        FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnel", match);
         FlowBuilder flowb = base().setId(flowid)
             .setPriority(Integer.valueOf(150))
-            .setMatch(mb.build())
+            .setMatch(match)
             .setInstructions(
                     instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg),
                             gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
         return flowb.build();
     }
 
-    private void syncEP(FlowMap flowMap, NodeId nodeId, Endpoint ep, NodeConnectorId ncId, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
+    private void syncEP(OfWriter ofWriter, NodeId nodeId, Endpoint ep, NodeConnectorId ncId, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
 
         // TODO alagalah Li/Be: We should also match on EndpointL3 with the appropriate
         // network containment. This would solve a lot of problems and prepare for EndpointL3 RPC.
@@ -244,20 +254,6 @@ public class SourceMapper extends FlowTable {
         int cgId = epFwdCtxOrds.getCgId();
         int tunnelId = epFwdCtxOrds.getTunnelId();
 
-        FlowId flowid = new FlowId(new StringBuilder().append(ncId.getValue())
-            .append("|")
-            .append(ep.getMacAddress().getValue())
-            .append("|")
-            .append(egId)
-            .append("|")
-            .append(bdId)
-            .append("|")
-            .append(fdId)
-            .append("|")
-            .append(l3Id)
-            .append("|")
-            .append(cgId)
-            .toString());
         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
@@ -265,16 +261,17 @@ public class SourceMapper extends FlowTable {
         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
         Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
 
+        Match match = new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
+                .setInPort(ncId)
+                .build();
+        FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "ep", match);
         FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
             .setId(flowid)
-            .setMatch(
-                    new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
-                        .setInPort(ncId)
-                        .build())
+            .setMatch(match)
             .setInstructions(
                     instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg,tunIdAction),
                             gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
-        flowMap.writeFlow(nodeId, TABLE_ID, flowb.build());
+        ofWriter.writeFlow(nodeId, TABLE_ID, flowb.build());
     }
 
 }