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