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