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