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