blob: 28966a909524158322611ecaf193972990e717e4 [file] [log] [blame]
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -07001package net.onrc.onos.ofcontroller.floodlightlistener;
Pankaj Berdeda809572013-02-22 15:31:20 -08002
3import java.util.ArrayList;
4import java.util.Collection;
Pavlin Radoslavov28069402013-10-18 18:43:11 -07005import java.util.HashMap;
Pankaj Berdeda809572013-02-22 15:31:20 -08006import java.util.Map;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -07007import java.util.concurrent.ScheduledExecutorService;
8import java.util.concurrent.TimeUnit;
Pankaj Berdeda809572013-02-22 15:31:20 -08009
Pankaj Berde465ac7c2013-05-23 13:47:49 -070010import org.openflow.protocol.OFPhysicalPort;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070011import org.openflow.util.HexString;
Pankaj Berdeda809572013-02-22 15:31:20 -080012import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
15import net.floodlightcontroller.core.IFloodlightProviderService;
16import net.floodlightcontroller.core.IOFSwitch;
17import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080018import net.floodlightcontroller.core.module.FloodlightModuleContext;
19import net.floodlightcontroller.core.module.FloodlightModuleException;
20import net.floodlightcontroller.core.module.IFloodlightModule;
21import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070022import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080023import net.floodlightcontroller.devicemanager.IDevice;
24import net.floodlightcontroller.devicemanager.IDeviceListener;
25import net.floodlightcontroller.devicemanager.IDeviceService;
Pankaj Berde00e90882013-06-10 21:25:05 -070026import net.floodlightcontroller.routing.Link;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070027import net.floodlightcontroller.threadpool.IThreadPoolService;
Pankaj Berde38646d62013-06-21 11:34:04 -070028import net.onrc.onos.graph.GraphDBConnection;
29import net.onrc.onos.graph.GraphDBOperation;
30import net.onrc.onos.graph.IDBConnection;
31import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070032import net.onrc.onos.ofcontroller.core.IDeviceStorage;
33import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta36cf0762013-06-14 14:25:38 -070034import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070035import net.onrc.onos.ofcontroller.core.ISwitchStorage;
36import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070037import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
38import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070039import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
40import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070041import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070042import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
43import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070044import net.onrc.onos.registry.controller.IControllerRegistryService;
45import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
46import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080047
Pavlin Radoslavov28069402013-10-18 18:43:11 -070048public class NetworkGraphPublisher implements IDeviceListener,
49 IOFSwitchListener,
50 IOFSwitchPortListener,
51 ILinkDiscoveryListener,
52 IFloodlightModule,
53 INetworkGraphService {
Pankaj Berdeda809572013-02-22 15:31:20 -080054
55 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070056 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070057 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070058 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080059 protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070060 protected IControllerRegistryService registryService;
Toshio Koide70ba38b2013-06-13 14:05:05 -070061 protected GraphDBOperation op;
Pankaj Berdeda809572013-02-22 15:31:20 -080062
63 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070064 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070065 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070066 protected IFloodlightProviderService floodlightProvider;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070067
Pankaj Berde62016142013-04-09 15:35:50 -070068 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070069 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070070 protected ILinkDiscoveryService linkDiscovery;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070071
72 /**
73 * Cleanup and synch switch state from registry
74 */
75 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
76 @Override
77 public void run() {
78 try {
79 log.debug("Running cleanup thread");
80 switchCleanup();
81 }
82 catch (Exception e) {
83 log.error("Error in cleanup thread", e);
84 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070085 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070086 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070087 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070088 }
89 }
90
91 @Override
92 public void controlChanged(long dpid, boolean hasControl) {
93 // TODO Auto-generated method stub
94
95 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070096 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070097 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
98 registryService.releaseControl(dpid);
99 }
100 }
101 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700102
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700103 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700104 op.close();
105 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700106
107 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700108 // For each switch check if a controller exists in controller registry
109 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700110 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700111 try {
112 long dpid = HexString.toLong(sw.getDPID());
113 String controller = registryService.getControllerForSwitch(dpid);
114 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700115 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700116 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700117 //} else {
118 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700119 }
120 } catch (NumberFormatException e) {
121 // TODO Auto-generated catch block
122 e.printStackTrace();
123 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700124 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700125 e.printStackTrace();
126 }
127 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700128 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700129 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800130
131 @Override
132 public void linkDiscoveryUpdate(LDUpdate update) {
133 // TODO Auto-generated method stub
Pankaj Berde00e90882013-06-10 21:25:05 -0700134 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200135 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700136
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200137 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700138 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200139 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Pankaj Berde00e90882013-06-10 21:25:05 -0700140 linkStore.update(lt, DM_OPERATION.DELETE);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700141 // TODO: Move network map link removal here
142 // reconcile paths here
143// IPortObject srcPort = conn.utils().searchPort(conn, HexString.toHexString(update.getSrc()), update.getSrcPort());
144 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700145 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200146 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Pankaj Berde00e90882013-06-10 21:25:05 -0700147 linkStore.update(lt, DM_OPERATION.UPDATE);
148 break;
149 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200150 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Pankaj Berde00e90882013-06-10 21:25:05 -0700151 linkStore.update(lt, DM_OPERATION.INSERT);
152 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700153
154 default:
155 break;
156 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800157
158 }
159
160 @Override
161 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700162
163 if (registryService.hasControl(sw.getId())) {
Pankaj Berdebbd38612013-06-22 05:59:12 -0700164 swStore.addSwitch(sw);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700165 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800166
167 }
168
169 @Override
170 public void removedSwitch(IOFSwitch sw) {
171 // TODO Auto-generated method stub
172
173 }
174
175 @Override
176 public void switchPortChanged(Long switchId) {
177 // TODO Auto-generated method stub
178
179 }
180
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700181
182 @Override
183 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
184 // TODO Auto-generated method stub
185 swStore.addPort(HexString.toHexString(switchId), port);
186 }
187
188 @Override
189 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
190 // TODO Auto-generated method stub
191 swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber());
192 }
193
Pankaj Berdeda809572013-02-22 15:31:20 -0800194 @Override
195 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700196 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800197 }
198
199 @Override
200 public void deviceAdded(IDevice device) {
201 // TODO Auto-generated method stub
202 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
203 devStore.addDevice(device);
204 }
205
206 @Override
207 public void deviceRemoved(IDevice device) {
208 // TODO Auto-generated method stub
209
210 }
211
212 @Override
213 public void deviceMoved(IDevice device) {
214 // TODO Auto-generated method stub
215 devStore.changeDeviceAttachments(device);
216
217 }
218
219 @Override
220 public void deviceIPV4AddrChanged(IDevice device) {
221 // TODO Auto-generated method stub
222 devStore.changeDeviceIPv4Address(device);
223
224 }
225
226 @Override
227 public void deviceVlanChanged(IDevice device) {
228 // TODO Auto-generated method stub
229 }
230
231
232 @Override
233 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700234 Collection<Class<? extends IFloodlightService>> l =
235 new ArrayList<Class<? extends IFloodlightService>>();
236 l.add(INetworkGraphService.class);
237 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800238 }
239
240 @Override
241 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700242 Map<Class<? extends IFloodlightService>,
243 IFloodlightService> m =
244 new HashMap<Class<? extends IFloodlightService>,
245 IFloodlightService>();
246 m.put(INetworkGraphService.class, this);
247 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800248 }
249
250 @Override
251 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
252 Collection<Class<? extends IFloodlightService>> l =
253 new ArrayList<Class<? extends IFloodlightService>>();
254 l.add(IFloodlightProviderService.class);
255 l.add(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700256 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800257 return l;
258 }
259
260 @Override
261 public void init(FloodlightModuleContext context)
262 throws FloodlightModuleException {
263 // TODO Auto-generated method stub
264 Map<String, String> configMap = context.getConfigParams(this);
265 String conf = configMap.get(DBConfigFile);
Toshio Koidebfe9b922013-06-18 10:56:05 -0700266 op = new GraphDBOperation(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800267
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700268 floodlightProvider =
269 context.getServiceImpl(IFloodlightProviderService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800270 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700271 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700272 threadPool = context.getServiceImpl(IThreadPoolService.class);
273 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800274
Pankaj Berdeda809572013-02-22 15:31:20 -0800275 devStore = new DeviceStorageImpl();
276 devStore.init(conf);
277
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700278 swStore = new SwitchStorageImpl();
279 swStore.init(conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700280
281 linkStore = new LinkStorageImpl();
282 linkStore.init(conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700283
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700284 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800285
286 }
287
288 @Override
289 public void startUp(FloodlightModuleContext context) {
290 // TODO Auto-generated method stub
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700291 Map<String, String> configMap = context.getConfigParams(this);
292 String cleanupNeeded = configMap.get(CleanupEnabled);
293
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700294 deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700295 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700296 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700297
298 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700299 IDBConnection conn = op.getDBConnection();
300 conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700301 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700302 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700303 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
304 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700305 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700306 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800307 }
308
309}