- Set DMAC correcting using SetField in ICMP Handler
 - Modify the event handling logic in SR Manager - now we wait for 20 sec before setting the first routing rule
 - Remove some functions for Dell switches

Change-Id: I205d3560153cf80744a0f292115d8067dbe92b6b
diff --git a/src/main/java/net/onrc/onos/apps/segmentrouting/IcmpHandler.java b/src/main/java/net/onrc/onos/apps/segmentrouting/IcmpHandler.java
index 7f12a1a..6c68d53 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/IcmpHandler.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/IcmpHandler.java
@@ -35,6 +35,7 @@
 import org.projectfloodlight.openflow.protocol.OFPacketOut;
 import org.projectfloodlight.openflow.protocol.action.OFAction;
 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
+import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst;
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
@@ -313,8 +314,11 @@
                     }
                     if (sw13 instanceof OFSwitchImplDellOSR) {
                         MacAddress mplsDstMac =
-                                MacAddress.of(packet.getDestinationMAC().toLong()+1);
-                        OFAction setDAAction = factory.actions().setDlDst(mplsDstMac);
+                                MacAddress.of(packet.getDestinationMAC().toLong());
+                        OFOxmEthDst dmac = factory.oxms().ethDst(mplsDstMac);
+                        OFAction setDAAction = factory.actions().buildSetField()
+                                .setField(dmac).build();
+
                         actions.add(setDAAction);
                     }
 
diff --git a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
index 591ff52..aada531 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
@@ -119,6 +119,7 @@
     private HashMap<Integer, HashMap<Integer, List<Integer>>> adjacencySidTable;
     private HashMap<String, HashMap<Integer, Integer>> adjcencyGroupIdTable;
 
+    private int operationMode = 0;
 
     // Flag whether transit router supports ECMP or not
     // private boolean supportTransitECMP = true;
@@ -262,8 +263,21 @@
      */
     public void topologyEvents(TopologyEvents topologyEvents)
     {
-        topologyEventQueue.add(topologyEvents);
-        discoveryTask.reschedule(100, TimeUnit.MILLISECONDS);
+
+        if (operationMode == 0) {
+            discoveryTask.reschedule(20, TimeUnit.SECONDS);
+            operationMode = 1; // Wait until all switches are up ..
+        }
+        else if (operationMode == 1){ // waiting for all switches to be up
+            // Do nothing
+        }
+        else if (operationMode == 2) { // all switches are up and we need to handle events quickly.
+            topologyEventQueue.add(topologyEvents);
+            discoveryTask.reschedule(100, TimeUnit.MILLISECONDS);
+        }
+
+        //discoveryTask.reschedule(100, TimeUnit.MILLISECONDS);
+        //log.debug("A task is scheduled to handle events {}", topologyEvents);
     }
 
     /**
@@ -271,6 +285,13 @@
      *
      */
     private void handleTopologyChangeEvents() {
+        operationMode = 2;  // all switches are up now..
+
+        if (topologyEventQueue.isEmpty()) {
+            populateEcmpRoutingRules(false);
+            return;
+        }
+
         numOfEventProcess ++;
 
         Collection<LinkData> linkEntriesAddedAll = new ArrayList<LinkData>();
@@ -714,7 +735,7 @@
             }
         }
         if (dstDpid == null) {
-            log.debug("Cannot find the destination switch for the adjacency ID {}", adjId);
+            //log.debug("Cannot find the destination switch for the adjacency ID {}", adjId);
             return;
         }
         Switch dstSw = mutableTopology.getSwitch(dstDpid);
@@ -1064,11 +1085,11 @@
         //If the next hop is the destination router, do PHP
         if (fwdSws.size() == 1 && mplsLabel.equals(getMplsLabel(fwdSw1))) {
             maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, true, true));
-            maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, true, false));
+            //maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, true, false));
         }
         else {
             maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, false, true));
-            maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, false, false));
+            //maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, false, false));
         }
         IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
                 sw.getDpid().value());
@@ -1412,7 +1433,7 @@
         for (OFBarrierReplyFuture replyFuture: replies) {
             OFBarrierReply br = null;
             try {
-                br = replyFuture.get(2, TimeUnit.SECONDS);
+                br = replyFuture.get(5, TimeUnit.SECONDS);
             } catch (TimeoutException | InterruptedException | ExecutionException e) {
                 // XXX for some reason these exceptions are not being thrown
             }
@@ -1477,7 +1498,20 @@
     private MatchActionOperationEntry buildMAEntry(Switch sw,
             String mplsLabel, List<String> fwdSws, boolean php,
             boolean Bos) {
+        IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
+                sw.getDpid().value());
+        if (sw13 == null) {
+            return null;
+        }
         MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), Bos);
+        /*
+        if (sw13 instanceof OFSwitchImplDellOSR && !Bos) {
+            mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel) * 10, Bos);
+        }
+        else {
+            mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), Bos);
+        }
+        */
         List<Action> actions = new ArrayList<Action>();
 
         PopMplsAction popActionBos = new PopMplsAction(EthType.IPv4);
@@ -1501,8 +1535,6 @@
             actions.add(decMplsTtlAction);
         }
 
-        IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
-                sw.getDpid().value());
         if ((sw13 instanceof OFSwitchImplDellOSR) && isTransitRouter(sw) && !php) {
             PortNumber port = pickOnePort(sw, fwdSws);
             if (port == null) {
diff --git a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplDellOSR.java b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplDellOSR.java
index 07522a0..f570266 100644
--- a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplDellOSR.java
+++ b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplDellOSR.java
@@ -1,23 +1,8 @@
 package net.onrc.onos.core.drivermanager;
 
-import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
-
-import net.floodlightcontroller.core.IOF13Switch.NeighborSet.groupPktType;
-import net.onrc.onos.core.matchaction.MatchAction;
-import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
-import net.onrc.onos.core.matchaction.action.Action;
-import net.onrc.onos.core.matchaction.action.GroupAction;
-import net.onrc.onos.core.matchaction.action.OutputAction;
-import net.onrc.onos.core.matchaction.action.PopMplsAction;
-import net.onrc.onos.core.matchaction.action.SetDAAction;
 import net.onrc.onos.core.matchaction.match.Ipv4Match;
 import net.onrc.onos.core.matchaction.match.Match;
-import net.onrc.onos.core.matchaction.match.MplsMatch;
-import net.onrc.onos.core.util.Dpid;
 import net.onrc.onos.core.util.IPv4Net;
-import net.onrc.onos.core.util.PortNumber;
 
 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
 import org.projectfloodlight.openflow.protocol.OFFactory;
@@ -27,7 +12,6 @@
 import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked;
 import org.projectfloodlight.openflow.types.EthType;
 import org.projectfloodlight.openflow.types.IPv4Address;
-import org.projectfloodlight.openflow.types.MacAddress;
 
 /**
  * OFDescriptionStatistics Vendor (Manufacturer Desc.): Dell Make (Hardware
@@ -77,7 +61,8 @@
         return oxmList;
     }
 
-    @Override
+
+    /*
     protected void setTableMissEntries() throws IOException {
         // set all table-miss-entries
         populateTableMissEntry(vlanTableId, true, false, false, -1);
@@ -88,7 +73,9 @@
                 aclTableId);
         populateTableMissEntry(aclTableId, true, false, false, -1);
     }
+    */
 
+    /*
     protected MacAddress getNeighborRouterMacAddress(Dpid ndpid,
             groupPktType outPktType) {
         if (outPktType == groupPktType.MPLS_OUTGOING)
@@ -97,19 +84,23 @@
             return super.getNeighborRouterMacAddress(ndpid,
                     groupPktType.IP_OUTGOING);
     }
+    */
     /* Dell Open Segment Router specific Implementation .
      * Gets the specified Router's MAC address to be used for MPLS flows
      * For Dell OSR, the MPLS MAC is IP MAC + 1
      */
-    @Override
+    /*
     public MacAddress getRouterMPLSMac(Dpid dpid) {
-        return MacAddress.of(super.getRouterIPMac(dpid).getLong() + 1);
+        //return MacAddress.of(super.getRouterIPMac(dpid).getLong() + 1);
+        return MacAddress.of(super.getRouterIPMac(dpid).getLong());
     }
+    */
 
+    /*
     protected void createGroupsAtTransitRouter(Set<Dpid> dpids) {
-        /* Create all possible Neighbor sets from this router
-         * NOTE: Avoid any pairings of edge routers only
-         */
+        // Create all possible Neighbor sets from this router
+        //NOTE: Avoid any pairings of edge routers only
+
         Set<Set<Dpid>> sets = getPowerSetOfNeighbors(dpids);
         sets = filterEdgeRouterOnlyPairings(sets);
         log.debug("createGroupsAtTransitRouter: The size of neighbor powerset "
@@ -138,7 +129,9 @@
             }
         }
     }
+   */
 
+    /*
     protected void analyzeAndUpdateMplsActions(
             MatchActionOperationEntry mao) {
         MatchAction ma = mao.getTarget();
@@ -177,4 +170,5 @@
             }
         }
     }
+    */
 }
\ No newline at end of file
diff --git a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplSpringOpenTTP.java b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplSpringOpenTTP.java
index fa1e517..65171fe 100644
--- a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplSpringOpenTTP.java
+++ b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplSpringOpenTTP.java
@@ -190,7 +190,7 @@
      */
     @Override
     public String toString() {
-        return "OFSwitchImplCPqD13 [" + ((channel != null)
+        return "OFSwitchImplSpringOpenTTP [" + ((channel != null)
                 ? channel.getRemoteAddress() : "?")
                 + " DPID[" + ((stringId != null) ? stringId : "?") + "]]";
     }