blob: beb6d4f8809c96459da62a0ae9e86a349f612553 [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 {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070014 protected final static Logger log = LoggerFactory.getLogger(FloodlightTestModuleLoader.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080015
16 // List of default modules to use unless specified otherwise
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017 public static final Class<? extends IFloodlightModule> DEFAULT_FLOODLIGHT_PRPOVIDER =
18 MockFloodlightProvider.class;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019 public static final Class<? extends IFloodlightModule> DEFAULT_THREADPOOL =
20 MockThreadPoolService.class;
mininet73e7fb72013-12-03 14:25:53 -080021
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022
23 protected static final Collection<Class<? extends IFloodlightModule>> DEFAULT_MODULE_LIST;
24
25 static {
26 DEFAULT_MODULE_LIST = new ArrayList<Class<? extends IFloodlightModule>>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027 DEFAULT_MODULE_LIST.add(DEFAULT_FLOODLIGHT_PRPOVIDER);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028 DEFAULT_MODULE_LIST.add(DEFAULT_THREADPOOL);
mininet73e7fb72013-12-03 14:25:53 -080029
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 }
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}