blob: 846ebd93ab41c2883ab5c5bf595fda669f930b46 [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.floodlightcontroller.onoslistener;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Map;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -07006import java.util.concurrent.ScheduledExecutorService;
7import java.util.concurrent.TimeUnit;
Pankaj Berdeda809572013-02-22 15:31:20 -08008
Pankaj Berdef08d5ff2013-03-14 17:03:07 -07009import org.openflow.util.HexString;
Pankaj Berdeda809572013-02-22 15:31:20 -080010import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import net.floodlightcontroller.core.IFloodlightProviderService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070014import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
15import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
16import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Pankaj Berdeda809572013-02-22 15:31:20 -080017import net.floodlightcontroller.core.IOFSwitch;
18import net.floodlightcontroller.core.IOFSwitchListener;
19import net.floodlightcontroller.core.ISwitchStorage;
20import net.floodlightcontroller.core.internal.SwitchStorageImpl;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070021import net.floodlightcontroller.core.internal.TopoSwitchServiceImpl;
Pankaj Berdeda809572013-02-22 15:31:20 -080022import net.floodlightcontroller.core.module.FloodlightModuleContext;
23import net.floodlightcontroller.core.module.FloodlightModuleException;
24import net.floodlightcontroller.core.module.IFloodlightModule;
25import net.floodlightcontroller.core.module.IFloodlightService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070026import net.floodlightcontroller.core.util.SingletonTask;
Pankaj Berdeda809572013-02-22 15:31:20 -080027import net.floodlightcontroller.devicemanager.IDevice;
28import net.floodlightcontroller.devicemanager.IDeviceListener;
29import net.floodlightcontroller.devicemanager.IDeviceService;
30import net.floodlightcontroller.devicemanager.IDeviceStorage;
31import net.floodlightcontroller.devicemanager.internal.DeviceStorageImpl;
32import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryListener;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070033import net.floodlightcontroller.threadpool.IThreadPoolService;
34import net.onrc.onos.registry.controller.IControllerRegistryService;
35import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
36import net.onrc.onos.registry.controller.RegistryException;
Pankaj Berde9d6b5072013-04-03 11:51:23 -070037import net.onrc.onos.util.GraphDBConnection;
38import net.onrc.onos.util.LocalTopologyEventListener;
Pankaj Berdeda809572013-02-22 15:31:20 -080039
40public class OnosPublisher implements IDeviceListener, IOFSwitchListener,
41 ILinkDiscoveryListener, IFloodlightModule {
42
43 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070044 protected ISwitchStorage swStore;
Pankaj Berdeda809572013-02-22 15:31:20 -080045 protected static Logger log;
46 protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070047 protected IControllerRegistryService registryService;
Pankaj Berde9d6b5072013-04-03 11:51:23 -070048 protected GraphDBConnection conn;
Pankaj Berdeda809572013-02-22 15:31:20 -080049
50 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070051 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070052 protected IThreadPoolService threadPool;
53
Pankaj Berde99fcee12013-03-18 09:41:53 -070054 protected final int CLEANUP_TASK_INTERVAL = 10; // 10 sec
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070055 protected SingletonTask cleanupTask;
56
57 /**
58 * Cleanup and synch switch state from registry
59 */
60 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
61 @Override
62 public void run() {
63 try {
64 log.debug("Running cleanup thread");
65 switchCleanup();
66 }
67 catch (Exception e) {
68 log.error("Error in cleanup thread", e);
69 } finally {
70 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070071 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070072 }
73 }
74
75 @Override
76 public void controlChanged(long dpid, boolean hasControl) {
77 // TODO Auto-generated method stub
78
79 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070080 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070081 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
82 registryService.releaseControl(dpid);
83 }
84 }
85 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070086
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070087 protected void switchCleanup() {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070088 TopoSwitchServiceImpl impl = new TopoSwitchServiceImpl();
89 Iterable<ISwitchObject> switches = impl.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -070090
91 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070092 // For each switch check if a controller exists in controller registry
93 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -070094 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070095 try {
96 long dpid = HexString.toLong(sw.getDPID());
97 String controller = registryService.getControllerForSwitch(dpid);
98 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070099 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700100 registryService.requestControl(dpid, new SwitchCleanup());
101 } else {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700102 log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700103 }
104 } catch (NumberFormatException e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700108 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700109 e.printStackTrace();
110 }
111 }
112 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800113
114 @Override
115 public void linkDiscoveryUpdate(LDUpdate update) {
116 // TODO Auto-generated method stub
117
118 }
119
120 @Override
121 public void addedSwitch(IOFSwitch sw) {
122 // TODO Auto-generated method stub
123
124 }
125
126 @Override
127 public void removedSwitch(IOFSwitch sw) {
128 // TODO Auto-generated method stub
129
130 }
131
132 @Override
133 public void switchPortChanged(Long switchId) {
134 // TODO Auto-generated method stub
135
136 }
137
138 @Override
139 public String getName() {
140 return "OnosPublisher";
141 }
142
143 @Override
144 public void deviceAdded(IDevice device) {
145 // TODO Auto-generated method stub
146 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
147 devStore.addDevice(device);
148 }
149
150 @Override
151 public void deviceRemoved(IDevice device) {
152 // TODO Auto-generated method stub
153
154 }
155
156 @Override
157 public void deviceMoved(IDevice device) {
158 // TODO Auto-generated method stub
159 devStore.changeDeviceAttachments(device);
160
161 }
162
163 @Override
164 public void deviceIPV4AddrChanged(IDevice device) {
165 // TODO Auto-generated method stub
166 devStore.changeDeviceIPv4Address(device);
167
168 }
169
170 @Override
171 public void deviceVlanChanged(IDevice device) {
172 // TODO Auto-generated method stub
173 }
174
175
176 @Override
177 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
178 // TODO Auto-generated method stub
179 return null;
180 }
181
182 @Override
183 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
184 // TODO Auto-generated method stub
185 return null;
186 }
187
188 @Override
189 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
190 Collection<Class<? extends IFloodlightService>> l =
191 new ArrayList<Class<? extends IFloodlightService>>();
192 l.add(IFloodlightProviderService.class);
193 l.add(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700194 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800195 return l;
196 }
197
198 @Override
199 public void init(FloodlightModuleContext context)
200 throws FloodlightModuleException {
201 // TODO Auto-generated method stub
202 Map<String, String> configMap = context.getConfigParams(this);
203 String conf = configMap.get(DBConfigFile);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700204 conn = GraphDBConnection.getInstance(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800205
206 log = LoggerFactory.getLogger(OnosPublisher.class);
207 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700208 threadPool = context.getServiceImpl(IThreadPoolService.class);
209 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800210
Pankaj Berdeda809572013-02-22 15:31:20 -0800211 devStore = new DeviceStorageImpl();
212 devStore.init(conf);
213
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700214 swStore = new SwitchStorageImpl();
215 swStore.init(conf);
216
Pankaj Berdeda809572013-02-22 15:31:20 -0800217 log.debug("Initializing OnosPublisher module with {}", conf);
218
219 }
220
221 @Override
222 public void startUp(FloodlightModuleContext context) {
223 // TODO Auto-generated method stub
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700224 Map<String, String> configMap = context.getConfigParams(this);
225 String cleanupNeeded = configMap.get(CleanupEnabled);
226
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700227 deviceService.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700228
229 log.debug("Adding EventListener");
230 conn.addEventListener(new LocalTopologyEventListener(conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700231 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700232 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700233 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
234 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700235 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700236 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800237 }
238
239}