blob: 0f6aef36a4da1ed7737408d6870381e3b00f9370 [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 Berde465ac7c2013-05-23 13:47:49 -070011import org.openflow.protocol.OFPhysicalPort;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070012import org.openflow.util.HexString;
Pankaj Berdeda809572013-02-22 15:31:20 -080013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Jonathan Hartd3003252013-11-15 09:44:46 -080016import com.google.common.net.InetAddresses;
17
Pankaj Berdeda809572013-02-22 15:31:20 -080018import net.floodlightcontroller.core.IFloodlightProviderService;
19import net.floodlightcontroller.core.IOFSwitch;
20import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080021import net.floodlightcontroller.core.module.FloodlightModuleContext;
22import net.floodlightcontroller.core.module.FloodlightModuleException;
23import net.floodlightcontroller.core.module.IFloodlightModule;
24import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070025import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080026import net.floodlightcontroller.devicemanager.IDevice;
27import net.floodlightcontroller.devicemanager.IDeviceListener;
28import net.floodlightcontroller.devicemanager.IDeviceService;
Pankaj Berde00e90882013-06-10 21:25:05 -070029import net.floodlightcontroller.routing.Link;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070030import net.floodlightcontroller.threadpool.IThreadPoolService;
yoshi2fd4c7e2013-11-22 15:47:55 -080031import net.onrc.onos.graph.DBOperation;
32import net.onrc.onos.graph.DBConnection;
33import net.onrc.onos.graph.GraphDBManager;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070034import net.onrc.onos.datagrid.IDatagridService;
Pankaj Berde38646d62013-06-21 11:34:04 -070035import net.onrc.onos.graph.IDBConnection;
36import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070037import net.onrc.onos.ofcontroller.core.IDeviceStorage;
38import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta36cf0762013-06-14 14:25:38 -070039import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070040import net.onrc.onos.ofcontroller.core.ISwitchStorage;
41import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070042import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
43import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070044import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
45import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070046import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070047import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
48import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070049import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartd3003252013-11-15 09:44:46 -080050import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
Pavlin Radoslavov8442e492013-10-25 21:54:13 -070051import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070052import net.onrc.onos.registry.controller.IControllerRegistryService;
53import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
54import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080055
Pavlin Radoslavov28069402013-10-18 18:43:11 -070056public class NetworkGraphPublisher implements IDeviceListener,
57 IOFSwitchListener,
58 IOFSwitchPortListener,
59 ILinkDiscoveryListener,
60 IFloodlightModule,
61 INetworkGraphService {
Pankaj Berdeda809572013-02-22 15:31:20 -080062
63 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);
Pankaj Berdeda809572013-02-22 15:31:20 -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;
Pankaj Berdeda809572013-02-22 15:31:20 -080070
71 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;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070076
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;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070082
83 /**
84 * Cleanup and synch switch state from registry
85 */
86 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
87 @Override
88 public void run() {
89 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);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070099 }
100 }
101
102 @Override
103 public void controlChanged(long dpid, boolean hasControl) {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700104 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700105 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Naoki Shiota987a5722013-10-23 11:59:36 -0700106 try {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800107 // Get the affected ports
108 List<Short> ports = swStore.getPorts(HexString.toHexString(dpid));
109 // Get the affected links
110 List<Link> links = linkStore.getLinks(HexString.toHexString(dpid));
111 // Get the affected reverse links
112 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid));
113 links.addAll(reverseLinks);
114
Jonathan Hartadc63892013-11-08 14:03:55 -0800115 //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) {
116 if (swStore.deactivateSwitch(HexString.toHexString(dpid))) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700117 registryService.releaseControl(dpid);
118
119 // TODO publish UPDATE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700120 //
121 // NOTE: Here we explicitly send
122 // notification to remove the
123 // switch, because it is inactive
124 //
125 TopologyElement topologyElement =
126 new TopologyElement(dpid);
127 datagridService.notificationSendTopologyElementRemoved(topologyElement);
128
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800129 // Publish: remove the affected ports
130 for (Short port : ports) {
131 TopologyElement topologyElementPort =
132 new TopologyElement(dpid, port);
133 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
134 }
135 // Publish: remove the affected links
136 for (Link link : links) {
137 TopologyElement topologyElementLink =
138 new TopologyElement(link.getSrc(),
139 link.getSrcPort(),
140 link.getDst(),
141 link.getDstPort());
142 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
143 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700144 }
145 } catch (Exception e) {
146 log.error("Error in SwitchCleanup:controlChanged ", e);
147 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700148 }
149 }
150 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700151
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700152 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700153 op.close();
154 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700155
156 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700157 // For each switch check if a controller exists in controller registry
158 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700159 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700160 try {
161 long dpid = HexString.toLong(sw.getDPID());
162 String controller = registryService.getControllerForSwitch(dpid);
163 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700164 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700165 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700166 //} else {
167 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700168 }
169 } catch (NumberFormatException e) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700170 log.debug("Caught NumberFormatException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700171 e.printStackTrace();
172 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700173 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700174 e.printStackTrace();
Naoki Shiota987a5722013-10-23 11:59:36 -0700175 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700176 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700177 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700178 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800179
180 @Override
181 public void linkDiscoveryUpdate(LDUpdate update) {
Pankaj Berde00e90882013-06-10 21:25:05 -0700182 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200183 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700184
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200185 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700186 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200187 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700188
189 if (linkStore.deleteLink(lt)) {
190 // TODO publish DELETE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700191 TopologyElement topologyElement =
192 new TopologyElement(update.getSrc(),
193 update.getSrcPort(),
194 update.getDst(),
195 update.getDstPort());
196 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700197 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700198 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700199 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200200 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700201
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700202 LinkInfo linfo = linkStore.getLinkInfo(lt);
203 // TODO update "linfo" using portState derived using "update"
Naoki Shiota987a5722013-10-23 11:59:36 -0700204 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
205 // TODO publish UPDATE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700206 //
207 // TODO NOTE: Here we assume that updated
208 // link is UP.
209 //
210 TopologyElement topologyElement =
211 new TopologyElement(update.getSrc(),
212 update.getSrcPort(),
213 update.getDst(),
214 update.getDstPort());
215 datagridService.notificationSendTopologyElementUpdated(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700216 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700217 break;
218 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200219 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700220
221 if (linkStore.addLink(lt)) {
222 // TODO publish ADD_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700223 TopologyElement topologyElement =
224 new TopologyElement(update.getSrc(),
225 update.getSrcPort(),
226 update.getDst(),
227 update.getDstPort());
228 datagridService.notificationSendTopologyElementAdded(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700229 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700230 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700231 default:
232 break;
233 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800234
235 }
236
237 @Override
238 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700239 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700240 if (swStore.addSwitch(sw)) {
241 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700242 TopologyElement topologyElement =
243 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800244 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800245
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800246 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700247 // TODO: Add only ports that are UP?
248 for (OFPhysicalPort port : sw.getPorts()) {
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800249 TopologyElement topologyElementPort =
250 new TopologyElement(sw.getId(),
251 port.getPortNumber());
252 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700253 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800254
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700255 // Add all links that might be connected already
256 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
257 // Add all reverse links as well
258 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
259 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800260
261 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700262 for (Link link : links) {
263 TopologyElement topologyElementLink =
264 new TopologyElement(link.getSrc(),
265 link.getSrcPort(),
266 link.getDst(),
267 link.getDstPort());
268 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
269 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700270 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800271 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800272 }
273
274 @Override
275 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800276 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700277 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800278 // Get the affected ports
279 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
280 // Get the affected links
281 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
282 // Get the affected reverse links
283 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
284 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800285
Naoki Shiota987a5722013-10-23 11:59:36 -0700286 if (swStore.deleteSwitch(sw.getStringId())) {
287 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700288 TopologyElement topologyElement =
289 new TopologyElement(sw.getId());
290 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800291
292 // Publish: remove the affected ports
293 for (Short port : ports) {
294 TopologyElement topologyElementPort =
295 new TopologyElement(sw.getId(), port);
296 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
297 }
298 // Publish: remove the affected links
299 for (Link link : links) {
300 TopologyElement topologyElementLink =
301 new TopologyElement(link.getSrc(),
302 link.getSrcPort(),
303 link.getDst(),
304 link.getDstPort());
305 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
306 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700307 }
308 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800309 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800310 }
311
312 @Override
313 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700314 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800315 }
316
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700317
318 @Override
319 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700320 if (swStore.addPort(HexString.toHexString(switchId), port)) {
321 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700322 TopologyElement topologyElement =
323 new TopologyElement(switchId, port.getPortNumber());
324 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800325
326 // Add all links that might be connected already
327 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
328 // Add all reverse links as well
329 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
330 links.addAll(reverseLinks);
331
332 // Publish: add the links
333 for (Link link : links) {
334 TopologyElement topologyElementLink =
335 new TopologyElement(link.getSrc(),
336 link.getSrcPort(),
337 link.getDst(),
338 link.getDstPort());
339 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
340 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700341 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700342 }
343
344 @Override
345 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800346 // Remove all links that might be connected already
347 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
348 // Remove all reverse links as well
349 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
350 links.addAll(reverseLinks);
351
Naoki Shiota987a5722013-10-23 11:59:36 -0700352 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
353 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700354 TopologyElement topologyElement =
355 new TopologyElement(switchId, port.getPortNumber());
356 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800357
358 // Publish: remove the links
359 for (Link link : links) {
360 TopologyElement topologyElementLink =
361 new TopologyElement(link.getSrc(),
362 link.getSrcPort(),
363 link.getDst(),
364 link.getDstPort());
365 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
366 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700367 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700368 }
369
Pankaj Berdeda809572013-02-22 15:31:20 -0800370 @Override
371 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700372 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800373 }
374
375 @Override
376 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800377 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
378 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800379 for (int intIpv4Address : device.getIPv4Addresses()) {
380 datagridService.sendArpRequest(
381 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
382 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800383 }
384
385 @Override
386 public void deviceRemoved(IDevice device) {
387 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700388 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800389 }
390
391 @Override
392 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800393 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800394 }
395
396 @Override
397 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800398 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800399 }
400
401 @Override
402 public void deviceVlanChanged(IDevice device) {
403 // TODO Auto-generated method stub
404 }
405
406
407 @Override
408 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700409 Collection<Class<? extends IFloodlightService>> l =
410 new ArrayList<Class<? extends IFloodlightService>>();
411 l.add(INetworkGraphService.class);
412 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800413 }
414
415 @Override
416 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700417 Map<Class<? extends IFloodlightService>,
418 IFloodlightService> m =
419 new HashMap<Class<? extends IFloodlightService>,
420 IFloodlightService>();
421 m.put(INetworkGraphService.class, this);
422 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800423 }
424
425 @Override
426 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
427 Collection<Class<? extends IFloodlightService>> l =
428 new ArrayList<Class<? extends IFloodlightService>>();
429 l.add(IFloodlightProviderService.class);
430 l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700431 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700432 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800433 return l;
434 }
435
436 @Override
437 public void init(FloodlightModuleContext context)
438 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800439 Map<String, String> configMap = context.getConfigParams(this);
440 String conf = configMap.get(DBConfigFile);
yoshi2fd4c7e2013-11-22 15:47:55 -0800441 String dbStore = configMap.get(GraphDBStore);
yoshia0839b52013-11-25 16:42:10 -0800442 System.out.println("conf" + conf + "dbStore" + dbStore);
yoshi2fd4c7e2013-11-22 15:47:55 -0800443 op = GraphDBManager.getDBOperation(dbStore, conf);
yoshia0839b52013-11-25 16:42:10 -0800444 if (op == null) {
445 System.out.println("publisher op is null");
446 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800447
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700448 floodlightProvider =
449 context.getServiceImpl(IFloodlightProviderService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800450 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700451 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700452 threadPool = context.getServiceImpl(IThreadPoolService.class);
453 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700454 datagridService = context.getServiceImpl(IDatagridService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800455
Pankaj Berdeda809572013-02-22 15:31:20 -0800456 devStore = new DeviceStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800457 devStore.init(dbStore, conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800458
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700459 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800460 swStore.init(dbStore, conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700461
462 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800463 linkStore.init(dbStore, conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700464
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700465 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800466
467 }
468
469 @Override
470 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700471 Map<String, String> configMap = context.getConfigParams(this);
472 String cleanupNeeded = configMap.get(CleanupEnabled);
473
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700474 deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700475 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700476 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700477
478 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700479 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800480 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700481 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700482 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700483 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
484 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700485 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700486 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700487
488 //
489 // NOTE: No need to register with the Datagrid Service,
490 // because we don't need to receive any notifications from it.
491 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800492 }
493
494}