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