blob: a6931e6b5e690dc206f6f852b9be85ac57842b13 [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 Berdef08d5ff2013-03-14 17:03:07 -070048 protected IThreadPoolService threadPool;
49
50 protected final int CLEANUP_TASK_INTERVAL = 999; // 999 ms
51 protected SingletonTask cleanupTask;
52
53 /**
54 * Cleanup and synch switch state from registry
55 */
56 protected class SwitchCleanup implements ControlChangeCallback, Runnable {
57 @Override
58 public void run() {
59 try {
60 log.debug("Running cleanup thread");
61 switchCleanup();
62 }
63 catch (Exception e) {
64 log.error("Error in cleanup thread", e);
65 } finally {
66 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
67 TimeUnit.MILLISECONDS);
68 }
69 }
70
71 @Override
72 public void controlChanged(long dpid, boolean hasControl) {
73 // TODO Auto-generated method stub
74
75 if (hasControl) {
76 log.debug("got control to set inactive sw {}", dpid);
77 swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE);
78 registryService.releaseControl(dpid);
79 }
80 }
81 }
82
83
84
85 protected void switchCleanup() {
86
87 TopoSwitchServiceImpl impl = new TopoSwitchServiceImpl();
88 Iterable<ISwitchObject> switches = impl.getActiveSwitches();
89 // For each switch check if a controller exists in controller registry
90 for (ISwitchObject sw: switches) {
91 log.debug("checking if switch is inactive: {}", sw.getDPID());
92 try {
93 long dpid = HexString.toLong(sw.getDPID());
94 String controller = registryService.getControllerForSwitch(dpid);
95 if (controller == null) {
96 log.debug("request Control to set inactive sw {}", dpid);
97 registryService.requestControl(dpid, new SwitchCleanup());
98 } else {
99 log.debug("sw {} is controlled by controller: {}",dpid,controller);
100 }
101 } catch (NumberFormatException e) {
102 // TODO Auto-generated catch block
103 e.printStackTrace();
104 } catch (RegistryException e) {
105 // TODO Auto-generated catch block
106 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 Berdef08d5ff2013-03-14 17:03:07 -0700220 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
221 deviceService.addListener(this);
222 // Setup the Cleanup task.
223 cleanupTask = new SingletonTask(ses, new SwitchCleanup());
224 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.MILLISECONDS);
Pankaj Berdeda809572013-02-22 15:31:20 -0800225 }
226
227}