blob: 5cf31f6beb668317ef4ccb82d8cedc8767378663 [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 Berdeda809572013-02-22 15:31:20 -080011import net.floodlightcontroller.core.IFloodlightProviderService;
12import net.floodlightcontroller.core.IOFSwitch;
13import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080014import net.floodlightcontroller.core.module.FloodlightModuleContext;
15import net.floodlightcontroller.core.module.FloodlightModuleException;
16import net.floodlightcontroller.core.module.IFloodlightModule;
17import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070018import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080019import net.floodlightcontroller.devicemanager.IDevice;
20import net.floodlightcontroller.devicemanager.IDeviceListener;
Pankaj Berde00e90882013-06-10 21:25:05 -070021import net.floodlightcontroller.routing.Link;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070022import net.floodlightcontroller.threadpool.IThreadPoolService;
yoshi2fd4c7e2013-11-22 15:47:55 -080023import net.onrc.onos.graph.DBOperation;
24import net.onrc.onos.graph.DBConnection;
25import net.onrc.onos.graph.GraphDBManager;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070026import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070027import net.onrc.onos.graph.IDBConnection;
28import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070029import net.onrc.onos.ofcontroller.core.IDeviceStorage;
30import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070031import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070032import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080033import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
34import net.onrc.onos.ofcontroller.core.ISwitchStorage;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070035import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
36import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070037import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070038import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
39import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070040import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartd3003252013-11-15 09:44:46 -080041import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070042import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070043import net.onrc.onos.registry.controller.IControllerRegistryService;
44import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
45import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080046
Jonathan Hartd857ad62013-12-14 18:08:17 -080047import org.openflow.protocol.OFPhysicalPort;
48import org.openflow.util.HexString;
49import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import com.google.common.net.InetAddresses;
53
Pavlin Radoslavov28069402013-10-18 18:43:11 -070054public class NetworkGraphPublisher implements IDeviceListener,
55 IOFSwitchListener,
56 IOFSwitchPortListener,
57 ILinkDiscoveryListener,
58 IFloodlightModule,
59 INetworkGraphService {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080060
Pankaj Berdeda809572013-02-22 15:31:20 -080061 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070062 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070063 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070064 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -080065 //protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070066 protected IControllerRegistryService registryService;
yoshi2fd4c7e2013-11-22 15:47:55 -080067 protected DBOperation op;
Pankaj Berdeda809572013-02-22 15:31:20 -080068
69 protected static final String DBConfigFile = "dbconf";
yoshi2fd4c7e2013-11-22 15:47:55 -080070 protected static final String GraphDBStore = "graph_db_store";
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;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080074
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;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080080
81 /**
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070082 * Cleanup and synch switch state from registry
83 */
84 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
85 @Override
86 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080087 String old = Thread.currentThread().getName();
Yuta HIGUCHI1412e8c2014-01-03 17:19:01 -080088 Thread.currentThread().setName("SwitchCleanup@" + old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070089 try {
90 log.debug("Running cleanup thread");
yoshib6a66eb2014-01-21 10:11:49 -080091 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070092 switchCleanup();
93 }
94 catch (Exception e) {
95 log.error("Error in cleanup thread", e);
96 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070097 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070098 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070099 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800100 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700101 }
102 }
103
104 @Override
105 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700106 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700107 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700108 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800109 // Get the affected ports
110 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
111 // Get the affected links
112 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
113 // Get the affected reverse links
114 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
115 links.addAll(reverseLinks);
116
Jonathan Hartadc63892013-11-08 14:03:55 -0800117 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
118 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700119 registryService.releaseControl(dpid);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800120
Naoki Shiota987a5722013-10-23 11:59:36 -0700121 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700122 //
123 // NOTE: Here we explicitly send
124 // notification to remove the
125 // switch, because it is inactive
126 //
127 TopologyElement topologyElement =
128 new TopologyElement(dpid);
129 datagridService.notificationSendTopologyElementRemoved(topologyElement);
130
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800131 // Publish: remove the affected ports
132 for (Short port : ports) {
133 TopologyElement topologyElementPort =
134 new TopologyElement(dpid, port);
135 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
136 }
137 // Publish: remove the affected links
138 for (Link link : links) {
139 TopologyElement topologyElementLink =
140 new TopologyElement(link.getSrc(),
141 link.getSrcPort(),
142 link.getDst(),
143 link.getDstPort());
144 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
145 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700146 }
147 } catch (Exception e) {
148 log.error("Error in SwitchCleanup:controlChanged ", e);
149 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800150 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700151 }
152 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700153
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700154 protected void switchCleanup() {
yoshi8b972bc2013-12-05 17:10:04 -0800155 //op.close();
Toshio Koide70ba38b2013-06-13 14:05:05 -0700156 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800157
Jonathan Hartf02a0932013-03-18 18:30:48 -0700158 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700159 // For each switch check if a controller exists in controller registry
160 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700161 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700162 try {
163 long dpid = HexString.toLong(sw.getDPID());
164 String controller = registryService.getControllerForSwitch(dpid);
165 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700166 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700167 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700168 //} else {
169 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700170 }
171 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700172 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700173 e.printStackTrace();
174 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700175 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700176 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700177 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700178 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700179 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700180 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800181
pingping-lin00926032013-12-18 12:13:08 +0800182 @Override
183 public void linkDiscoveryUpdate(LDUpdate update) {
184 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
185 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berdeda809572013-02-22 15:31:20 -0800186
pingping-lin00926032013-12-18 12:13:08 +0800187 switch (update.getOperation()) {
188 case LINK_REMOVED:
189 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
190
191 if (linkStore.deleteLink(lt)) {
192 // TODO publish DELETE_LINK event here
193 TopologyElement topologyElement =
194 new TopologyElement(update.getSrc(),
195 update.getSrcPort(),
196 update.getDst(),
197 update.getDstPort());
198 datagridService.notificationSendTopologyElementRemoved(topologyElement);
199 }
200 break;
201 case LINK_UPDATED:
202 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
203
204 LinkInfo linfo = linkStore.getLinkInfo(lt);
205 // TODO update "linfo" using portState derived using "update"
206 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
207 // TODO publish UPDATE_LINK event here
208 //
209 // TODO NOTE: Here we assume that updated
210 // link is UP.
211 //
212 TopologyElement topologyElement =
213 new TopologyElement(update.getSrc(),
214 update.getSrcPort(),
215 update.getDst(),
216 update.getDstPort());
217 datagridService.notificationSendTopologyElementUpdated(topologyElement);
218 }
219 break;
220 case LINK_ADDED:
221 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
222
223 if (linkStore.addLink(lt)) {
224 // TODO publish ADD_LINK event here
225 TopologyElement topologyElement =
226 new TopologyElement(update.getSrc(),
227 update.getSrcPort(),
228 update.getDst(),
229 update.getDstPort());
230 datagridService.notificationSendTopologyElementAdded(topologyElement);
231 }
232
233 break;
234 default:
235 break;
236 }
237
238 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800239
240 @Override
241 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700242 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700243 if (swStore.addSwitch(sw)) {
244 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700245 TopologyElement topologyElement =
246 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800247 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800248
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800249 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700250 // TODO: Add only ports that are UP?
251 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800252 TopologyElement topologyElementPort =
253 new TopologyElement(sw.getId(), port.getPortNumber());
254 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800255
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800256 // Allow links to be discovered on this port now that it's
257 // in the database
258 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700259 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800260
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700261 // Add all links that might be connected already
262 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
263 // Add all reverse links as well
264 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
265 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800266
267 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700268 for (Link link : links) {
269 TopologyElement topologyElementLink =
270 new TopologyElement(link.getSrc(),
271 link.getSrcPort(),
272 link.getDst(),
273 link.getDstPort());
274 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
275 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700276 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800277 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800278 }
279
280 @Override
281 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800282 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700283 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800284 // Get the affected ports
285 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
286 // Get the affected links
287 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
288 // Get the affected reverse links
289 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
290 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800291
Naoki Shiota987a5722013-10-23 11:59:36 -0700292 if (swStore.deleteSwitch(sw.getStringId())) {
293 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700294 TopologyElement topologyElement =
295 new TopologyElement(sw.getId());
296 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800297
298 // Publish: remove the affected ports
299 for (Short port : ports) {
300 TopologyElement topologyElementPort =
301 new TopologyElement(sw.getId(), port);
302 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
303 }
304 // Publish: remove the affected links
305 for (Link link : links) {
306 TopologyElement topologyElementLink =
307 new TopologyElement(link.getSrc(),
308 link.getSrcPort(),
309 link.getDst(),
310 link.getDstPort());
311 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
312 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700313 }
314 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800315 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800316 }
317
318 @Override
319 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700320 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800321 }
322
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700323
324 @Override
325 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700326 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800327 // Allow links to be discovered on this port now that it's
328 // in the database
329 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800330
Naoki Shiota987a5722013-10-23 11:59:36 -0700331 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700332 TopologyElement topologyElement =
333 new TopologyElement(switchId, port.getPortNumber());
334 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800335
336 // Add all links that might be connected already
337 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
338 // Add all reverse links as well
339 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
340 links.addAll(reverseLinks);
341
342 // Publish: add the links
343 for (Link link : links) {
344 TopologyElement topologyElementLink =
345 new TopologyElement(link.getSrc(),
346 link.getSrcPort(),
347 link.getDst(),
348 link.getDstPort());
349 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
350 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700351 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700352 }
353
354 @Override
355 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800356 // Remove all links that might be connected already
357 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
358 // Remove all reverse links as well
359 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
360 links.addAll(reverseLinks);
361
Naoki Shiota987a5722013-10-23 11:59:36 -0700362 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
363 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700364 TopologyElement topologyElement =
365 new TopologyElement(switchId, port.getPortNumber());
366 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800367
368 // Publish: remove the links
369 for (Link link : links) {
370 TopologyElement topologyElementLink =
371 new TopologyElement(link.getSrc(),
372 link.getSrcPort(),
373 link.getDst(),
374 link.getDstPort());
375 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
376 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700377 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700378 }
379
Pankaj Berdeda809572013-02-22 15:31:20 -0800380 @Override
381 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700382 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800383 }
384
385 @Override
386 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800387 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
388 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800389 for (int intIpv4Address : device.getIPv4Addresses()) {
390 datagridService.sendArpRequest(
391 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
392 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800393 }
394
395 @Override
396 public void deviceRemoved(IDevice device) {
397 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700398 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800399 }
400
401 @Override
402 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800403 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800404 }
405
406 @Override
407 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800408 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800409 }
410
411 @Override
412 public void deviceVlanChanged(IDevice device) {
413 // TODO Auto-generated method stub
414 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800415
Pankaj Berdeda809572013-02-22 15:31:20 -0800416
417 @Override
418 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700419 Collection<Class<? extends IFloodlightService>> l =
420 new ArrayList<Class<? extends IFloodlightService>>();
421 l.add(INetworkGraphService.class);
422 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800423 }
424
425 @Override
426 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700427 Map<Class<? extends IFloodlightService>,
428 IFloodlightService> m =
429 new HashMap<Class<? extends IFloodlightService>,
430 IFloodlightService>();
431 m.put(INetworkGraphService.class, this);
432 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800433 }
434
435 @Override
436 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
437 Collection<Class<? extends IFloodlightService>> l =
438 new ArrayList<Class<? extends IFloodlightService>>();
439 l.add(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800440 //l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700441 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700442 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800443 return l;
444 }
445
446 @Override
447 public void init(FloodlightModuleContext context)
448 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800449 Map<String, String> configMap = context.getConfigParams(this);
450 String conf = configMap.get(DBConfigFile);
yoshi2fd4c7e2013-11-22 15:47:55 -0800451 String dbStore = configMap.get(GraphDBStore);
yoshia0839b52013-11-25 16:42:10 -0800452 System.out.println("conf" + conf + "dbStore" + dbStore);
yoshib6a66eb2014-01-21 10:11:49 -0800453 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
yoshid38cd312013-12-02 19:54:44 -0800454 //op = GraphDBManager.getDBOperation(dbStore, conf);
yoshia0839b52013-11-25 16:42:10 -0800455 if (op == null) {
456 System.out.println("publisher op is null");
457 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800458
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700459 floodlightProvider =
460 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800461 //deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700462 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700463 threadPool = context.getServiceImpl(IThreadPoolService.class);
464 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700465 datagridService = context.getServiceImpl(IDatagridService.class);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800466
Pankaj Berdeda809572013-02-22 15:31:20 -0800467 devStore = new DeviceStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800468 devStore.init(dbStore, conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800469
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700470 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800471 swStore.init(dbStore, conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700472
473 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800474 linkStore.init(dbStore, conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700475
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700476 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800477
Pankaj Berdeda809572013-02-22 15:31:20 -0800478 }
479
480 @Override
481 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700482 Map<String, String> configMap = context.getConfigParams(this);
483 String cleanupNeeded = configMap.get(CleanupEnabled);
484
Jonathan Hartd857ad62013-12-14 18:08:17 -0800485 //deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700486 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700487 linkDiscovery.addListener(this);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800488
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700489 log.debug("Adding EventListener");
yoshi34bcb3e2013-11-25 16:49:25 -0800490 System.out.println("start Up op " + op);
Toshio Koide70ba38b2013-06-13 14:05:05 -0700491 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800492 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700493 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700494 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700495 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
496 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700497 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700498 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700499
500 //
501 // NOTE: No need to register with the Datagrid Service,
502 // because we don't need to receive any notifications from it.
503 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800504 }
505
506}