blob: 723fe1c7ea6889cdbb088fcae82e386fb7209f62 [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 Berde62016142013-04-09 15:35:50 -070054 protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min
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 {
Pankaj Berdea2e14a92013-04-15 11:59:15 -070070 conn.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070071 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Pankaj Berde99fcee12013-03-18 09:41:53 -070072 TimeUnit.SECONDS);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070073 }
74 }
75
76 @Override
77 public void controlChanged(long dpid, boolean hasControl) {
78 // TODO Auto-generated method stub
79
80 if (hasControl) {
Pankaj Berde99fcee12013-03-18 09:41:53 -070081 log.debug("got control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070082 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
83 registryService.releaseControl(dpid);
84 }
85 }
86 }
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070087
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070088 protected void switchCleanup() {
Pankaj Berdea2e14a92013-04-15 11:59:15 -070089 conn.close();
90 Iterable<ISwitchObject> switches = conn.utils().getActiveSwitches(conn);
Jonathan Hartf02a0932013-03-18 18:30:48 -070091
92 log.debug("Checking for inactive switches");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070093 // For each switch check if a controller exists in controller registry
94 for (ISwitchObject sw: switches) {
Jonathan Hartf02a0932013-03-18 18:30:48 -070095 //log.debug("checking if switch is inactive: {}", sw.getDPID());
Pankaj Berdef08d5ff2013-03-14 17:03:07 -070096 try {
97 long dpid = HexString.toLong(sw.getDPID());
98 String controller = registryService.getControllerForSwitch(dpid);
99 if (controller == null) {
Pankaj Berde99fcee12013-03-18 09:41:53 -0700100 log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700101 registryService.requestControl(dpid, new SwitchCleanup());
Umesh Krishnaswamy255b9882013-04-02 19:55:43 -0700102 //} else {
103 // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700104 }
105 } catch (NumberFormatException e) {
106 // TODO Auto-generated catch block
107 e.printStackTrace();
108 } catch (RegistryException e) {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700109 log.debug("Caught RegistryException trying to requestControl in cleanup thread");
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700110 e.printStackTrace();
111 }
112 }
Pankaj Berdea2e14a92013-04-15 11:59:15 -0700113 conn.close();
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700114 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800115
116 @Override
117 public void linkDiscoveryUpdate(LDUpdate update) {
118 // TODO Auto-generated method stub
119
120 }
121
122 @Override
123 public void addedSwitch(IOFSwitch sw) {
124 // TODO Auto-generated method stub
125
126 }
127
128 @Override
129 public void removedSwitch(IOFSwitch sw) {
130 // TODO Auto-generated method stub
131
132 }
133
134 @Override
135 public void switchPortChanged(Long switchId) {
136 // TODO Auto-generated method stub
137
138 }
139
140 @Override
141 public String getName() {
142 return "OnosPublisher";
143 }
144
145 @Override
146 public void deviceAdded(IDevice device) {
147 // TODO Auto-generated method stub
148 log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString());
149 devStore.addDevice(device);
150 }
151
152 @Override
153 public void deviceRemoved(IDevice device) {
154 // TODO Auto-generated method stub
155
156 }
157
158 @Override
159 public void deviceMoved(IDevice device) {
160 // TODO Auto-generated method stub
161 devStore.changeDeviceAttachments(device);
162
163 }
164
165 @Override
166 public void deviceIPV4AddrChanged(IDevice device) {
167 // TODO Auto-generated method stub
168 devStore.changeDeviceIPv4Address(device);
169
170 }
171
172 @Override
173 public void deviceVlanChanged(IDevice device) {
174 // TODO Auto-generated method stub
175 }
176
177
178 @Override
179 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
180 // TODO Auto-generated method stub
181 return null;
182 }
183
184 @Override
185 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
186 // TODO Auto-generated method stub
187 return null;
188 }
189
190 @Override
191 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
192 Collection<Class<? extends IFloodlightService>> l =
193 new ArrayList<Class<? extends IFloodlightService>>();
194 l.add(IFloodlightProviderService.class);
195 l.add(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700196 l.add(IThreadPoolService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800197 return l;
198 }
199
200 @Override
201 public void init(FloodlightModuleContext context)
202 throws FloodlightModuleException {
203 // TODO Auto-generated method stub
204 Map<String, String> configMap = context.getConfigParams(this);
205 String conf = configMap.get(DBConfigFile);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700206 conn = GraphDBConnection.getInstance(conf);
Pankaj Berdeda809572013-02-22 15:31:20 -0800207
208 log = LoggerFactory.getLogger(OnosPublisher.class);
209 deviceService = context.getServiceImpl(IDeviceService.class);
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700210 threadPool = context.getServiceImpl(IThreadPoolService.class);
211 registryService = context.getServiceImpl(IControllerRegistryService.class);
Pankaj Berdeda809572013-02-22 15:31:20 -0800212
Pankaj Berdeda809572013-02-22 15:31:20 -0800213 devStore = new DeviceStorageImpl();
214 devStore.init(conf);
215
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700216 swStore = new SwitchStorageImpl();
217 swStore.init(conf);
218
Pankaj Berdeda809572013-02-22 15:31:20 -0800219 log.debug("Initializing OnosPublisher module with {}", conf);
220
221 }
222
223 @Override
224 public void startUp(FloodlightModuleContext context) {
225 // TODO Auto-generated method stub
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700226 Map<String, String> configMap = context.getConfigParams(this);
227 String cleanupNeeded = configMap.get(CleanupEnabled);
228
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700229 deviceService.addListener(this);
Pankaj Berde9d6b5072013-04-03 11:51:23 -0700230
231 log.debug("Adding EventListener");
232 conn.addEventListener(new LocalTopologyEventListener(conn));
Pankaj Berdef08d5ff2013-03-14 17:03:07 -0700233 // Setup the Cleanup task.
Pankaj Berde99fcee12013-03-18 09:41:53 -0700234 if (cleanupNeeded == null || !cleanupNeeded.equals("False")) {
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700235 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
236 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700237 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS);
Pankaj Berde1e3e5ba2013-03-15 13:17:53 -0700238 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800239 }
240
241}