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; |
| 25 | //import net.onrc.onos.core.topology.Topology; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 26 | |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 30 | public class PlanInstallModule implements IFloodlightModule { |
| 31 | protected volatile IFloodlightProviderService floodlightProvider; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame^] | 32 | protected volatile ITopologyService topologyService; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 33 | protected volatile IDatagridService datagridService; |
| 34 | protected volatile IFlowPusherService flowPusher; |
| 35 | private PlanCalcRuntime planCalc; |
| 36 | private PlanInstallRuntime planInstall; |
| 37 | private EventListener eventListener; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 38 | private IEventChannel<Long, IntentStateList> intentStateChannel; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 39 | private static final Logger log = LoggerFactory.getLogger(PlanInstallModule.class); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 40 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 41 | |
| 42 | private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent"; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 43 | private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state"; |
| 44 | |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 45 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 46 | @Override |
| 47 | public void init(FloodlightModuleContext context) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | throws FloodlightModuleException { |
| 49 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame^] | 50 | topologyService = context.getServiceImpl(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 52 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | planCalc = new PlanCalcRuntime(); |
| 54 | planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher); |
| 55 | eventListener = new EventListener(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | class EventListener extends Thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | implements IEventChannelListener<Long, IntentOperationList> { |
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 | private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>(); |
| 62 | private Long key = Long.valueOf(0); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void run() { |
| 66 | while (true) { |
| 67 | try { |
| 68 | IntentOperationList intents = intentQueue.take(); |
| 69 | //TODO: consider draining the remaining intent lists |
| 70 | // and processing in one big batch |
| 71 | // List<IntentOperationList> remaining = new LinkedList<>(); |
| 72 | // intentQueue.drainTo(remaining); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 73 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | processIntents(intents); |
| 75 | } catch (InterruptedException e) { |
| 76 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
| 77 | } |
| 78 | } |
| 79 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 80 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | private void processIntents(IntentOperationList intents) { |
| 82 | log("start_processIntents"); |
| 83 | log.debug("Processing OperationList {}", intents); |
| 84 | log("begin_computePlan"); |
| 85 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
| 86 | log("end_computePlan"); |
| 87 | log.debug("Plan: {}", plan); |
| 88 | log("begin_installPlan"); |
| 89 | boolean success = planInstall.installPlan(plan); |
| 90 | log("end_installPlan"); |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 91 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | log("begin_sendInstallNotif"); |
| 93 | sendNotifications(intents, true, success); |
| 94 | log("end_sendInstallNotif"); |
| 95 | log("finish"); |
| 96 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) { |
| 99 | IntentStateList states = new IntentStateList(); |
| 100 | for (IntentOperation i : intents) { |
| 101 | IntentState newState; |
| 102 | switch (i.operator) { |
| 103 | case REMOVE: |
| 104 | if (installed) { |
| 105 | newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING; |
| 106 | } else { |
| 107 | newState = IntentState.DEL_REQ; |
| 108 | } |
| 109 | break; |
| 110 | case ADD: |
| 111 | default: |
| 112 | if (installed) { |
| 113 | newState = success ? IntentState.INST_ACK : IntentState.INST_NACK; |
| 114 | } else { |
| 115 | newState = IntentState.INST_REQ; |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | states.put(i.intent.getId(), newState); |
| 120 | } |
| 121 | intentStateChannel.addEntry(key, states); |
| 122 | // XXX: Send notifications using the same key every time |
| 123 | // and receive them by entryAdded() and entryUpdated() |
| 124 | // key += 1; |
| 125 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 126 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void entryAdded(IntentOperationList value) { |
| 129 | entryUpdated(value); |
| 130 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public void entryRemoved(IntentOperationList value) { |
| 134 | // This channel is a queue, so this method is not needed |
| 135 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 136 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public void entryUpdated(IntentOperationList value) { |
| 139 | log("start_intentNotifRecv"); |
| 140 | log("begin_sendReceivedNotif"); |
| 141 | sendNotifications(value, false, false); |
| 142 | log("end_sendReceivedNotif"); |
| 143 | log("finish"); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 144 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | log.debug("Added OperationList {}", value); |
| 146 | try { |
| 147 | intentQueue.put(value); |
| 148 | } catch (InterruptedException e) { |
| 149 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 150 | } |
| 151 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 152 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 153 | |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 154 | public static void log(String step) { |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 155 | log.debug("Time:{}, Step:{}", System.nanoTime(), step); |
Brian O'Connor | 85dd8f2 | 2014-02-25 11:43:07 -0800 | [diff] [blame] | 156 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 157 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 158 | @Override |
| 159 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | // start subscriber |
| 161 | datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
| 162 | eventListener, |
| 163 | Long.class, |
| 164 | IntentOperationList.class); |
| 165 | eventListener.start(); |
| 166 | // start publisher |
| 167 | intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME, |
| 168 | Long.class, |
| 169 | IntentStateList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -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 Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | Collection<Class<? extends IFloodlightService>> l = |
| 175 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 176 | l.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame^] | 177 | l.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | l.add(IDatagridService.class); |
| 179 | l.add(IFlowPusherService.class); |
| 180 | return l; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 181 | } |
Toshio Koide | 3a52ef3 | 2014-02-28 12:10:26 -0800 | [diff] [blame] | 182 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 183 | @Override |
| 184 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 185 | // no services, for now |
| 186 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | // no services, for now |
| 192 | return null; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | } |