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