blob: 79ed3eda2f09ddc1f67c4551b2806a3c31e8c954 [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");
yoshi55e1e8f2013-12-05 11:10:38 -080091 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070092 switchCleanup();
93 }
94 catch (Exception e) {
95 log.error("Error in cleanup thread", e);
96 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070097 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070098 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070099 TimeUnit.SECONDS);
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);
119
120 // 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 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700149 }
150 }
151 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700152
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700153 protected void switchCleanup() {
yoshi8b972bc2013-12-05 17:10:04 -0800154 //op.close();
Toshio Koide70ba38b2013-06-13 14:05:05 -0700155 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700156
157 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
181 @Override
182 public void linkDiscoveryUpdate(LDUpdate update) {
Pankaj Berde00e90882013-06-10 21:25:05 -0700183 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200184 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700185
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200186 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700187 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200188 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700189
190 if (linkStore.deleteLink(lt)) {
191 // TODO publish DELETE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700192 TopologyElement topologyElement =
193 new TopologyElement(update.getSrc(),
194 update.getSrcPort(),
195 update.getDst(),
196 update.getDstPort());
197 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700198 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700199 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700200 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200201 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700202
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700203 LinkInfo linfo = linkStore.getLinkInfo(lt);
204 // TODO update "linfo" using portState derived using "update"
Naoki Shiota987a5722013-10-23 11:59:36 -0700205 if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) {
206 // TODO publish UPDATE_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700207 //
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);
Naoki Shiota987a5722013-10-23 11:59:36 -0700217 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700218 break;
219 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200220 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700221
222 if (linkStore.addLink(lt)) {
223 // TODO publish ADD_LINK event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700224 TopologyElement topologyElement =
225 new TopologyElement(update.getSrc(),
226 update.getSrcPort(),
227 update.getDst(),
228 update.getDstPort());
229 datagridService.notificationSendTopologyElementAdded(topologyElement);
Naoki Shiota987a5722013-10-23 11:59:36 -0700230 }
Pankaj Berde00e90882013-06-10 21:25:05 -0700231 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700232 default:
233 break;
234 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800235
236 }
237
238 @Override
239 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700240 if (registryService.hasControl(sw.getId())) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 if (swStore.addSwitch(sw)) {
242 // TODO publish ADD_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700243 TopologyElement topologyElement =
244 new TopologyElement(sw.getId());
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800245 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pankaj Berdeda809572013-02-22 15:31:20 -0800246
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800247 // Publish: add the ports
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700248 // TODO: Add only ports that are UP?
249 for (OFPhysicalPort port : sw.getPorts()) {
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800250 TopologyElement topologyElementPort =
251 new TopologyElement(sw.getId(),
252 port.getPortNumber());
253 datagridService.notificationSendTopologyElementAdded(topologyElementPort);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700254 }
Pavlin Radoslavov649c97d2013-11-04 16:00:23 -0800255
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700256 // Add all links that might be connected already
257 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
258 // Add all reverse links as well
259 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
260 links.addAll(reverseLinks);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800261
262 // Publish: add the links
Pavlin Radoslavov0fe70022013-11-02 16:13:12 -0700263 for (Link link : links) {
264 TopologyElement topologyElementLink =
265 new TopologyElement(link.getSrc(),
266 link.getSrcPort(),
267 link.getDst(),
268 link.getDstPort());
269 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
270 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700271 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800272 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800273 }
274
275 @Override
276 public void removedSwitch(IOFSwitch sw) {
Jonathan Hartadc63892013-11-08 14:03:55 -0800277 /*
Naoki Shiota987a5722013-10-23 11:59:36 -0700278 if (registryService.hasControl(sw.getId())) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800279 // Get the affected ports
280 List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId()));
281 // Get the affected links
282 List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId()));
283 // Get the affected reverse links
284 List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId()));
285 links.addAll(reverseLinks);
Pankaj Berdeda809572013-02-22 15:31:20 -0800286
Naoki Shiota987a5722013-10-23 11:59:36 -0700287 if (swStore.deleteSwitch(sw.getStringId())) {
288 // TODO publish DELETE_SWITCH event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700289 TopologyElement topologyElement =
290 new TopologyElement(sw.getId());
291 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800292
293 // Publish: remove the affected ports
294 for (Short port : ports) {
295 TopologyElement topologyElementPort =
296 new TopologyElement(sw.getId(), port);
297 datagridService.notificationSendTopologyElementRemoved(topologyElementPort);
298 }
299 // Publish: remove the affected links
300 for (Link link : links) {
301 TopologyElement topologyElementLink =
302 new TopologyElement(link.getSrc(),
303 link.getSrcPort(),
304 link.getDst(),
305 link.getDstPort());
306 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
307 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700308 }
309 }
Jonathan Hartadc63892013-11-08 14:03:55 -0800310 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800311 }
312
313 @Override
314 public void switchPortChanged(Long switchId) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700315 // NOTE: Event not needed here. This callback always coincide with add/remove callback.
Pankaj Berdeda809572013-02-22 15:31:20 -0800316 }
317
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700318
319 @Override
320 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700321 if (swStore.addPort(HexString.toHexString(switchId), port)) {
322 // TODO publish ADD_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700323 TopologyElement topologyElement =
324 new TopologyElement(switchId, port.getPortNumber());
325 datagridService.notificationSendTopologyElementAdded(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800326
327 // Add all links that might be connected already
328 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
329 // Add all reverse links as well
330 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
331 links.addAll(reverseLinks);
332
333 // Publish: add the links
334 for (Link link : links) {
335 TopologyElement topologyElementLink =
336 new TopologyElement(link.getSrc(),
337 link.getSrcPort(),
338 link.getDst(),
339 link.getDstPort());
340 datagridService.notificationSendTopologyElementAdded(topologyElementLink);
341 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700342 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700343 }
344
345 @Override
346 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800347 // Remove all links that might be connected already
348 List<Link> links = linkStore.getLinks(switchId, port.getPortNumber());
349 // Remove all reverse links as well
350 List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber());
351 links.addAll(reverseLinks);
352
Naoki Shiota987a5722013-10-23 11:59:36 -0700353 if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) {
354 // TODO publish DELETE_PORT event here
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700355 TopologyElement topologyElement =
356 new TopologyElement(switchId, port.getPortNumber());
357 datagridService.notificationSendTopologyElementRemoved(topologyElement);
Pavlin Radoslavovfb157282013-11-05 08:40:13 -0800358
359 // Publish: remove the links
360 for (Link link : links) {
361 TopologyElement topologyElementLink =
362 new TopologyElement(link.getSrc(),
363 link.getSrcPort(),
364 link.getDst(),
365 link.getDstPort());
366 datagridService.notificationSendTopologyElementRemoved(topologyElementLink);
367 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700368 }
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700369 }
370
Pankaj Berdeda809572013-02-22 15:31:20 -0800371 @Override
372 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700373 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800374 }
375
376 @Override
377 public void deviceAdded(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800378 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
379 devStore.addDevice(device);
Jonathan Hartd3003252013-11-15 09:44:46 -0800380 for (int intIpv4Address : device.getIPv4Addresses()) {
381 datagridService.sendArpRequest(
382 ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address)));
383 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800384 }
385
386 @Override
387 public void deviceRemoved(IDevice device) {
388 // TODO Auto-generated method stub
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700389 devStore.removeDevice(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800390 }
391
392 @Override
393 public void deviceMoved(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800394 devStore.changeDeviceAttachments(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800395 }
396
397 @Override
398 public void deviceIPV4AddrChanged(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800399 devStore.changeDeviceIPv4Address(device);
Pankaj Berdeda809572013-02-22 15:31:20 -0800400 }
401
402 @Override
403 public void deviceVlanChanged(IDevice device) {
404 // TODO Auto-generated method stub
405 }
406
407
408 @Override
409 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700410 Collection<Class<? extends IFloodlightService>> l =
411 new ArrayList<Class<? extends IFloodlightService>>();
412 l.add(INetworkGraphService.class);
413 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800414 }
415
416 @Override
417 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700418 Map<Class<? extends IFloodlightService>,
419 IFloodlightService> m =
420 new HashMap<Class<? extends IFloodlightService>,
421 IFloodlightService>();
422 m.put(INetworkGraphService.class, this);
423 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800424 }
425
426 @Override
427 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
428 Collection<Class<? extends IFloodlightService>> l =
429 new ArrayList<Class<? extends IFloodlightService>>();
430 l.add(IFloodlightProviderService.class);
431 l.add(IDeviceService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700432 l.add(IDatagridService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700433 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800434 return l;
435 }
436
437 @Override
438 public void init(FloodlightModuleContext context)
439 throws FloodlightModuleException {
Pankaj Berdeda809572013-02-22 15:31:20 -0800440 Map<String, String> configMap = context.getConfigParams(this);
441 String conf = configMap.get(DBConfigFile);
yoshi2fd4c7e2013-11-22 15:47:55 -0800442 String dbStore = configMap.get(GraphDBStore);
yoshia0839b52013-11-25 16:42:10 -0800443 System.out.println("conf" + conf + "dbStore" + dbStore);
yoshid38cd312013-12-02 19:54:44 -0800444 op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
445 //op = GraphDBManager.getDBOperation(dbStore, conf);
yoshia0839b52013-11-25 16:42:10 -0800446 if (op == null) {
447 System.out.println("publisher op is null");
448 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800449
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700450 floodlightProvider =
451 context.getServiceImpl(IFloodlightProviderService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800452 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700453 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700454 threadPool = context.getServiceImpl(IThreadPoolService.class);
455 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700456 datagridService = context.getServiceImpl(IDatagridService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800457
Pankaj Berdeda809572013-02-22 15:31:20 -0800458 devStore = new DeviceStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800459 devStore.init(dbStore, conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800460
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700461 swStore = new SwitchStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800462 swStore.init(dbStore, conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700463
464 linkStore = new LinkStorageImpl();
yoshi2fd4c7e2013-11-22 15:47:55 -0800465 linkStore.init(dbStore, conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700466
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700467 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800468
469 }
470
471 @Override
472 public void startUp(FloodlightModuleContext context) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700473 Map<String, String> configMap = context.getConfigParams(this);
474 String cleanupNeeded = configMap.get(CleanupEnabled);
475
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700476 deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700477 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700478 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700479
480 log.debug("Adding EventListener");
yoshi34bcb3e2013-11-25 16:49:25 -0800481 System.out.println("start Up op " + op);
Toshio Koide70ba38b2013-06-13 14:05:05 -0700482 IDBConnection conn = op.getDBConnection();
yoshi2fd4c7e2013-11-22 15:47:55 -0800483 conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700484 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700485 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700486 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
487 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700488 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700489 }
Pavlin Radoslavov8442e492013-10-25 21:54:13 -0700490
491 //
492 // NOTE: No need to register with the Datagrid Service,
493 // because we don't need to receive any notifications from it.
494 //
Pankaj Berdeda809572013-02-22 15:31:20 -0800495 }
496
497}