blob: e3181feaa9a73bac5661464a732050120de0de23 [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
19public class StandaloneRegistry implements IFloodlightModule,
20 IControllerRegistryService {
21 protected static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class);
22
23 protected IRestApiService restApi;
24
25 //protected String controllerId;
26
27 protected String controllerId = null;
28 protected Map<String, ControlChangeCallback> switchCallbacks;
29
30
31 @Override
32 public void requestControl(long dpid, ControlChangeCallback cb)
33 throws RegistryException {
34 if (controllerId == null) {
35 throw new RuntimeException("Must register a controller before calling requestControl");
36 }
37
38 switchCallbacks.put(HexString.toHexString(dpid), cb);
39
40 log.debug("Control granted for {}", HexString.toHexString(dpid));
41
42 //Immediately grant request for control
43 if (cb != null) {
44 cb.controlChanged(dpid, true);
45 }
46 }
47
48 @Override
49 public void releaseControl(long dpid) {
50 ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid));
51
52 log.debug("Control released for {}", HexString.toHexString(dpid));
53
54 if (cb != null){
55 cb.controlChanged(dpid, false);
56 }
57 }
58
59 @Override
60 public boolean hasControl(long dpid) {
61 return switchCallbacks.containsKey(HexString.toHexString(dpid));
62 }
63
64 @Override
65 public void setMastershipId(String id) {
66 // TODO Auto-generated method stub
67
68 }
69
70 @Override
71 public String getMastershipId() {
72 return controllerId;
73 }
74
75 @Override
76 public void registerController(String controllerId)
77 throws RegistryException {
78 this.controllerId = controllerId;
79 }
80
81 @Override
82 public Collection<String> getAllControllers() throws RegistryException {
83 List<String> l = new ArrayList<String>();
84 l.add(controllerId);
85 return l;
86 }
87
88 @Override
89 public String getControllerForSwitch(long dpid) throws RegistryException {
90 return controllerId;
91 }
92
93 @Override
94 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
95 Map<String, List<ControllerRegistryEntry>> switches =
96 new HashMap<String, List<ControllerRegistryEntry>>();
97
98 for (String strSwitch : switchCallbacks.keySet()){
99 log.debug("Swtich _{}", strSwitch);
100 List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>();
101 list.add(new ControllerRegistryEntry(controllerId, 0));
102
103 switches.put(strSwitch, list);
104 }
105
106 return switches;
107 }
108
109 @Override
110 public Collection<Long> getSwitchesControlledByController(
111 String controllerId) {
112 throw new RuntimeException("Not yet implemented");
113 }
114
115 @Override
116 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
117 Collection<Class<? extends IFloodlightService>> l =
118 new ArrayList<Class<? extends IFloodlightService>>();
119 l.add(IControllerRegistryService.class);
120 return l;
121 }
122
123 @Override
124 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
125 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
126 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
127 m.put(IControllerRegistryService.class, this);
128 return m;
129 }
130
131 @Override
132 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
133 Collection<Class<? extends IFloodlightService>> l =
134 new ArrayList<Class<? extends IFloodlightService>>();
135 l.add(IRestApiService.class);
136 return l;
137 }
138
139 @Override
140 public void init(FloodlightModuleContext context)
141 throws FloodlightModuleException {
142 restApi = context.getServiceImpl(IRestApiService.class);
143
144 switchCallbacks = new HashMap<String, ControlChangeCallback>();
145
146 //Put some data in for testing
147 /*
148 try {
149 registerController("hurro");
150 requestControl(2L, null);
151 } catch (RegistryException e1) {
152 // TODO Auto-generated catch block
153 e1.printStackTrace();
154 }*/
155 }
156
157 @Override
158 public void startUp(FloodlightModuleContext context) {
159 restApi.addRestletRoutable(new RegistryWebRoutable());
160 }
161
162}