Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.module; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.HashSet; |
| 6 | import java.util.Map; |
| 7 | import java.util.Set; |
| 8 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 9 | import org.restlet.resource.Get; |
| 10 | import org.restlet.resource.ServerResource; |
| 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
| 14 | /** |
| 15 | * Returns list of modules loaded by Floodlight. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 16 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | * @author Rob Sherwood |
| 18 | */ |
| 19 | public class ModuleLoaderResource extends ServerResource { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 20 | protected final static Logger log = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | LoggerFactory.getLogger(ModuleLoaderResource.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 22 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Retrieves information about loaded modules. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 25 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | * @return Information about loaded modules. |
| 27 | */ |
| 28 | @Get("json") |
| 29 | public Map<String, Object> retrieve() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 30 | return retrieveInternal(false); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 31 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 32 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Retrieves all modules and their dependencies available |
| 35 | * to Floodlight. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 36 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | * @param loadedOnly Whether to return all modules available or only the ones loaded. |
| 38 | * @return Information about modules available or loaded. |
| 39 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 40 | public Map<String, Object> retrieveInternal(boolean loadedOnly) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | Map<String, Object> model = new HashMap<String, Object>(); |
| 42 | |
| 43 | Set<String> loadedModules = new HashSet<String>(); |
| 44 | for (Object val : getContext().getAttributes().values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 45 | if ((val instanceof IFloodlightModule) || (val instanceof IFloodlightService)) { |
| 46 | String serviceImpl = val.getClass().getCanonicalName(); |
| 47 | loadedModules.add(serviceImpl); |
| 48 | // log.debug("Tracking serviceImpl " + serviceImpl); |
| 49 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 52 | for (String moduleName : |
| 53 | FloodlightModuleLoader.moduleNameMap.keySet()) { |
| 54 | Map<String, Object> moduleInfo = new HashMap<String, Object>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 56 | IFloodlightModule module = |
| 57 | FloodlightModuleLoader.moduleNameMap.get( |
| 58 | moduleName); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 59 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 60 | Collection<Class<? extends IFloodlightService>> deps = |
| 61 | module.getModuleDependencies(); |
| 62 | if (deps == null) |
| 63 | deps = new HashSet<Class<? extends IFloodlightService>>(); |
| 64 | Map<String, Object> depsMap = new HashMap<String, Object>(); |
| 65 | for (Class<? extends IFloodlightService> service : deps) { |
| 66 | Object serviceImpl = getContext().getAttributes().get(service.getCanonicalName()); |
| 67 | if (serviceImpl != null) |
| 68 | depsMap.put(service.getCanonicalName(), serviceImpl.getClass().getCanonicalName()); |
| 69 | else |
| 70 | depsMap.put(service.getCanonicalName(), "<unresolved>"); |
| 71 | |
| 72 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 73 | moduleInfo.put("depends", depsMap); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 75 | Collection<Class<? extends IFloodlightService>> provides = |
| 76 | module.getModuleServices(); |
| 77 | if (provides == null) |
| 78 | provides = new HashSet<Class<? extends IFloodlightService>>(); |
| 79 | Map<String, Object> providesMap = new HashMap<String, Object>(); |
| 80 | for (Class<? extends IFloodlightService> service : provides) { |
| 81 | providesMap.put(service.getCanonicalName(), module.getServiceImpls().get(service).getClass().getCanonicalName()); |
| 82 | } |
| 83 | moduleInfo.put("provides", providesMap); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 85 | moduleInfo.put("loaded", false); // not loaded, by default |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 86 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 87 | // check if this module is loaded directly |
| 88 | if (loadedModules.contains(module.getClass().getCanonicalName())) { |
| 89 | moduleInfo.put("loaded", true); |
| 90 | } else { |
| 91 | // if not, then maybe one of the services it exports is loaded |
| 92 | for (Class<? extends IFloodlightService> service : provides) { |
| 93 | String modString = module.getServiceImpls().get(service).getClass().getCanonicalName(); |
| 94 | if (loadedModules.contains(modString)) |
| 95 | moduleInfo.put("loaded", true); |
| 96 | /* else |
| 97 | log.debug("ServiceImpl not loaded " + modString); */ |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if ((Boolean) moduleInfo.get("loaded") || !loadedOnly) |
| 102 | model.put(moduleName, moduleInfo); |
| 103 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | return model; |
| 105 | } |
| 106 | } |