Analysis of Openflowplugin ContextChain Creation and Lifecycle Management
This article provides a detailed walkthrough of Openflowplugin's ContextChain creation process, explaining how DeviceContext, RpcContext, StatisticsContext, and RoleContext are instantiated and linked, and how the resulting ContextChain supports switch lifecycle handling and master/slave role election in an SDN controller.
The article examines the second part of the Openflowplugin source code analysis, focusing on the core logic that creates the ContextChain for a newly connected switch after the handshake phase.
When a switch connects, ContextChainHolderImpl.deviceConnected invokes ContextChainHolderImpl.createContextChain , which builds a unique ContextChain object to manage the switch's lifecycle within Openflowplugin.
1. Create DeviceContext
The method calls deviceManager.createContext(connectionContext) to obtain a DeviceContext . It registers a mastership watcher, configures the packet‑in filter, creates an OutboundQueueHandler , a DeviceContextImpl , and an OpenflowProtocolListenerFullImpl to handle all messages from the switch.
final DeviceContext deviceContext = deviceManager.createContext(connectionContext);
deviceContext.registerMastershipWatcher(this);2. Create RpcContext
Using the previously created deviceContext , the code calls rpcManager.createContext(deviceContext) to obtain an RpcContext and registers the same mastership watcher.
final RpcContext rpcContext = rpcManager.createContext(deviceContext);
rpcContext.registerMastershipWatcher(this);3. Create StatisticsContext
The method creates a StatisticsContext via statisticsManager.createContext(deviceContext, ownershipChangeListener.isReconciliationFrameworkRegistered()) , registers a watcher, and also creates a MultipartWriterProvider to write operational YANG data to the device.
final StatisticsContext statisticsContext = statisticsManager.createContext(deviceContext, ownershipChangeListener.isReconciliationFrameworkRegistered());
statisticsContext.registerMastershipWatcher(this);4. Create RoleContext
A RoleContext is created with roleManager.createContext(deviceContext) , a RoleContextImpl and a SalRoleServiceImpl are instantiated to manage master/slave role assignment.
final RoleContext roleContext = roleManager.createContext(deviceContext);
roleContext.registerMastershipWatcher(this);5. Assemble ContextChain
After the four individual contexts are ready, a ContextChainImpl is instantiated. Each context is wrapped in a GuardedContextImpl and added via addContext . The chain registers a device‑removed handler, stores the chain in ContextChainHolderImpl.contextChainMap , and finally registers services with contextChain.registerServices(singletonServiceProvider) .
@Override
public
void addContext(@Nonnull final T context) {
contexts.add(new GuardedContextImpl(context));
}6. Master/Slave Election
The article notes that after the ContextChain is built, the next crucial step is the call to contextChain.registerServices , which triggers the master/slave role election among multiple controllers in a cluster.
Summary
In summary, once a switch completes the Openflow handshake, Openflowplugin creates four specialized contexts (Device, Rpc, Statistics, Role), aggregates them into a ContextChain , and prepares the switch for further operations, including high‑availability master/slave role election.
NetEase Game Operations Platform
The NetEase Game Automated Operations Platform delivers stable services for thousands of NetEase titles, focusing on efficient ops workflows, intelligent monitoring, and virtualization.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.