Module 是指继承了 IFloodlightModule 接口的类服务器
IFloodlightModule的相关注释ide
Defines an interface for loadable Floodlight modules. At a high level, these functions are called in the following order: getModuleServices() : 得到模块实现的服务列表 getServiceImpls(); 实例化并返回:服务实现类-实现此服务的对象 映射 getModuleDependencies() : 得到模块依赖的列表 init() : internal initializations (<em>don't</em> touch other modules) startUp() : external initializations (<em>do</em> touch other modules)
全部可加载的模块都有这几种方法,在接下来的加载模块时须要使用到。
每一个具体的模块都会重写这几个函数,下面举个 FloodlightProvider 的例子。函数
@Override public Collection<Class<? extends IFloodlightService>> getModuleServices() { Collection<Class<? extends IFloodlightService>> services = new ArrayList<Class<? extends IFloodlightService>>(1); services.add(IFloodlightProviderService.class); return services; }
得到 FloodlightProvider 的服务 IFloodlightProviderServiceoop
@Override public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { controller = new Controller(); Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); m.put(IFloodlightProviderService.class, controller); return m; }
返回服务实现类和实现用的对象的Map。spa
@Override public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { Collection<Class<? extends IFloodlightService>> dependencies = new ArrayList<Class<? extends IFloodlightService>>(4); dependencies.add(IStorageSourceService.class); dependencies.add(IPktInProcessingTimeService.class); dependencies.add(IRestApiService.class); dependencies.add(IDebugCounterService.class); dependencies.add(IOFSwitchService.class); dependencies.add(IThreadPoolService.class); dependencies.add(ISyncService.class); return dependencies; }
返回依赖的列表命令行
init(),startup(),到对应的模块去看就能够了。
有的模块是没有服务依赖的,好比 ThreadPool模块。debug
Service 是指继承了 IFloodlightService 接口的类。code
public abstract interface IFloodlightService { // This space is intentionally left blank....don't touch it }
模块使用getModuleServices()方法能够得到对应的服务列表,能够到源码去看对应的服务功能。对象
CmdLineSetting 定义了命令行参数的格式继承
public static final String DEFAULT_CONFIG_FILE = "src/main/resources/floodlightdefault.properties"; @Option(name="-cf", aliases="--configFile", metaVar="FILE", usage="Floodlight configuration file") private String configFile = DEFAULT_CONFIG_FILE;
若是没有使用-cf指定配置文件路径,则使用默认路径“src/main/resources/floodlightdefault.properties”
CmdLineParser 解析命令参数
CmdLineParser parser = new CmdLineParser(settings) parser.parserArgument(args)
moduleContext=fml.loadModulesFromConfig(settings.getModuleFile())
settings.getModuleFile()提供配置文件的路径
这个方法比较重要的是这个 ServiceLoader,返回服务加载器
ServiceLoader<IFloodlightModule> moduleLoader = ServiceLoader.load(IFloodlightModule.class, cl);
ServiceLoader 为了注册服务,须要在类路径 src/main/resources/META_INF/services文件夹内列好注册服务的模块,能够获得继承了 IFloodlightModule 接口的实现类
使用moduleLoader.iterator()迭代器去填充moduleNameMap,moduleServiceMap,ServiceMap
moduleNameMap 模块名称-模块对象 Map
moduleServiceMAP 模块名称-模块服务 Map
ServiceMap 模块服务-模块名称 Map
取配置文件的一条参数配置net.floodlightcontroller.forwarding.Forwarding.match=in-port, vlan, mac, ip, transport举例
key:net.floodlightcontroller.forwarding.Forwarding.match
String configKey=key.substring(LastPeriod+1)
获到的就是 match,即 configKey=match
String systemKey = System.getProperty(key); if (systemKey != null) { configValue = systemKey; } else { configValue = prop.getProperty(key); }
若是系统属性已经存在,则使用系统属性的值,若是不存在,则configValue = prop.getProperty(key);【即 configValue 在此处为in-port, vlan, mac, ip, transport】
FloodlightModuleLoader 类下的方法
public void addConfigParam(IFloodlightModule mod, String key, String value) { Map<String, String> moduleParams = configParams.get(mod.getClass()); if (moduleParams == null) { moduleParams = new HashMap<String, String>(); configParams.put(mod.getClass(), moduleParams); } moduleParams.put(key, value); }
得到模块的服务实例
Map<Class<? extends IFloodlightService>, IFloodlightService> simpls = module.getServiceImpls();
添加服务到 floodlightModuleContext
if (floodlightModuleContext.getServiceImpl(s.getKey()) == null) { floodlightModuleContext.addService(s.getKey(), s.getValue());
遍历已加载模块集,开始初始化模块,调用模块的方法:init
for (IFloodlightModule module : moduleSet) { // init the module if (logger.isDebugEnabled()) { logger.debug("Initializing " + module.getClass().getCanonicalName()); } module.init(floodlightModuleContext); }
遍历已加载模块集,调用每一个模块的启动方法
for (IFloodlightModule m : moduleSet) { if (logger.isDebugEnabled()) { logger.debug("Starting " + m.getClass().getCanonicalName()); } m.startUp(floodlightModuleContext); }
返回公共环境变量
返回一个不可修改的已加载模块序列,若是没被初始化则返回 null
public List<IFloodlightModule> getModuleList() { if (loadedModuleList == null) return Collections.emptyList(); else return Collections.unmodifiableList(loadedModuleList); }
runModules()
for (IFloodlightModule m : getModuleList()) { for (Method method : m.getClass().getDeclaredMethods()) { Run runAnnotation = method.getAnnotation(Run.class); if (runAnnotation != null) { RunMethod runMethod = new RunMethod(m, method); if(runAnnotation.mainLoop()) { mainLoopMethods.add(runMethod); } else { runMethod.run(); } } } }
for 循环遍历模块中的方法,找到@run注解的方法为主方法,如下的就是 FloodlightProvider 提供的 run 方法
@Run(mainLoop=true) public void run() throws FloodlightModuleException { controller.run(); }
运行控制器的模块
mainLoopMethods.get(0).run();