blob: fefaadfc03e066ce092488db7699194b23cfcab5 [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
Pavlin Radoslavove2497672014-01-12 18:03:35 -080048import net.onrc.onos.ofcontroller.flowmanager.PerformanceMonitor;
49
Jonathan Hartd857ad62013-12-14 18:08:17 -080050import org.openflow.protocol.OFPhysicalPort;
51import org.openflow.util.HexString;
52import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55import com.google.common.net.InetAddresses;
56
Pavlin Radoslavov28069402013-10-18 18:43:11 -070057public class NetworkGraphPublisher implements IDeviceListener,
58 IOFSwitchListener,
59 IOFSwitchPortListener,
60 ILinkDiscoveryListener,
61 IFloodlightModule,
62 INetworkGraphService {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080063
Pankaj Berdeda809572013-02-22 15:31:20 -080064 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070065 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070066 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070067 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -080068 //protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070069 protected IControllerRegistryService registryService;
yoshi2fd4c7e2013-11-22 15:47:55 -080070 protected DBOperation op;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080071
Pankaj Berdeda809572013-02-22 15:31:20 -080072 protected static final String DBConfigFile = "dbconf";
yoshi2fd4c7e2013-11-22 15:47:55 -080073 protected static final String GraphDBStore = "graph_db_store";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070074 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070075 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070076 protected IFloodlightProviderService floodlightProvider;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080077
Pankaj Berde62016142013-04-09 15:35:50 -070078 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070079 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070080 protected ILinkDiscoveryService linkDiscovery;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070081
82 protected IDatagridService datagridService;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080083
84 /**
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070085 * Cleanup and synch switch state from registry
86 */
87 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
88 @Override
89 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080090 String old = Thread.currentThread().getName();
Yuta HIGUCHI1412e8c2014-01-03 17:19:01 -080091 Thread.currentThread().setName("SwitchCleanup@" + old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070092 try {
93 log.debug("Running cleanup thread");
yoshib6a66eb2014-01-21 10:11:49 -080094 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070095 switchCleanup();
96 }
97 catch (Exception e) {
98 log.error("Error in cleanup thread", e);
99 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700100 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700101 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -0700102 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800103 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700104 }
105 }
106
107 @Override
108 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700109 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700110 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700111 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800112 // Get the affected ports
113 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
114 // Get the affected links
115 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
116 // Get the affected reverse links
117 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
118 links.addAll(reverseLinks);
119
Jonathan Hartadc63892013-11-08 14:03:55 -0800120 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
121 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700122 registryService.releaseControl(dpid);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800123
Naoki Shiota987a5722013-10-23 11:59:36 -0700124 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700125 //
126 // NOTE: Here we explicitly send
127 // notification to remove the
128 // switch, because it is inactive
129 //
130 TopologyElement topologyElement =
131 new TopologyElement(dpid);
132 datagridService.notificationSendTopologyElementRemoved(topologyElement);
133
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800134 // Publish: remove the affected ports
135 for (Short port : ports) {
136 TopologyElement topologyElementPort =
137 new TopologyElement(dpid, port);
138 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
139 }
140 // Publish: remove the affected links
141 for (Link link : links) {
142 TopologyElement topologyElementLink =
143 new TopologyElement(link.getSrc(),
144 link.getSrcPort(),
145 link.getDst(),
146 link.getDstPort());
147 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
148 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700149 }
150 } catch (Exception e) {
151 log.error("Error in SwitchCleanup:controlChanged ", e);
152 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800153 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700154 }
155 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700156
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700157 protected void switchCleanup() {
yoshi8b972bc2013-12-05 17:10:04 -0800158 //op.close();
Toshio Koide70ba38b2013-06-13 14:05:05 -0700159 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800160
Jonathan Hartf02a0932013-03-18 18:30:48 -0700161 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700162 // For each switch check if a controller exists in controller registry
163 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700164 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700165 try {
166 long dpid = HexString.toLong(sw.getDPID());
167 String controller = registryService.getControllerForSwitch(dpid);
168 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700169 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700170 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700171 //} else {
172 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700173 }
174 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700175 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700176 e.printStackTrace();
177 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700178 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700179 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700180 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700181 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700182 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700183 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800184
pingping-lin00926032013-12-18 12:13:08 +0800185 @Override
186 public void linkDiscoveryUpdate(LDUpdate update) {
187 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
188 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berdeda809572013-02-22 15:31:20 -0800189
pingping-lin00926032013-12-18 12:13:08 +0800190 switch (update.getOperation()) {
191 case LINK_REMOVED:
192 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
193
194 if (linkStore.deleteLink(lt)) {
195 // TODO publish DELETE_LINK event here
196 TopologyElement topologyElement =
197 new TopologyElement(update.getSrc(),
198 update.getSrcPort(),
199 update.getDst(),
200 update.getDstPort());
201 datagridService.notificationSendTopologyElementRemoved(topologyElement);
202 }
203 break;
204 case LINK_UPDATED:
205 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
206
207 LinkInfo linfo = linkStore.getLinkInfo(lt);
208 // TODO update "linfo" using portState derived using "update"
209 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
210 // TODO publish UPDATE_LINK event here
211 //
212 // TODO NOTE: Here we assume that updated
213 // link is UP.
214 //
215 TopologyElement topologyElement =
216 new TopologyElement(update.getSrc(),
217 update.getSrcPort(),
218 update.getDst(),
219 update.getDstPort());
220 datagridService.notificationSendTopologyElementUpdated(topologyElement);
221 }
222 break;
223 case LINK_ADDED:
224 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
225
226 if (linkStore.addLink(lt)) {
227 // TODO publish ADD_LINK event here
228 TopologyElement topologyElement =
229 new TopologyElement(update.getSrc(),
230 update.getSrcPort(),
231 update.getDst(),
232 update.getDstPort());
233 datagridService.notificationSendTopologyElementAdded(topologyElement);
234 }
235
236 break;
237 default:
238 break;
239 }
240
241 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800242
243 @Override
244 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700245 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700246 if (swStore.addSwitch(sw)) {
247 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700248 TopologyElement topologyElement =
249 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800250 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800251
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800252 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700253 // TODO: Add only ports that are UP?
254 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800255 TopologyElement topologyElementPort =
256 new TopologyElement(sw.getId(), port.getPortNumber());
257 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800258
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800259 // Allow links to be discovered on this port now that it's
260 // in the database
261 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700262 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800263
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700264 // Add all links that might be connected already
265 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
266 // Add all reverse links as well
267 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
268 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800269
270 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700271 for (Link link : links) {
272 TopologyElement topologyElementLink =
273 new TopologyElement(link.getSrc(),
274 link.getSrcPort(),
275 link.getDst(),
276 link.getDstPort());
277 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
278 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700279 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800280 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800281 }
282
283 @Override
284 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800285 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700286 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800287 // Get the affected ports
288 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
289 // Get the affected links
290 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
291 // Get the affected reverse links
292 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
293 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800294
Naoki Shiota987a5722013-10-23 11:59:36 -0700295 if (swStore.deleteSwitch(sw.getStringId())) {
296 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700297 TopologyElement topologyElement =
298 new TopologyElement(sw.getId());
299 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800300
301 // Publish: remove the affected ports
302 for (Short port : ports) {
303 TopologyElement topologyElementPort =
304 new TopologyElement(sw.getId(), port);
305 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
306 }
307 // Publish: remove the affected links
308 for (Link link : links) {
309 TopologyElement topologyElementLink =
310 new TopologyElement(link.getSrc(),
311 link.getSrcPort(),
312 link.getDst(),
313 link.getDstPort());
314 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
315 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 }
317 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800318 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800319 }
320
321 @Override
322 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700323 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800324 }
325
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700326
327 @Override
328 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700329 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800330 // Allow links to be discovered on this port now that it's
331 // in the database
332 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800333
Naoki Shiota987a5722013-10-23 11:59:36 -0700334 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700335 TopologyElement topologyElement =
336 new TopologyElement(switchId, port.getPortNumber());
337 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800338
339 // Add all links that might be connected already
340 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
341 // Add all reverse links as well
342 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
343 links.addAll(reverseLinks);
344
345 // Publish: add the links
346 for (Link link : links) {
347 TopologyElement topologyElementLink =
348 new TopologyElement(link.getSrc(),
349 link.getSrcPort(),
350 link.getDst(),
351 link.getDstPort());
352 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
353 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700354 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700355 }
356
357 @Override
358 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800359 // Remove all links that might be connected already
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800360 PerformanceMonitor.start("SwitchPortRemoved.DbAccess");
361
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800362 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
363 // Remove all reverse links as well
364 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
365 links.addAll(reverseLinks);
366
Naoki Shiota987a5722013-10-23 11:59:36 -0700367 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800368 PerformanceMonitor.stop("SwitchPortRemoved.DbAccess");
369 PerformanceMonitor.start("SwitchPortRemoved.NotificationSend");
Naoki Shiota987a5722013-10-23 11:59:36 -0700370 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700371 TopologyElement topologyElement =
372 new TopologyElement(switchId, port.getPortNumber());
373 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800374
375 // Publish: remove the links
376 for (Link link : links) {
377 TopologyElement topologyElementLink =
378 new TopologyElement(link.getSrc(),
379 link.getSrcPort(),
380 link.getDst(),
381 link.getDstPort());
382 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
383 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800384 PerformanceMonitor.stop("SwitchPortRemoved.NotificationSend");
Pavlin Radoslavov8bd6d112014-01-12 20:12:37 -0800385 PerformanceMonitor.report("SwitchPortRemoved.DbAccess");
386 PerformanceMonitor.report("TopologyEntryRemoved.NotificationReceived");
Naoki Shiota987a5722013-10-23 11:59:36 -0700387 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700388 }
389
Pankaj Berdeda809572013-02-22 15:31:20 -0800390 @Override
391 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700392 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800393 }
394
395 @Override
396 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800397 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
398 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800399 for (int intIpv4Address : device.getIPv4Addresses()) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800400 datagridService.sendArpReplyNotification(new ArpReplyNotification(
401 InetAddresses.fromInteger(intIpv4Address),
402 MACAddress.valueOf(device.getMACAddress())));
Jonathan Hartd3003252013-11-15 09:44:46 -0800403 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800404 }
405
406 @Override
407 public void deviceRemoved(IDevice device) {
408 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700409 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800410 }
411
412 @Override
413 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800414 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800415 }
416
417 @Override
418 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800419 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800420 }
421
422 @Override
423 public void deviceVlanChanged(IDevice device) {
424 // TODO Auto-generated method stub
425 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800426
Pankaj Berdeda809572013-02-22 15:31:20 -0800427
428 @Override
429 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700430 Collection<Class<? extends IFloodlightService>> l =
431 new ArrayList<Class<? extends IFloodlightService>>();
432 l.add(INetworkGraphService.class);
433 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800434 }
435
436 @Override
437 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700438 Map<Class<? extends IFloodlightService>,
439 IFloodlightService> m =
440 new HashMap<Class<? extends IFloodlightService>,
441 IFloodlightService>();
442 m.put(INetworkGraphService.class, this);
443 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800444 }
445
446 @Override
447 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
448 Collection<Class<? extends IFloodlightService>> l =
449 new ArrayList<Class<? extends IFloodlightService>>();
450 l.add(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800451 //l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700452 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700453 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800454 return l;
455 }
456
457 @Override
458 public void init(FloodlightModuleContext context)
459 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800460 Map<String, String> configMap = context.getConfigParams(this);
461 String conf = configMap.get(DBConfigFile);
yoshi2fd4c7e2013-11-22 15:47:55 -0800462 String dbStore = configMap.get(GraphDBStore);
yoshib1f40702014-01-22 13:07:52 -0800463 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800464
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700465 floodlightProvider =
466 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800467 //deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700468 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700469 threadPool = context.getServiceImpl(IThreadPoolService.class);
470 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700471 datagridService = context.getServiceImpl(IDatagridService.class);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800472
Pankaj Berdeda809572013-02-22 15:31:20 -0800473 devStore = new DeviceStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800474 devStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800475
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700476 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800477 swStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800478
Pankaj Berde00e90882013-06-10 21:25:05 -0700479 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800480 linkStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800481
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700482 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800483
Pankaj Berdeda809572013-02-22 15:31:20 -0800484 }
485
486 @Override
487 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700488 Map<String, String> configMap = context.getConfigParams(this);
489 String cleanupNeeded = configMap.get(CleanupEnabled);
490
Jonathan Hartd857ad62013-12-14 18:08:17 -0800491 //deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700492 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700493 linkDiscovery.addListener(this);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800494
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700495 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700496 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800497 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800498 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700499 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700500 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
501 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700502 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700503 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700504
505 //
506 // NOTE: No need to register with the Datagrid Service,
507 // because we don't need to receive any notifications from it.
508 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800509 }
510
511}