blob: 99357be2357782f0a39e8b4f7615895313dd17d5 [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,
Jonathan Hart4b5bbb52014-02-06 10:09:31 -080061 IFloodlightModule {
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080062
Pankaj Berdeda809572013-02-22 15:31:20 -080063 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070064 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070065 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070066 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -080067 //protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070068 protected IControllerRegistryService registryService;
yoshi2fd4c7e2013-11-22 15:47:55 -080069 protected DBOperation op;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080070
Pankaj Berdeda809572013-02-22 15:31:20 -080071 protected static final String DBConfigFile = "dbconf";
yoshi2fd4c7e2013-11-22 15:47:55 -080072 protected static final String GraphDBStore = "graph_db_store";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070073 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070074 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070075 protected IFloodlightProviderService floodlightProvider;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080076
Pankaj Berde62016142013-04-09 15:35:50 -070077 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070078 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070079 protected ILinkDiscoveryService linkDiscovery;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070080
81 protected IDatagridService datagridService;
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -080082
83 /**
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070084 * Cleanup and synch switch state from registry
85 */
86 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
87 @Override
88 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080089 String old = Thread.currentThread().getName();
Yuta HIGUCHI1412e8c2014-01-03 17:19:01 -080090 Thread.currentThread().setName("SwitchCleanup@" + old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070091 try {
92 log.debug("Running cleanup thread");
Yoshi Muroi5804ce92014-02-08 03:58:04 -080093 op = GraphDBManager.getDBOperation();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070094 switchCleanup();
95 }
96 catch (Exception e) {
97 log.error("Error in cleanup thread", e);
98 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070099 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700100 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -0700101 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800102 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700103 }
104 }
105
106 @Override
107 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700108 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700109 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700110 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800111 // Get the affected ports
112 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
113 // Get the affected links
114 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
115 // Get the affected reverse links
116 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
117 links.addAll(reverseLinks);
118
Jonathan Hartadc63892013-11-08 14:03:55 -0800119 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
120 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700121 registryService.releaseControl(dpid);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800122
Naoki Shiota987a5722013-10-23 11:59:36 -0700123 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700124 //
125 // NOTE: Here we explicitly send
126 // notification to remove the
127 // switch, because it is inactive
128 //
129 TopologyElement topologyElement =
130 new TopologyElement(dpid);
131 datagridService.notificationSendTopologyElementRemoved(topologyElement);
132
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800133 // Publish: remove the affected ports
134 for (Short port : ports) {
135 TopologyElement topologyElementPort =
136 new TopologyElement(dpid, port);
137 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
138 }
139 // Publish: remove the affected links
140 for (Link link : links) {
141 TopologyElement topologyElementLink =
142 new TopologyElement(link.getSrc(),
143 link.getSrcPort(),
144 link.getDst(),
145 link.getDstPort());
146 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
147 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700148 }
149 } catch (Exception e) {
150 log.error("Error in SwitchCleanup:controlChanged ", e);
151 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800152 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700153 }
154 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700155
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700156 protected void switchCleanup() {
yoshi8b972bc2013-12-05 17:10:04 -0800157 //op.close();
Toshio Koide70ba38b2013-06-13 14:05:05 -0700158 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800159
Jonathan Hartf02a0932013-03-18 18:30:48 -0700160 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700161 // For each switch check if a controller exists in controller registry
162 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700163 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700164 try {
165 long dpid = HexString.toLong(sw.getDPID());
166 String controller = registryService.getControllerForSwitch(dpid);
167 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700168 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700169 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700170 //} else {
171 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700172 }
173 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700174 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700175 e.printStackTrace();
176 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700177 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700178 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700179 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700180 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700181 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700182 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800183
pingping-lin00926032013-12-18 12:13:08 +0800184 @Override
185 public void linkDiscoveryUpdate(LDUpdate update) {
186 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
187 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berdeda809572013-02-22 15:31:20 -0800188
pingping-lin00926032013-12-18 12:13:08 +0800189 switch (update.getOperation()) {
190 case LINK_REMOVED:
191 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
192
193 if (linkStore.deleteLink(lt)) {
194 // TODO publish DELETE_LINK event here
195 TopologyElement topologyElement =
196 new TopologyElement(update.getSrc(),
197 update.getSrcPort(),
198 update.getDst(),
199 update.getDstPort());
200 datagridService.notificationSendTopologyElementRemoved(topologyElement);
201 }
202 break;
203 case LINK_UPDATED:
204 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
205
206 LinkInfo linfo = linkStore.getLinkInfo(lt);
207 // TODO update "linfo" using portState derived using "update"
208 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
209 // TODO publish UPDATE_LINK event here
210 //
211 // TODO NOTE: Here we assume that updated
212 // link is UP.
213 //
214 TopologyElement topologyElement =
215 new TopologyElement(update.getSrc(),
216 update.getSrcPort(),
217 update.getDst(),
218 update.getDstPort());
219 datagridService.notificationSendTopologyElementUpdated(topologyElement);
220 }
221 break;
222 case LINK_ADDED:
223 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
224
225 if (linkStore.addLink(lt)) {
226 // TODO publish ADD_LINK event here
227 TopologyElement topologyElement =
228 new TopologyElement(update.getSrc(),
229 update.getSrcPort(),
230 update.getDst(),
231 update.getDstPort());
232 datagridService.notificationSendTopologyElementAdded(topologyElement);
233 }
234
235 break;
236 default:
237 break;
238 }
239
240 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800241
242 @Override
243 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700244 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700245 if (swStore.addSwitch(sw)) {
246 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700247 TopologyElement topologyElement =
248 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800249 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800250
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800251 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700252 // TODO: Add only ports that are UP?
253 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800254 TopologyElement topologyElementPort =
255 new TopologyElement(sw.getId(), port.getPortNumber());
256 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800257
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800258 // Allow links to be discovered on this port now that it's
259 // in the database
260 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700261 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800262
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700263 // Add all links that might be connected already
264 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
265 // Add all reverse links as well
266 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
267 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800268
269 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700270 for (Link link : links) {
271 TopologyElement topologyElementLink =
272 new TopologyElement(link.getSrc(),
273 link.getSrcPort(),
274 link.getDst(),
275 link.getDstPort());
276 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
277 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700278 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800279 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800280 }
281
282 @Override
283 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800284 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700285 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800286 // Get the affected ports
287 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
288 // Get the affected links
289 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
290 // Get the affected reverse links
291 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
292 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800293
Naoki Shiota987a5722013-10-23 11:59:36 -0700294 if (swStore.deleteSwitch(sw.getStringId())) {
295 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700296 TopologyElement topologyElement =
297 new TopologyElement(sw.getId());
298 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800299
300 // Publish: remove the affected ports
301 for (Short port : ports) {
302 TopologyElement topologyElementPort =
303 new TopologyElement(sw.getId(), port);
304 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
305 }
306 // Publish: remove the affected links
307 for (Link link : links) {
308 TopologyElement topologyElementLink =
309 new TopologyElement(link.getSrc(),
310 link.getSrcPort(),
311 link.getDst(),
312 link.getDstPort());
313 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
314 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700315 }
316 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800317 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800318 }
319
320 @Override
321 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700322 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800323 }
324
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700325
326 @Override
327 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700328 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800329 // Allow links to be discovered on this port now that it's
330 // in the database
331 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800332
Naoki Shiota987a5722013-10-23 11:59:36 -0700333 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700334 TopologyElement topologyElement =
335 new TopologyElement(switchId, port.getPortNumber());
336 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800337
338 // Add all links that might be connected already
339 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
340 // Add all reverse links as well
341 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
342 links.addAll(reverseLinks);
343
344 // Publish: add the links
345 for (Link link : links) {
346 TopologyElement topologyElementLink =
347 new TopologyElement(link.getSrc(),
348 link.getSrcPort(),
349 link.getDst(),
350 link.getDstPort());
351 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
352 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700353 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700354 }
355
356 @Override
357 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800358 // Remove all links that might be connected already
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800359 PerformanceMonitor.start("SwitchPortRemoved.DbAccess");
360
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800361 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
362 // Remove all reverse links as well
363 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
364 links.addAll(reverseLinks);
365
Naoki Shiota987a5722013-10-23 11:59:36 -0700366 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800367 PerformanceMonitor.stop("SwitchPortRemoved.DbAccess");
368 PerformanceMonitor.start("SwitchPortRemoved.NotificationSend");
Naoki Shiota987a5722013-10-23 11:59:36 -0700369 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700370 TopologyElement topologyElement =
371 new TopologyElement(switchId, port.getPortNumber());
372 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800373
374 // Publish: remove the links
375 for (Link link : links) {
376 TopologyElement topologyElementLink =
377 new TopologyElement(link.getSrc(),
378 link.getSrcPort(),
379 link.getDst(),
380 link.getDstPort());
381 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
382 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800383 PerformanceMonitor.stop("SwitchPortRemoved.NotificationSend");
Pavlin Radoslavov8bd6d112014-01-12 20:12:37 -0800384 PerformanceMonitor.report("SwitchPortRemoved.DbAccess");
385 PerformanceMonitor.report("TopologyEntryRemoved.NotificationReceived");
Naoki Shiota987a5722013-10-23 11:59:36 -0700386 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700387 }
388
Pankaj Berdeda809572013-02-22 15:31:20 -0800389 @Override
390 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700391 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800392 }
393
394 @Override
395 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800396 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
397 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800398 for (int intIpv4Address : device.getIPv4Addresses()) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800399 datagridService.sendArpReplyNotification(new ArpReplyNotification(
400 InetAddresses.fromInteger(intIpv4Address),
401 MACAddress.valueOf(device.getMACAddress())));
Jonathan Hartd3003252013-11-15 09:44:46 -0800402 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800403 }
404
405 @Override
406 public void deviceRemoved(IDevice device) {
407 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700408 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800409 }
410
411 @Override
412 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800413 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800414 }
415
416 @Override
417 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800418 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800419 }
420
421 @Override
422 public void deviceVlanChanged(IDevice device) {
423 // TODO Auto-generated method stub
424 }
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800425
Pankaj Berdeda809572013-02-22 15:31:20 -0800426
427 @Override
428 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800429 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -0800430 }
431
432 @Override
433 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800434 return null;
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>>();
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800441 l.add(IFloodlightProviderService.class);
442 //l.add(IDeviceService.class);
443 l.add(IDatagridService.class);
444 l.add(IThreadPoolService.class);
445 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800446 }
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);
Yoshi Muroi5804ce92014-02-08 03:58:04 -0800454 op = GraphDBManager.getDBOperation();
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800455
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700456 floodlightProvider =
457 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800458 //deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700459 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700460 threadPool = context.getServiceImpl(IThreadPoolService.class);
461 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700462 datagridService = context.getServiceImpl(IDatagridService.class);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800463
Pankaj Berdeda809572013-02-22 15:31:20 -0800464 devStore = new DeviceStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800465 devStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800466
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700467 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800468 swStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800469
Pankaj Berde00e90882013-06-10 21:25:05 -0700470 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800471 linkStore.init(dbStore, conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800472
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700473 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800474
Pankaj Berdeda809572013-02-22 15:31:20 -0800475 }
476
477 @Override
478 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700479 Map<String, String> configMap = context.getConfigParams(this);
480 String cleanupNeeded = configMap.get(CleanupEnabled);
481
Jonathan Hartd857ad62013-12-14 18:08:17 -0800482 //deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700483 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700484 linkDiscovery.addListener(this);
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800485
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700486 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700487 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800488 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Yuta HIGUCHI3ab1fd02013-12-17 11:01:02 -0800489 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700490 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700491 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
492 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700493 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700494 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700495
496 //
497 // NOTE: No need to register with the Datagrid Service,
498 // because we don't need to receive any notifications from it.
499 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800500 }
501
502}