blob: 1e2934b8b06c8590c4c27b51a224fd796447958e [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
Pavlin Radoslavovc35229e2014-02-06 16:19:37 -08009import net.floodlightcontroller.core.IFloodlightProviderService;
Jonathan Hartbd766972013-02-22 15:13:03 -080010import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.core.module.FloodlightModuleException;
12import net.floodlightcontroller.core.module.IFloodlightModule;
13import net.floodlightcontroller.core.module.IFloodlightService;
14import net.floodlightcontroller.restserver.IRestApiService;
Naoki Shiotab32edf52013-12-12 14:09:36 -080015import net.onrc.onos.registry.controller.web.RegistryWebRoutable;
Jonathan Hartbd766972013-02-22 15:13:03 -080016
17import org.openflow.util.HexString;
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
Jonathan Hart7bf62172013-02-28 13:17:18 -080021/**
22 * Implementation of a registry that doesn't rely on any external registry
23 * service. This is designed to be used only in single-node setups (e.g. for
24 * development). All registry data is stored in local memory.
25 * @author jono
26 *
27 */
Jonathan Hartbd766972013-02-22 15:13:03 -080028public class StandaloneRegistry implements IFloodlightModule,
29 IControllerRegistryService {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070030 protected final static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class);
Jonathan Hartbd766972013-02-22 15:13:03 -080031
32 protected IRestApiService restApi;
33
Jonathan Hartbd766972013-02-22 15:13:03 -080034 protected String controllerId = null;
35 protected Map<String, ControlChangeCallback> switchCallbacks;
36
37
38 @Override
39 public void requestControl(long dpid, ControlChangeCallback cb)
40 throws RegistryException {
41 if (controllerId == null) {
Jonathan Hartd10008d2013-02-23 17:04:08 -080042 throw new RuntimeException(
43 "Must register a controller before calling requestControl");
Jonathan Hartbd766972013-02-22 15:13:03 -080044 }
45
46 switchCallbacks.put(HexString.toHexString(dpid), cb);
47
48 log.debug("Control granted for {}", HexString.toHexString(dpid));
49
50 //Immediately grant request for control
51 if (cb != null) {
52 cb.controlChanged(dpid, true);
53 }
54 }
55
56 @Override
57 public void releaseControl(long dpid) {
58 ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid));
59
60 log.debug("Control released for {}", HexString.toHexString(dpid));
61
62 if (cb != null){
63 cb.controlChanged(dpid, false);
64 }
65 }
66
67 @Override
68 public boolean hasControl(long dpid) {
69 return switchCallbacks.containsKey(HexString.toHexString(dpid));
70 }
71
72 @Override
Pavlin Radoslavovf1377ce2014-02-05 17:37:24 -080073 public boolean isClusterLeader() {
74 return true;
75 }
76
77 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -080078 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -080079 return controllerId;
80 }
81
82 @Override
83 public void registerController(String controllerId)
84 throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -080085 if (this.controllerId != null) {
86 throw new RegistryException(
87 "Controller already registered with id " + this.controllerId);
88 }
Jonathan Hartbd766972013-02-22 15:13:03 -080089 this.controllerId = controllerId;
90 }
91
92 @Override
93 public Collection<String> getAllControllers() throws RegistryException {
94 List<String> l = new ArrayList<String>();
95 l.add(controllerId);
96 return l;
97 }
98
99 @Override
100 public String getControllerForSwitch(long dpid) throws RegistryException {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700101 return (switchCallbacks.get(HexString.toHexString(dpid)) != null)? controllerId: null;
Jonathan Hartbd766972013-02-22 15:13:03 -0800102 }
103
104 @Override
105 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
106 Map<String, List<ControllerRegistryEntry>> switches =
107 new HashMap<String, List<ControllerRegistryEntry>>();
108
109 for (String strSwitch : switchCallbacks.keySet()){
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800110 log.debug("Switch _{}", strSwitch);
Jonathan Hartbd766972013-02-22 15:13:03 -0800111 List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>();
112 list.add(new ControllerRegistryEntry(controllerId, 0));
113
114 switches.put(strSwitch, list);
115 }
116
117 return switches;
118 }
119
120 @Override
121 public Collection<Long> getSwitchesControlledByController(
122 String controllerId) {
123 throw new RuntimeException("Not yet implemented");
124 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700125
Naoki Shiotad00accf2013-06-25 14:40:37 -0700126 private long blockTop = 0L;
127 private static final long BLOCK_SIZE = 0x1000000L;
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700128
129 /**
130 * Returns a block of IDs which are unique and unused.
131 * Range of IDs is fixed size and is assigned incrementally as this method called.
132 */
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700133 @Override
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700134 public synchronized IdBlock allocateUniqueIdBlock(){
Naoki Shiotad00accf2013-06-25 14:40:37 -0700135 long blockHead = blockTop;
136 long blockTail = blockTop + BLOCK_SIZE;
137
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700138 IdBlock block = new IdBlock(blockHead, blockTail - 1, BLOCK_SIZE);
Naoki Shiotad00accf2013-06-25 14:40:37 -0700139 blockTop = blockTail;
140
141 return block;
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700142 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800143
144 @Override
145 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
146 Collection<Class<? extends IFloodlightService>> l =
147 new ArrayList<Class<? extends IFloodlightService>>();
148 l.add(IControllerRegistryService.class);
149 return l;
150 }
151
152 @Override
153 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
154 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
155 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
156 m.put(IControllerRegistryService.class, this);
157 return m;
158 }
159
160 @Override
161 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
162 Collection<Class<? extends IFloodlightService>> l =
163 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovc35229e2014-02-06 16:19:37 -0800164 l.add(IFloodlightProviderService.class);
165 l.add(IRestApiService.class);
Jonathan Hartbd766972013-02-22 15:13:03 -0800166 return l;
167 }
168
169 @Override
170 public void init(FloodlightModuleContext context)
171 throws FloodlightModuleException {
172 restApi = context.getServiceImpl(IRestApiService.class);
173
174 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hartbd766972013-02-22 15:13:03 -0800175 }
176
177 @Override
178 public void startUp(FloodlightModuleContext context) {
179 restApi.addRestletRoutable(new RegistryWebRoutable());
180 }
181
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800182 @Override
183 public IdBlock allocateUniqueIdBlock(long range) {
184 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
185 }
186
Jonathan Hartbd766972013-02-22 15:13:03 -0800187}