WIP: default driver ignition
Change-Id: Ia37de8dcaee2ff2be0908fa12c567acf99ef3a13
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 49d9696..840d482 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
@@ -31,6 +31,8 @@
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.behaviour.Pipeliner;
+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;
@@ -73,18 +75,23 @@
private MastershipListener mastershipListener = new InnerMastershipListener();
+ private DeviceListener deviceListener = new InnerDeviceListener();
+
private Map<DeviceId, DriverHandler> driverHandlers =
Maps.newConcurrentMap();
@Activate
protected void activate() {
mastershipService.addListener(mastershipListener);
+ deviceService.addListener(deviceListener);
+ deviceService.getDevices().forEach(device -> setupDriver(device.id()));
log.info("Started");
}
@Deactivate
protected void deactivate() {
mastershipService.removeListener(mastershipListener);
+ deviceService.removeListener(deviceListener);
log.info("Stopped");
}
@@ -121,27 +128,15 @@
return pipe.next(nextObjectives);
}
+
+
private class InnerMastershipListener implements MastershipListener {
@Override
public void event(MastershipEvent event) {
switch (event.type()) {
case MASTER_CHANGED:
- //TODO: refactor this into a method
- if (event.roleInfo().master().equals(
- clusterService.getLocalNode().id())) {
- DriverHandler handler = lookupDriver(event.subject());
- if (handler != null) {
- Pipeliner pipe = handler.behaviour(Pipeliner.class);
- pipe.init(event.subject(), serviceDirectory);
- driverHandlers.put(event.subject(), handler);
- log.info("Driver {} bound to device {}",
- handler.data().type().name(), event.subject());
- } else {
- log.error("No driver for device {}", event.subject());
- }
-
- }
+ setupDriver(event.subject());
break;
case BACKUPS_CHANGED:
@@ -151,13 +146,64 @@
}
}
- private DriverHandler lookupDriver(DeviceId deviceId) {
- Device device = deviceService.getDevice(deviceId);
- Driver driver = driverService.getDriver(device.manufacturer(),
- device.hwVersion(), device.swVersion());
+ }
- return driverService.createHandler(driver.name(), deviceId);
+ 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:
+ break;
+ default:
+ log.warn("Unknown event type {}", event.type());
+ }
}
}
+
+ 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);
+ driverHandlers.put(deviceId, handler);
+ log.info("Driver {} bound to device {}",
+ handler.data().type().name(), deviceId);
+ } else {
+ log.error("No driver for device {}", deviceId);
+ }
+ }
+ }
+
+
+ private DriverHandler lookupDriver(DeviceId deviceId) {
+ Device device = deviceService.getDevice(deviceId);
+ if (device == null) {
+ log.warn("Device is null!");
+ return null;
+ }
+ Driver driver = driverService.getDriver(device.manufacturer(),
+ device.hwVersion(), device.swVersion());
+
+ return driverService.createHandler(driver.name(), deviceId);
+ }
+
}
diff --git a/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java b/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
new file mode 100644
index 0000000..84dd5db
--- /dev/null
+++ b/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
@@ -0,0 +1,365 @@
+/*
+ * 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.
+ */
+package org.onosproject.driver.pipeline;
+
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.driver.DriverData;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperations;
+import org.onosproject.net.flow.FlowRuleOperationsContext;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.FilteringObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.net.flowobjective.NextObjective;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.concurrent.Future;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Created by ash on 07/04/15.
+ */
+public class OVSCorsaPipeline implements Pipeliner {
+
+ private static final int CONTROLLER_PRIORITY = 255;
+ private static final int DROP_PRIORITY = 0;
+ private static final int HIGHEST_PRIORITY = 0xffff;
+
+ private final Logger log = getLogger(getClass());
+
+ private ServiceDirectory serviceDirectory;
+ private FlowRuleService flowRuleService;
+ private CoreService coreService;
+ private DeviceId deviceId;
+ private ApplicationId appId;
+
+ @Override
+ public void init(DeviceId deviceId, ServiceDirectory serviceDirectory) {
+ this.serviceDirectory = serviceDirectory;
+ this.deviceId = deviceId;
+
+
+ coreService = serviceDirectory.get(CoreService.class);
+ flowRuleService = serviceDirectory.get(FlowRuleService.class);
+
+ appId = coreService.registerApplication(
+ "org.onosproject.driver.OVSCorsaPipeline");
+
+ pushDefaultRules();
+
+ }
+
+ @Override
+ public Future<Boolean> filter(Collection<FilteringObjective> filteringObjectives) {
+ return null;
+ }
+
+ @Override
+ public Future<Boolean> forward(Collection<ForwardingObjective> forwardObjectives) {
+ return null;
+ }
+
+ @Override
+ public Future<Boolean> next(Collection<NextObjective> nextObjectives) {
+ return null;
+ }
+
+ @Override
+ public void setData(DriverData data) {
+
+ }
+
+
+ private void pushDefaultRules() {
+ boolean install = true;
+ processTableZero(install);
+ processTableOne(install);
+ processTableTwo(install);
+ processTableFour(install);
+ processTableFive(install);
+ processTableSix(install);
+ processTableNine(install);
+ }
+
+ private void processTableZero(boolean install) {
+ TrafficSelector.Builder selector;
+ TrafficTreatment.Builder treatment;
+
+ // Bcast rule
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ selector.matchEthDst(MacAddress.BROADCAST);
+ treatment.transition(FlowRule.Type.VLAN_MPLS);
+
+ FlowRule rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(),
+ CONTROLLER_PRIORITY, appId, 0,
+ true, FlowRule.Type.FIRST);
+
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+
+
+ //Drop rule
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ treatment.drop();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), DROP_PRIORITY, appId,
+ 0, true, FlowRule.Type.FIRST);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned default table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision default table for bgp router");
+ }
+ }));
+
+ }
+
+ private void processTableOne(boolean install) {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ TrafficTreatment.Builder treatment = DefaultTrafficTreatment
+ .builder();
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+ selector.matchVlanId(VlanId.ANY);
+ treatment.transition(FlowRule.Type.VLAN);
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), CONTROLLER_PRIORITY,
+ appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned vlan/mpls table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info(
+ "Failed to provision vlan/mpls table for bgp router");
+ }
+ }));
+
+ }
+
+ private void processTableTwo(boolean install) {
+ TrafficSelector.Builder selector;
+ TrafficTreatment.Builder treatment;
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+
+
+ //Drop rule
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ treatment.drop();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), DROP_PRIORITY, appId,
+ 0, true, FlowRule.Type.VLAN);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned vlan table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision vlan table for bgp router");
+ }
+ }));
+ }
+
+ private void processTableFour(boolean install) {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ TrafficTreatment.Builder treatment = DefaultTrafficTreatment
+ .builder();
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+ selector.matchEthType(Ethernet.TYPE_ARP);
+ treatment.punt();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), CONTROLLER_PRIORITY,
+ appId, 0, true, FlowRule.Type.ETHER);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ selector.matchEthType(Ethernet.TYPE_IPV4);
+ treatment.transition(FlowRule.Type.COS);
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), CONTROLLER_PRIORITY,
+ appId, 0, true, FlowRule.Type.ETHER);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ //Drop rule
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ treatment.drop();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), DROP_PRIORITY, appId,
+ 0, true, FlowRule.Type.ETHER);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned ether table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision ether table for bgp router");
+ }
+ }));
+
+ }
+
+ private void processTableFive(boolean install) {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ TrafficTreatment.Builder treatment = DefaultTrafficTreatment
+ .builder();
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+ treatment.transition(FlowRule.Type.IP);
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), DROP_PRIORITY, appId,
+ 0, true, FlowRule.Type.COS);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned cos table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision cos table for bgp router");
+ }
+ }));
+
+ }
+
+ private void processTableSix(boolean install) {
+ TrafficSelector.Builder selector;
+ TrafficTreatment.Builder treatment;
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+ //Drop rule
+ selector = DefaultTrafficSelector.builder();
+ treatment = DefaultTrafficTreatment.builder();
+
+ treatment.drop();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), DROP_PRIORITY, appId,
+ 0, true, FlowRule.Type.IP);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned FIB table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision FIB table for bgp router");
+ }
+ }));
+ }
+
+ private void processTableNine(boolean install) {
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ TrafficTreatment.Builder treatment = DefaultTrafficTreatment
+ .builder();
+ FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+ FlowRule rule;
+
+ treatment.punt();
+
+ rule = new DefaultFlowRule(deviceId, selector.build(),
+ treatment.build(), CONTROLLER_PRIORITY,
+ appId, 0, true, FlowRule.Type.DEFAULT);
+
+ ops = install ? ops.add(rule) : ops.remove(rule);
+
+ flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
+ @Override
+ public void onSuccess(FlowRuleOperations ops) {
+ log.info("Provisioned Local table for bgp router");
+ }
+
+ @Override
+ public void onError(FlowRuleOperations ops) {
+ log.info("Failed to provision Local table for bgp router");
+ }
+ }));
+ }
+
+}
diff --git a/drivers/src/main/resources/default.xml b/drivers/src/main/resources/default.xml
index 6626412..f1a1017 100644
--- a/drivers/src/main/resources/default.xml
+++ b/drivers/src/main/resources/default.xml
@@ -19,4 +19,8 @@
<behaviour api="org.onosproject.net.behaviour.Pipeliner"
impl="org.onosproject.driver.pipeline.DefaultSingleTablePipeline"/>
</driver>
+ <driver name="ovs-corsa" manufacturer="Nicira, Inc." hwVersion="Open vSwitch" swVersion="2.3.0">
+ <behaviour api="org.onosproject.net.behaviour.Pipeliner"
+ impl="org.onosproject.driver.pipeline.OVSCorsaPipeline"/>
+ </driver>
</drivers>
\ No newline at end of file