blob: 69e7b3e5c2f045b77d7bdaf1db64fce5551e75e3 [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
Jonathan Hart7bf62172013-02-28 13:17:18 -080072 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -080073 return controllerId;
74 }
75
76 @Override
77 public void registerController(String controllerId)
78 throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -080079 if (this.controllerId != null) {
80 throw new RegistryException(
81 "Controller already registered with id " + this.controllerId);
82 }
Jonathan Hartbd766972013-02-22 15:13:03 -080083 this.controllerId = controllerId;
84 }
85
86 @Override
87 public Collection<String> getAllControllers() throws RegistryException {
88 List<String> l = new ArrayList<String>();
89 l.add(controllerId);
90 return l;
91 }
92
93 @Override
94 public String getControllerForSwitch(long dpid) throws RegistryException {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070095 return (switchCallbacks.get(HexString.toHexString(dpid)) != null)? controllerId: null;
Jonathan Hartbd766972013-02-22 15:13:03 -080096 }
97
98 @Override
99 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
100 Map<String, List<ControllerRegistryEntry>> switches =
101 new HashMap<String, List<ControllerRegistryEntry>>();
102
103 for (String strSwitch : switchCallbacks.keySet()){
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800104 log.debug("Switch _{}", strSwitch);
Jonathan Hartbd766972013-02-22 15:13:03 -0800105 List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>();
106 list.add(new ControllerRegistryEntry(controllerId, 0));
107
108 switches.put(strSwitch, list);
109 }
110
111 return switches;
112 }
113
114 @Override
115 public Collection<Long> getSwitchesControlledByController(
116 String controllerId) {
117 throw new RuntimeException("Not yet implemented");
118 }
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700119
Naoki Shiotad00accf2013-06-25 14:40:37 -0700120 private long blockTop = 0L;
121 private static final long BLOCK_SIZE = 0x1000000L;
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700122
123 /**
124 * Returns a block of IDs which are unique and unused.
125 * Range of IDs is fixed size and is assigned incrementally as this method called.
126 */
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700127 @Override
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700128 public synchronized IdBlock allocateUniqueIdBlock(){
Naoki Shiotad00accf2013-06-25 14:40:37 -0700129 long blockHead = blockTop;
130 long blockTail = blockTop + BLOCK_SIZE;
131
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700132 IdBlock block = new IdBlock(blockHead, blockTail - 1, BLOCK_SIZE);
Naoki Shiotad00accf2013-06-25 14:40:37 -0700133 blockTop = blockTail;
134
135 return block;
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700136 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800137
138 @Override
139 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
140 Collection<Class<? extends IFloodlightService>> l =
141 new ArrayList<Class<? extends IFloodlightService>>();
142 l.add(IControllerRegistryService.class);
143 return l;
144 }
145
146 @Override
147 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
148 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
149 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
150 m.put(IControllerRegistryService.class, this);
151 return m;
152 }
153
154 @Override
155 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
156 Collection<Class<? extends IFloodlightService>> l =
157 new ArrayList<Class<? extends IFloodlightService>>();
158 l.add(IRestApiService.class);
159 return l;
160 }
161
162 @Override
163 public void init(FloodlightModuleContext context)
164 throws FloodlightModuleException {
165 restApi = context.getServiceImpl(IRestApiService.class);
166
167 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hartbd766972013-02-22 15:13:03 -0800168 }
169
170 @Override
171 public void startUp(FloodlightModuleContext context) {
172 restApi.addRestletRoutable(new RegistryWebRoutable());
173 }
174
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800175 @Override
176 public IdBlock allocateUniqueIdBlock(long range) {
177 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
178 }
179
Jonathan Hartbd766972013-02-22 15:13:03 -0800180}