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; |
| 20 | import net.onrc.onos.intent.IntentOperationList; |
| 21 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 24 | |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 28 | public class PlanInstallModule implements IFloodlightModule { |
| 29 | protected volatile IFloodlightProviderService floodlightProvider; |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 30 | protected volatile INetworkGraphService networkGraph; |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 31 | protected volatile IDatagridService datagridService; |
| 32 | protected volatile IFlowPusherService flowPusher; |
| 33 | private PlanCalcRuntime planCalc; |
| 34 | private PlanInstallRuntime planInstall; |
| 35 | private EventListener eventListener; |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 36 | private IEventChannel<Long, IntentOperationList> channel; |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 37 | private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class); |
| 38 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 39 | |
| 40 | private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent"; |
| 41 | |
| 42 | @Override |
| 43 | public void init(FloodlightModuleContext context) |
| 44 | throws FloodlightModuleException { |
| 45 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 46 | networkGraph = context.getServiceImpl(INetworkGraphService.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 47 | datagridService = context.getServiceImpl(IDatagridService.class); |
| 48 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 49 | NetworkGraph graph = networkGraph.getNetworkGraph(); |
| 50 | planCalc = new PlanCalcRuntime(graph); |
| 51 | planInstall = new PlanInstallRuntime(graph, floodlightProvider, flowPusher); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 52 | eventListener = new EventListener(); |
| 53 | } |
| 54 | |
| 55 | class EventListener extends Thread |
| 56 | implements IEventChannelListener<Long, IntentOperationList> { |
| 57 | |
| 58 | private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>(); |
| 59 | |
| 60 | @Override |
| 61 | public void run() { |
| 62 | while(true) { |
| 63 | try { |
| 64 | IntentOperationList intents = intentQueue.take(); |
| 65 | //TODO: drain the remaining intent lists |
| 66 | processIntents(intents); |
| 67 | } catch (InterruptedException e) { |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 68 | log.warn("Error taking from intent queue: {}", e.getMessage()); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private void processIntents(IntentOperationList intents) { |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 74 | log.debug("Processing OperationList {}", intents); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 75 | List<Set<FlowEntry>> plan = planCalc.computePlan(intents); |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 76 | log.debug("Plan: {}", plan); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 77 | planInstall.installPlan(plan); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void entryAdded(IntentOperationList value) { |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 82 | log.debug("Added OperationList {}", value); |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 83 | try { |
| 84 | intentQueue.put(value); |
| 85 | } catch (InterruptedException e) { |
| 86 | log.warn("Error putting to intent queue: {}", e.getMessage()); |
| 87 | } |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void entryRemoved(IntentOperationList value) { |
| 92 | // This channel is a queue, so this method is not needed |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void entryUpdated(IntentOperationList value) { |
| 97 | // This channel is a queue, so this method is not needed |
| 98 | } |
| 99 | } |
| 100 | @Override |
| 101 | public void startUp(FloodlightModuleContext context) { |
Brian O'Connor | 5e73c01 | 2014-02-20 14:54:34 -0800 | [diff] [blame] | 102 | channel = datagridService.addListener(PATH_INTENT_CHANNEL_NAME, |
| 103 | eventListener, |
| 104 | Long.class, |
| 105 | IntentOperationList.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 106 | eventListener.start(); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 111 | Collection<Class<? extends IFloodlightService>> l = |
| 112 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 113 | l.add(IFloodlightProviderService.class); |
Brian O'Connor | 135a95e | 2014-02-20 13:48:44 -0800 | [diff] [blame] | 114 | l.add(INetworkGraphService.class); |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 115 | l.add(IDatagridService.class); |
| 116 | l.add(IFlowPusherService.class); |
| 117 | return l; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 122 | // TODO Auto-generated method stub |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 128 | // TODO Auto-generated method stub |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | } |