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