blob: 0cbae3234a4e2b2a9baf8e78d9161e097737e825 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.core.module;
2
3import java.util.Collection;
4import java.util.HashMap;
5import java.util.Map;
6
7/**
8 * The service registry for an IFloodlightProvider.
9 * @author alexreimers
10 */
11public class FloodlightModuleContext implements IFloodlightModuleContext {
12 protected Map<Class<? extends IFloodlightService>, IFloodlightService> serviceMap;
13 protected Map<Class<? extends IFloodlightModule>, Map<String, String>> configParams;
14 protected Collection<IFloodlightModule> moduleSet;
15
16 /**
17 * Creates the ModuleContext for use with this IFloodlightProvider.
18 * This will be used as a module registry for all IFloodlightModule(s).
19 */
20 public FloodlightModuleContext() {
21 serviceMap =
22 new HashMap<Class<? extends IFloodlightService>,
23 IFloodlightService>();
24 configParams =
25 new HashMap<Class<? extends IFloodlightModule>,
26 Map<String, String>>();
27 }
28
29 /**
30 * Adds a IFloodlightModule for this Context.
31 * @param clazz the service class
32 * @param service The IFloodlightService to add to the registry
33 */
34 public void addService(Class<? extends IFloodlightService> clazz,
35 IFloodlightService service) {
36 serviceMap.put(clazz, service);
37 }
38
39 @SuppressWarnings("unchecked")
40 @Override
41 public <T extends IFloodlightService> T getServiceImpl(Class<T> service) {
42 IFloodlightService s = serviceMap.get(service);
43 return (T)s;
44 }
45
46 @Override
47 public Collection<Class<? extends IFloodlightService>> getAllServices() {
48 return serviceMap.keySet();
49 }
50
51 @Override
52 public Collection<IFloodlightModule> getAllModules() {
53 return moduleSet;
54 }
55
56 public void setModuleSet(Collection<IFloodlightModule> modSet) {
57 this.moduleSet = modSet;
58 }
59
60 /**
61 * Gets the configuration parameter map for a module
62 * @param module The module to get the configuration map for, usually yourself
63 * @return A map containing all the configuration parameters for the module, may be empty
64 */
65 @Override
66 public Map<String, String> getConfigParams(IFloodlightModule module) {
67 Map<String, String> retMap = configParams.get(module.getClass());
68 if (retMap == null) {
69 // Return an empty map if none exists so the module does not
70 // need to null check the map
71 retMap = new HashMap<String, String>();
72 configParams.put(module.getClass(), retMap);
73 }
74
75 // also add any configuration parameters for superclasses, but
76 // only if more specific configuration does not override it
77 for (Class<? extends IFloodlightModule> c : configParams.keySet()) {
78 if (c.isInstance(module)) {
79 for (Map.Entry<String, String> ent : configParams.get(c).entrySet()) {
80 if (!retMap.containsKey(ent.getKey())) {
81 retMap.put(ent.getKey(), ent.getValue());
82 }
83 }
84 }
85 }
86
87 return retMap;
88 }
89
90 /**
91 * Adds a configuration parameter for a module
92 * @param mod The fully qualified module name to add the parameter to
93 * @param key The configuration parameter key
94 * @param value The configuration parameter value
95 */
96 public void addConfigParam(IFloodlightModule mod, String key, String value) {
97 Map<String, String> moduleParams = configParams.get(mod.getClass());
98 if (moduleParams == null) {
99 moduleParams = new HashMap<String, String>();
100 configParams.put(mod.getClass(), moduleParams);
101 }
102 moduleParams.put(key, value);
103 }
104 }