Modify MPLS/VLAN query

- ONOS-3507 VlanQuery to return available VLAN IDs
- ONOS-3508 MplsQuery to return available MPLS Labels
- Advertise that VLAN and MPLS resources are available on OVS

Change-Id: I74cd05393c8919b4823d0666348008adb93c9290
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
index 9df5f50..6e944af 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.newresource.impl;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import org.onlab.packet.MplsLabel;
 import org.onlab.packet.VlanId;
@@ -44,6 +45,7 @@
 
 import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.concurrent.ExecutorService;
 import java.util.stream.Collectors;
@@ -58,16 +60,6 @@
 
     private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
 
-    private static final int MAX_VLAN_ID = VlanId.MAX_VLAN;
-    private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
-
-    // Ref: http://www.iana.org/assignments/mpls-label-values/mpls-label-values.xhtml
-    // Smallest non-reserved MPLS label
-    private static final int MIN_UNRESERVED_LABEL = 0x10;
-    // Max non-reserved MPLS label = 239
-    private static final int MAX_UNRESERVED_LABEL = 0xEF;
-    private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels();
-
     private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
     private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
     private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
@@ -142,13 +134,19 @@
             adminService.registerResources(portPath);
 
             // for VLAN IDs
-            if (isVlanEnabled(device.id(), port.number())) {
-                adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, portPath::child));
+            Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
+            if (!vlans.isEmpty()) {
+                adminService.registerResources(vlans.stream()
+                                               .map(portPath::child)
+                                               .collect(Collectors.toList()));
             }
 
             // for MPLS labels
-            if (isMplsEnabled(device.id(), port.number())) {
-                adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, portPath::child));
+            Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
+            if (!mplsLabels.isEmpty()) {
+                adminService.registerResources(mplsLabels.stream()
+                                               .map(portPath::child)
+                                               .collect(Collectors.toList()));
             }
 
             // for Lambdas
@@ -215,61 +213,55 @@
         }
     }
 
-    private boolean isVlanEnabled(DeviceId device, PortNumber port) {
+    private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
         try {
             // DriverHandler does not provide a way to check if a
             // behaviour is supported.
             Driver driver = driverService.getDriver(device);
             if (driver == null || !driver.hasBehaviour(VlanQuery.class)) {
                 // device does not support this
-                return false;
+                return ImmutableSet.of();
             }
 
             DriverHandler handler = driverService.createHandler(device);
             if (handler == null) {
-                return false;
+                return ImmutableSet.of();
             }
 
             VlanQuery query = handler.behaviour(VlanQuery.class);
-            return query != null && query.isEnabled(port);
+            if (query == null) {
+                return ImmutableSet.of();
+            }
+            return query.queryVlanIds(port);
         } catch (ItemNotFoundException e) {
-            return false;
+            return ImmutableSet.of();
         }
     }
 
-    private boolean isMplsEnabled(DeviceId device, PortNumber port) {
+    private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
         try {
             // DriverHandler does not provide a way to check if a
             // behaviour is supported.
             Driver driver = driverService.getDriver(device);
             if (driver == null || !driver.hasBehaviour(MplsQuery.class)) {
                 // device does not support this
-                return false;
+                return ImmutableSet.of();
             }
             DriverHandler handler = driverService.createHandler(device);
             if (handler == null) {
-                return false;
+                return ImmutableSet.of();
             }
 
             MplsQuery query = handler.behaviour(MplsQuery.class);
-            return query != null && query.isEnabled(port);
+            if (query == null) {
+                return ImmutableSet.of();
+            }
+            return query.queryMplsLabels(port);
         } catch (ItemNotFoundException e) {
-            return false;
+            return ImmutableSet.of();
         }
     }
 
-    private static List<VlanId> getEntireVlans() {
-        return IntStream.range(0, MAX_VLAN_ID)
-                .mapToObj(x -> VlanId.vlanId((short) x))
-                .collect(Collectors.toList());
-    }
-
-    private static List<MplsLabel> getEntireMplsLabels() {
-        return IntStream.range(MIN_UNRESERVED_LABEL, MAX_UNRESERVED_LABEL + 1)
-                .mapToObj(MplsLabel::mplsLabel)
-                .collect(Collectors.toList());
-    }
-
     private static List<TributarySlot> getEntireOdu2TributarySlots() {
         return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
                 .mapToObj(TributarySlot::of)