blob: 585fc36eab95a9144d07e3fe34fa6147642bc064 [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 Berdeda809572013-02-22 15:31:20 -080037
38public class OnosPublisher implements IDeviceListener, IOFSwitchListener,
39 ILinkDiscoveryListener, IFloodlightModule {
40
41 protected IDeviceStorage devStore;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070042 protected ISwitchStorage swStore;
Pankaj Berdeda809572013-02-22 15:31:20 -080043 protected static Logger log;
44 protected IDeviceService deviceService;
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070045 protected IControllerRegistryService registryService;
Pankaj Berdeda809572013-02-22 15:31:20 -080046
47 protected static final String DBConfigFile = "dbconf";
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -070048 protected static final String CleanupEnabled = "EnableCleanup";
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070049 protected IThreadPoolService threadPool;
50
Pankaj Berde99fcee12013-03-18 09:41:53 -070051 protected final int CLEANUP_TASK_INTERVAL = 10; // 10 sec
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070052 protected SingletonTask cleanupTask;
53
54 /**
55 * Cleanup and synch switch state from registry
56 */
57 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
58 @Override
59 public void run() {
60 try {
61 log.debug("Running cleanup thread");
62 switchCleanup();
63 }
64 catch (Exception e) {
65 log.error("Error in cleanup thread", e);
66 } finally {
67 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070068 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070069 }
70 }
71
72 @Override
73 public void controlChanged(long dpid, boolean hasControl) {
74 // TODO Auto-generated method stub
75
76 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070077 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070078 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
79 registryService.releaseControl(dpid);
80 }
81 }
82 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070083
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070084 protected void switchCleanup() {
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070085 TopoSwitchServiceImpl impl = new TopoSwitchServiceImpl();
86 Iterable<ISwitchObject> switches = impl.getActiveSwitches();
Jonathan Hartf02a0932013-03-18 18:30:48 -070087
88 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070089 // For each switch check if a controller exists in controller registry
90 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -070091 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070092 try {
93 long dpid = HexString.toLong(sw.getDPID());
94 String controller = registryService.getControllerForSwitch(dpid);
95 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070096 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070097 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -070098 //} else {
99 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700100 }
101 } catch (NumberFormatException e) {
102 // TODO Auto-generated catch block
103 e.printStackTrace();
104 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700105 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700106 e.printStackTrace();
107 }
108 }
109 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800110
111 @Override
112 public void linkDiscoveryUpdate(LDUpdate update) {
113 // TODO Auto-generated method stub
114
115 }
116
117 @Override
118 public void addedSwitch(IOFSwitch sw) {
119 // TODO Auto-generated method stub
120
121 }
122
123 @Override
124 public void removedSwitch(IOFSwitch sw) {
125 // TODO Auto-generated method stub
126
127 }
128
129 @Override
130 public void switchPortChanged(Long switchId) {
131 // TODO Auto-generated method stub
132
133 }
134
135 @Override
136 public String getName() {
137 return "OnosPublisher";
138 }
139
140 @Override
141 public void deviceAdded(IDevice device) {
142 // TODO Auto-generated method stub
143 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
144 devStore.addDevice(device);
145 }
146
147 @Override
148 public void deviceRemoved(IDevice device) {
149 // TODO Auto-generated method stub
150
151 }
152
153 @Override
154 public void deviceMoved(IDevice device) {
155 // TODO Auto-generated method stub
156 devStore.changeDeviceAttachments(device);
157
158 }
159
160 @Override
161 public void deviceIPV4AddrChanged(IDevice device) {
162 // TODO Auto-generated method stub
163 devStore.changeDeviceIPv4Address(device);
164
165 }
166
167 @Override
168 public void deviceVlanChanged(IDevice device) {
169 // TODO Auto-generated method stub
170 }
171
172
173 @Override
174 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
175 // TODO Auto-generated method stub
176 return null;
177 }
178
179 @Override
180 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
181 // TODO Auto-generated method stub
182 return null;
183 }
184
185 @Override
186 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
187 Collection<Class<? extends IFloodlightService>> l =
188 new ArrayList<Class<? extends IFloodlightService>>();
189 l.add(IFloodlightProviderService.class);
190 l.add(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700191 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800192 return l;
193 }
194
195 @Override
196 public void init(FloodlightModuleContext context)
197 throws FloodlightModuleException {
198 // TODO Auto-generated method stub
199 Map<String, String> configMap = context.getConfigParams(this);
200 String conf = configMap.get(DBConfigFile);
201
202 log = LoggerFactory.getLogger(OnosPublisher.class);
203 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700204 threadPool = context.getServiceImpl(IThreadPoolService.class);
205 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800206
Pankaj Berdeda809572013-02-22 15:31:20 -0800207 devStore = new DeviceStorageImpl();
208 devStore.init(conf);
209
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700210 swStore = new SwitchStorageImpl();
211 swStore.init(conf);
212
Pankaj Berdeda809572013-02-22 15:31:20 -0800213 log.debug("Initializing OnosPublisher module with {}", conf);
214
215 }
216
217 @Override
218 public void startUp(FloodlightModuleContext context) {
219 // TODO Auto-generated method stub
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700220 Map<String, String> configMap = context.getConfigParams(this);
221 String cleanupNeeded = configMap.get(CleanupEnabled);
222
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700223 deviceService.addListener(this);
224 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700225 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700226 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
227 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700228 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700229 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800230 }
231
232}