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