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; |
| 9 | import java.util.concurrent.LinkedBlockingQueue; |
| 10 | |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 17 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 18 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.intent.FlowEntry; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.intent.IntentOperation; |
| 23 | import net.onrc.onos.core.intent.IntentOperationList; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 25 | |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 29 | public class PlanInstallModule implements IFloodlightModule { |
| 30 | protected volatile IFloodlightProviderService floodlightProvider; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 31 | protected volatile ITopologyService topologyService; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 32 | protected volatile IDatagridService datagridService; |
| 33 | protected volatile IFlowPusherService flowPusher; |
| 34 | private PlanCalcRuntime planCalc; |
| 35 | private PlanInstallRuntime planInstall; |
| 36 | private EventListener eventListener; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 37 | private IEventChannel<Long, IntentStateList> intentStateChannel; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 38 | private static final Logger log = LoggerFactory.getLogger(PlanInstallModule.class); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 39 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 40 | private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent"; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 41 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
| 42 | |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 43 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 44 | @Override |
| 45 | public void init(FloodlightModuleContext context) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | throws FloodlightModuleException { |
| 47 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 48 | topologyService = context.getServiceImpl(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 50 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | planCalc = new PlanCalcRuntime(); |
| 52 | planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher); |
| 53 | eventListener = new EventListener(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | class EventListener extends Thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | implements IEventChannelListener<Long, IntentOperationList> { |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 58 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>(); |
| 60 | private Long key = Long.valueOf(0); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public void run() { |
| 64 | while (true) { |
| 65 | try { |
| 66 | IntentOperationList intents = intentQueue.take(); |
| 67 | //TODO: consider draining the remaining intent lists |
| 68 | // and processing in one big batch |
| 69 | // List<IntentOperationList> remaining = new LinkedList<>(); |
| 70 | // intentQueue.drainTo(remaining); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 71 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | processIntents(intents); |
| 73 | } catch (InterruptedException e) { |
| 74 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
| 75 | } |
| 76 | } |
| 77 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | private void processIntents(IntentOperationList intents) { |
| 80 | log("start_processIntents"); |
| 81 | log.debug("Processing OperationList {}", intents); |
| 82 | log("begin_computePlan"); |
| 83 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
| 84 | log("end_computePlan"); |
| 85 | log.debug("Plan: {}", plan); |
| 86 | log("begin_installPlan"); |
| 87 | boolean success = planInstall.installPlan(plan); |
| 88 | log("end_installPlan"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame^] | 89 | Set<Long> domainSwitchDpids = floodlightProvider.getSwitches().keySet(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | log("begin_sendInstallNotif"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame^] | 91 | sendNotifications(intents, true, success, domainSwitchDpids); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | log("end_sendInstallNotif"); |
| 93 | log("finish"); |
| 94 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 95 | |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame^] | 96 | /*** |
| 97 | * This function is for sending intent state notification to other ONOS instances. |
| 98 | * The argument of "domainSwitchDpids" is required for dispatching this ONOS's managed switches. |
| 99 | * @param intents |
| 100 | * @param installed |
| 101 | * @param success |
| 102 | * @param domainSwitchDpids |
| 103 | */ |
| 104 | private void sendNotifications(IntentOperationList intents, 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()) { |
| 132 | log.trace("domainSwitchDpids {}", states.domainSwitchDpids); |
| 133 | } |
| 134 | |
| 135 | intentStateChannel.addTransientEntry(key, states); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | // XXX: Send notifications using the same key every time |
| 137 | // and receive them by entryAdded() and entryUpdated() |
| 138 | // key += 1; |
| 139 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 140 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public void entryAdded(IntentOperationList value) { |
| 143 | entryUpdated(value); |
| 144 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 145 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void entryRemoved(IntentOperationList value) { |
| 148 | // This channel is a queue, so this method is not needed |
| 149 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 150 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public void entryUpdated(IntentOperationList value) { |
| 153 | log("start_intentNotifRecv"); |
| 154 | log("begin_sendReceivedNotif"); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame^] | 155 | sendNotifications(value, false, false, null); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | log("end_sendReceivedNotif"); |
| 157 | log("finish"); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 158 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | log.debug("Added OperationList {}", value); |
| 160 | try { |
| 161 | intentQueue.put(value); |
| 162 | } catch (InterruptedException e) { |
| 163 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 164 | } |
| 165 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 166 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 167 | |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 168 | public static void log(String step) { |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 169 | log.debug("Time:{}, Step:{}", System.nanoTime(), step); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 170 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 171 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 172 | @Override |
| 173 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | // start subscriber |
| 175 | datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
| 176 | eventListener, |
| 177 | Long.class, |
| 178 | IntentOperationList.class); |
| 179 | eventListener.start(); |
| 180 | // start publisher |
| 181 | intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME, |
| 182 | Long.class, |
| 183 | IntentStateList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 184 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 185 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 186 | @Override |
| 187 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | Collection<Class<? extends IFloodlightService>> l = |
| 189 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 190 | l.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 191 | l.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | l.add(IDatagridService.class); |
| 193 | l.add(IFlowPusherService.class); |
| 194 | return l; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 195 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 196 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 197 | @Override |
| 198 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | // no services, for now |
| 200 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | // no services, for now |
| 206 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 207 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 208 | } |