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