Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | import java.util.Set; |
| 8 | import java.util.concurrent.BlockingQueue; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 9 | import java.util.concurrent.ConcurrentHashMap; |
| 10 | import java.util.concurrent.ConcurrentMap; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 11 | import java.util.concurrent.LinkedBlockingQueue; |
| 12 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 13 | import net.floodlightcontroller.core.FloodlightContext; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.IFloodlightProviderService; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IOFMessageListener; |
| 16 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 22 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 23 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.intent.FlowEntry; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.intent.IntentOperation; |
| 29 | import net.onrc.onos.core.intent.IntentOperationList; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.intent.PathIntent; |
| 31 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 33 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 34 | import org.openflow.protocol.OFFlowRemoved; |
| 35 | import org.openflow.protocol.OFMessage; |
| 36 | import org.openflow.protocol.OFType; |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 40 | public class PlanInstallModule implements IFloodlightModule, IOFMessageListener { |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 41 | protected volatile IFloodlightProviderService floodlightProvider; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 42 | protected volatile ITopologyService topologyService; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 43 | protected volatile IDatagridService datagridService; |
| 44 | protected volatile IFlowPusherService flowPusher; |
| 45 | private PlanCalcRuntime planCalc; |
| 46 | private PlanInstallRuntime planInstall; |
| 47 | private EventListener eventListener; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 48 | private IEventChannel<Long, IntentStateList> intentStateChannel; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 49 | private static final Logger log = LoggerFactory.getLogger(PlanInstallModule.class); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 50 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 51 | private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent"; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 52 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 53 | private ConcurrentMap<String, Intent> parentIntentMap = new ConcurrentHashMap<String, Intent>(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 54 | |
| 55 | class EventListener extends Thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | implements IEventChannelListener<Long, IntentOperationList> { |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 57 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>(); |
| 59 | private Long key = Long.valueOf(0); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void run() { |
| 63 | while (true) { |
| 64 | try { |
| 65 | IntentOperationList intents = intentQueue.take(); |
| 66 | //TODO: consider draining the remaining intent lists |
| 67 | // and processing in one big batch |
| 68 | // List<IntentOperationList> remaining = new LinkedList<>(); |
| 69 | // intentQueue.drainTo(remaining); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 70 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | processIntents(intents); |
| 72 | } catch (InterruptedException e) { |
| 73 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
| 74 | } |
| 75 | } |
| 76 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | private void processIntents(IntentOperationList intents) { |
| 79 | log("start_processIntents"); |
| 80 | log.debug("Processing OperationList {}", intents); |
| 81 | log("begin_computePlan"); |
| 82 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
| 83 | log("end_computePlan"); |
| 84 | log.debug("Plan: {}", plan); |
| 85 | log("begin_installPlan"); |
| 86 | boolean success = planInstall.installPlan(plan); |
| 87 | log("end_installPlan"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 88 | Set<Long> domainSwitchDpids = floodlightProvider.getSwitches().keySet(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | log("begin_sendInstallNotif"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 90 | sendNotifications(intents, true, success, domainSwitchDpids); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | log("end_sendInstallNotif"); |
| 92 | log("finish"); |
| 93 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 94 | |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 95 | /*** |
| 96 | * This function is for sending intent state notification to other ONOS instances. |
| 97 | * The argument of "domainSwitchDpids" is required for dispatching this ONOS's managed switches. |
| 98 | * @param intents |
| 99 | * @param installed |
| 100 | * @param success |
| 101 | * @param domainSwitchDpids |
| 102 | */ |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 103 | private void sendNotifications(IntentOperationList intents, |
| 104 | boolean installed, boolean success, Set<Long> domainSwitchDpids) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | IntentStateList states = new IntentStateList(); |
| 106 | for (IntentOperation i : intents) { |
| 107 | IntentState newState; |
| 108 | switch (i.operator) { |
| 109 | case REMOVE: |
| 110 | if (installed) { |
| 111 | newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING; |
| 112 | } else { |
| 113 | newState = IntentState.DEL_REQ; |
| 114 | } |
| 115 | break; |
| 116 | case ADD: |
| 117 | default: |
| 118 | if (installed) { |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 119 | if (domainSwitchDpids != null) { |
| 120 | states.domainSwitchDpids.addAll(domainSwitchDpids); |
| 121 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | newState = success ? IntentState.INST_ACK : IntentState.INST_NACK; |
| 123 | } else { |
| 124 | newState = IntentState.INST_REQ; |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | states.put(i.intent.getId(), newState); |
| 129 | } |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 130 | |
| 131 | if (log.isTraceEnabled()) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 132 | log.trace("sendNotifications, states {}, domainSwitchDpids {}", |
| 133 | states, states.domainSwitchDpids); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | intentStateChannel.addTransientEntry(key, states); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | // XXX: Send notifications using the same key every time |
| 138 | // and receive them by entryAdded() and entryUpdated() |
| 139 | // key += 1; |
| 140 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 141 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void entryAdded(IntentOperationList value) { |
| 144 | entryUpdated(value); |
| 145 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public void entryRemoved(IntentOperationList value) { |
| 149 | // This channel is a queue, so this method is not needed |
| 150 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 151 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | @Override |
| 153 | public void entryUpdated(IntentOperationList value) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 154 | putIntentOpsInfoInParentMap(value); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | log("start_intentNotifRecv"); |
| 156 | log("begin_sendReceivedNotif"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 157 | sendNotifications(value, false, false, null); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | log("end_sendReceivedNotif"); |
| 159 | log("finish"); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 160 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | log.debug("Added OperationList {}", value); |
| 162 | try { |
| 163 | intentQueue.put(value); |
| 164 | } catch (InterruptedException e) { |
| 165 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 166 | } |
| 167 | } |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 168 | |
| 169 | private void putIntentOpsInfoInParentMap(IntentOperationList intentOps) { |
| 170 | for (IntentOperation i : intentOps) { |
| 171 | if (!(i.intent instanceof PathIntent)) { |
| 172 | log.warn("Not a path intent: {}", i); |
| 173 | continue; |
| 174 | } |
| 175 | PathIntent intent = (PathIntent) i.intent; |
| 176 | Intent parent = intent.getParentIntent(); |
| 177 | if (parent instanceof ShortestPathIntent) { |
| 178 | parentIntentMap.put(parent.getId(), parent); |
| 179 | } else { |
| 180 | log.warn("Unsupported Intent: {}", parent); |
| 181 | continue; |
| 182 | } |
| 183 | } |
| 184 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 185 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 186 | |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 187 | public static void log(String step) { |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 188 | log.debug("Time:{}, Step:{}", System.nanoTime(), step); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 189 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 190 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 191 | @Override |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 192 | public void init(FloodlightModuleContext context) |
| 193 | throws FloodlightModuleException { |
| 194 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 195 | topologyService = context.getServiceImpl(ITopologyService.class); |
| 196 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 197 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 198 | planCalc = new PlanCalcRuntime(); |
| 199 | planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher); |
| 200 | eventListener = new EventListener(); |
| 201 | } |
| 202 | |
| 203 | @Override |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 204 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | // start subscriber |
| 206 | datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
| 207 | eventListener, |
| 208 | Long.class, |
| 209 | IntentOperationList.class); |
| 210 | eventListener.start(); |
| 211 | // start publisher |
| 212 | intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME, |
| 213 | Long.class, |
| 214 | IntentStateList.class); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 215 | floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 216 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 217 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 218 | @Override |
| 219 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 220 | Collection<Class<? extends IFloodlightService>> l = |
| 221 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 222 | l.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 223 | l.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 224 | l.add(IDatagridService.class); |
| 225 | l.add(IFlowPusherService.class); |
| 226 | return l; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 227 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 228 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 229 | @Override |
| 230 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | // no services, for now |
| 232 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | // no services, for now |
| 238 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 239 | } |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 240 | |
| 241 | @Override |
| 242 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 243 | if (msg.getType().equals(OFType.FLOW_REMOVED) && |
| 244 | (msg instanceof OFFlowRemoved)) { |
| 245 | OFFlowRemoved flowRemovedMsg = (OFFlowRemoved) msg; |
| 246 | |
| 247 | if (log.isTraceEnabled()) { |
| 248 | log.trace("Receive flowRemoved from sw {} : Cookie {}", |
| 249 | sw.getId(), flowRemovedMsg.getCookie()); |
| 250 | } |
| 251 | |
| 252 | String intentParentId = Long.toString(flowRemovedMsg.getCookie()); |
| 253 | Intent intent = parentIntentMap.get(intentParentId); |
| 254 | |
| 255 | //We assume if the path src sw flow entry is expired, |
| 256 | //the path is expired. |
| 257 | if (!isFlowSrcRemoved(sw.getId(), intentParentId)) { |
| 258 | return Command.CONTINUE; |
| 259 | } |
| 260 | |
| 261 | ShortestPathIntent spfIntent = null; |
| 262 | if (!(intent instanceof ShortestPathIntent)) { |
| 263 | return Command.CONTINUE; |
| 264 | } |
| 265 | spfIntent = (ShortestPathIntent) intent; |
| 266 | String pathIntentId = spfIntent.getPathIntentId(); |
| 267 | |
| 268 | IntentStateList states = new IntentStateList(); |
| 269 | IntentState newState = IntentState.DEL_ACK; |
| 270 | states.put(pathIntentId, newState); |
| 271 | Set<Long> domainSwitchDpids = floodlightProvider.getSwitches().keySet(); |
| 272 | if (domainSwitchDpids != null) { |
| 273 | states.domainSwitchDpids.addAll(domainSwitchDpids); |
| 274 | } |
| 275 | parentIntentMap.remove(intentParentId); |
| 276 | log.debug("addEntry to intentStateChannel intentId {}, states {}", flowRemovedMsg.getCookie(), states); |
| 277 | |
| 278 | intentStateChannel.addTransientEntry(flowRemovedMsg.getCookie(), states); |
| 279 | } |
| 280 | |
| 281 | return Command.CONTINUE; |
| 282 | } |
| 283 | |
| 284 | private boolean isFlowSrcRemoved(long dpid, String shortestPathIntentId) { |
| 285 | Intent intent = parentIntentMap.get(shortestPathIntentId); |
| 286 | ShortestPathIntent spfIntent = null; |
| 287 | if (intent instanceof ShortestPathIntent) { |
| 288 | spfIntent = (ShortestPathIntent) intent; |
| 289 | } |
| 290 | |
| 291 | if (spfIntent == null) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | long srcSwDpid = spfIntent.getSrcSwitchDpid(); |
| 296 | if (srcSwDpid == dpid) { |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public String getName() { |
| 305 | // TODO Auto-generated method stub |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 311 | // TODO Auto-generated method stub |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 317 | // TODO Auto-generated method stub |
| 318 | return false; |
| 319 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 320 | } |