如何在OpenDaylight(Carbon)中为不同交换机部署不同应用?
Great question! In OpenDaylight Carbon, you can achieve this by leveraging the controller's event notification system to detect switch connections, then trigger application-specific configurations based on the switch's unique ID. Here's a detailed breakdown of how to implement this:
核心思路
ODL generates topology notifications whenever a switch connects or disconnects. We'll listen for the NodeAdded notification (fired when a switch first connects), extract the switch's ID (like openflow:1), and run deployment logic tailored to that specific switch.
步骤1:实现交换机连接事件监听
First, create a listener class that implements ODL's NotificationListener interface to catch NodeAdded events. This is where you'll add the logic to distinguish switches and trigger deployments.
import org.opendaylight.yang.gen.v1.urn.opendaylight.topology.notification.rev130819.NodeAdded; import org.opendaylight.yangtools.yang.binding.NotificationListener; public class SwitchDeploymentListener implements NotificationListener { @Override public void onNotification(NodeAdded notification) { // Extract the switch's unique ID (e.g., "openflow:1", "openflow:2") String switchId = notification.getNode().getNodeId().getValue(); // Route to deployment logic based on switch ID switch (switchId) { case "openflow:1": deployL2SwitchApplication(); break; case "openflow:2": deployCustomApplication(); break; default: // Handle unrecognized switches (e.g., default deployment or no action) System.out.println("No deployment logic for switch: " + switchId); break; } } private void deployL2SwitchApplication() { // Logic to set up L2 switching for this switch // Example steps: // 1. Install a default flow to forward unknown MAC packets to the controller // 2. Register a PacketIn listener to learn MAC-port mappings // 3. Install flow entries for learned MAC addresses to enable layer-2 forwarding System.out.println("Deploying L2 Switch app to openflow:1"); } private void deployCustomApplication() { // Logic for your custom application (e.g., ACL rules, QoS, routing) // Example steps: // 1. Install access control flow entries // 2. Configure group tables for traffic shaping System.out.println("Deploying custom app to openflow:2"); } }
步骤2:注册监听器到ODL框架
ODL uses Blueprint (OSGi) for component registration. Add this configuration to your application's blueprint.xml file to hook your listener into the controller's notification system:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"> <!-- Reference ODL's NotificationService to register our listener --> <reference id="notificationService" interface="org.opendaylight.controller.md.sal.binding.api.NotificationService"/> <!-- Instantiate our custom listener --> <bean id="switchDeploymentListener" class="com.your.package.name.SwitchDeploymentListener"/> <!-- Register the listener to receive NodeAdded notifications --> <odl:notification-listener ref="switchDeploymentListener" notification-service="notificationService"> <odl:notification-type type="org.opendaylight.yang.gen.v1.urn.opendaylight.topology.notification.rev130819.NodeAdded"/> </odl:notification-listener> </blueprint>
步骤3:编写具体的应用部署逻辑
The deployL2SwitchApplication() and deployCustomApplication() methods are where you'll implement the actual functionality for each switch. For example:
- For the L2 switch app: Use ODL's
FlowServiceto install flow entries that forward traffic based on MAC addresses. You'll also need to listen forPacketInnotifications to learn new MAC-port mappings. - For your custom app: Install ACL rules, configure QoS meters, or set up routing policies using ODL's MD-SAL services.
关键注意事项
- Dependency Setup: Ensure your
pom.xmlincludes dependencies for ODL modules liketopology-notification,md-sal-binding, andopenflowpluginto access the required APIs. - Switch ID Format: The switch ID follows the pattern
openflow:<dpid>, where<dpid>is the hexadecimal switch ID (e.g.,openflow:1corresponds to DPID00:00:00:00:00:00:00:01). - Reconnection Handling: If you need to handle switches disconnecting and reconnecting, add a listener for the
NodeRemovednotification to clean up old flows or state before redeploying. - Permissions: Make sure your application has the necessary ACL permissions in ODL to modify flow tables (e.g., grant
flow:writeaccess in the controller'setc/opendaylight/datastore/initial/config/ietf-netconf-acl.xml).
内容的提问来源于stack exchange,提问作者LostInTheFrequencyDomain

