blob: 45f59b39585bfffccc73a75657879d7deb319516 [file] [log] [blame]
Brian O'Connor8c166a72013-11-14 18:41:48 -08001package net.onrc.onos.ofcontroller.flowprogrammer;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.Map;
Brian O'Connorea1efbe2013-11-25 22:57:43 -08007
8import org.openflow.protocol.OFFlowRemoved;
9import org.openflow.protocol.OFMessage;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080010import org.openflow.protocol.OFType;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
Brian O'Connorea1efbe2013-11-25 22:57:43 -080014import net.floodlightcontroller.core.FloodlightContext;
Brian O'Connor8c166a72013-11-14 18:41:48 -080015import net.floodlightcontroller.core.IFloodlightProviderService;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080016import net.floodlightcontroller.core.IOFMessageListener;
17import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080018import net.floodlightcontroller.core.IOFSwitchListener;
Brian O'Connor8c166a72013-11-14 18:41:48 -080019import net.floodlightcontroller.core.module.FloodlightModuleContext;
20import net.floodlightcontroller.core.module.FloodlightModuleException;
21import net.floodlightcontroller.core.module.IFloodlightModule;
22import net.floodlightcontroller.core.module.IFloodlightService;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080023import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080024
Brian O'Connorea1efbe2013-11-25 22:57:43 -080025public class FlowProgrammer implements IFloodlightModule,
26 IOFMessageListener,
27 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080028 @SuppressWarnings("unused")
Brian O'Connore600f1d2013-11-25 23:09:18 -080029 private static final boolean enableFlowSync = false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080030 protected static Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080031 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080032 protected volatile IControllerRegistryService registryService;
33
Brian O'Connor8c166a72013-11-14 18:41:48 -080034
35 protected FlowPusher pusher;
36 private static final int NUM_PUSHER_THREAD = 1;
37
38 protected FlowSynchronizer synchronizer;
39
40 public FlowProgrammer() {
41 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080042 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080043 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080044 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080045 }
46
47 @Override
48 public void init(FloodlightModuleContext context)
49 throws FloodlightModuleException {
50 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080051 registryService = context.getServiceImpl(IControllerRegistryService.class);
Naoki Shiotac1601d32013-11-20 10:47:34 -080052 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Naoki Shiota8739faa2013-11-18 17:00:25 -080053 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080054 synchronizer.init(pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080055 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080056 }
57
58 @Override
59 public void startUp(FloodlightModuleContext context) {
60 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080061 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
62 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080063 }
64
65 @Override
66 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
67 Collection<Class<? extends IFloodlightService>> l =
68 new ArrayList<Class<? extends IFloodlightService>>();
69 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080070 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080071 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080072 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080073 return l;
74 }
75
76 @Override
77 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
78 Map<Class<? extends IFloodlightService>,
79 IFloodlightService> m =
80 new HashMap<Class<? extends IFloodlightService>,
81 IFloodlightService>();
82 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080083 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080084 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -080085 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080086 return m;
87 }
88
89 @Override
90 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
91 Collection<Class<? extends IFloodlightService>> l =
92 new ArrayList<Class<? extends IFloodlightService>>();
93 l.add(IFloodlightProviderService.class);
94 return l;
95 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -080096
97 @Override
98 public String getName() {
99 // TODO Auto-generated method stub
100 return "FlowProgrammer";
101 }
102
103 @Override
104 public boolean isCallbackOrderingPrereq(OFType type, String name) {
105 // TODO Auto-generated method stub
106 return false;
107 }
108
109 @Override
110 public boolean isCallbackOrderingPostreq(OFType type, String name) {
111 // TODO Auto-generated method stub
112 return false;
113 }
114
115 @Override
116 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
117 switch (msg.getType()) {
118 case FLOW_REMOVED:
119 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
120 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
121 break;
122 default:
123 break;
124 }
125
126 return Command.CONTINUE;
127 }
128
129 @Override
130 public void addedSwitch(IOFSwitch sw) {
131 log.debug("Switch added: {}", sw.getId());
132
Brian O'Connore600f1d2013-11-25 23:09:18 -0800133 if (enableFlowSync && registryService.hasControl(sw.getId())) {
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800134 synchronizer.synchronize(sw);
135 }
136 }
137
138 @Override
139 public void removedSwitch(IOFSwitch sw) {
140 log.debug("Switch removed: {}", sw.getId());
141
Brian O'Connore600f1d2013-11-25 23:09:18 -0800142 if (enableFlowSync) {
143 synchronizer.interrupt(sw);
144 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800145 }
146
147 @Override
148 public void switchPortChanged(Long switchId) {
149 // TODO Auto-generated method stub
150 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800151
Brian O'Connor8c166a72013-11-14 18:41:48 -0800152}