blob: 4d564e97ff86d6d4a7b40df5ef74935cff900bc6 [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;
Jonathan Hart7804bea2014-01-07 10:50:52 -080023import net.floodlightcontroller.util.MACAddress;
yoshi2fd4c7e2013-11-22 15:47:55 -080024import net.onrc.onos.graph.DBOperation;
25import net.onrc.onos.graph.DBConnection;
26import net.onrc.onos.graph.GraphDBManager;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070027import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070028import net.onrc.onos.graph.IDBConnection;
29import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070030import net.onrc.onos.ofcontroller.core.IDeviceStorage;
31import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070032import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070033import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080034import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
35import net.onrc.onos.ofcontroller.core.ISwitchStorage;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070036import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
37import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070038import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070039import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
40import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070041import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hart7804bea2014-01-07 10:50:52 -080042import net.onrc.onos.ofcontroller.proxyarp.ArpReplyNotification;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070043import net.onrc.onos.ofcontroller.topology.TopologyElement;
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
Jonathan Hartd857ad62013-12-14 18:08:17 -080048import org.openflow.protocol.OFPhysicalPort;
49import org.openflow.util.HexString;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
53import com.google.common.net.InetAddresses;
54
Pavlin Radoslavov28069402013-10-18 18:43:11 -070055public class NetworkGraphPublisher implements IDeviceListener,
56 IOFSwitchListener,
57 IOFSwitchPortListener,
58 ILinkDiscoveryListener,
59 IFloodlightModule,
60 INetworkGraphService {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080061
Pankaj Berdeda809572013-02-22 15:31:20 -080062 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);
Jonathan Hartd857ad62013-12-14 18:08:17 -080066 //protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070067 protected IControllerRegistryService registryService;
yoshi2fd4c7e2013-11-22 15:47:55 -080068 protected DBOperation op;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080069
Pankaj Berdeda809572013-02-22 15:31:20 -080070 protected static final String DBConfigFile = "dbconf";
yoshi2fd4c7e2013-11-22 15:47:55 -080071 protected static final String GraphDBStore = "graph_db_store";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070072 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070073 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070074 protected IFloodlightProviderService floodlightProvider;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080075
Pankaj Berde62016142013-04-09 15:35:50 -070076 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070077 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070078 protected ILinkDiscoveryService linkDiscovery;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070079
80 protected IDatagridService datagridService;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080081
82 /**
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070083 * Cleanup and synch switch state from registry
84 */
85 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
86 @Override
87 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080088 String old = Thread.currentThread().getName();
Yuta HIGUCHI1412e8c2014-01-03 17:19:01 -080089 Thread.currentThread().setName("SwitchCleanup@" + old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070090 try {
91 log.debug("Running cleanup thread");
yoshib6a66eb2014-01-21 10:11:49 -080092 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070093 switchCleanup();
94 }
95 catch (Exception e) {
96 log.error("Error in cleanup thread", e);
97 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070098 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070099 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -0700100 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800101 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700102 }
103 }
104
105 @Override
106 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700107 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700108 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700109 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800110 // Get the affected ports
111 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
112 // Get the affected links
113 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
114 // Get the affected reverse links
115 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
116 links.addAll(reverseLinks);
117
Jonathan Hartadc63892013-11-08 14:03:55 -0800118 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
119 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700120 registryService.releaseControl(dpid);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800121
Naoki Shiota987a5722013-10-23 11:59:36 -0700122 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700123 //
124 // NOTE: Here we explicitly send
125 // notification to remove the
126 // switch, because it is inactive
127 //
128 TopologyElement topologyElement =
129 new TopologyElement(dpid);
130 datagridService.notificationSendTopologyElementRemoved(topologyElement);
131
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800132 // Publish: remove the affected ports
133 for (Short port : ports) {
134 TopologyElement topologyElementPort =
135 new TopologyElement(dpid, port);
136 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
137 }
138 // Publish: remove the affected links
139 for (Link link : links) {
140 TopologyElement topologyElementLink =
141 new TopologyElement(link.getSrc(),
142 link.getSrcPort(),
143 link.getDst(),
144 link.getDstPort());
145 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
146 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700147 }
148 } catch (Exception e) {
149 log.error("Error in SwitchCleanup:controlChanged ", e);
150 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800151 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700152 }
153 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700154
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700155 protected void switchCleanup() {
yoshi8b972bc2013-12-05 17:10:04 -0800156 //op.close();
Toshio Koide70ba38b2013-06-13 14:05:05 -0700157 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800158
Jonathan Hartf02a0932013-03-18 18:30:48 -0700159 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700160 // For each switch check if a controller exists in controller registry
161 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700162 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700163 try {
164 long dpid = HexString.toLong(sw.getDPID());
165 String controller = registryService.getControllerForSwitch(dpid);
166 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700167 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700168 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700169 //} else {
170 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700171 }
172 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700173 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700174 e.printStackTrace();
175 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700176 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700177 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700178 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700179 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700180 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700181 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800182
pingping-lin00926032013-12-18 12:13:08 +0800183 @Override
184 public void linkDiscoveryUpdate(LDUpdate update) {
185 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
186 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berdeda809572013-02-22 15:31:20 -0800187
pingping-lin00926032013-12-18 12:13:08 +0800188 switch (update.getOperation()) {
189 case LINK_REMOVED:
190 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
191
192 if (linkStore.deleteLink(lt)) {
193 // TODO publish DELETE_LINK event here
194 TopologyElement topologyElement =
195 new TopologyElement(update.getSrc(),
196 update.getSrcPort(),
197 update.getDst(),
198 update.getDstPort());
199 datagridService.notificationSendTopologyElementRemoved(topologyElement);
200 }
201 break;
202 case LINK_UPDATED:
203 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
204
205 LinkInfo linfo = linkStore.getLinkInfo(lt);
206 // TODO update "linfo" using portState derived using "update"
207 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
208 // TODO publish UPDATE_LINK event here
209 //
210 // TODO NOTE: Here we assume that updated
211 // link is UP.
212 //
213 TopologyElement topologyElement =
214 new TopologyElement(update.getSrc(),
215 update.getSrcPort(),
216 update.getDst(),
217 update.getDstPort());
218 datagridService.notificationSendTopologyElementUpdated(topologyElement);
219 }
220 break;
221 case LINK_ADDED:
222 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
223
224 if (linkStore.addLink(lt)) {
225 // TODO publish ADD_LINK event here
226 TopologyElement topologyElement =
227 new TopologyElement(update.getSrc(),
228 update.getSrcPort(),
229 update.getDst(),
230 update.getDstPort());
231 datagridService.notificationSendTopologyElementAdded(topologyElement);
232 }
233
234 break;
235 default:
236 break;
237 }
238
239 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800240
241 @Override
242 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700243 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 if (swStore.addSwitch(sw)) {
245 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700246 TopologyElement topologyElement =
247 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800248 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800249
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800250 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700251 // TODO: Add only ports that are UP?
252 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800253 TopologyElement topologyElementPort =
254 new TopologyElement(sw.getId(), port.getPortNumber());
255 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800256
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800257 // Allow links to be discovered on this port now that it's
258 // in the database
259 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700260 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800261
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700262 // Add all links that might be connected already
263 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
264 // Add all reverse links as well
265 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
266 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800267
268 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700269 for (Link link : links) {
270 TopologyElement topologyElementLink =
271 new TopologyElement(link.getSrc(),
272 link.getSrcPort(),
273 link.getDst(),
274 link.getDstPort());
275 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
276 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700277 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800278 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800279 }
280
281 @Override
282 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800283 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700284 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800285 // Get the affected ports
286 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
287 // Get the affected links
288 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
289 // Get the affected reverse links
290 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
291 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800292
Naoki Shiota987a5722013-10-23 11:59:36 -0700293 if (swStore.deleteSwitch(sw.getStringId())) {
294 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700295 TopologyElement topologyElement =
296 new TopologyElement(sw.getId());
297 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800298
299 // Publish: remove the affected ports
300 for (Short port : ports) {
301 TopologyElement topologyElementPort =
302 new TopologyElement(sw.getId(), port);
303 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
304 }
305 // Publish: remove the affected links
306 for (Link link : links) {
307 TopologyElement topologyElementLink =
308 new TopologyElement(link.getSrc(),
309 link.getSrcPort(),
310 link.getDst(),
311 link.getDstPort());
312 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
313 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700314 }
315 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800316 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800317 }
318
319 @Override
320 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700321 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800322 }
323
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700324
325 @Override
326 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800328 // Allow links to be discovered on this port now that it's
329 // in the database
330 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800331
Naoki Shiota987a5722013-10-23 11:59:36 -0700332 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700333 TopologyElement topologyElement =
334 new TopologyElement(switchId, port.getPortNumber());
335 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800336
337 // Add all links that might be connected already
338 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
339 // Add all reverse links as well
340 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
341 links.addAll(reverseLinks);
342
343 // Publish: add the links
344 for (Link link : links) {
345 TopologyElement topologyElementLink =
346 new TopologyElement(link.getSrc(),
347 link.getSrcPort(),
348 link.getDst(),
349 link.getDstPort());
350 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
351 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700352 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700353 }
354
355 @Override
356 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800357 // Remove all links that might be connected already
358 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
359 // Remove all reverse links as well
360 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
361 links.addAll(reverseLinks);
362
Naoki Shiota987a5722013-10-23 11:59:36 -0700363 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
364 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700365 TopologyElement topologyElement =
366 new TopologyElement(switchId, port.getPortNumber());
367 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800368
369 // Publish: remove the links
370 for (Link link : links) {
371 TopologyElement topologyElementLink =
372 new TopologyElement(link.getSrc(),
373 link.getSrcPort(),
374 link.getDst(),
375 link.getDstPort());
376 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
377 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700378 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700379 }
380
Pankaj Berdeda809572013-02-22 15:31:20 -0800381 @Override
382 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700383 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800384 }
385
386 @Override
387 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800388 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
389 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800390 for (int intIpv4Address : device.getIPv4Addresses()) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800391 datagridService.sendArpReplyNotification(new ArpReplyNotification(
392 InetAddresses.fromInteger(intIpv4Address),
393 MACAddress.valueOf(device.getMACAddress())));
Jonathan Hartd3003252013-11-15 09:44:46 -0800394 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800395 }
396
397 @Override
398 public void deviceRemoved(IDevice device) {
399 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700400 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800401 }
402
403 @Override
404 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800405 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800406 }
407
408 @Override
409 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800410 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800411 }
412
413 @Override
414 public void deviceVlanChanged(IDevice device) {
415 // TODO Auto-generated method stub
416 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800417
Pankaj Berdeda809572013-02-22 15:31:20 -0800418
419 @Override
420 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700421 Collection<Class<? extends IFloodlightService>> l =
422 new ArrayList<Class<? extends IFloodlightService>>();
423 l.add(INetworkGraphService.class);
424 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800425 }
426
427 @Override
428 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700429 Map<Class<? extends IFloodlightService>,
430 IFloodlightService> m =
431 new HashMap<Class<? extends IFloodlightService>,
432 IFloodlightService>();
433 m.put(INetworkGraphService.class, this);
434 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800435 }
436
437 @Override
438 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
439 Collection<Class<? extends IFloodlightService>> l =
440 new ArrayList<Class<? extends IFloodlightService>>();
441 l.add(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800442 //l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700443 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700444 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800445 return l;
446 }
447
448 @Override
449 public void init(FloodlightModuleContext context)
450 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800451 Map<String, String> configMap = context.getConfigParams(this);
452 String conf = configMap.get(DBConfigFile);
yoshi2fd4c7e2013-11-22 15:47:55 -0800453 String dbStore = configMap.get(GraphDBStore);
yoshib1f40702014-01-22 13:07:52 -0800454 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
yoshia0839b52013-11-25 16:42:10 -0800455 if (op == null) {
456 System.out.println("publisher op is null");
457 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -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);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800469
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700470 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800471 swStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800472
Pankaj Berde00e90882013-06-10 21:25:05 -0700473 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800474 linkStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800475
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");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700490 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800491 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800492 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700493 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700494 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
495 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700496 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700497 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700498
499 //
500 // NOTE: No need to register with the Datagrid Service,
501 // because we don't need to receive any notifications from it.
502 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800503 }
504
505}