Enhancing driver subsystem to support asynchronous event notifications.

Change-Id: I6850aae4f660b8328378da98460529eb58531732
diff --git a/apps/optical-model/src/test/java/org/onosproject/net/optical/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java b/apps/optical-model/src/test/java/org/onosproject/net/optical/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
index 84649c0..ab0af5a 100644
--- a/apps/optical-model/src/test/java/org/onosproject/net/optical/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
+++ b/apps/optical-model/src/test/java/org/onosproject/net/optical/intent/impl/compiler/OpticalCircuitIntentCompilerTest.java
@@ -50,8 +50,6 @@
 import org.onosproject.net.driver.Behaviour;
 import org.onosproject.net.driver.DefaultDriver;
 import org.onosproject.net.driver.Driver;
-import org.onosproject.net.driver.DriverHandler;
-import org.onosproject.net.driver.DriverService;
 import org.onosproject.net.driver.DriverServiceAdapter;
 import org.onosproject.net.driver.TestBehaviourImpl;
 import org.onosproject.net.driver.TestBehaviourTwoImpl;
@@ -202,31 +200,7 @@
     /**
      * Mocks the driver service so it will appear supporting TributarySlotQuery Behaviour in the test.
      */
-    private static class MockDriverServiceWithTs implements DriverService {
-        @Override
-        public Driver getDriver(String driverName) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Set<Driver> getDrivers() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Driver getDriver(String mfr, String hw, String sw) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
+    private static class MockDriverServiceWithTs extends DriverServiceAdapter {
         @Override
         public Driver getDriver(DeviceId deviceId) {
             DefaultDriver ddp = new DefaultDriver("foo.base", new ArrayList<>(), "Circus", "lux", "1.2a",
@@ -237,43 +211,12 @@
                     ImmutableMap.of("foo", "bar"));
             return ddp;
         }
-
-        @Override
-        public DriverHandler createHandler(DeviceId deviceId,
-                String... credentials) {
-            // TODO Auto-generated method stub
-            return null;
-        }
     }
 
     /**
      * Mocks the driver service so it will appear not-supporting TributarySlotQuery Behaviour in the test.
      */
-    private static class MockDriverServiceNoTs implements DriverService {
-        @Override
-        public Driver getDriver(String driverName) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Set<Driver> getDrivers() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        @Override
-        public Driver getDriver(String mfr, String hw, String sw) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
+    private static class MockDriverServiceNoTs extends DriverServiceAdapter {
         @Override
         public Driver getDriver(DeviceId deviceId) {
             DefaultDriver ddp = new DefaultDriver("foo.base", new ArrayList<>(), "Circus", "lux", "1.2a",
@@ -282,13 +225,6 @@
                     ImmutableMap.of("foo", "bar"));
             return ddp;
         }
-
-        @Override
-        public DriverHandler createHandler(DeviceId deviceId,
-                String... credentials) {
-            // TODO Auto-generated method stub
-            return null;
-        }
     }
 
     private static class MockIntentSetMultimap implements IntentSetMultimap {
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverAdminService.java b/core/api/src/main/java/org/onosproject/net/driver/DriverAdminService.java
index 6263940..90b3d28 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverAdminService.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverAdminService.java
@@ -20,8 +20,7 @@
 /**
  * Service for managing drivers and driver behaviour implementations.
  */
-public interface DriverAdminService
-        extends DriverRegistry, BehaviourClassResolver {
+public interface DriverAdminService extends DriverRegistry, BehaviourClassResolver {
 
     /**
      * Returns the set of driver providers currently registered.
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverEvent.java b/core/api/src/main/java/org/onosproject/net/driver/DriverEvent.java
new file mode 100644
index 0000000..238ede4
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverEvent.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.driver;
+
+import org.joda.time.LocalDateTime;
+import org.onosproject.event.AbstractEvent;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Driver configuration change event.
+ */
+public class DriverEvent extends AbstractEvent<DriverEvent.Type, Driver> {
+
+    /**
+     * Type of driver events.
+     */
+    public enum Type {
+        /**
+         * Signifies that the driver configuration has changed in an additive
+         * manner. Either new behaviours were added, their implementations
+         * changed, or there is a new driver entirely.
+         */
+        DRIVER_ENHANCED,
+
+        /**
+         * Signifies that the driver configuration has been reduced in some way.
+         * Either behaviours or their implementations were withdrawn or the
+         * driver was removed entirely.
+         */
+        DRIVER_REDUCED
+    }
+
+    /**
+     * Creates an event of a given type and for the specified driver and the
+     * current time.
+     *
+     * @param type   device event type
+     * @param driver event driver subject
+     */
+    public DriverEvent(Type type, Driver driver) {
+        super(type, driver);
+    }
+
+    /**
+     * Creates an event of a given type and for the specified driver and time.
+     *
+     * @param type   device event type
+     * @param driver event driver subject
+     * @param time   occurrence time
+     */
+    public DriverEvent(Type type, Driver driver, long time) {
+        super(type, driver, time);
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("time", new LocalDateTime(time()))
+                .add("type", type())
+                .add("subject", subject())
+                .toString();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverListener.java b/core/api/src/main/java/org/onosproject/net/driver/DriverListener.java
new file mode 100644
index 0000000..e6a6788
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverListener.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.driver;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Entity capable of receiving driver related events.
+ */
+public interface DriverListener extends EventListener<DriverEvent> {
+}
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverRegistry.java b/core/api/src/main/java/org/onosproject/net/driver/DriverRegistry.java
index 36a7642..99152c6 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverRegistry.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverRegistry.java
@@ -15,12 +15,15 @@
  */
 package org.onosproject.net.driver;
 
+import org.onosproject.event.ListenerService;
+
 import java.util.Set;
 
 /**
  * Service for obtaining drivers and driver behaviour implementations.
  */
-public interface DriverRegistry extends DriverPrimordialResolver, DriverResolver {
+public interface DriverRegistry extends DriverPrimordialResolver, DriverResolver,
+        ListenerService<DriverEvent, DriverListener> {
 
     /**
      * Returns the overall set of drivers being provided.
diff --git a/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverTest.java b/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverTest.java
index 178a9ac..807dd22 100644
--- a/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverTest.java
+++ b/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverTest.java
@@ -28,14 +28,14 @@
 import static org.onosproject.net.driver.DefaultDriverDataTest.DEVICE_ID;
 
 public class DefaultDriverTest {
-    private static final String MFR = "mfr";
-    private static final String HW = "hw";
-    private static final String SW = "sw";
-    private static final String KEY = "key";
-    private static final String VALUE = "value";
-    private static final String ROOT = "rootDriver";
-    private static final String CHILD = "childDriver";
-    private static final String GRAND_CHILD = "grandChilDriver";
+    public static final String MFR = "mfr";
+    public static final String HW = "hw";
+    public static final String SW = "sw";
+    public static final String KEY = "key";
+    public static final String VALUE = "value";
+    public static final String ROOT = "rootDriver";
+    public static final String CHILD = "childDriver";
+    public static final String GRAND_CHILD = "grandChildDriver";
 
     @Test
     public void basics() {
diff --git a/core/api/src/test/java/org/onosproject/net/driver/DriverAdminServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/driver/DriverAdminServiceAdapter.java
index f234779..2cfed4a 100644
--- a/core/api/src/test/java/org/onosproject/net/driver/DriverAdminServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/driver/DriverAdminServiceAdapter.java
@@ -53,4 +53,12 @@
     public Set<Driver> getDrivers() {
         return null;
     }
+
+    @Override
+    public void addListener(DriverListener listener) {
+    }
+
+    @Override
+    public void removeListener(DriverListener listener) {
+    }
 }
diff --git a/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
index 813befc..b2644db 100644
--- a/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
@@ -53,4 +53,12 @@
     public Driver getDriver(String driverName) {
         return null;
     }
+
+    @Override
+    public void addListener(DriverListener listener) {
+    }
+
+    @Override
+    public void removeListener(DriverListener listener) {
+    }
 }
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 c96b6ef..47d4f91 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
@@ -31,6 +31,7 @@
 import org.onosproject.net.driver.DefaultDriverHandler;
 import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverListener;
 import org.onosproject.net.driver.DriverRegistry;
 import org.onosproject.net.driver.DriverService;
 import org.slf4j.Logger;
@@ -129,4 +130,13 @@
         return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
     }
 
+    @Override
+    public void addListener(DriverListener listener) {
+        registry.addListener(listener);
+    }
+
+    @Override
+    public void removeListener(DriverListener listener) {
+        registry.removeListener(listener);
+    }
 }
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverRegistryManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverRegistryManager.java
index 5b3fdbe..fbb21ec 100644
--- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverRegistryManager.java
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverRegistryManager.java
@@ -28,11 +28,15 @@
 import org.apache.felix.scr.annotations.Service;
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.component.ComponentService;
+import org.onosproject.event.EventDeliveryService;
+import org.onosproject.event.ListenerRegistry;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.driver.Behaviour;
 import org.onosproject.net.driver.DefaultDriverProvider;
 import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverAdminService;
+import org.onosproject.net.driver.DriverEvent;
+import org.onosproject.net.driver.DriverListener;
 import org.onosproject.net.driver.DriverProvider;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
@@ -47,6 +51,8 @@
 import static com.google.common.base.Strings.isNullOrEmpty;
 import static org.onlab.util.Tools.get;
 import static org.onlab.util.Tools.nullIsNotFound;
+import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_ENHANCED;
+import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_REDUCED;
 import static org.onosproject.security.AppGuard.checkPermission;
 import static org.onosproject.security.AppPermission.Type.DRIVER_READ;
 
@@ -76,6 +82,8 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ComponentService componenService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventDeliveryService eventDispatcher;
 
     private static final String DEFAULT_REQUIRED_DRIVERS = "default";
     @Property(name = "requiredDrivers", value = DEFAULT_REQUIRED_DRIVERS,
@@ -87,11 +95,16 @@
     private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
     private Map<String, Class<? extends Behaviour>> classes = Maps.newConcurrentMap();
 
+    private ListenerRegistry<DriverEvent, DriverListener> listenerRegistry;
+
+
     private boolean isStarted = false;
 
     @Activate
     protected void activate(ComponentContext context) {
         componentConfigService.registerProperties(getClass());
+        listenerRegistry = new ListenerRegistry<>();
+        eventDispatcher.addSink(DriverEvent.class, listenerRegistry);
         modified(context);
         log.info("Started");
     }
@@ -99,6 +112,7 @@
     @Deactivate
     protected void deactivate() {
         componentConfigService.unregisterProperties(getClass(), false);
+        eventDispatcher.removeSink(DriverEvent.class);
         providers.clear();
         driverByKey.clear();
         classes.clear();
@@ -134,6 +148,7 @@
                 classes.put(b.getName(), b);
                 classes.put(implementation.getName(), implementation);
             });
+            post(new DriverEvent(DRIVER_ENHANCED, driver));
         });
         providers.add(provider);
         checkRequiredDrivers();
@@ -146,6 +161,7 @@
             driverByKey.remove(key(driver.manufacturer(),
                                    driver.hwVersion(),
                                    driver.swVersion()));
+            post(new DriverEvent(DRIVER_REDUCED, driver));
         });
         providers.remove(provider);
         checkRequiredDrivers();
@@ -225,4 +241,22 @@
     static String key(String mfr, String hw, String sw) {
         return String.format("%s-%s-%s", mfr, hw, sw);
     }
+
+    @Override
+    public void addListener(DriverListener listener) {
+        listenerRegistry.addListener(listener);
+    }
+
+    @Override
+    public void removeListener(DriverListener listener) {
+        listenerRegistry.removeListener(listener);
+    }
+
+    // Safely posts the specified event to the local event dispatcher.
+    private void post(DriverEvent event) {
+        if (eventDispatcher != null) {
+            eventDispatcher.post(event);
+        }
+    }
+
 }
diff --git a/core/net/src/test/java/org/onosproject/net/driver/impl/DriverRegistryManagerTest.java b/core/net/src/test/java/org/onosproject/net/driver/impl/DriverRegistryManagerTest.java
new file mode 100644
index 0000000..053bc29
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/driver/impl/DriverRegistryManagerTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.driver.impl;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.cfg.ComponentConfigAdapter;
+import org.onosproject.common.event.impl.TestEventDispatcher;
+import org.onosproject.component.ComponentService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.DefaultDriverProvider;
+import org.onosproject.net.driver.DriverEvent;
+import org.onosproject.net.driver.DriverListener;
+import org.onosproject.net.driver.TestBehaviour;
+import org.onosproject.net.driver.TestBehaviourImpl;
+
+import static org.junit.Assert.*;
+import static org.onosproject.net.driver.DefaultDriverTest.*;
+import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_ENHANCED;
+import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_REDUCED;
+
+/**
+ * Suite of tests for the driver registry mechanism.
+ */
+public class DriverRegistryManagerTest {
+
+    private DriverRegistryManager mgr;
+    private TestEventListener testListener = new TestEventListener();
+    private TestComponentService componentService = new TestComponentService();
+
+    @Before
+    public void setUp() {
+        mgr = new DriverRegistryManager();
+        mgr.deviceService = new DeviceServiceAdapter();
+        mgr.componentConfigService = new ComponentConfigAdapter();
+        mgr.eventDispatcher = new TestEventDispatcher();
+        mgr.componenService = componentService;
+        mgr.activate(null);
+    }
+
+    @After
+    public void tearDown() {
+        mgr.deactivate();
+    }
+
+    @Test
+    public void basicEvents() {
+        mgr.addListener(testListener);
+        DefaultDriverProvider mockProvider = new DefaultDriverProvider();
+        DefaultDriver driver = new DefaultDriver("foo", Lists.newArrayList(),
+                                                 MFR, HW, SW,
+                                                 ImmutableMap.of(TestBehaviour.class,
+                                                                 TestBehaviourImpl.class),
+                                                 ImmutableMap.of("foo", "bar"));
+        mockProvider.addDriver(driver);
+        mgr.registerProvider(mockProvider);
+        assertEquals("wrong driver event type", DRIVER_ENHANCED, testListener.event.type());
+        assertSame("wrong driver event subject", driver, testListener.event.subject());
+
+        mgr.unregisterProvider(mockProvider);
+        assertEquals("wrong driver event type", DRIVER_REDUCED, testListener.event.type());
+        assertSame("wrong driver event subject", driver, testListener.event.subject());
+
+        mgr.removeListener(testListener);
+    }
+
+    @Test
+    public void managerStart() {
+        DefaultDriverProvider mockProvider = new DefaultDriverProvider();
+        DefaultDriver driver = new DefaultDriver("default", Lists.newArrayList(),
+                                                 MFR, HW, SW,
+                                                 ImmutableMap.of(TestBehaviour.class,
+                                                                 TestBehaviourImpl.class),
+                                                 ImmutableMap.of("foo", "bar"));
+        mockProvider.addDriver(driver);
+        mgr.registerProvider(mockProvider);
+        assertTrue("should be activated", componentService.activated);
+
+        mgr.unregisterProvider(mockProvider);
+        assertFalse("should not be dactivated", componentService.activated);
+    }
+
+    @Test
+    public void basicQueries() {
+        DefaultDriverProvider mockProvider = new DefaultDriverProvider();
+        DefaultDriver driver = new DefaultDriver("default", Lists.newArrayList(),
+                                                 MFR, HW, SW,
+                                                 ImmutableMap.of(TestBehaviour.class,
+                                                                 TestBehaviourImpl.class),
+                                                 ImmutableMap.of("foo", "bar"));
+        mockProvider.addDriver(driver);
+        mgr.registerProvider(mockProvider);
+        assertSame("driver is missing", driver, mgr.getDriver("default"));
+        assertSame("driver is missing", driver, mgr.getDriver(MFR, HW, SW));
+        assertArrayEquals("driver list is wrong",
+                          ImmutableList.of(driver).toArray(),
+                          mgr.getDrivers().toArray());
+        assertArrayEquals("provider list is wrong",
+                          ImmutableList.of(mockProvider).toArray(),
+                          mgr.getProviders().toArray());
+        assertEquals("wrong behaviour class", TestBehaviourImpl.class,
+                     mgr.getBehaviourClass("org.onosproject.net.driver.TestBehaviourImpl"));
+    }
+
+    // TODO: add tests for REGEX matching and for driver inheritance
+
+    private class TestEventListener implements DriverListener {
+        private DriverEvent event;
+
+        @Override
+        public void event(DriverEvent event) {
+            this.event = event;
+        }
+    }
+
+    private class TestComponentService implements ComponentService {
+        private boolean activated;
+
+        @Override
+        public void activate(ApplicationId appId, String name) {
+            activated = true;
+        }
+
+        @Override
+        public void deactivate(ApplicationId appId, String name) {
+            activated = false;
+        }
+    }
+}
\ No newline at end of file