blob: b31682ebd2b9b72caef53d758dca2d6b2184bfb6 [file] [log] [blame]
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001/**
2 * Copyright 2013, Big Switch Networks, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License. You may obtain
6 * a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 **/
16
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017package net.floodlightcontroller.core.module;
18
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Iterator;
22
Jonathan Hart2fa28062013-11-25 20:16:28 -080023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070026import net.floodlightcontroller.core.module.FloodlightModuleLoader;
27import net.floodlightcontroller.core.module.IFloodlightModule;
28import net.floodlightcontroller.core.test.MockFloodlightProvider;
29import net.floodlightcontroller.core.test.MockThreadPoolService;
30import net.onrc.onos.apps.websocket.WebSocketModule;
31import net.onrc.onos.core.datagrid.HazelcastDatagrid;
32import net.onrc.onos.core.flowprogrammer.FlowProgrammer;
33import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
34import net.onrc.onos.core.intent.runtime.PlanInstallModule;
35import net.onrc.onos.core.metrics.OnosMetricsModule;
36import net.onrc.onos.core.registry.ZookeeperRegistry;
37import net.onrc.onos.core.topology.TopologyPublisher;
38
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039public class FloodlightTestModuleLoader extends FloodlightModuleLoader {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070040 protected static Logger log = LoggerFactory.getLogger(FloodlightTestModuleLoader.class);
mininet73e7fb72013-12-03 14:25:53 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 // List of default modules to use unless specified otherwise
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070043 public static final Class<? extends IFloodlightModule> DEFAULT_HZ_DATAGRID =
44 HazelcastDatagrid.class;
45 public static final Class<? extends IFloodlightModule> DEFAULT_FLOODLIGHT_PROVIDER =
Ray Milkey269ffb92014-04-03 14:43:30 -070046 MockFloodlightProvider.class;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070047 public static final Class<? extends IFloodlightModule> DEFAULT_TOPOLOGY_PUBLISHER =
48 TopologyPublisher.class;
Ray Milkey269ffb92014-04-03 14:43:30 -070049 public static final Class<? extends IFloodlightModule> DEFAULT_THREADPOOL =
50 MockThreadPoolService.class;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070051 public static final Class<? extends IFloodlightModule> DEFAULT_FLOW_PROGRAMMER =
52 FlowProgrammer.class;
53 public static final Class<? extends IFloodlightModule> DEFAULT_PATHCALC_RUNTIME =
54 PathCalcRuntimeModule.class;
55 public static final Class<? extends IFloodlightModule> DEFAULT_PLAN_INSTALL =
56 PlanInstallModule.class;
57 public static final Class<? extends IFloodlightModule> DEFAULT_ZOOKEEPER_REGISTRY =
58 ZookeeperRegistry.class;
59 public static final Class<? extends IFloodlightModule> DEFAULT_METRICS_MODULE =
60 OnosMetricsModule.class;
61 public static final Class<? extends IFloodlightModule> DEFAULT_WEBSOCKET_MODULE =
62 WebSocketModule.class;
Ray Milkey269ffb92014-04-03 14:43:30 -070063
64 protected static final Collection<Class<? extends IFloodlightModule>> DEFAULT_MODULE_LIST;
65
66 static {
67 DEFAULT_MODULE_LIST = new ArrayList<Class<? extends IFloodlightModule>>();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070068 DEFAULT_MODULE_LIST.add(DEFAULT_HZ_DATAGRID);
69 DEFAULT_MODULE_LIST.add(DEFAULT_FLOODLIGHT_PROVIDER);
70 DEFAULT_MODULE_LIST.add(DEFAULT_FLOW_PROGRAMMER);
71 DEFAULT_MODULE_LIST.add(DEFAULT_TOPOLOGY_PUBLISHER);
72 DEFAULT_MODULE_LIST.add(DEFAULT_PATHCALC_RUNTIME);
Ray Milkey269ffb92014-04-03 14:43:30 -070073 DEFAULT_MODULE_LIST.add(DEFAULT_THREADPOOL);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070074 DEFAULT_MODULE_LIST.add(DEFAULT_PLAN_INSTALL);
75 DEFAULT_MODULE_LIST.add(DEFAULT_ZOOKEEPER_REGISTRY);
76 DEFAULT_MODULE_LIST.add(DEFAULT_METRICS_MODULE);
77 DEFAULT_MODULE_LIST.add(DEFAULT_WEBSOCKET_MODULE);
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
79
80 protected IFloodlightModuleContext fmc;
81
82 /**
83 * Adds default modules to the list of modules to load. This is done
84 * in order to avoid the module loader throwing errors about duplicate
85 * modules and neither one is specified by the user.
Ray Milkey269ffb92014-04-03 14:43:30 -070086 * @param userModules The list of user specified modules to add to.
87 */
88 protected void addDefaultModules(Collection<Class<? extends IFloodlightModule>> userModules) {
89 Collection<Class<? extends IFloodlightModule>> defaultModules =
90 new ArrayList<Class<? extends IFloodlightModule>>(DEFAULT_MODULE_LIST.size());
91 defaultModules.addAll(DEFAULT_MODULE_LIST);
92
93 Iterator<Class<? extends IFloodlightModule>> modIter = userModules.iterator();
94 while (modIter.hasNext()) {
95 Class<? extends IFloodlightModule> userMod = modIter.next();
96 Iterator<Class<? extends IFloodlightModule>> dmIter = defaultModules.iterator();
97 while (dmIter.hasNext()) {
98 Class<? extends IFloodlightModule> dmMod = dmIter.next();
99 Collection<Class<? extends IFloodlightService>> userModServs;
100 Collection<Class<? extends IFloodlightService>> dmModServs;
101 try {
102 dmModServs = dmMod.newInstance().getModuleServices();
103 userModServs = userMod.newInstance().getModuleServices();
104 } catch (InstantiationException e) {
105 log.error(e.getMessage());
106 break;
107 } catch (IllegalAccessException e) {
108 log.error(e.getMessage());
109 break;
110 }
111
112 // If either of these are null continue as they have no services
113 if (dmModServs == null || userModServs == null) continue;
114
115 // If the user supplied modules has a service
116 // that is in the default module list we remove
117 // the default module from the list.
118 boolean shouldBreak = false;
119 Iterator<Class<? extends IFloodlightService>> userModServsIter
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700120 = userModServs.iterator();
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 while (userModServsIter.hasNext()) {
122 Class<? extends IFloodlightService> userModServIntf = userModServsIter.next();
123 Iterator<Class<? extends IFloodlightService>> dmModsServsIter
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700124 = dmModServs.iterator();
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 while (dmModsServsIter.hasNext()) {
126 Class<? extends IFloodlightService> dmModServIntf
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700127 = dmModsServsIter.next();
Ray Milkey269ffb92014-04-03 14:43:30 -0700128
129 if (dmModServIntf.getCanonicalName().equals(
130 userModServIntf.getCanonicalName())) {
131 logger.debug("Removing default module {} because it was " +
132 "overriden by an explicitly specified module",
133 dmModServIntf.getCanonicalName());
134 dmIter.remove();
135 shouldBreak = true;
136 break;
137 }
138 }
139 if (shouldBreak) break;
140 }
141 if (shouldBreak) break;
142 }
143 }
144
145 // Append the remaining default modules to the user specified ones.
146 // This avoids the module loader throwing duplicate module errors.
147 userModules.addAll(defaultModules);
148 log.debug("Using module set " + userModules.toString());
149 }
150
151 /**
152 * Sets up all modules and their dependencies.
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700153 * @param modules The list of modules that the user wants to load.
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 * @param mockedServices The list of services that will be mocked. Any
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700155 * module that provides this service will not be loaded.
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 */
157 public void setupModules(Collection<Class<? extends IFloodlightModule>> modules,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700158 Collection<IFloodlightService> mockedServices) throws FloodlightModuleException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700159 addDefaultModules(modules);
160 Collection<String> modulesAsString = new ArrayList<String>();
161 for (Class<? extends IFloodlightModule> m : modules) {
162 modulesAsString.add(m.getCanonicalName());
163 }
164
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700165 fmc = loadModulesFromList(modulesAsString, null, mockedServices);
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 }
167
168 /**
169 * Gets the inited/started instance of a module from the context.
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 * @param ifl The name if the module to get, i.e. "LearningSwitch.class".
171 * @return The inited/started instance of the module.
172 */
173 public IFloodlightModule getModuleByName(Class<? extends IFloodlightModule> ifl) {
174 Collection<IFloodlightModule> modules = fmc.getAllModules();
175 for (IFloodlightModule m : modules) {
176 if (ifl.getCanonicalName().equals(m.getClass().getCanonicalName())) {
177 return m;
178 }
179 }
180 return null;
181 }
182
183 /**
184 * Gets an inited/started instance of a service from the context.
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 * @param ifs The name of the service to get, i.e. "ITopologyService.class".
186 * @return The inited/started instance of the service from teh context.
187 */
188 public IFloodlightService getModuleByService(Class<? extends IFloodlightService> ifs) {
189 Collection<IFloodlightModule> modules = fmc.getAllModules();
190 for (IFloodlightModule m : modules) {
191 Collection<Class<? extends IFloodlightService>> mServs = m.getModuleServices();
192 if (mServs == null) continue;
193 for (Class<? extends IFloodlightService> mServClass : mServs) {
194 if (mServClass.getCanonicalName().equals(ifs.getCanonicalName())) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700195 assert(m instanceof IFloodlightService);
196 return (IFloodlightService)m;
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 }
198 }
199 }
200 return null;
201 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800202}