Cleaning up and enhancing driver subsystem and the flow objective subsystem.

Change-Id: Ica600ef1aaa46d19e764cd7a197454a4e0f85a08
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 dc9c6aa..9c7fddb 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
@@ -21,29 +21,45 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.driver.Behaviour;
 import org.onosproject.net.driver.DefaultDriverData;
 import org.onosproject.net.driver.DefaultDriverHandler;
 import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverAdminService;
-import org.onosproject.net.driver.DriverData;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
+import static org.onlab.util.Tools.nullIsNotFound;
+import static org.onosproject.net.AnnotationKeys.DRIVER;
 
+/**
+ * Manages inventory of device drivers.
+ */
 @Component(immediate = true)
 @Service
 public class DriverManager implements DriverAdminService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String NO_DRIVER = "Driver not found";
+    private static final String NO_DEVICE = "Device not found";
+    private static final String DEFAULT = "default";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
     private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
     private Map<String, Driver> driverByName = Maps.newConcurrentMap();
     private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
@@ -88,32 +104,65 @@
 
     @Override
     public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) {
-        //TODO
-        return null;
+        ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
+        for (Class<? extends Behaviour> behaviour : withBehaviours) {
+            driverByName.forEach((name, driver) -> {
+                if (driver.hasBehaviour(behaviour)) {
+                    builder.add(driver);
+                }
+            });
+        }
+        return builder.build();
     }
 
     @Override
     public Driver getDriver(String driverName) {
-        //TODO: replace with fallback driver.
-        return driverByName.getOrDefault(driverName, driverByName.get("default"));
+        return nullIsNotFound(driverByName.get(driverName), NO_DRIVER);
     }
 
     @Override
     public Driver getDriver(String mfr, String hw, String sw) {
-        return driverByKey.getOrDefault(key(mfr, hw, sw), driverByName.get("default"));
+        // First attempt a literal search.
+        Driver driver = driverByKey.get(key(mfr, hw, sw));
+        if (driver != null) {
+            return driver;
+        }
+
+        // Otherwise, sweep through the key space and attempt to match using
+        // regular expression matching.
+        Optional<Driver> optional = driverByKey.values().stream()
+                .filter(d -> matches(d, mfr, hw, sw)).findFirst();
+
+        // If no matching driver is found, return default.
+        return optional.isPresent() ? optional.get() : driverByName.get(DEFAULT);
+    }
+
+    // Matches the given driver using ERE matching against the given criteria.
+    private boolean matches(Driver d, String mfr, String hw, String sw) {
+        // TODO: consider pre-compiling the expressions in the future
+        return mfr.matches(d.manufacturer()) &&
+                hw.matches(d.hwVersion()) &&
+                sw.matches(d.swVersion());
     }
 
     @Override
-    public DriverHandler createHandler(String driverName, DeviceId deviceId, String... credentials) {
-        Driver driver = driverByName.get(driverName);
+    public Driver getDriver(DeviceId deviceId) {
+        Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
+        String driverName = device.annotations().value(DRIVER);
+        if (driverName != null) {
+            return getDriver(driverName);
+        }
+        return nullIsNotFound(getDriver(device.manufacturer(),
+                                        device.hwVersion(), device.swVersion()),
+                              NO_DRIVER);
+    }
+
+    @Override
+    public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
+        Driver driver = getDriver(deviceId);
         return new DefaultDriverHandler(new DefaultDriverData(driver));
     }
 
-    @Override
-    public DriverHandler createHandler(DriverData data, DeviceId deviceId, String... credentials) {
-        return null;
-    }
-
     private String key(String mfr, String hw, String sw) {
         return String.format("%s-%s-%s", mfr, hw, sw);
     }
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java
new file mode 100644
index 0000000..2006391
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Implementation of the device driver management subsystem.
+ */
+package org.onosproject.net.driver.impl;
\ No newline at end of file
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
index 840d482..1a57ffb 100644
--- a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
@@ -24,23 +24,26 @@
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onlab.osgi.ServiceDirectory;
+import org.onlab.util.ItemNotFoundException;
 import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipEvent;
 import org.onosproject.mastership.MastershipListener;
 import org.onosproject.mastership.MastershipService;
-import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.behaviour.PipelinerContext;
 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.flow.FlowRuleService;
 import org.onosproject.net.flowobjective.FilteringObjective;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.flowobjective.ForwardingObjective;
 import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.group.GroupService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,7 +54,7 @@
 import static com.google.common.base.Preconditions.checkState;
 
 /**
- * Created by ash on 07/04/15.
+ * Provides implementation of the flow objective programming service.
  */
 @Component(immediate = true)
 @Service
@@ -59,6 +62,8 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    public static final String NOT_INITIALIZED = "Driver not initialized";
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DriverService driverService;
 
@@ -71,20 +76,31 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ClusterService clusterService;
 
+    // Note: The following dependencies are added on behalf of the pipeline
+    // driver behaviours to assure these services are available for their
+    // initialization.
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GroupService groupService;
+
+
+    private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
+
+    private final PipelinerContext context = new InnerPipelineContext();
+    private final MastershipListener mastershipListener = new InnerMastershipListener();
+    private final DeviceListener deviceListener = new InnerDeviceListener();
+
     protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
-
-    private MastershipListener mastershipListener = new InnerMastershipListener();
-
-    private DeviceListener deviceListener = new InnerDeviceListener();
-
-    private Map<DeviceId, DriverHandler> driverHandlers =
-            Maps.newConcurrentMap();
+    private NodeId localNode;
 
     @Activate
     protected void activate() {
+        localNode = clusterService.getLocalNode().id();
         mastershipService.addListener(mastershipListener);
         deviceService.addListener(deviceListener);
-        deviceService.getDevices().forEach(device -> setupDriver(device.id()));
+        deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
         log.info("Started");
     }
 
@@ -97,113 +113,90 @@
 
     @Override
     public Future<Boolean> filter(DeviceId deviceId,
-                                  Collection<FilteringObjective> filterObjectives) {
-        DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.filter(filterObjectives);
+                                  Collection<FilteringObjective> filteringObjectives) {
+        return getDevicePipeliner(deviceId).filter(filteringObjectives);
     }
 
     @Override
     public Future<Boolean> forward(DeviceId deviceId,
                                    Collection<ForwardingObjective> forwardingObjectives) {
-        DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.forward(forwardingObjectives);
+        return getDevicePipeliner(deviceId).forward(forwardingObjectives);
     }
 
     @Override
     public Future<Boolean> next(DeviceId deviceId,
                                 Collection<NextObjective> nextObjectives) {
+        return getDevicePipeliner(deviceId).next(nextObjectives);
+    }
+
+    // Retrieves the device handler pipeline behaviour from the cache.
+    private Pipeliner getDevicePipeliner(DeviceId deviceId) {
         DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.next(nextObjectives);
+        checkState(handler != null, NOT_INITIALIZED);
+        return handler != null ? handler.behaviour(Pipeliner.class) : null;
     }
 
 
-
+    // Triggers driver setup when the local node becomes a device master.
     private class InnerMastershipListener implements MastershipListener {
         @Override
         public void event(MastershipEvent event) {
             switch (event.type()) {
-
                 case MASTER_CHANGED:
-                    setupDriver(event.subject());
-
-                    break;
-                case BACKUPS_CHANGED:
+                    setupPipelineHandler(event.subject());
                     break;
                 default:
-                    log.warn("Unknown mastership type {}", event.type());
+                    break;
             }
         }
-
-
     }
 
+    // Triggers driver setup when a device is (re)detected.
     private class InnerDeviceListener implements DeviceListener {
         @Override
         public void event(DeviceEvent event) {
             switch (event.type()) {
                 case DEVICE_ADDED:
                 case DEVICE_AVAILABILITY_CHANGED:
-                    setupDriver(event.subject().id());
-                    break;
-                case DEVICE_UPDATED:
-                    break;
-                case DEVICE_REMOVED:
-                    break;
-                case DEVICE_SUSPENDED:
-                    break;
-                case PORT_ADDED:
-                    break;
-                case PORT_UPDATED:
-                    break;
-                case PORT_REMOVED:
+                    setupPipelineHandler(event.subject().id());
                     break;
                 default:
-                    log.warn("Unknown event type {}", event.type());
+                    break;
             }
         }
     }
 
-    private void setupDriver(DeviceId deviceId) {
-        //TODO: Refactor this to make it nicer and use a cache.
-        if (mastershipService.getMasterFor(
-                deviceId).equals(clusterService.getLocalNode().id())) {
-
-            DriverHandler handler = lookupDriver(deviceId);
-            if (handler != null) {
-                Pipeliner pipe = handler.behaviour(Pipeliner.class);
-                pipe.init(deviceId, serviceDirectory);
+    private void setupPipelineHandler(DeviceId deviceId) {
+        if (localNode.equals(mastershipService.getMasterFor(deviceId))) {
+            // Attempt to lookup the handler in the cache
+            DriverHandler handler = driverHandlers.get(deviceId);
+            if (handler == null) {
+                try {
+                    // Otherwise create it and if it has pipeline behaviour, cache it
+                    handler = driverService.createHandler(deviceId);
+                    if (!handler.driver().hasBehaviour(Pipeliner.class)) {
+                        log.warn("Pipeline behaviour not supported for device {}",
+                                 deviceId);
+                        return;
+                    }
+                } catch (ItemNotFoundException e) {
+                    log.warn("No applicable driver for device {}", deviceId);
+                    return;
+                }
                 driverHandlers.put(deviceId, handler);
-                log.info("Driver {} bound to device {}",
-                         handler.data().type().name(), deviceId);
-            } else {
-                log.error("No driver for device {}", deviceId);
             }
+
+            // Always (re)initialize the pipeline behaviour
+            handler.behaviour(Pipeliner.class).init(deviceId, context);
+            log.info("Driver {} bound to device {}", handler.driver().name(), deviceId);
         }
     }
 
-
-    private DriverHandler lookupDriver(DeviceId deviceId) {
-        Device device = deviceService.getDevice(deviceId);
-        if (device == null) {
-            log.warn("Device is null!");
-            return null;
+    // Processing context for initializing pipeline driver behaviours.
+    private class InnerPipelineContext implements PipelinerContext {
+        @Override
+        public ServiceDirectory directory() {
+            return serviceDirectory;
         }
-        Driver driver = driverService.getDriver(device.manufacturer(),
-                                                device.hwVersion(), device.swVersion());
-
-        return driverService.createHandler(driver.name(), deviceId);
     }
-
 }
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java
new file mode 100644
index 0000000..c0779dc
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Implementations of the flow objective programming subsystem.
+ */
+package org.onosproject.net.flowobjective.impl;
\ No newline at end of file