blob: bd547552902e78eeb466f767df906b1a47eea549 [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
Naoki Shiotae3199732013-11-25 16:14:43 -080025/**
26 * FlowProgrammer is a module responsible to maintain flows installed to switches.
27 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
28 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizez
29 * flows between GraphDB and switches.
30 * @author Brian
31 *
32 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080033public class FlowProgrammer implements IFloodlightModule,
34 IOFMessageListener,
35 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080036 @SuppressWarnings("unused")
Naoki Shiotae3199732013-11-25 16:14:43 -080037 // flag to enable FlowSynchronizer
Brian O'Connore600f1d2013-11-25 23:09:18 -080038 private static final boolean enableFlowSync = false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080039 protected static Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080040 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080041 protected volatile IControllerRegistryService registryService;
42
Brian O'Connor8c166a72013-11-14 18:41:48 -080043
44 protected FlowPusher pusher;
45 private static final int NUM_PUSHER_THREAD = 1;
46
47 protected FlowSynchronizer synchronizer;
Naoki Shiotae3199732013-11-25 16:14:43 -080048
Brian O'Connor8c166a72013-11-14 18:41:48 -080049 public FlowProgrammer() {
50 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080051 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080052 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080053 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080054 }
55
56 @Override
57 public void init(FloodlightModuleContext context)
58 throws FloodlightModuleException {
59 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080060 registryService = context.getServiceImpl(IControllerRegistryService.class);
Naoki Shiotac1601d32013-11-20 10:47:34 -080061 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Naoki Shiota8739faa2013-11-18 17:00:25 -080062 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080063 synchronizer.init(pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080064 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080065 }
66
67 @Override
68 public void startUp(FloodlightModuleContext context) {
69 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080070 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
71 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080072 }
73
74 @Override
75 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
76 Collection<Class<? extends IFloodlightService>> l =
77 new ArrayList<Class<? extends IFloodlightService>>();
78 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080079 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080080 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080081 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080082 return l;
83 }
84
85 @Override
86 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
87 Map<Class<? extends IFloodlightService>,
88 IFloodlightService> m =
89 new HashMap<Class<? extends IFloodlightService>,
90 IFloodlightService>();
91 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080092 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080093 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -080094 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080095 return m;
96 }
97
98 @Override
99 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
100 Collection<Class<? extends IFloodlightService>> l =
101 new ArrayList<Class<? extends IFloodlightService>>();
102 l.add(IFloodlightProviderService.class);
103 return l;
104 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800105
106 @Override
107 public String getName() {
108 // TODO Auto-generated method stub
109 return "FlowProgrammer";
110 }
111
112 @Override
113 public boolean isCallbackOrderingPrereq(OFType type, String name) {
114 // TODO Auto-generated method stub
115 return false;
116 }
117
118 @Override
119 public boolean isCallbackOrderingPostreq(OFType type, String name) {
120 // TODO Auto-generated method stub
121 return false;
122 }
123
124 @Override
125 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
126 switch (msg.getType()) {
127 case FLOW_REMOVED:
128 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
129 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
130 break;
131 default:
132 break;
133 }
134
135 return Command.CONTINUE;
136 }
137
138 @Override
139 public void addedSwitch(IOFSwitch sw) {
140 log.debug("Switch added: {}", sw.getId());
141
Brian O'Connore600f1d2013-11-25 23:09:18 -0800142 if (enableFlowSync && registryService.hasControl(sw.getId())) {
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800143 synchronizer.synchronize(sw);
144 }
145 }
146
147 @Override
148 public void removedSwitch(IOFSwitch sw) {
149 log.debug("Switch removed: {}", sw.getId());
150
Brian O'Connore600f1d2013-11-25 23:09:18 -0800151 if (enableFlowSync) {
152 synchronizer.interrupt(sw);
153 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800154 }
155
156 @Override
157 public void switchPortChanged(Long switchId) {
158 // TODO Auto-generated method stub
159 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800160
Brian O'Connor8c166a72013-11-14 18:41:48 -0800161}