blob: 8a468bc32fd86e4f2a0162959ff71479ea3f794d [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) {
Jonathan Hartd10008d2013-02-23 17:04:08 -080035 throw new RuntimeException(
36 "Must register a controller before calling requestControl");
Jonathan Hartbd766972013-02-22 15:13:03 -080037 }
38
39 switchCallbacks.put(HexString.toHexString(dpid), cb);
40
41 log.debug("Control granted for {}", HexString.toHexString(dpid));
42
43 //Immediately grant request for control
44 if (cb != null) {
45 cb.controlChanged(dpid, true);
46 }
47 }
48
49 @Override
50 public void releaseControl(long dpid) {
51 ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid));
52
53 log.debug("Control released for {}", HexString.toHexString(dpid));
54
55 if (cb != null){
56 cb.controlChanged(dpid, false);
57 }
58 }
59
60 @Override
61 public boolean hasControl(long dpid) {
62 return switchCallbacks.containsKey(HexString.toHexString(dpid));
63 }
64
65 @Override
66 public void setMastershipId(String id) {
67 // TODO Auto-generated method stub
68
69 }
70
71 @Override
72 public String getMastershipId() {
73 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 {
95 return controllerId;
96 }
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()){
104 log.debug("Swtich _{}", strSwitch);
105 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 }
119
120 @Override
121 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
122 Collection<Class<? extends IFloodlightService>> l =
123 new ArrayList<Class<? extends IFloodlightService>>();
124 l.add(IControllerRegistryService.class);
125 return l;
126 }
127
128 @Override
129 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
130 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
131 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
132 m.put(IControllerRegistryService.class, this);
133 return m;
134 }
135
136 @Override
137 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
138 Collection<Class<? extends IFloodlightService>> l =
139 new ArrayList<Class<? extends IFloodlightService>>();
140 l.add(IRestApiService.class);
141 return l;
142 }
143
144 @Override
145 public void init(FloodlightModuleContext context)
146 throws FloodlightModuleException {
147 restApi = context.getServiceImpl(IRestApiService.class);
148
149 switchCallbacks = new HashMap<String, ControlChangeCallback>();
150
151 //Put some data in for testing
152 /*
153 try {
154 registerController("hurro");
155 requestControl(2L, null);
156 } catch (RegistryException e1) {
157 // TODO Auto-generated catch block
158 e1.printStackTrace();
159 }*/
160 }
161
162 @Override
163 public void startUp(FloodlightModuleContext context) {
164 restApi.addRestletRoutable(new RegistryWebRoutable());
165 }
166
167}