blob: efe0116f4293efeab9cadf7bb51429e566bf5819 [file] [log] [blame]
Brian O'Connor12861f72014-02-19 20:40:32 -08001package net.onrc.onos.intent.runtime;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.List;
6import java.util.Map;
7import java.util.Set;
8import java.util.concurrent.BlockingQueue;
9import java.util.concurrent.LinkedBlockingQueue;
10
Brian O'Connor9b712f62014-02-20 14:22:20 -080011import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
Brian O'Connor12861f72014-02-19 20:40:32 -080014import net.floodlightcontroller.core.IFloodlightProviderService;
15import net.floodlightcontroller.core.module.FloodlightModuleContext;
16import net.floodlightcontroller.core.module.FloodlightModuleException;
17import net.floodlightcontroller.core.module.IFloodlightModule;
18import net.floodlightcontroller.core.module.IFloodlightService;
19import net.onrc.onos.datagrid.IDatagridService;
20import net.onrc.onos.datagrid.IEventChannelListener;
21import net.onrc.onos.intent.FlowEntry;
22import net.onrc.onos.intent.IntentOperationList;
23import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Brian O'Connor135a95e2014-02-20 13:48:44 -080024import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Brian O'Connor12861f72014-02-19 20:40:32 -080025import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
26
27public class PlanInstallModule implements IFloodlightModule {
28 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080029 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080030 protected volatile IDatagridService datagridService;
31 protected volatile IFlowPusherService flowPusher;
32 private PlanCalcRuntime planCalc;
33 private PlanInstallRuntime planInstall;
34 private EventListener eventListener;
Brian O'Connor9b712f62014-02-20 14:22:20 -080035 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
36
Brian O'Connor12861f72014-02-19 20:40:32 -080037
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'Connor135a95e2014-02-20 13:48:44 -080044 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080045 datagridService = context.getServiceImpl(IDatagridService.class);
46 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080047 NetworkGraph graph = networkGraph.getNetworkGraph();
48 planCalc = new PlanCalcRuntime(graph);
49 planInstall = new PlanInstallRuntime(graph, floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080050 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'Connor9b712f62014-02-20 14:22:20 -080072 log.debug("Processing OperationList {}", intents);
Brian O'Connor12861f72014-02-19 20:40:32 -080073 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor9b712f62014-02-20 14:22:20 -080074 log.debug("Plan: {}", plan);
Brian O'Connor12861f72014-02-19 20:40:32 -080075 planInstall.installPlan(plan);
76 }
77
78 @Override
79 public void entryAdded(IntentOperationList value) {
Brian O'Connor9b712f62014-02-20 14:22:20 -080080 log.debug("Added OperationList {}", value);
Brian O'Connor12861f72014-02-19 20:40:32 -080081 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'Connor135a95e2014-02-20 13:48:44 -0800108 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800109 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}