Fix for remove and add port from/to groups
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 74dc0de..8c49a6a 100644
--- a/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/net/onrc/onos/apps/segmentrouting/SegmentRoutingManager.java
@@ -370,7 +370,7 @@
                 IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
                         getSwId(dstPort.getDpid().toString()));
                 if (dstSw != null) {
-                    //dstSw.removePortFromGroups(dstPort.getNumber());
+                    dstSw.removePortFromGroups(dstPort.getNumber());
                     log.debug("Switch {} is gone: remove port {}", sw.getDpid(), dstPort);
                 }
             }
@@ -416,12 +416,14 @@
             IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
                     getSwId(srcPort.getDpid().toString()));
             IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
-                    getSwId(srcPort.getDpid().toString()));
+                    getSwId(dstPort.getDpid().toString()));
 
-            if (srcSw != null)
-                srcSw.addPortToGroups(srcPort.getPortNumber());
-            if (dstSw != null)
-                dstSw.addPortToGroups(dstPort.getPortNumber());
+            if ((srcSw != null) && (dstSw != null))
+                /* If this link is not between two switches, ignore it */
+                continue;
+
+            srcSw.addPortToGroups(srcPort.getPortNumber());
+            dstSw.addPortToGroups(dstPort.getPortNumber());
 
             /*
             if (!topologyLinks.contains(link)) {
@@ -458,13 +460,12 @@
             IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
                     getSwId(srcPort.getDpid().toString()));
             IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
-                    getSwId(srcPort.getDpid().toString()));
-            /*
-            if (srcSw != null)
-                srcSw.removePortFromGroups(srcPort.getPortNumber());
-            if (dstSw != null)
-                dstSw.removePortFromGroups(dstPort.getPortNumber());
-            */
+                    getSwId(dstPort.getDpid().toString()));
+            if ((srcSw != null) && (dstSw != null))
+                /* If this link is not between two switches, ignore it */
+                continue;
+            srcSw.removePortFromGroups(srcPort.getPortNumber());
+            dstSw.removePortFromGroups(dstPort.getPortNumber());
 
             Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid());
             if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) {
@@ -486,7 +487,6 @@
      * @param portEntries
      */
     private void processPortRemoval(Collection<PortData> portEntries) {
-        /*
         for (PortData port : portEntries) {
             Dpid dpid = port.getDpid();
 
@@ -496,7 +496,6 @@
                 sw.removePortFromGroups(port.getPortNumber());
             log.debug("Remove port {} from switch {}", port, dpid);
         }
-        */
     }
 
     /**
diff --git a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplCPqD13.java b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplCPqD13.java
index 0f476b6..968f960 100644
--- a/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplCPqD13.java
+++ b/src/main/java/net/onrc/onos/core/drivermanager/OFSwitchImplCPqD13.java
@@ -5,6 +5,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
@@ -214,60 +215,78 @@
     public void removePortFromGroups(PortNumber port) {
         /* FIX: removePortFromGroups is not working */
 
-        /*        ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
-                if (portNSSet == null)
-                {
-        *//* No Groups are created with this port yet */
-        /*            log.warn("removePortFromGroups: No groups exist with Switch {} port {}",
+        log.debug("removePortFromGroups: Remove port {} from Switch {}",
+                port, getStringId());
+        ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
+        if (portNSSet == null)
+        {
+            /* No Groups are created with this port yet */
+            log.warn("removePortFromGroups: No groups exist with Switch {} port {}",
                             getStringId(), port);
-                    return;
+            return;
+        }
+        log.debug("removePortFromGroups: Neighborsets that the port {} is part"
+                + "of on Switch {} are {}",
+                port, getStringId(), portNSSet);
+
+        for (NeighborSet ns : portNSSet) {
+            /* Delete the first matched bucket */
+            EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
+            Iterator<BucketInfo> it = portEcmpInfo.buckets.iterator();
+            while (it.hasNext()) {
+                BucketInfo bucket = it.next();
+                if (bucket.outport.equals(port)) {
+                    it.remove();
                 }
-                for (NeighborSet ns : portNSSet) {
-        *//* Delete the first matched bucket */
-        /*            EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
-                    Iterator<BucketInfo> it = portEcmpInfo.buckets.iterator();
-                    while (it.hasNext()) {
-                        BucketInfo bucket = it.next();
-                        if (bucket.outport.equals(port)) {
-                            it.remove();
-                        }
-                    }
-                    modifyEcmpGroup(portEcmpInfo);
-                }
-        *//* Don't delete the entry from portNeighborSetMap because
+            }
+            log.debug("removePortFromGroups: Modifying Group on Switch {} "
+                    + "and Neighborset {} with {}",
+                    getStringId(), ns, portEcmpInfo);
+            modifyEcmpGroup(portEcmpInfo);
+        }
+        /* Don't delete the entry from portNeighborSetMap because
           * when the port is up again this info is needed
           */
         return;
     }
 
     public void addPortToGroups(PortNumber port) {
-        /*        ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
-                if (portNSSet == null) {
-        *//* Unknown Port  */
-        /*            log.warn("addPortToGroups: Switch {} port {} is unknown",
+        log.debug("addPortToGroups: Add port {} to Switch {}",
+                port, getStringId());
+        ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
+        if (portNSSet == null) {
+            /* Unknown Port  */
+            log.warn("addPortToGroups: Switch {} port {} is unknown",
                             getStringId(), port);
-                    return;
-                }
-                Dpid neighborDpid = portToNeighbors.get(port);
-                for (NeighborSet ns : portNSSet) {
-        *//* Delete the first matched bucket */
-        /*            EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
-                    BucketInfo b = new BucketInfo(neighborDpid,
+            return;
+        }
+        log.debug("addPortToGroups: Neighborsets that the port {} is part"
+                + "of on Switch {} are {}",
+                port, getStringId(), portNSSet);
+
+        Dpid neighborDpid = portToNeighbors.get(port);
+        for (NeighborSet ns : portNSSet) {
+            /* Delete the first matched bucket */
+            EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
+            BucketInfo b = new BucketInfo(neighborDpid,
                             MacAddress.of(srConfig.getRouterMac()),
                             getNeighborRouterMacAddress(neighborDpid),
                             port,
                             ns.getEdgeLabel());
-                    List<BucketInfo> buckets = portEcmpInfo.buckets;
-                    if (buckets == null) {
-                        buckets = new ArrayList<BucketInfo>();
-                        buckets.add(b);
-                        portEcmpInfo.buckets = buckets;
-                    } else {
-                        buckets.add(b);
-                    }
-                    modifyEcmpGroup(portEcmpInfo);
-                }
-        */return;
+            List<BucketInfo> buckets = portEcmpInfo.buckets;
+            if (buckets == null) {
+                buckets = new ArrayList<BucketInfo>();
+                buckets.add(b);
+                portEcmpInfo.buckets = buckets;
+            } else {
+                buckets.add(b);
+            }
+            log.debug("addPortToGroups: Modifying Group on Switch {} "
+                    + "and Neighborset {} with {}",
+                    getStringId(), ns, portEcmpInfo);
+            modifyEcmpGroup(portEcmpInfo);
+        }
+        return;
     }
 
     // *****************************