blob: 422949a5b18c4aea59c42e6dd11b7aabd1f81697 [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;
Naoki Shiotab32edf52013-12-12 14:09:36 -080014import net.onrc.onos.registry.controller.web.RegistryWebRoutable;
Jonathan Hartbd766972013-02-22 15:13:03 -080015
16import org.openflow.util.HexString;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
Jonathan Hart7bf62172013-02-28 13:17:18 -080020/**
21 * Implementation of a registry that doesn't rely on any external registry
22 * service. This is designed to be used only in single-node setups (e.g. for
23 * development). All registry data is stored in local memory.
24 * @author jono
25 *
26 */
Jonathan Hartbd766972013-02-22 15:13:03 -080027public class StandaloneRegistry implements IFloodlightModule,
28 IControllerRegistryService {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070029 protected final static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class);
Jonathan Hartbd766972013-02-22 15:13:03 -080030
31 protected IRestApiService restApi;
32
Jonathan Hartbd766972013-02-22 15:13:03 -080033 protected String controllerId = null;
34 protected Map<String, ControlChangeCallback> switchCallbacks;
35
36
37 @Override
38 public void requestControl(long dpid, ControlChangeCallback cb)
39 throws RegistryException {
40 if (controllerId == null) {
Jonathan Hartd10008d2013-02-23 17:04:08 -080041 throw new RuntimeException(
42 "Must register a controller before calling requestControl");
Jonathan Hartbd766972013-02-22 15:13:03 -080043 }
44
45 switchCallbacks.put(HexString.toHexString(dpid), cb);
46
47 log.debug("Control granted for {}", HexString.toHexString(dpid));
48
49 //Immediately grant request for control
50 if (cb != null) {
51 cb.controlChanged(dpid, true);
52 }
53 }
54
55 @Override
56 public void releaseControl(long dpid) {
57 ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid));
58
59 log.debug("Control released for {}", HexString.toHexString(dpid));
60
61 if (cb != null){
62 cb.controlChanged(dpid, false);
63 }
64 }
65
66 @Override
67 public boolean hasControl(long dpid) {
68 return switchCallbacks.containsKey(HexString.toHexString(dpid));
69 }
70
71 @Override
Pavlin Radoslavovf1377ce2014-02-05 17:37:24 -080072 public boolean isClusterLeader() {
73 return true;
74 }
75
76 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -080077 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -080078 return controllerId;
79 }
80
81 @Override
82 public void registerController(String controllerId)
83 throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -080084 if (this.controllerId != null) {
85 throw new RegistryException(
86 "Controller already registered with id " + this.controllerId);
87 }
Jonathan Hartbd766972013-02-22 15:13:03 -080088 this.controllerId = controllerId;
89 }
90
91 @Override
92 public Collection<String> getAllControllers() throws RegistryException {
93 List<String> l = new ArrayList<String>();
94 l.add(controllerId);
95 return l;
96 }
97
98 @Override
99 public String getControllerForSwitch(long dpid) throws RegistryException {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700100 return (switchCallbacks.get(HexString.toHexString(dpid)) != null)? controllerId: null;
Jonathan Hartbd766972013-02-22 15:13:03 -0800101 }
102
103 @Override
104 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
105 Map<String, List<ControllerRegistryEntry>> switches =
106 new HashMap<String, List<ControllerRegistryEntry>>();
107
108 for (String strSwitch : switchCallbacks.keySet()){
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800109 log.debug("Switch _{}", strSwitch);
Jonathan Hartbd766972013-02-22 15:13:03 -0800110 List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>();
111 list.add(new ControllerRegistryEntry(controllerId, 0));
112
113 switches.put(strSwitch, list);
114 }
115
116 return switches;
117 }
118
119 @Override
120 public Collection<Long> getSwitchesControlledByController(
121 String controllerId) {
122 throw new RuntimeException("Not yet implemented");
123 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700124
Naoki Shiotad00accf2013-06-25 14:40:37 -0700125 private long blockTop = 0L;
126 private static final long BLOCK_SIZE = 0x1000000L;
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700127
128 /**
129 * Returns a block of IDs which are unique and unused.
130 * Range of IDs is fixed size and is assigned incrementally as this method called.
131 */
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700132 @Override
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700133 public synchronized IdBlock allocateUniqueIdBlock(){
Naoki Shiotad00accf2013-06-25 14:40:37 -0700134 long blockHead = blockTop;
135 long blockTail = blockTop + BLOCK_SIZE;
136
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700137 IdBlock block = new IdBlock(blockHead, blockTail - 1, BLOCK_SIZE);
Naoki Shiotad00accf2013-06-25 14:40:37 -0700138 blockTop = blockTail;
139
140 return block;
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700141 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800142
143 @Override
144 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
145 Collection<Class<? extends IFloodlightService>> l =
146 new ArrayList<Class<? extends IFloodlightService>>();
147 l.add(IControllerRegistryService.class);
148 return l;
149 }
150
151 @Override
152 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
153 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
154 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
155 m.put(IControllerRegistryService.class, this);
156 return m;
157 }
158
159 @Override
160 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
161 Collection<Class<? extends IFloodlightService>> l =
162 new ArrayList<Class<? extends IFloodlightService>>();
163 l.add(IRestApiService.class);
164 return l;
165 }
166
167 @Override
168 public void init(FloodlightModuleContext context)
169 throws FloodlightModuleException {
170 restApi = context.getServiceImpl(IRestApiService.class);
171
172 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hartbd766972013-02-22 15:13:03 -0800173 }
174
175 @Override
176 public void startUp(FloodlightModuleContext context) {
177 restApi.addRestletRoutable(new RegistryWebRoutable());
178 }
179
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800180 @Override
181 public IdBlock allocateUniqueIdBlock(long range) {
182 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
183 }
184
Jonathan Hartbd766972013-02-22 15:13:03 -0800185}