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(); |
| 70 | //TODO: drain the remaining intent lists |
| 71 | processIntents(intents); |
| 72 | } catch (InterruptedException e) { |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 73 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private void processIntents(IntentOperationList intents) { |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 79 | log.debug("Processing OperationList {}", intents); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 80 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 81 | log.debug("Plan: {}", plan); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 82 | boolean success = planInstall.installPlan(plan); |
| 83 | |
| 84 | sendNotifications(intents, true, success); |
| 85 | } |
| 86 | |
| 87 | private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) { |
| 88 | IntentStateList states = new IntentStateList(); |
| 89 | for(IntentOperation i : intents) { |
| 90 | IntentState newState; |
| 91 | switch(i.operator) { |
| 92 | case REMOVE: |
| 93 | if(installed) { |
| 94 | newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING; |
| 95 | } |
| 96 | else { |
| 97 | newState = IntentState.DEL_REQ; |
| 98 | } |
| 99 | break; |
| 100 | case ADD: |
| 101 | default: |
| 102 | if(installed) { |
| 103 | newState = success ? IntentState.INST_ACK : IntentState.INST_NACK; |
| 104 | } |
| 105 | else { |
| 106 | newState = IntentState.INST_REQ; |
| 107 | } |
| 108 | break; |
| 109 | } |
| 110 | states.put(i.intent.getId(), newState); |
| 111 | } |
| 112 | intentStateChannel.addEntry(key, states); |
| 113 | key += 1; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void entryAdded(IntentOperationList value) { |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 118 | sendNotifications(value, false, false); |
| 119 | |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 120 | log.debug("Added OperationList {}", value); |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 121 | try { |
| 122 | intentQueue.put(value); |
| 123 | } catch (InterruptedException e) { |
| 124 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 125 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void entryRemoved(IntentOperationList value) { |
| 130 | // This channel is a queue, so this method is not needed |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void entryUpdated(IntentOperationList value) { |
| 135 | // This channel is a queue, so this method is not needed |
| 136 | } |
| 137 | } |
| 138 | @Override |
| 139 | public void startUp(FloodlightModuleContext context) { |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 140 | // start subscriber |
| 141 | datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 142 | eventListener, |
| 143 | Long.class, |
| 144 | IntentOperationList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 145 | eventListener.start(); |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 146 | // start publisher |
| 147 | intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME, |
| 148 | Long.class, |
| 149 | IntentStateList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 154 | Collection<Class<? extends IFloodlightService>> l = |
| 155 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 156 | l.add(IFloodlightProviderService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 157 | l.add(INetworkGraphService.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 158 | l.add(IDatagridService.class); |
| 159 | l.add(IFlowPusherService.class); |
| 160 | return l; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 165 | // TODO Auto-generated method stub |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 171 | // TODO Auto-generated method stub |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | } |