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