blob: 734508482a28c4a5a71e27f167be6eafa2a63887 [file] [log] [blame]
Jonathan Hartbd766972013-02-22 15:13:03 -08001package net.onrc.onos.registry.controller;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import net.floodlightcontroller.core.module.FloodlightModuleContext;
10import net.floodlightcontroller.core.module.FloodlightModuleException;
11import net.floodlightcontroller.core.module.IFloodlightModule;
12import net.floodlightcontroller.core.module.IFloodlightService;
13import net.floodlightcontroller.restserver.IRestApiService;
14
15import org.openflow.util.HexString;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
Jonathan Hart7bf62172013-02-28 13:17:18 -080019/**
20 * Implementation of a registry that doesn't rely on any external registry
21 * service. This is designed to be used only in single-node setups (e.g. for
22 * development). All registry data is stored in local memory.
23 * @author jono
24 *
25 */
Jonathan Hartbd766972013-02-22 15:13:03 -080026public class StandaloneRegistry implements IFloodlightModule,
27 IControllerRegistryService {
28 protected static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class);
29
30 protected IRestApiService restApi;
31
Jonathan Hartbd766972013-02-22 15:13:03 -080032 protected String controllerId = null;
33 protected Map<String, ControlChangeCallback> switchCallbacks;
34
35
36 @Override
37 public void requestControl(long dpid, ControlChangeCallback cb)
38 throws RegistryException {
39 if (controllerId == null) {
Jonathan Hartd10008d2013-02-23 17:04:08 -080040 throw new RuntimeException(
41 "Must register a controller before calling requestControl");
Jonathan Hartbd766972013-02-22 15:13:03 -080042 }
43
44 switchCallbacks.put(HexString.toHexString(dpid), cb);
45
46 log.debug("Control granted for {}", HexString.toHexString(dpid));
47
48 //Immediately grant request for control
49 if (cb != null) {
50 cb.controlChanged(dpid, true);
51 }
52 }
53
54 @Override
55 public void releaseControl(long dpid) {
56 ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid));
57
58 log.debug("Control released for {}", HexString.toHexString(dpid));
59
60 if (cb != null){
61 cb.controlChanged(dpid, false);
62 }
63 }
64
65 @Override
66 public boolean hasControl(long dpid) {
67 return switchCallbacks.containsKey(HexString.toHexString(dpid));
68 }
69
70 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -080071 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -080072 return controllerId;
73 }
74
75 @Override
76 public void registerController(String controllerId)
77 throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -080078 if (this.controllerId != null) {
79 throw new RegistryException(
80 "Controller already registered with id " + this.controllerId);
81 }
Jonathan Hartbd766972013-02-22 15:13:03 -080082 this.controllerId = controllerId;
83 }
84
85 @Override
86 public Collection<String> getAllControllers() throws RegistryException {
87 List<String> l = new ArrayList<String>();
88 l.add(controllerId);
89 return l;
90 }
91
92 @Override
93 public String getControllerForSwitch(long dpid) throws RegistryException {
94 return controllerId;
95 }
96
97 @Override
98 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
99 Map<String, List<ControllerRegistryEntry>> switches =
100 new HashMap<String, List<ControllerRegistryEntry>>();
101
102 for (String strSwitch : switchCallbacks.keySet()){
103 log.debug("Swtich _{}", strSwitch);
104 List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>();
105 list.add(new ControllerRegistryEntry(controllerId, 0));
106
107 switches.put(strSwitch, list);
108 }
109
110 return switches;
111 }
112
113 @Override
114 public Collection<Long> getSwitchesControlledByController(
115 String controllerId) {
116 throw new RuntimeException("Not yet implemented");
117 }
118
119 @Override
120 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
121 Collection<Class<? extends IFloodlightService>> l =
122 new ArrayList<Class<? extends IFloodlightService>>();
123 l.add(IControllerRegistryService.class);
124 return l;
125 }
126
127 @Override
128 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
129 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
130 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
131 m.put(IControllerRegistryService.class, this);
132 return m;
133 }
134
135 @Override
136 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
137 Collection<Class<? extends IFloodlightService>> l =
138 new ArrayList<Class<? extends IFloodlightService>>();
139 l.add(IRestApiService.class);
140 return l;
141 }
142
143 @Override
144 public void init(FloodlightModuleContext context)
145 throws FloodlightModuleException {
146 restApi = context.getServiceImpl(IRestApiService.class);
147
148 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hartbd766972013-02-22 15:13:03 -0800149 }
150
151 @Override
152 public void startUp(FloodlightModuleContext context) {
153 restApi.addRestletRoutable(new RegistryWebRoutable());
154 }
155
156}