blob: 994c741fad84cd3eee2e82672681d75e9b405c5a [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;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070023import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070024import net.onrc.onos.graph.GraphDBConnection;
25import net.onrc.onos.graph.GraphDBOperation;
26import net.onrc.onos.graph.IDBConnection;
27import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070028import net.onrc.onos.ofcontroller.core.IDeviceStorage;
29import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070030import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070031import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080032import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
33import net.onrc.onos.ofcontroller.core.ISwitchStorage;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070034import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
35import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070036import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070037import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
38import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070039import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartd3003252013-11-15 09:44:46 -080040import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070041import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070042import net.onrc.onos.registry.controller.IControllerRegistryService;
43import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
44import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080045
Pavlin Radoslavove2497672014-01-12 18:03:35 -080046import net.onrc.onos.ofcontroller.flowmanager.PerformanceMonitor;
47
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;
Toshio Koide70ba38b2013-06-13 14:05:05 -070068 protected GraphDBOperation op;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080069
Pankaj Berdeda809572013-02-22 15:31:20 -080070 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;
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");
91 switchCleanup();
92 }
93 catch (Exception e) {
94 log.error("Error in cleanup thread", e);
95 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070096 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070097 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070098 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080099 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700100 }
101 }
102
103 @Override
104 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700105 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700106 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700107 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800108 // Get the affected ports
109 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
110 // Get the affected links
111 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
112 // Get the affected reverse links
113 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
114 links.addAll(reverseLinks);
115
Jonathan Hartadc63892013-11-08 14:03:55 -0800116 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
117 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700118 registryService.releaseControl(dpid);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800119
Naoki Shiota987a5722013-10-23 11:59:36 -0700120 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700121 //
122 // NOTE: Here we explicitly send
123 // notification to remove the
124 // switch, because it is inactive
125 //
126 TopologyElement topologyElement =
127 new TopologyElement(dpid);
128 datagridService.notificationSendTopologyElementRemoved(topologyElement);
129
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800130 // Publish: remove the affected ports
131 for (Short port : ports) {
132 TopologyElement topologyElementPort =
133 new TopologyElement(dpid, port);
134 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
135 }
136 // Publish: remove the affected links
137 for (Link link : links) {
138 TopologyElement topologyElementLink =
139 new TopologyElement(link.getSrc(),
140 link.getSrcPort(),
141 link.getDst(),
142 link.getDstPort());
143 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
144 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700145 }
146 } catch (Exception e) {
147 log.error("Error in SwitchCleanup:controlChanged ", e);
148 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800149 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700150 }
151 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700152
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700153 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700154 op.close();
155 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800156
Jonathan Hartf02a0932013-03-18 18:30:48 -0700157 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700158 // For each switch check if a controller exists in controller registry
159 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700160 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700161 try {
162 long dpid = HexString.toLong(sw.getDPID());
163 String controller = registryService.getControllerForSwitch(dpid);
164 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700165 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700166 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700167 //} else {
168 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700169 }
170 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700172 e.printStackTrace();
173 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700174 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700175 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700177 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700178 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700179 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800180
pingping-lin00926032013-12-18 12:13:08 +0800181 @Override
182 public void linkDiscoveryUpdate(LDUpdate update) {
183 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
184 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800185
pingping-lin00926032013-12-18 12:13:08 +0800186 switch (update.getOperation()) {
187 case LINK_REMOVED:
188 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800189
pingping-lin00926032013-12-18 12:13:08 +0800190 if (linkStore.deleteLink(lt)) {
191 // TODO publish DELETE_LINK event here
192 TopologyElement topologyElement =
193 new TopologyElement(update.getSrc(),
194 update.getSrcPort(),
195 update.getDst(),
196 update.getDstPort());
197 datagridService.notificationSendTopologyElementRemoved(topologyElement);
198 }
199 break;
200 case LINK_UPDATED:
201 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800202
pingping-lin00926032013-12-18 12:13:08 +0800203 LinkInfo linfo = linkStore.getLinkInfo(lt);
204 // TODO update "linfo" using portState derived using "update"
205 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
206 // TODO publish UPDATE_LINK event here
207 //
208 // TODO NOTE: Here we assume that updated
209 // link is UP.
210 //
211 TopologyElement topologyElement =
212 new TopologyElement(update.getSrc(),
213 update.getSrcPort(),
214 update.getDst(),
215 update.getDstPort());
216 datagridService.notificationSendTopologyElementUpdated(topologyElement);
217 }
218 break;
219 case LINK_ADDED:
220 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800221
pingping-lin00926032013-12-18 12:13:08 +0800222 if (linkStore.addLink(lt)) {
223 // TODO publish ADD_LINK event here
224 TopologyElement topologyElement =
225 new TopologyElement(update.getSrc(),
226 update.getSrcPort(),
227 update.getDst(),
228 update.getDstPort());
229 datagridService.notificationSendTopologyElementAdded(topologyElement);
230 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800231
pingping-lin00926032013-12-18 12:13:08 +0800232 break;
233 default:
234 break;
235 }
236
237 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800238
239 @Override
240 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700241 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700242 if (swStore.addSwitch(sw)) {
243 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700244 TopologyElement topologyElement =
245 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800246 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800247
248 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700249 // TODO: Add only ports that are UP?
250 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800251 TopologyElement topologyElementPort =
252 new TopologyElement(sw.getId(), port.getPortNumber());
253 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800254
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800255 // Allow links to be discovered on this port now that it's
256 // in the database
257 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700258 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800259
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700260 // Add all links that might be connected already
261 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
262 // Add all reverse links as well
263 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
264 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800265
266 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700267 for (Link link : links) {
268 TopologyElement topologyElementLink =
269 new TopologyElement(link.getSrc(),
270 link.getSrcPort(),
271 link.getDst(),
272 link.getDstPort());
273 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
274 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700275 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700276 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800277 }
278
279 @Override
280 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800281 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700282 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800283 // Get the affected ports
284 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
285 // Get the affected links
286 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
287 // Get the affected reverse links
288 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
289 links.addAll(reverseLinks);
290
Naoki Shiota987a5722013-10-23 11:59:36 -0700291 if (swStore.deleteSwitch(sw.getStringId())) {
292 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700293 TopologyElement topologyElement =
294 new TopologyElement(sw.getId());
295 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800296
297 // Publish: remove the affected ports
298 for (Short port : ports) {
299 TopologyElement topologyElementPort =
300 new TopologyElement(sw.getId(), port);
301 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
302 }
303 // Publish: remove the affected links
304 for (Link link : links) {
305 TopologyElement topologyElementLink =
306 new TopologyElement(link.getSrc(),
307 link.getSrcPort(),
308 link.getDst(),
309 link.getDstPort());
310 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
311 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700312 }
313 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800314 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800315 }
316
317 @Override
318 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700319 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800320 }
321
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700322
323 @Override
324 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700325 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800326 // Allow links to be discovered on this port now that it's
327 // in the database
328 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800329
Naoki Shiota987a5722013-10-23 11:59:36 -0700330 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700331 TopologyElement topologyElement =
332 new TopologyElement(switchId, port.getPortNumber());
333 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800334
335 // Add all links that might be connected already
336 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
337 // Add all reverse links as well
338 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
339 links.addAll(reverseLinks);
340
341 // Publish: add the links
342 for (Link link : links) {
343 TopologyElement topologyElementLink =
344 new TopologyElement(link.getSrc(),
345 link.getSrcPort(),
346 link.getDst(),
347 link.getDstPort());
348 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
349 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700350 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700351 }
352
353 @Override
354 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800355 // Remove all links that might be connected already
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800356 PerformanceMonitor.start("SwitchPortRemoved.DbAccess");
357
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800358 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())) {
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800364 PerformanceMonitor.stop("SwitchPortRemoved.DbAccess");
365 PerformanceMonitor.start("SwitchPortRemoved.NotificationSend");
Naoki Shiota987a5722013-10-23 11:59:36 -0700366 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700367 TopologyElement topologyElement =
368 new TopologyElement(switchId, port.getPortNumber());
369 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800370
371 // Publish: remove the links
372 for (Link link : links) {
373 TopologyElement topologyElementLink =
374 new TopologyElement(link.getSrc(),
375 link.getSrcPort(),
376 link.getDst(),
377 link.getDstPort());
378 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
379 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800380 PerformanceMonitor.stop("SwitchPortRemoved.NotificationSend");
381 PerformanceMonitor.report();
Naoki Shiota987a5722013-10-23 11:59:36 -0700382 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700383 }
384
Pankaj Berdeda809572013-02-22 15:31:20 -0800385 @Override
386 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700387 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800388 }
389
390 @Override
391 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800392 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
393 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800394 for (int intIpv4Address : device.getIPv4Addresses()) {
395 datagridService.sendArpRequest(
396 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
397 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800398 }
399
400 @Override
401 public void deviceRemoved(IDevice device) {
402 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700403 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800404 }
405
406 @Override
407 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800408 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800409 }
410
411 @Override
412 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800413 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800414 }
415
416 @Override
417 public void deviceVlanChanged(IDevice device) {
418 // TODO Auto-generated method stub
419 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800420
Pankaj Berdeda809572013-02-22 15:31:20 -0800421
422 @Override
423 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700424 Collection<Class<? extends IFloodlightService>> l =
425 new ArrayList<Class<? extends IFloodlightService>>();
426 l.add(INetworkGraphService.class);
427 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800428 }
429
430 @Override
431 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700432 Map<Class<? extends IFloodlightService>,
433 IFloodlightService> m =
434 new HashMap<Class<? extends IFloodlightService>,
435 IFloodlightService>();
436 m.put(INetworkGraphService.class, this);
437 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800438 }
439
440 @Override
441 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
442 Collection<Class<? extends IFloodlightService>> l =
443 new ArrayList<Class<? extends IFloodlightService>>();
444 l.add(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800445 //l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700446 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700447 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800448 return l;
449 }
450
451 @Override
452 public void init(FloodlightModuleContext context)
453 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800454 Map<String, String> configMap = context.getConfigParams(this);
455 String conf = configMap.get(DBConfigFile);
Toshio Koidebfe9b922013-06-18 10:56:05 -0700456 op = new GraphDBOperation(conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800457
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700458 floodlightProvider =
459 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800460 //deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700461 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700462 threadPool = context.getServiceImpl(IThreadPoolService.class);
463 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700464 datagridService = context.getServiceImpl(IDatagridService.class);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800465
Pankaj Berdeda809572013-02-22 15:31:20 -0800466 devStore = new DeviceStorageImpl();
467 devStore.init(conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800468
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700469 swStore = new SwitchStorageImpl();
470 swStore.init(conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800471
Pankaj Berde00e90882013-06-10 21:25:05 -0700472 linkStore = new LinkStorageImpl();
473 linkStore.init(conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800474
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700475 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800476
Pankaj Berdeda809572013-02-22 15:31:20 -0800477 }
478
479 @Override
480 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700481 Map<String, String> configMap = context.getConfigParams(this);
482 String cleanupNeeded = configMap.get(CleanupEnabled);
483
Jonathan Hartd857ad62013-12-14 18:08:17 -0800484 //deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700485 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700486 linkDiscovery.addListener(this);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800487
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700488 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700489 IDBConnection conn = op.getDBConnection();
490 conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn));
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800491 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700492 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700493 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
494 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700495 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700496 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700497
498 //
499 // NOTE: No need to register with the Datagrid Service,
500 // because we don't need to receive any notifications from it.
501 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800502 }
503
504}