blob: 187465fab7f7a810844eba26ba223f1487f8da5f [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
Jonathan Hartd857ad62013-12-14 18:08:17 -080046import org.openflow.protocol.OFPhysicalPort;
47import org.openflow.util.HexString;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51import com.google.common.net.InetAddresses;
52
Pavlin Radoslavov28069402013-10-18 18:43:11 -070053public class NetworkGraphPublisher implements IDeviceListener,
54 IOFSwitchListener,
55 IOFSwitchPortListener,
56 ILinkDiscoveryListener,
57 IFloodlightModule,
58 INetworkGraphService {
Pankaj Berdeda809572013-02-22 15:31:20 -080059
60 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070061 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070062 protected ILinkStorage linkStore;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070063 protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -080064 //protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070065 protected IControllerRegistryService registryService;
Toshio Koide70ba38b2013-06-13 14:05:05 -070066 protected GraphDBOperation op;
Pankaj Berdeda809572013-02-22 15:31:20 -080067
68 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070069 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070070 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070071 protected IFloodlightProviderService floodlightProvider;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070072
Pankaj Berde62016142013-04-09 15:35:50 -070073 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070074 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070075 protected ILinkDiscoveryService linkDiscovery;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070076
77 protected IDatagridService datagridService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070078
79 /**
80 * Cleanup and synch switch state from registry
81 */
82 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
83 @Override
84 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080085 String old = Thread.currentThread().getName();
86 Thread.currentThread().setName("SwitchCleanup " + Thread.currentThread().getId());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070087 try {
88 log.debug("Running cleanup thread");
89 switchCleanup();
90 }
91 catch (Exception e) {
92 log.error("Error in cleanup thread", e);
93 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070094 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070095 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070096 TimeUnit.SECONDS);
Yuta HIGUCHI61509a42013-12-17 10:41:04 -080097 Thread.currentThread().setName(old);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070098 }
99 }
100
101 @Override
102 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700103 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700104 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700105 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800106 // Get the affected ports
107 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
108 // Get the affected links
109 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
110 // Get the affected reverse links
111 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
112 links.addAll(reverseLinks);
113
Jonathan Hartadc63892013-11-08 14:03:55 -0800114 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
115 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700116 registryService.releaseControl(dpid);
117
118 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700119 //
120 // NOTE: Here we explicitly send
121 // notification to remove the
122 // switch, because it is inactive
123 //
124 TopologyElement topologyElement =
125 new TopologyElement(dpid);
126 datagridService.notificationSendTopologyElementRemoved(topologyElement);
127
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800128 // Publish: remove the affected ports
129 for (Short port : ports) {
130 TopologyElement topologyElementPort =
131 new TopologyElement(dpid, port);
132 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
133 }
134 // Publish: remove the affected links
135 for (Link link : links) {
136 TopologyElement topologyElementLink =
137 new TopologyElement(link.getSrc(),
138 link.getSrcPort(),
139 link.getDst(),
140 link.getDstPort());
141 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
142 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700143 }
144 } catch (Exception e) {
145 log.error("Error in SwitchCleanup:controlChanged ", e);
146 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700147 }
148 }
149 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700150
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700151 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700152 op.close();
153 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700154
155 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700156 // For each switch check if a controller exists in controller registry
157 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700158 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700159 try {
160 long dpid = HexString.toLong(sw.getDPID());
161 String controller = registryService.getControllerForSwitch(dpid);
162 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700163 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700164 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700165 //} else {
166 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700167 }
168 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700169 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700170 e.printStackTrace();
171 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700172 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700173 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700174 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700175 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700176 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700177 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800178
179 @Override
180 public void linkDiscoveryUpdate(LDUpdate update) {
Pankaj Berde00e90882013-06-10 21:25:05 -0700181 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200182 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700183
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200184 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700185 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200186 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700187
188 if (linkStore.deleteLink(lt)) {
189 // TODO publish DELETE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700190 TopologyElement topologyElement =
191 new TopologyElement(update.getSrc(),
192 update.getSrcPort(),
193 update.getDst(),
194 update.getDstPort());
195 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700196 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700197 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700198 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200199 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700200
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700201 LinkInfo linfo = linkStore.getLinkInfo(lt);
202 // TODO update "linfo" using portState derived using "update"
Naoki Shiota987a5722013-10-23 11:59:36 -0700203 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
204 // TODO publish UPDATE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700205 //
206 // TODO NOTE: Here we assume that updated
207 // link is UP.
208 //
209 TopologyElement topologyElement =
210 new TopologyElement(update.getSrc(),
211 update.getSrcPort(),
212 update.getDst(),
213 update.getDstPort());
214 datagridService.notificationSendTopologyElementUpdated(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700215 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700216 break;
217 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200218 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700219
220 if (linkStore.addLink(lt)) {
221 // TODO publish ADD_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700222 TopologyElement topologyElement =
223 new TopologyElement(update.getSrc(),
224 update.getSrcPort(),
225 update.getDst(),
226 update.getDstPort());
227 datagridService.notificationSendTopologyElementAdded(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700228 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700229 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700230 default:
231 break;
232 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800233
234 }
235
236 @Override
237 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700238 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700239 if (swStore.addSwitch(sw)) {
240 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700241 TopologyElement topologyElement =
242 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800243 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800244
245 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700246 // TODO: Add only ports that are UP?
247 for (OFPhysicalPort port : sw.getPorts()) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800248 TopologyElement topologyElementPort =
249 new TopologyElement(sw.getId(), port.getPortNumber());
250 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
251
252 // Allow links to be discovered on this port now that it's
253 // in the database
254 linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber());
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700255 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800256
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700257 // Add all links that might be connected already
258 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
259 // Add all reverse links as well
260 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
261 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800262
263 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700264 for (Link link : links) {
265 TopologyElement topologyElementLink =
266 new TopologyElement(link.getSrc(),
267 link.getSrcPort(),
268 link.getDst(),
269 link.getDstPort());
270 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
271 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700272 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700273 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800274 }
275
276 @Override
277 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800278 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700279 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800280 // Get the affected ports
281 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
282 // Get the affected links
283 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
284 // Get the affected reverse links
285 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
286 links.addAll(reverseLinks);
287
Naoki Shiota987a5722013-10-23 11:59:36 -0700288 if (swStore.deleteSwitch(sw.getStringId())) {
289 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700290 TopologyElement topologyElement =
291 new TopologyElement(sw.getId());
292 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800293
294 // Publish: remove the affected ports
295 for (Short port : ports) {
296 TopologyElement topologyElementPort =
297 new TopologyElement(sw.getId(), port);
298 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
299 }
300 // Publish: remove the affected links
301 for (Link link : links) {
302 TopologyElement topologyElementLink =
303 new TopologyElement(link.getSrc(),
304 link.getSrcPort(),
305 link.getDst(),
306 link.getDstPort());
307 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
308 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700309 }
310 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800311 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800312 }
313
314 @Override
315 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800317 }
318
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700319
320 @Override
321 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700322 if (swStore.addPort(HexString.toHexString(switchId), port)) {
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800323 // Allow links to be discovered on this port now that it's
324 // in the database
325 linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber());
326
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700328 TopologyElement topologyElement =
329 new TopologyElement(switchId, port.getPortNumber());
330 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800331
332 // Add all links that might be connected already
333 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
334 // Add all reverse links as well
335 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
336 links.addAll(reverseLinks);
337
338 // Publish: add the links
339 for (Link link : links) {
340 TopologyElement topologyElementLink =
341 new TopologyElement(link.getSrc(),
342 link.getSrcPort(),
343 link.getDst(),
344 link.getDstPort());
345 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
346 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700347 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700348 }
349
350 @Override
351 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800352 // Remove all links that might be connected already
353 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
354 // Remove all reverse links as well
355 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
356 links.addAll(reverseLinks);
357
Naoki Shiota987a5722013-10-23 11:59:36 -0700358 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
359 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700360 TopologyElement topologyElement =
361 new TopologyElement(switchId, port.getPortNumber());
362 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800363
364 // Publish: remove the links
365 for (Link link : links) {
366 TopologyElement topologyElementLink =
367 new TopologyElement(link.getSrc(),
368 link.getSrcPort(),
369 link.getDst(),
370 link.getDstPort());
371 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
372 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700373 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700374 }
375
Pankaj Berdeda809572013-02-22 15:31:20 -0800376 @Override
377 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700378 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800379 }
380
381 @Override
382 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800383 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
384 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800385 for (int intIpv4Address : device.getIPv4Addresses()) {
386 datagridService.sendArpRequest(
387 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
388 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800389 }
390
391 @Override
392 public void deviceRemoved(IDevice device) {
393 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700394 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800395 }
396
397 @Override
398 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800399 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800400 }
401
402 @Override
403 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800404 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800405 }
406
407 @Override
408 public void deviceVlanChanged(IDevice device) {
409 // TODO Auto-generated method stub
410 }
411
412
413 @Override
414 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700415 Collection<Class<? extends IFloodlightService>> l =
416 new ArrayList<Class<? extends IFloodlightService>>();
417 l.add(INetworkGraphService.class);
418 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800419 }
420
421 @Override
422 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700423 Map<Class<? extends IFloodlightService>,
424 IFloodlightService> m =
425 new HashMap<Class<? extends IFloodlightService>,
426 IFloodlightService>();
427 m.put(INetworkGraphService.class, this);
428 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800429 }
430
431 @Override
432 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
433 Collection<Class<? extends IFloodlightService>> l =
434 new ArrayList<Class<? extends IFloodlightService>>();
435 l.add(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800436 //l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700437 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700438 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800439 return l;
440 }
441
442 @Override
443 public void init(FloodlightModuleContext context)
444 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800445 Map<String, String> configMap = context.getConfigParams(this);
446 String conf = configMap.get(DBConfigFile);
Toshio Koidebfe9b922013-06-18 10:56:05 -0700447 op = new GraphDBOperation(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800448
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700449 floodlightProvider =
450 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800451 //deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700452 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700453 threadPool = context.getServiceImpl(IThreadPoolService.class);
454 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700455 datagridService = context.getServiceImpl(IDatagridService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800456
Pankaj Berdeda809572013-02-22 15:31:20 -0800457 devStore = new DeviceStorageImpl();
458 devStore.init(conf);
459
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700460 swStore = new SwitchStorageImpl();
461 swStore.init(conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700462
463 linkStore = new LinkStorageImpl();
464 linkStore.init(conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700465
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700466 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800467
468 }
469
470 @Override
471 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700472 Map<String, String> configMap = context.getConfigParams(this);
473 String cleanupNeeded = configMap.get(CleanupEnabled);
474
Jonathan Hartd857ad62013-12-14 18:08:17 -0800475 //deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700476 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700477 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700478
479 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700480 IDBConnection conn = op.getDBConnection();
481 conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700482 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700483 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700484 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
485 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700486 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700487 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700488
489 //
490 // NOTE: No need to register with the Datagrid Service,
491 // because we don't need to receive any notifications from it.
492 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800493 }
494
495}