Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.module; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 7 | import net.floodlightcontroller.core.test.MockFloodlightProvider; |
| 8 | import net.floodlightcontroller.core.test.MockThreadPoolService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 9 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
| 12 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 13 | public class FloodlightTestModuleLoader extends FloodlightModuleLoader { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 14 | protected final static Logger log = LoggerFactory.getLogger(FloodlightTestModuleLoader.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 15 | |
| 16 | // List of default modules to use unless specified otherwise |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | public static final Class<? extends IFloodlightModule> DEFAULT_FLOODLIGHT_PRPOVIDER = |
| 18 | MockFloodlightProvider.class; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | public static final Class<? extends IFloodlightModule> DEFAULT_THREADPOOL = |
| 20 | MockThreadPoolService.class; |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 21 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | |
| 23 | protected static final Collection<Class<? extends IFloodlightModule>> DEFAULT_MODULE_LIST; |
| 24 | |
| 25 | static { |
| 26 | DEFAULT_MODULE_LIST = new ArrayList<Class<? extends IFloodlightModule>>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | DEFAULT_MODULE_LIST.add(DEFAULT_FLOODLIGHT_PRPOVIDER); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | DEFAULT_MODULE_LIST.add(DEFAULT_THREADPOOL); |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 29 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | protected IFloodlightModuleContext fmc; |
| 33 | |
| 34 | /** |
| 35 | * Adds default modules to the list of modules to load. This is done |
| 36 | * in order to avoid the module loader throwing errors about duplicate |
| 37 | * modules and neither one is specified by the user. |
| 38 | * @param userModules The list of user specified modules to add to. |
| 39 | */ |
| 40 | protected void addDefaultModules(Collection<Class<? extends IFloodlightModule>> userModules) { |
| 41 | Collection<Class<? extends IFloodlightModule>> defaultModules = |
| 42 | new ArrayList<Class<? extends IFloodlightModule>>(DEFAULT_MODULE_LIST.size()); |
| 43 | defaultModules.addAll(DEFAULT_MODULE_LIST); |
| 44 | |
| 45 | Iterator<Class<? extends IFloodlightModule>> modIter = userModules.iterator(); |
| 46 | while (modIter.hasNext()) { |
| 47 | Class<? extends IFloodlightModule> userMod = modIter.next(); |
| 48 | Iterator<Class<? extends IFloodlightModule>> dmIter = defaultModules.iterator(); |
| 49 | while (dmIter.hasNext()) { |
| 50 | Class<? extends IFloodlightModule> dmMod = dmIter.next(); |
| 51 | Collection<Class<? extends IFloodlightService>> userModServs; |
| 52 | Collection<Class<? extends IFloodlightService>> dmModServs; |
| 53 | try { |
| 54 | dmModServs = dmMod.newInstance().getModuleServices(); |
| 55 | userModServs = userMod.newInstance().getModuleServices(); |
| 56 | } catch (InstantiationException e) { |
| 57 | log.error(e.getMessage()); |
| 58 | break; |
| 59 | } catch (IllegalAccessException e) { |
| 60 | log.error(e.getMessage()); |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | // If either of these are null continue as they have no services |
| 65 | if (dmModServs == null || userModServs == null) continue; |
| 66 | |
| 67 | // If the user supplied modules has a service |
| 68 | // that is in the default module list we remove |
| 69 | // the default module from the list. |
| 70 | boolean shouldBreak = false; |
| 71 | Iterator<Class<? extends IFloodlightService>> userModServsIter |
| 72 | = userModServs.iterator(); |
| 73 | while (userModServsIter.hasNext()) { |
| 74 | Class<? extends IFloodlightService> userModServIntf = userModServsIter.next(); |
| 75 | Iterator<Class<? extends IFloodlightService>> dmModsServsIter |
| 76 | = dmModServs.iterator(); |
| 77 | while (dmModsServsIter.hasNext()) { |
| 78 | Class<? extends IFloodlightService> dmModServIntf |
| 79 | = dmModsServsIter.next(); |
| 80 | |
| 81 | if (dmModServIntf.getCanonicalName().equals( |
| 82 | userModServIntf.getCanonicalName())) { |
| 83 | logger.debug("Removing default module {} because it was " + |
| 84 | "overriden by an explicitly specified module", |
| 85 | dmModServIntf.getCanonicalName()); |
| 86 | dmIter.remove(); |
| 87 | shouldBreak = true; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (shouldBreak) break; |
| 92 | } |
| 93 | if (shouldBreak) break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Append the remaining default modules to the user specified ones. |
| 98 | // This avoids the module loader throwing duplicate module errors. |
| 99 | userModules.addAll(defaultModules); |
| 100 | log.debug("Using module set " + userModules.toString()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets up all modules and their dependencies. |
| 105 | * @param modules The list of modules that the user wants to load. |
| 106 | * @param mockedServices The list of services that will be mocked. Any |
| 107 | * module that provides this service will not be loaded. |
| 108 | */ |
| 109 | public void setupModules(Collection<Class<? extends IFloodlightModule>> modules, |
| 110 | Collection<IFloodlightService> mockedServices) { |
| 111 | addDefaultModules(modules); |
| 112 | Collection<String> modulesAsString = new ArrayList<String>(); |
| 113 | for (Class<? extends IFloodlightModule> m : modules) { |
| 114 | modulesAsString.add(m.getCanonicalName()); |
| 115 | } |
| 116 | |
| 117 | try { |
| 118 | fmc = loadModulesFromList(modulesAsString, null, mockedServices); |
| 119 | } catch (FloodlightModuleException e) { |
| 120 | log.error(e.getMessage()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Gets the inited/started instance of a module from the context. |
| 126 | * @param ifl The name if the module to get, i.e. "LearningSwitch.class". |
| 127 | * @return The inited/started instance of the module. |
| 128 | */ |
| 129 | public IFloodlightModule getModuleByName(Class<? extends IFloodlightModule> ifl) { |
| 130 | Collection<IFloodlightModule> modules = fmc.getAllModules(); |
| 131 | for (IFloodlightModule m : modules) { |
| 132 | if (ifl.getCanonicalName().equals(m.getClass().getCanonicalName())) { |
| 133 | return m; |
| 134 | } |
| 135 | } |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets an inited/started instance of a service from the context. |
| 141 | * @param ifs The name of the service to get, i.e. "ITopologyService.class". |
| 142 | * @return The inited/started instance of the service from teh context. |
| 143 | */ |
| 144 | public IFloodlightService getModuleByService(Class<? extends IFloodlightService> ifs) { |
| 145 | Collection<IFloodlightModule> modules = fmc.getAllModules(); |
| 146 | for (IFloodlightModule m : modules) { |
| 147 | Collection<Class<? extends IFloodlightService>> mServs = m.getModuleServices(); |
| 148 | if (mServs == null) continue; |
| 149 | for (Class<? extends IFloodlightService> mServClass : mServs) { |
| 150 | if (mServClass.getCanonicalName().equals(ifs.getCanonicalName())) { |
| 151 | assert(m instanceof IFloodlightService); |
| 152 | return (IFloodlightService)m; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | } |