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; |
| 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; |
| 16 | import net.onrc.onos.datagrid.IDatagridService; |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 17 | import net.onrc.onos.datagrid.IEventChannel; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 18 | import net.onrc.onos.datagrid.IEventChannelListener; |
| 19 | import net.onrc.onos.intent.FlowEntry; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 20 | import net.onrc.onos.intent.Intent.IntentState; |
| 21 | import net.onrc.onos.intent.IntentOperation; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 22 | import net.onrc.onos.intent.IntentOperationList; |
| 23 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 25 | //import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [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; |
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; |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 39 | private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class); |
| 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 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | public void init(FloodlightModuleContext context) |
| 48 | throws FloodlightModuleException { |
| 49 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 50 | networkGraph = context.getServiceImpl(INetworkGraphService.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 51 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 52 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 53 | // NetworkGraph graph = networkGraph.getNetworkGraph(); |
| 54 | planCalc = new PlanCalcRuntime(); |
| 55 | planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 56 | eventListener = new EventListener(); |
| 57 | } |
| 58 | |
| 59 | class EventListener extends Thread |
| 60 | implements IEventChannelListener<Long, IntentOperationList> { |
| 61 | |
| 62 | private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>(); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 63 | private Long key = Long.valueOf(0); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 64 | |
| 65 | @Override |
| 66 | public void run() { |
| 67 | while(true) { |
| 68 | try { |
| 69 | IntentOperationList intents = intentQueue.take(); |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 70 | //TODO: consider draining the remaining intent lists |
| 71 | // and processing in one big batch |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 72 | processIntents(intents); |
| 73 | } catch (InterruptedException e) { |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 74 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private void processIntents(IntentOperationList intents) { |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 80 | log.debug("Processing OperationList {}", intents); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 81 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 82 | log.debug("Plan: {}", plan); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 83 | boolean success = planInstall.installPlan(plan); |
| 84 | |
| 85 | sendNotifications(intents, true, success); |
| 86 | } |
| 87 | |
| 88 | private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) { |
| 89 | IntentStateList states = new IntentStateList(); |
| 90 | for(IntentOperation i : intents) { |
| 91 | IntentState newState; |
| 92 | switch(i.operator) { |
| 93 | case REMOVE: |
| 94 | if(installed) { |
| 95 | newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING; |
| 96 | } |
| 97 | else { |
| 98 | newState = IntentState.DEL_REQ; |
| 99 | } |
| 100 | break; |
| 101 | case ADD: |
| 102 | default: |
| 103 | if(installed) { |
| 104 | newState = success ? IntentState.INST_ACK : IntentState.INST_NACK; |
| 105 | } |
| 106 | else { |
| 107 | newState = IntentState.INST_REQ; |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | states.put(i.intent.getId(), newState); |
| 112 | } |
| 113 | intentStateChannel.addEntry(key, states); |
| 114 | key += 1; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void entryAdded(IntentOperationList value) { |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 119 | sendNotifications(value, false, false); |
| 120 | |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 121 | log.debug("Added OperationList {}", value); |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 122 | try { |
| 123 | intentQueue.put(value); |
| 124 | } catch (InterruptedException e) { |
| 125 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 126 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void entryRemoved(IntentOperationList value) { |
| 131 | // This channel is a queue, so this method is not needed |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void entryUpdated(IntentOperationList value) { |
| 136 | // This channel is a queue, so this method is not needed |
| 137 | } |
| 138 | } |
| 139 | @Override |
| 140 | public void startUp(FloodlightModuleContext context) { |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 141 | // start subscriber |
| 142 | datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 143 | eventListener, |
| 144 | Long.class, |
| 145 | IntentOperationList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 146 | eventListener.start(); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 147 | // start publisher |
| 148 | intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME, |
| 149 | Long.class, |
| 150 | IntentStateList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 155 | Collection<Class<? extends IFloodlightService>> l = |
| 156 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 157 | l.add(IFloodlightProviderService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 158 | l.add(INetworkGraphService.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 159 | l.add(IDatagridService.class); |
| 160 | l.add(IFlowPusherService.class); |
| 161 | return l; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 166 | // no services, for now |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 167 | return null; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Brian O'Connor | 067b95d | 2014-02-21 18:43:27 -0800 | [diff] [blame] | 172 | // no services, for now |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 173 | return null; |
| 174 | } |
| 175 | |
| 176 | } |