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