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