blob: 104032b7b5391e4e4825195f870d59f54b3562a9 [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;
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -07006import java.util.List;
Pankaj Berdeda809572013-02-22 15:31:20 -08007import java.util.Map;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -07008import java.util.concurrent.ScheduledExecutorService;
9import java.util.concurrent.TimeUnit;
Pankaj Berdeda809572013-02-22 15:31:20 -080010
Pankaj Berde465ac7c2013-05-23 13:47:49 -070011import org.openflow.protocol.OFPhysicalPort;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070012import org.openflow.util.HexString;
Pankaj Berdeda809572013-02-22 15:31:20 -080013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Jonathan Hartd3003252013-11-15 09:44:46 -080016import com.google.common.net.InetAddresses;
17
Pankaj Berdeda809572013-02-22 15:31:20 -080018import net.floodlightcontroller.core.IFloodlightProviderService;
19import net.floodlightcontroller.core.IOFSwitch;
20import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080021import net.floodlightcontroller.core.module.FloodlightModuleContext;
22import net.floodlightcontroller.core.module.FloodlightModuleException;
23import net.floodlightcontroller.core.module.IFloodlightModule;
24import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070025import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080026import net.floodlightcontroller.devicemanager.IDevice;
27import net.floodlightcontroller.devicemanager.IDeviceListener;
28import net.floodlightcontroller.devicemanager.IDeviceService;
Pankaj Berde00e90882013-06-10 21:25:05 -070029import net.floodlightcontroller.routing.Link;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070030import net.floodlightcontroller.threadpool.IThreadPoolService;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070031import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070032import net.onrc.onos.graph.GraphDBConnection;
33import net.onrc.onos.graph.GraphDBOperation;
34import net.onrc.onos.graph.IDBConnection;
35import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070036import net.onrc.onos.ofcontroller.core.IDeviceStorage;
37import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta36cf0762013-06-14 14:25:38 -070038import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070039import net.onrc.onos.ofcontroller.core.ISwitchStorage;
40import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070041import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
42import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070043import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
44import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070045import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070046import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
47import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070048import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartd3003252013-11-15 09:44:46 -080049import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070050import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070051import net.onrc.onos.registry.controller.IControllerRegistryService;
52import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
53import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080054
Pavlin Radoslavov28069402013-10-18 18:43:11 -070055public class NetworkGraphPublisher implements IDeviceListener,
56 IOFSwitchListener,
57 IOFSwitchPortListener,
58 ILinkDiscoveryListener,
59 IFloodlightModule,
60 INetworkGraphService {
Pankaj Berdeda809572013-02-22 15:31:20 -080061
62 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070063 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070064 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070065 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080066 protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070067 protected IControllerRegistryService registryService;
Toshio Koide70ba38b2013-06-13 14:05:05 -070068 protected GraphDBOperation op;
Pankaj Berdeda809572013-02-22 15:31:20 -080069
70 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070071 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070072 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070073 protected IFloodlightProviderService floodlightProvider;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070074
Pankaj Berde62016142013-04-09 15:35:50 -070075 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070076 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070077 protected ILinkDiscoveryService linkDiscovery;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070078
79 protected IDatagridService datagridService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070080
81 /**
82 * Cleanup and synch switch state from registry
83 */
84 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
85 @Override
86 public void run() {
87 try {
88 log.debug("Running cleanup thread");
89 switchCleanup();
90 }
91 catch (Exception e) {
92 log.error("Error in cleanup thread", e);
93 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070094 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070095 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070096 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070097 }
98 }
99
100 @Override
101 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700102 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700103 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700104 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800105 // Get the affected ports
106 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
107 // Get the affected links
108 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
109 // Get the affected reverse links
110 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
111 links.addAll(reverseLinks);
112
Jonathan Hartadc63892013-11-08 14:03:55 -0800113 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
114 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700115 registryService.releaseControl(dpid);
116
117 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700118 //
119 // NOTE: Here we explicitly send
120 // notification to remove the
121 // switch, because it is inactive
122 //
123 TopologyElement topologyElement =
124 new TopologyElement(dpid);
125 datagridService.notificationSendTopologyElementRemoved(topologyElement);
126
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800127 // Publish: remove the affected ports
128 for (Short port : ports) {
129 TopologyElement topologyElementPort =
130 new TopologyElement(dpid, port);
131 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
132 }
133 // Publish: remove the affected links
134 for (Link link : links) {
135 TopologyElement topologyElementLink =
136 new TopologyElement(link.getSrc(),
137 link.getSrcPort(),
138 link.getDst(),
139 link.getDstPort());
140 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
141 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700142 }
143 } catch (Exception e) {
144 log.error("Error in SwitchCleanup:controlChanged ", e);
145 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700146 }
147 }
148 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700149
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700150 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700151 op.close();
152 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700153
154 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700155 // For each switch check if a controller exists in controller registry
156 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700157 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700158 try {
159 long dpid = HexString.toLong(sw.getDPID());
160 String controller = registryService.getControllerForSwitch(dpid);
161 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700162 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700163 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700164 //} else {
165 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700166 }
167 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700168 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700169 e.printStackTrace();
170 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700171 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700172 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700173 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700174 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700175 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700176 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800177
178 @Override
179 public void linkDiscoveryUpdate(LDUpdate update) {
Pankaj Berde00e90882013-06-10 21:25:05 -0700180 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200181 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700182
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200183 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700184 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200185 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700186
187 if (linkStore.deleteLink(lt)) {
188 // TODO publish DELETE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700189 TopologyElement topologyElement =
190 new TopologyElement(update.getSrc(),
191 update.getSrcPort(),
192 update.getDst(),
193 update.getDstPort());
194 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700195 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700196 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700197 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200198 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700199
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700200 LinkInfo linfo = linkStore.getLinkInfo(lt);
201 // TODO update "linfo" using portState derived using "update"
Naoki Shiota987a5722013-10-23 11:59:36 -0700202 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
203 // TODO publish UPDATE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700204 //
205 // TODO NOTE: Here we assume that updated
206 // link is UP.
207 //
208 TopologyElement topologyElement =
209 new TopologyElement(update.getSrc(),
210 update.getSrcPort(),
211 update.getDst(),
212 update.getDstPort());
213 datagridService.notificationSendTopologyElementUpdated(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700214 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700215 break;
216 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200217 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700218
219 if (linkStore.addLink(lt)) {
220 // TODO publish ADD_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700221 TopologyElement topologyElement =
222 new TopologyElement(update.getSrc(),
223 update.getSrcPort(),
224 update.getDst(),
225 update.getDstPort());
226 datagridService.notificationSendTopologyElementAdded(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700227 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700228 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700229 default:
230 break;
231 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800232
233 }
234
235 @Override
236 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700237 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700238 if (swStore.addSwitch(sw)) {
239 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700240 TopologyElement topologyElement =
241 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800242 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800243
244 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700245 // TODO: Add only ports that are UP?
246 for (OFPhysicalPort port : sw.getPorts()) {
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800247 TopologyElement topologyElementPort =
248 new TopologyElement(sw.getId(),
249 port.getPortNumber());
250 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700251 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800252
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700253 // Add all links that might be connected already
254 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
255 // Add all reverse links as well
256 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
257 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800258
259 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700260 for (Link link : links) {
261 TopologyElement topologyElementLink =
262 new TopologyElement(link.getSrc(),
263 link.getSrcPort(),
264 link.getDst(),
265 link.getDstPort());
266 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
267 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700268 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700269 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800270 }
271
272 @Override
273 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800274 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700275 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800276 // Get the affected ports
277 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
278 // Get the affected links
279 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
280 // Get the affected reverse links
281 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
282 links.addAll(reverseLinks);
283
Naoki Shiota987a5722013-10-23 11:59:36 -0700284 if (swStore.deleteSwitch(sw.getStringId())) {
285 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700286 TopologyElement topologyElement =
287 new TopologyElement(sw.getId());
288 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800289
290 // Publish: remove the affected ports
291 for (Short port : ports) {
292 TopologyElement topologyElementPort =
293 new TopologyElement(sw.getId(), port);
294 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
295 }
296 // Publish: remove the affected links
297 for (Link link : links) {
298 TopologyElement topologyElementLink =
299 new TopologyElement(link.getSrc(),
300 link.getSrcPort(),
301 link.getDst(),
302 link.getDstPort());
303 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
304 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700305 }
306 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800307 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800308 }
309
310 @Override
311 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700312 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800313 }
314
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700315
316 @Override
317 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700318 if (swStore.addPort(HexString.toHexString(switchId), port)) {
319 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700320 TopologyElement topologyElement =
321 new TopologyElement(switchId, port.getPortNumber());
322 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800323
324 // Add all links that might be connected already
325 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
326 // Add all reverse links as well
327 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
328 links.addAll(reverseLinks);
329
330 // Publish: add the links
331 for (Link link : links) {
332 TopologyElement topologyElementLink =
333 new TopologyElement(link.getSrc(),
334 link.getSrcPort(),
335 link.getDst(),
336 link.getDstPort());
337 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
338 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700339 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700340 }
341
342 @Override
343 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800344 // Remove all links that might be connected already
345 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
346 // Remove all reverse links as well
347 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
348 links.addAll(reverseLinks);
349
Naoki Shiota987a5722013-10-23 11:59:36 -0700350 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
351 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700352 TopologyElement topologyElement =
353 new TopologyElement(switchId, port.getPortNumber());
354 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800355
356 // Publish: remove the links
357 for (Link link : links) {
358 TopologyElement topologyElementLink =
359 new TopologyElement(link.getSrc(),
360 link.getSrcPort(),
361 link.getDst(),
362 link.getDstPort());
363 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
364 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700365 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700366 }
367
Pankaj Berdeda809572013-02-22 15:31:20 -0800368 @Override
369 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700370 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800371 }
372
373 @Override
374 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800375 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
376 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800377 for (int intIpv4Address : device.getIPv4Addresses()) {
378 datagridService.sendArpRequest(
379 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
380 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800381 }
382
383 @Override
384 public void deviceRemoved(IDevice device) {
385 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700386 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800387 }
388
389 @Override
390 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800391 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800392 }
393
394 @Override
395 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800396 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800397 }
398
399 @Override
400 public void deviceVlanChanged(IDevice device) {
401 // TODO Auto-generated method stub
402 }
403
404
405 @Override
406 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700407 Collection<Class<? extends IFloodlightService>> l =
408 new ArrayList<Class<? extends IFloodlightService>>();
409 l.add(INetworkGraphService.class);
410 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800411 }
412
413 @Override
414 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700415 Map<Class<? extends IFloodlightService>,
416 IFloodlightService> m =
417 new HashMap<Class<? extends IFloodlightService>,
418 IFloodlightService>();
419 m.put(INetworkGraphService.class, this);
420 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800421 }
422
423 @Override
424 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
425 Collection<Class<? extends IFloodlightService>> l =
426 new ArrayList<Class<? extends IFloodlightService>>();
427 l.add(IFloodlightProviderService.class);
428 l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700429 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700430 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800431 return l;
432 }
433
434 @Override
435 public void init(FloodlightModuleContext context)
436 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800437 Map<String, String> configMap = context.getConfigParams(this);
438 String conf = configMap.get(DBConfigFile);
Toshio Koidebfe9b922013-06-18 10:56:05 -0700439 op = new GraphDBOperation(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800440
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700441 floodlightProvider =
442 context.getServiceImpl(IFloodlightProviderService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800443 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700444 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700445 threadPool = context.getServiceImpl(IThreadPoolService.class);
446 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700447 datagridService = context.getServiceImpl(IDatagridService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800448
Pankaj Berdeda809572013-02-22 15:31:20 -0800449 devStore = new DeviceStorageImpl();
450 devStore.init(conf);
451
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700452 swStore = new SwitchStorageImpl();
453 swStore.init(conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700454
455 linkStore = new LinkStorageImpl();
456 linkStore.init(conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700457
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700458 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800459
460 }
461
462 @Override
463 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700464 Map<String, String> configMap = context.getConfigParams(this);
465 String cleanupNeeded = configMap.get(CleanupEnabled);
466
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700467 deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700468 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700469 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700470
471 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700472 IDBConnection conn = op.getDBConnection();
473 conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700474 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700475 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700476 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
477 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700478 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700479 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700480
481 //
482 // NOTE: No need to register with the Datagrid Service,
483 // because we don't need to receive any notifications from it.
484 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800485 }
486
487}