[Emu] new ResourceService related bug fixes

- ResourceDeviceListener should also react to availability events.
- Should check if device supports the behavior before
  retrieving Behaviour class
- ResourceManager should unregister when requested
- opticalUtils was specifying wrong driver name. Should've been "linc-oe"

Change-Id: I9364b6307cb537b04b57ac00f2451c13e3379471
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
index e698026..2d68fa5 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
@@ -35,7 +35,7 @@
      * Creates an instance with the specified subject, resource and consumer.
      *
      * @param resource resource of the subject
-     * @param consumer consumer ot this resource
+     * @param consumer consumer of this resource
      */
     public ResourceAllocation(ResourcePath resource, ResourceConsumer consumer) {
         this.resource = checkNotNull(resource);
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
index 53bf30a..c793ca4 100644
--- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
@@ -24,6 +24,7 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.ItemNotFoundException;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.device.DeviceService;
@@ -166,8 +167,13 @@
         Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
         String driverName = device.annotations().value(DRIVER);
         if (driverName != null) {
-            return getDriver(driverName);
+            try {
+                return getDriver(driverName);
+            } catch (ItemNotFoundException e) {
+                log.warn("Specified driver {} not found, falling back.", driverName);
+            }
         }
+
         return nullIsNotFound(getDriver(device.manufacturer(),
                                         device.hwVersion(), device.swVersion()),
                               NO_DRIVER);
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 bfc6a99..46aeeb0 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
@@ -32,6 +32,8 @@
 import org.onosproject.net.behaviour.VlanQuery;
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverService;
 import org.onosproject.net.newresource.ResourceAdminService;
@@ -67,18 +69,22 @@
     private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
 
     private final ResourceAdminService adminService;
+    private final DeviceService deviceService;
     private final DriverService driverService;
     private final ExecutorService executor;
 
+
     /**
      * Creates an instance with the specified ResourceAdminService and ExecutorService.
      *
      * @param adminService instance invoked to register resources
+     * @param deviceService {@link DeviceService} to be used.
      * @param executor executor used for processing resource registration
      */
-    ResourceDeviceListener(ResourceAdminService adminService, DriverService driverService,
+    ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
                            ExecutorService executor) {
         this.adminService = checkNotNull(adminService);
+        this.deviceService = checkNotNull(deviceService);
         this.driverService = checkNotNull(driverService);
         this.executor = checkNotNull(executor);
     }
@@ -93,8 +99,21 @@
             case DEVICE_REMOVED:
                 unregisterDeviceResource(device);
                 break;
+            case DEVICE_AVAILABILITY_CHANGED:
+                if (deviceService.isAvailable(device.id())) {
+                    registerDeviceResource(device);
+                    // TODO: do we need to walk the ports?
+                } else {
+                    unregisterDeviceResource(device);
+                }
+                break;
             case PORT_ADDED:
-                registerPortResource(device, event.port());
+            case PORT_UPDATED:
+                if (event.port().isEnabled()) {
+                    registerPortResource(device, event.port());
+                } else {
+                    unregisterPortResource(device, event.port());
+                }
                 break;
             case PORT_REMOVED:
                 unregisterPortResource(device, event.port());
@@ -168,12 +187,22 @@
 
     private SortedSet<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
         try {
+            // DriverHandler does not provide a way to check if a
+            // behaviour is supported.
+            Driver driver = driverService.getDriver(did);
+            if (driver == null || !driver.hasBehaviour(LambdaQuery.class)) {
+                return Collections.emptySortedSet();
+            }
             DriverHandler handler = driverService.createHandler(did);
             if (handler == null) {
                 return Collections.emptySortedSet();
             }
             LambdaQuery query = handler.behaviour(LambdaQuery.class);
-            return query.queryLambdas(port);
+            if (query != null) {
+                return query.queryLambdas(port);
+            } else {
+                return Collections.emptySortedSet();
+            }
         } catch (ItemNotFoundException e) {
             return Collections.emptySortedSet();
         }
@@ -181,6 +210,14 @@
 
     private boolean isVlanEnabled(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;
+            }
+
             DriverHandler handler = driverService.createHandler(device);
             if (handler == null) {
                 return false;
@@ -195,6 +232,13 @@
 
     private boolean isMplsEnabled(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;
+            }
             DriverHandler handler = driverService.createHandler(device);
             if (handler == null) {
                 return false;
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
index 3014951..a5aae0f 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
@@ -172,7 +172,7 @@
     public boolean unregisterResources(List<ResourcePath> resources) {
         checkNotNull(resources);
 
-        return store.register(resources);
+        return store.unregister(resources);
     }
 
     private class InternalStoreDelegate implements ResourceStoreDelegate {
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
index e804266..c3863ca 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java
@@ -53,12 +53,13 @@
 
     @Activate
     public void activate() {
-        deviceListener = new ResourceDeviceListener(adminService, driverService, executor);
+        deviceListener = new ResourceDeviceListener(adminService, deviceService, driverService, executor);
         deviceService.addListener(deviceListener);
     }
 
     @Deactivate
     public void deactivate() {
         deviceService.removeListener(deviceListener);
+        executor.shutdownNow();
     }
 }
diff --git a/tools/test/topos/opticalUtils.py b/tools/test/topos/opticalUtils.py
index 87903cc..44f4e37 100644
--- a/tools/test/topos/opticalUtils.py
+++ b/tools/test/topos/opticalUtils.py
@@ -197,7 +197,7 @@
         self.configDict[ 'uri' ] = 'of:' + self.dpid
         self.configDict[ 'annotations' ] = self.annotations
         self.configDict[ 'annotations' ].setdefault('name', self.name)
-        self.configDict[ 'hw' ] = 'LINC-OE'
+        self.configDict[ 'hw' ] = 'linc-oe'
         self.configDict[ 'mfr' ] = 'Linc'
         self.configDict[ 'mac' ] = 'ffffffffffff' + self.dpid[-2] + self.dpid[-1]
         self.configDict[ 'type' ] = self.switchType