blob: 852cbb8071d94c38205bb54cd6d41081b5f04c6c [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;
Pankaj Berdeda809572013-02-22 15:31:20 -08006import java.util.Map;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -07007import java.util.concurrent.ScheduledExecutorService;
8import java.util.concurrent.TimeUnit;
Pankaj Berdeda809572013-02-22 15:31:20 -08009
Pankaj Berde465ac7c2013-05-23 13:47:49 -070010import org.openflow.protocol.OFPhysicalPort;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070011import org.openflow.util.HexString;
Pankaj Berdeda809572013-02-22 15:31:20 -080012import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
15import net.floodlightcontroller.core.IFloodlightProviderService;
16import net.floodlightcontroller.core.IOFSwitch;
17import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080018import net.floodlightcontroller.core.module.FloodlightModuleContext;
19import net.floodlightcontroller.core.module.FloodlightModuleException;
20import net.floodlightcontroller.core.module.IFloodlightModule;
21import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070022import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080023import net.floodlightcontroller.devicemanager.IDevice;
24import net.floodlightcontroller.devicemanager.IDeviceListener;
25import net.floodlightcontroller.devicemanager.IDeviceService;
Pankaj Berde00e90882013-06-10 21:25:05 -070026import net.floodlightcontroller.routing.Link;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070027import net.floodlightcontroller.threadpool.IThreadPoolService;
Pankaj Berde38646d62013-06-21 11:34:04 -070028import net.onrc.onos.graph.GraphDBConnection;
29import net.onrc.onos.graph.GraphDBOperation;
30import net.onrc.onos.graph.IDBConnection;
31import net.onrc.onos.graph.LocalTopologyEventListener;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070032import net.onrc.onos.ofcontroller.core.IDeviceStorage;
33import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta36cf0762013-06-14 14:25:38 -070034import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070035import net.onrc.onos.ofcontroller.core.ISwitchStorage;
36import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070037import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
38import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070039import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
40import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070041import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070042import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener;
43import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070044import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070045import net.onrc.onos.registry.controller.IControllerRegistryService;
46import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
47import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berdeda809572013-02-22 15:31:20 -080048
Pavlin Radoslavov28069402013-10-18 18:43:11 -070049public class NetworkGraphPublisher implements IDeviceListener,
50 IOFSwitchListener,
51 IOFSwitchPortListener,
52 ILinkDiscoveryListener,
53 IFloodlightModule,
54 INetworkGraphService {
Pankaj Berdeda809572013-02-22 15:31:20 -080055
56 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070057 protected ISwitchStorage swStore;
Pankaj Berde00e90882013-06-10 21:25:05 -070058 protected ILinkStorage linkStore;
Pankaj Berdeda809572013-02-22 15:31:20 -080059 protected static Logger log;
60 protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070061 protected IControllerRegistryService registryService;
Toshio Koide70ba38b2013-06-13 14:05:05 -070062 protected GraphDBOperation op;
Pankaj Berdeda809572013-02-22 15:31:20 -080063
64 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070065 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070066 protected IThreadPoolService threadPool;
Pankaj Berde465ac7c2013-05-23 13:47:49 -070067 protected IFloodlightProviderService floodlightProvider;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070068
Pankaj Berde62016142013-04-09 15:35:50 -070069 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070070 protected SingletonTask cleanupTask;
Pankaj Berde00e90882013-06-10 21:25:05 -070071 protected ILinkDiscoveryService linkDiscovery;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070072
73 /**
74 * Cleanup and synch switch state from registry
75 */
76 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
77 @Override
78 public void run() {
79 try {
80 log.debug("Running cleanup thread");
81 switchCleanup();
82 }
83 catch (Exception e) {
84 log.error("Error in cleanup thread", e);
85 } finally {
Toshio Koide70ba38b2013-06-13 14:05:05 -070086 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070087 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070088 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070089 }
90 }
91
92 @Override
93 public void controlChanged(long dpid, boolean hasControl) {
94 // TODO Auto-generated method stub
95
96 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070097 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070098 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
99 registryService.releaseControl(dpid);
100 }
101 }
102 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700103
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700104 protected void switchCleanup() {
Toshio Koide70ba38b2013-06-13 14:05:05 -0700105 op.close();
106 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -0700107
108 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700109 // For each switch check if a controller exists in controller registry
110 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -0700111 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700112 try {
113 long dpid = HexString.toLong(sw.getDPID());
114 String controller = registryService.getControllerForSwitch(dpid);
115 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700116 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700117 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700118 //} else {
119 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700120 }
121 } catch (NumberFormatException e) {
122 // TODO Auto-generated catch block
123 e.printStackTrace();
124 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700125 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700126 e.printStackTrace();
127 }
128 }
Toshio Koide70ba38b2013-06-13 14:05:05 -0700129 op.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700130 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800131
132 @Override
133 public void linkDiscoveryUpdate(LDUpdate update) {
134 // TODO Auto-generated method stub
Pankaj Berde00e90882013-06-10 21:25:05 -0700135 Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort());
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200136 //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700137
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200138 switch (update.getOperation()) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700139 case LINK_REMOVED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200140 log.debug("LinkDiscoveryUpdate(): Removing link {}", lt);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700141 linkStore.deleteLink(lt);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700142 // TODO: Move network map link removal here
143 // reconcile paths here
144// IPortObject srcPort = conn.utils().searchPort(conn, HexString.toHexString(update.getSrc()), update.getSrcPort());
145 break;
Pankaj Berde00e90882013-06-10 21:25:05 -0700146 case LINK_UPDATED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200147 log.debug("LinkDiscoveryUpdate(): Updating link {}", lt);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700148 LinkInfo linfo = linkStore.getLinkInfo(lt);
149 // TODO update "linfo" using portState derived using "update"
150 linkStore.update(lt, linfo, DM_OPERATION.UPDATE);
Pankaj Berde00e90882013-06-10 21:25:05 -0700151 break;
152 case LINK_ADDED:
Jonathan Harte7c2d2f2013-07-27 18:08:34 +1200153 log.debug("LinkDiscoveryUpdate(): Adding link {}", lt);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700154 linkStore.addLink(lt);
Pankaj Berde00e90882013-06-10 21:25:05 -0700155 break;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700156
157 default:
158 break;
159 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800160
161 }
162
163 @Override
164 public void addedSwitch(IOFSwitch sw) {
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700165
166 if (registryService.hasControl(sw.getId())) {
Pankaj Berdebbd38612013-06-22 05:59:12 -0700167 swStore.addSwitch(sw);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700168 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800169
170 }
171
172 @Override
173 public void removedSwitch(IOFSwitch sw) {
174 // TODO Auto-generated method stub
175
176 }
177
178 @Override
179 public void switchPortChanged(Long switchId) {
180 // TODO Auto-generated method stub
181
182 }
183
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700184
185 @Override
186 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
187 // TODO Auto-generated method stub
188 swStore.addPort(HexString.toHexString(switchId), port);
189 }
190
191 @Override
192 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
193 // TODO Auto-generated method stub
194 swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber());
195 }
196
Pankaj Berdeda809572013-02-22 15:31:20 -0800197 @Override
198 public String getName() {
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700199 return "NetworkGraphPublisher";
Pankaj Berdeda809572013-02-22 15:31:20 -0800200 }
201
202 @Override
203 public void deviceAdded(IDevice device) {
204 // TODO Auto-generated method stub
205 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
206 devStore.addDevice(device);
207 }
208
209 @Override
210 public void deviceRemoved(IDevice device) {
211 // TODO Auto-generated method stub
212
213 }
214
215 @Override
216 public void deviceMoved(IDevice device) {
217 // TODO Auto-generated method stub
218 devStore.changeDeviceAttachments(device);
219
220 }
221
222 @Override
223 public void deviceIPV4AddrChanged(IDevice device) {
224 // TODO Auto-generated method stub
225 devStore.changeDeviceIPv4Address(device);
226
227 }
228
229 @Override
230 public void deviceVlanChanged(IDevice device) {
231 // TODO Auto-generated method stub
232 }
233
234
235 @Override
236 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700237 Collection<Class<? extends IFloodlightService>> l =
238 new ArrayList<Class<? extends IFloodlightService>>();
239 l.add(INetworkGraphService.class);
240 return l;
Pankaj Berdeda809572013-02-22 15:31:20 -0800241 }
242
243 @Override
244 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Pavlin Radoslavov28069402013-10-18 18:43:11 -0700245 Map<Class<? extends IFloodlightService>,
246 IFloodlightService> m =
247 new HashMap<Class<? extends IFloodlightService>,
248 IFloodlightService>();
249 m.put(INetworkGraphService.class, this);
250 return m;
Pankaj Berdeda809572013-02-22 15:31:20 -0800251 }
252
253 @Override
254 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
255 Collection<Class<? extends IFloodlightService>> l =
256 new ArrayList<Class<? extends IFloodlightService>>();
257 l.add(IFloodlightProviderService.class);
258 l.add(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700259 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800260 return l;
261 }
262
263 @Override
264 public void init(FloodlightModuleContext context)
265 throws FloodlightModuleException {
266 // TODO Auto-generated method stub
267 Map<String, String> configMap = context.getConfigParams(this);
268 String conf = configMap.get(DBConfigFile);
Toshio Koidebfe9b922013-06-18 10:56:05 -0700269 op = new GraphDBOperation(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800270
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700271 log = LoggerFactory.getLogger(NetworkGraphPublisher.class);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700272 floodlightProvider =
273 context.getServiceImpl(IFloodlightProviderService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800274 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berde00e90882013-06-10 21:25:05 -0700275 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700276 threadPool = context.getServiceImpl(IThreadPoolService.class);
277 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800278
Pankaj Berdeda809572013-02-22 15:31:20 -0800279 devStore = new DeviceStorageImpl();
280 devStore.init(conf);
281
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700282 swStore = new SwitchStorageImpl();
283 swStore.init(conf);
Pankaj Berde00e90882013-06-10 21:25:05 -0700284
285 linkStore = new LinkStorageImpl();
286 linkStore.init(conf);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700287
HIGUCHI Yuta155cfc52013-06-14 15:54:16 -0700288 log.debug("Initializing NetworkGraphPublisher module with {}", conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800289
290 }
291
292 @Override
293 public void startUp(FloodlightModuleContext context) {
294 // TODO Auto-generated method stub
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700295 Map<String, String> configMap = context.getConfigParams(this);
296 String cleanupNeeded = configMap.get(CleanupEnabled);
297
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700298 deviceService.addListener(this);
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700299 floodlightProvider.addOFSwitchListener(this);
Pankaj Berde00e90882013-06-10 21:25:05 -0700300 linkDiscovery.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700301
302 log.debug("Adding EventListener");
Toshio Koide70ba38b2013-06-13 14:05:05 -0700303 IDBConnection conn = op.getDBConnection();
304 conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700305 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700306 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700307 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
308 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700309 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700310 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800311 }
312
313}