blob: cc780fbaaa00e4f7f805a0e61bbc2823210e0399 [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
11import net.floodlightcontroller.core.IFloodlightProviderService;
12import net.floodlightcontroller.core.module.FloodlightModuleContext;
13import net.floodlightcontroller.core.module.FloodlightModuleException;
14import net.floodlightcontroller.core.module.IFloodlightModule;
15import net.floodlightcontroller.core.module.IFloodlightService;
16import net.onrc.onos.datagrid.IDatagridService;
Brian O'Connor5e73c012014-02-20 14:54:34 -080017import net.onrc.onos.datagrid.IEventChannel;
Brian O'Connor12861f72014-02-19 20:40:32 -080018import net.onrc.onos.datagrid.IEventChannelListener;
19import net.onrc.onos.intent.FlowEntry;
20import net.onrc.onos.intent.IntentOperationList;
21import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Brian O'Connor135a95e2014-02-20 13:48:44 -080022import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Brian O'Connor12861f72014-02-19 20:40:32 -080023import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
24
Brian O'Connor5e73c012014-02-20 14:54:34 -080025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Brian O'Connor12861f72014-02-19 20:40:32 -080028public class PlanInstallModule implements IFloodlightModule {
29 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080030 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080031 protected volatile IDatagridService datagridService;
32 protected volatile IFlowPusherService flowPusher;
33 private PlanCalcRuntime planCalc;
34 private PlanInstallRuntime planInstall;
35 private EventListener eventListener;
Brian O'Connor5e73c012014-02-20 14:54:34 -080036 private IEventChannel<Long, IntentOperationList> channel;
Brian O'Connor9b712f62014-02-20 14:22:20 -080037 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
38
Brian O'Connor12861f72014-02-19 20:40:32 -080039
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'Connor135a95e2014-02-20 13:48:44 -080046 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080047 datagridService = context.getServiceImpl(IDatagridService.class);
48 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080049 NetworkGraph graph = networkGraph.getNetworkGraph();
50 planCalc = new PlanCalcRuntime(graph);
51 planInstall = new PlanInstallRuntime(graph, floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080052 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'Connor5e73c012014-02-20 14:54:34 -080068 log.warn("Error taking from intent queue: {}", e.getMessage());
Brian O'Connor12861f72014-02-19 20:40:32 -080069 }
70 }
71 }
72
73 private void processIntents(IntentOperationList intents) {
Brian O'Connor9b712f62014-02-20 14:22:20 -080074 log.debug("Processing OperationList {}", intents);
Brian O'Connor12861f72014-02-19 20:40:32 -080075 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor9b712f62014-02-20 14:22:20 -080076 log.debug("Plan: {}", plan);
Brian O'Connor12861f72014-02-19 20:40:32 -080077 planInstall.installPlan(plan);
78 }
79
80 @Override
81 public void entryAdded(IntentOperationList value) {
Brian O'Connor9b712f62014-02-20 14:22:20 -080082 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -080083 try {
84 intentQueue.put(value);
85 } catch (InterruptedException e) {
86 log.warn("Error putting to intent queue: {}", e.getMessage());
87 }
Brian O'Connor12861f72014-02-19 20:40:32 -080088 }
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'Connor5e73c012014-02-20 14:54:34 -0800102 channel = datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
103 eventListener,
104 Long.class,
105 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800106 eventListener.start();
Brian O'Connor12861f72014-02-19 20:40:32 -0800107 }
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'Connor135a95e2014-02-20 13:48:44 -0800114 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800115 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}