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