Add ability to administratively remove ports of an offline device.

Change-Id: Iaee085be1cd53f783ed80e7c277403eb65ef6d8f
diff --git a/core/api/src/main/java/org/onosproject/net/device/DeviceAdminService.java b/core/api/src/main/java/org/onosproject/net/device/DeviceAdminService.java
index bfd467c..c95e7e6 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DeviceAdminService.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DeviceAdminService.java
@@ -40,4 +40,13 @@
      * @param enable true if port is to be enabled, false to disable
      */
     void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable);
+
+    /**
+     * Removes the ports of a device with the specified identifier. The device
+     * must be presently unavailable, i.e. offline.
+     *
+     * @param deviceId device identifier
+     */
+    default void removeDevicePorts(DeviceId deviceId) {
+    }
 }
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
index e274ae8..be24991 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
@@ -392,6 +392,24 @@
         }
     }
 
+    @Override
+    public void removeDevicePorts(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_ID_NULL);
+        if (isAvailable(deviceId)) {
+            log.debug("Cannot remove ports of device {} while it is available.", deviceId);
+            return;
+        }
+
+        List<PortDescription> portDescriptions = ImmutableList.of();
+        List<DeviceEvent> events = store.updatePorts(getProvider(deviceId).id(),
+                                                     deviceId, portDescriptions);
+        if (events != null) {
+            for (DeviceEvent event : events) {
+                post(event);
+            }
+        }
+    }
+
     private void handlePortRequest(InternalPortUpDownEvent event) {
         DeviceId deviceId = event.deviceId();
         checkNotNull(deviceId, DEVICE_ID_NULL);
diff --git a/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java b/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
index 601e6c3..3e1e4d1 100644
--- a/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/device/impl/DeviceManagerTest.java
@@ -246,7 +246,31 @@
         assertNotNull("device should be found", service.getDevice(DID2));
         assertEquals("incorrect device count", 1, service.getDeviceCount());
         assertEquals("incorrect available device count", 1, service.getAvailableDeviceCount());
+    }
 
+    @Test
+    public void removeDevicePorts() {
+        connectDevice(DID1, SW1);
+        List<PortDescription> pds = new ArrayList<>();
+        pds.add(DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build());
+        pds.add(DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
+        pds.add(DefaultPortDescription.builder().withPortNumber(P3).isEnabled(true).build());
+        providerService.updatePorts(DID1, pds);
+        validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED);
+
+        // Try removing ports while device is available/connected; it should be a no-op.
+        admin.removeDevicePorts(DID1);
+        assertEquals("wrong port count", 3, service.getPorts(DID1).size());
+
+        // Disconnect device
+        providerService.deviceDisconnected(DID1);
+        assertFalse("device should not be available", service.isAvailable(DID1));
+        validateEvents(DEVICE_AVAILABILITY_CHANGED);
+
+        // Now remove ports for real
+        admin.removeDevicePorts(DID1);
+        validateEvents(PORT_REMOVED, PORT_REMOVED, PORT_REMOVED);
+        assertEquals("wrong port count", 0, service.getPorts(DID1).size());
     }
 
     protected void validateEvents(Enum... types) {