Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 1 | /** |
| 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | package net.floodlightcontroller.core.module; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collection; |
| 21 | import java.util.Iterator; |
| 22 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 26 | import net.floodlightcontroller.core.module.FloodlightModuleLoader; |
| 27 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 28 | import net.floodlightcontroller.core.test.MockFloodlightProvider; |
| 29 | import net.floodlightcontroller.core.test.MockThreadPoolService; |
| 30 | import net.onrc.onos.apps.websocket.WebSocketModule; |
| 31 | import net.onrc.onos.core.datagrid.HazelcastDatagrid; |
| 32 | import net.onrc.onos.core.flowprogrammer.FlowProgrammer; |
| 33 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 34 | import net.onrc.onos.core.intent.runtime.PlanInstallModule; |
| 35 | import net.onrc.onos.core.metrics.OnosMetricsModule; |
| 36 | import net.onrc.onos.core.registry.ZookeeperRegistry; |
| 37 | import net.onrc.onos.core.topology.TopologyPublisher; |
| 38 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 39 | public class FloodlightTestModuleLoader extends FloodlightModuleLoader { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 40 | protected static Logger log = LoggerFactory.getLogger(FloodlightTestModuleLoader.class); |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | // List of default modules to use unless specified otherwise |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 43 | public static final Class<? extends IFloodlightModule> DEFAULT_HZ_DATAGRID = |
| 44 | HazelcastDatagrid.class; |
| 45 | public static final Class<? extends IFloodlightModule> DEFAULT_FLOODLIGHT_PROVIDER = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | MockFloodlightProvider.class; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 47 | public static final Class<? extends IFloodlightModule> DEFAULT_TOPOLOGY_PUBLISHER = |
| 48 | TopologyPublisher.class; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | public static final Class<? extends IFloodlightModule> DEFAULT_THREADPOOL = |
| 50 | MockThreadPoolService.class; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 51 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | |
| 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 68 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | DEFAULT_MODULE_LIST.add(DEFAULT_THREADPOOL); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 74 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | } |
| 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | * @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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 120 | = userModServs.iterator(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | while (userModServsIter.hasNext()) { |
| 122 | Class<? extends IFloodlightService> userModServIntf = userModServsIter.next(); |
| 123 | Iterator<Class<? extends IFloodlightService>> dmModsServsIter |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 124 | = dmModServs.iterator(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | while (dmModsServsIter.hasNext()) { |
| 126 | Class<? extends IFloodlightService> dmModServIntf |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 127 | = dmModsServsIter.next(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | |
| 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 153 | * @param modules The list of modules that the user wants to load. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | * @param mockedServices The list of services that will be mocked. Any |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 155 | * module that provides this service will not be loaded. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | */ |
| 157 | public void setupModules(Collection<Class<? extends IFloodlightModule>> modules, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 158 | Collection<IFloodlightService> mockedServices) throws FloodlightModuleException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 165 | fmc = loadModulesFromList(modulesAsString, null, mockedServices); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Gets the inited/started instance of a module from the context. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | * @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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 185 | * @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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 195 | assert(m instanceof IFloodlightService); |
| 196 | return (IFloodlightService)m; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | return null; |
| 201 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 202 | } |