Merge branch 'master' of ssh://gerrit.onlab.us:29418/onos-next

Conflicts:
	providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/OpenFlowLinkProvider.java
diff --git a/providers/of/device/src/main/java/org/onlab/onos/provider/of/device/impl/OpenFlowDeviceProvider.java b/providers/of/device/src/main/java/org/onlab/onos/provider/of/device/impl/OpenFlowDeviceProvider.java
index 5dbcd80..61b1c18 100644
--- a/providers/of/device/src/main/java/org/onlab/onos/provider/of/device/impl/OpenFlowDeviceProvider.java
+++ b/providers/of/device/src/main/java/org/onlab/onos/provider/of/device/impl/OpenFlowDeviceProvider.java
@@ -14,6 +14,7 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onlab.onos.net.Device;
+import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.MastershipRole;
 import org.onlab.onos.net.PortNumber;
 import org.onlab.onos.net.device.DefaultDeviceDescription;
@@ -66,14 +67,22 @@
     public void activate() {
         providerService = providerRegistry.register(this);
         controller.addListener(listener);
+        for (OpenFlowSwitch sw : controller.getSwitches()) {
+            listener.switchAdded(new Dpid(sw.getId()));
+        }
         LOG.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        for (OpenFlowSwitch sw : controller.getSwitches()) {
+            providerService.deviceDisconnected(DeviceId.deviceId("of:"
+                    + Long.toHexString(sw.getId())));
+        }
         providerRegistry.unregister(this);
         controller.removeListener(listener);
         providerService = null;
+
         LOG.info("Stopped");
     }
 
diff --git a/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/LinkDiscovery.java b/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/LinkDiscovery.java
index 407ded1..da89681 100644
--- a/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/LinkDiscovery.java
+++ b/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/LinkDiscovery.java
@@ -86,6 +86,7 @@
     private final OpenFlowController ctrl;
     private final LinkProviderService linkProvider;
     private final Map<Integer, OFPortDesc> ports;
+    private Timeout timeout;
 
     /**
      * Instantiates discovery manager for the given physical switch. Creates a
@@ -127,7 +128,7 @@
                 addPort(port);
             }
         }
-        Timer.getTimer().newTimeout(this, this.probeRate,
+        timeout = Timer.getTimer().newTimeout(this, this.probeRate,
                 TimeUnit.MILLISECONDS);
         this.log.debug("Started discovery manager for switch {}",
                 sw.getId());
@@ -186,6 +187,10 @@
                         portnum);
             }
         }
+        ConnectPoint cp = new ConnectPoint(
+                DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
+                PortNumber.portNumber(port.getPortNo().getPortNumber()));
+        linkProvider.linksVanished(cp);
 
     }
 
@@ -380,14 +385,19 @@
         }
 
         // reschedule timer
-        Timer.getTimer().newTimeout(this, this.probeRate,
+        timeout = Timer.getTimer().newTimeout(this, this.probeRate,
                 TimeUnit.MILLISECONDS);
     }
 
     public void removeAllPorts() {
-        for (OFPortDesc port : sw.getPorts()) {
+        for (OFPortDesc port : ports.values()) {
             removePort(port);
         }
     }
 
+    public void stop() {
+        removeAllPorts();
+        timeout.cancel();
+    }
+
 }
diff --git a/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/OpenFlowLinkProvider.java b/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/OpenFlowLinkProvider.java
index a2bf045..218145d 100644
--- a/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/OpenFlowLinkProvider.java
+++ b/providers/of/link/src/main/java/org/onlab/onos/provider/of/link/impl/OpenFlowLinkProvider.java
@@ -1,18 +1,11 @@
 package org.onlab.onos.provider.of.link.impl;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.onlab.onos.net.ConnectPoint;
 import org.onlab.onos.net.DeviceId;
-import org.onlab.onos.net.PortNumber;
 import org.onlab.onos.net.link.LinkProvider;
 import org.onlab.onos.net.link.LinkProviderRegistry;
 import org.onlab.onos.net.link.LinkProviderService;
@@ -20,16 +13,21 @@
 import org.onlab.onos.net.provider.ProviderId;
 import org.onlab.onos.of.controller.Dpid;
 import org.onlab.onos.of.controller.OpenFlowController;
+import org.onlab.onos.of.controller.OpenFlowSwitch;
 import org.onlab.onos.of.controller.OpenFlowSwitchListener;
 import org.onlab.onos.of.controller.PacketContext;
 import org.onlab.onos.of.controller.PacketListener;
-import org.onlab.util.Timer;
 import org.projectfloodlight.openflow.protocol.OFPortConfig;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 import org.projectfloodlight.openflow.protocol.OFPortState;
 import org.projectfloodlight.openflow.protocol.OFPortStatus;
 import org.slf4j.Logger;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
 /**
  * Provider which uses an OpenFlow controller to detect network
  * infrastructure links.
@@ -51,6 +49,8 @@
 
     private final InternalLinkProvider listener = new InternalLinkProvider();
 
+    private final Map<Dpid, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
+
     /**
      * Creates an OpenFlow link provider.
      */
@@ -63,23 +63,28 @@
         providerService = providerRegistry.register(this);
         controller.addListener(listener);
         controller.addPacketListener(0, listener);
+        for (OpenFlowSwitch sw : controller.getSwitches()) {
+            listener.switchAdded(new Dpid(sw.getId()));
+        }
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        for (LinkDiscovery ld : discoverers.values()) {
+            ld.stop();
+        }
         providerRegistry.unregister(this);
         controller.removeListener(listener);
         controller.removePacketListener(listener);
         providerService = null;
-        Timer.getTimer().stop();
+
         log.info("Stopped");
     }
 
 
     private class InternalLinkProvider implements PacketListener, OpenFlowSwitchListener {
 
-        private final Map<Dpid, LinkDiscovery> discoverers = new ConcurrentHashMap<>();
 
         @Override
         public void handlePacket(PacketContext pktCtx) {
@@ -96,13 +101,13 @@
         @Override
         public void switchAdded(Dpid dpid) {
             discoverers.put(dpid, new LinkDiscovery(controller.getSwitch(dpid),
-                    controller, providerService, useBDDP));
+                                                    controller, providerService, useBDDP));
 
         }
 
         @Override
         public void switchRemoved(Dpid dpid) {
-            LinkDiscovery ld = this.discoverers.remove(dpid);
+            LinkDiscovery ld = discoverers.remove(dpid);
             if (ld != null) {
                 ld.removeAllPorts();
             }
@@ -122,10 +127,9 @@
             if (enabled) {
                 ld.addPort(port);
             } else {
-                ConnectPoint cp = new ConnectPoint(
-                        DeviceId.deviceId("of:" + Long.toHexString(dpid.value())),
-                        PortNumber.portNumber(port.getPortNo().getPortNumber()));
-                providerService.linksVanished(cp);
+                /*
+                 * remove port calls linkVanished
+                 */
                 ld.removePort(port);
             }