[ONOS-8147] Add option to NetconfDeviceProvider to force periodic port updates

Change-Id: If4139a10864784b656ccb1ec4f338de1cc2fa9a6
diff --git a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
index 2a0533f..98b6be6 100644
--- a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
+++ b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
@@ -111,7 +111,8 @@
         property = {
                 POLL_FREQUENCY_SECONDS + ":Integer=" + POLL_FREQUENCY_SECONDS_DEFAULT,
                 MAX_RETRIES + ":Integer=" + MAX_RETRIES_DEFAULT,
-                RETRY_FREQUENCY_SECONDS + ":Integer=" + RETRY_FREQUENCY_SECONDS_DEFAULT
+                RETRY_FREQUENCY_SECONDS + ":Integer=" + RETRY_FREQUENCY_SECONDS_DEFAULT,
+                FORCE_PORT_UPDATES + ":Boolean=" + FORCE_PORT_UPDATES_DEFAULT
         })
 public class NetconfDeviceProvider extends AbstractProvider
         implements DeviceProvider {
@@ -171,6 +172,11 @@
      */
     private int retryFrequency = RETRY_FREQUENCY_SECONDS_DEFAULT;
 
+    /**
+     * Configure option to allow ports to be periodically updated; default is false.
+     */
+    private boolean forcePortUpdates = FORCE_PORT_UPDATES_DEFAULT;
+
     protected ExecutorService connectionExecutor = Executors.newFixedThreadPool(CORE_POOL_SIZE,
             groupedThreads("onos/netconfDeviceProviderConnection",
                     "connection-executor-%d", log));
@@ -273,6 +279,13 @@
                 log.info("Configured. Retry frequency is configured to {} seconds", retryFrequency);
             }
 
+            boolean newForcePortUpdates = Tools.isPropertyEnabled(properties, FORCE_PORT_UPDATES,
+                                          FORCE_PORT_UPDATES_DEFAULT);
+            if (newForcePortUpdates != forcePortUpdates) {
+                forcePortUpdates = newForcePortUpdates;
+            }
+            log.info("Configured. Force port updates flag is set to {}", forcePortUpdates);
+
             maxRetries = Tools.getIntegerProperty(properties, MAX_RETRIES, MAX_RETRIES_DEFAULT);
             log.info("Configured. Number of retries is configured to {} times", maxRetries);
         }
@@ -626,23 +639,21 @@
         }
     }
 
-
     private void discoverOrUpdatePorts(DeviceId deviceId) {
         retriedPortDiscoveryMap.put(deviceId, new AtomicInteger(0));
         AtomicInteger count = retriedPortDiscoveryMap.get(deviceId);
-        //TODO this does not enable port discovery if port changes.
         Device device = deviceService.getDevice(deviceId);
         if (device == null) {
-            log.debug("Cant' reach device {}, not updating ports", deviceId);
+            log.debug("Can't reach device {}, not updating ports", deviceId);
             return;
         }
-        if (deviceService.getPorts(deviceId).isEmpty()
-                && count != null && count.getAndIncrement() < maxRetries) {
+        if (forcePortUpdates || (deviceService.getPorts(deviceId).isEmpty()
+                && count != null && count.getAndIncrement() < maxRetries)) {
             if (device.is(DeviceDescriptionDiscovery.class)) {
                 providerService.updatePorts(deviceId,
                         device.as(DeviceDescriptionDiscovery.class).discoverPortDetails());
             } else {
-                log.warn("No DeviceDescirption behaviour for device {}", deviceId);
+                log.warn("No DeviceDescription behaviour for device {}", deviceId);
             }
 
         }
diff --git a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/OsgiPropertyConstants.java b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/OsgiPropertyConstants.java
index e97f0a1..df714f7 100644
--- a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/OsgiPropertyConstants.java
+++ b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/OsgiPropertyConstants.java
@@ -32,4 +32,7 @@
     public static final String MAX_RETRIES = "maxRetries";
     public static final int MAX_RETRIES_DEFAULT = 5;
 
+    public static final String FORCE_PORT_UPDATES = "forcePortUpdates";
+    public static final boolean FORCE_PORT_UPDATES_DEFAULT = false;
+
 }