blob: b6d980a2e327b699689aec40fd4227e0f15691f6 [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 {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070028 protected final static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class);
Jonathan Hartbd766972013-02-22 15:13:03 -080029
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 {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070094 return (switchCallbacks.get(HexString.toHexString(dpid)) != null)? controllerId: null;
Jonathan Hartbd766972013-02-22 15:13:03 -080095 }
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 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700118
Naoki Shiotad00accf2013-06-25 14:40:37 -0700119 private long blockTop = 0L;
120 private static final long BLOCK_SIZE = 0x1000000L;
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700121
122 /**
123 * Returns a block of IDs which are unique and unused.
124 * Range of IDs is fixed size and is assigned incrementally as this method called.
125 */
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700126 @Override
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700127 public synchronized IdBlock allocateUniqueIdBlock(){
Naoki Shiotad00accf2013-06-25 14:40:37 -0700128 long blockHead = blockTop;
129 long blockTail = blockTop + BLOCK_SIZE;
130
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700131 IdBlock block = new IdBlock(blockHead, blockTail - 1, BLOCK_SIZE);
Naoki Shiotad00accf2013-06-25 14:40:37 -0700132 blockTop = blockTail;
133
134 return block;
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700135 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800136
137 @Override
138 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
139 Collection<Class<? extends IFloodlightService>> l =
140 new ArrayList<Class<? extends IFloodlightService>>();
141 l.add(IControllerRegistryService.class);
142 return l;
143 }
144
145 @Override
146 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
147 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
148 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
149 m.put(IControllerRegistryService.class, this);
150 return m;
151 }
152
153 @Override
154 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
155 Collection<Class<? extends IFloodlightService>> l =
156 new ArrayList<Class<? extends IFloodlightService>>();
157 l.add(IRestApiService.class);
158 return l;
159 }
160
161 @Override
162 public void init(FloodlightModuleContext context)
163 throws FloodlightModuleException {
164 restApi = context.getServiceImpl(IRestApiService.class);
165
166 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hartbd766972013-02-22 15:13:03 -0800167 }
168
169 @Override
170 public void startUp(FloodlightModuleContext context) {
171 restApi.addRestletRoutable(new RegistryWebRoutable());
172 }
173
174}