blob: 293a33de4c9031be77a9517c92ba9ff76b1df86e [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;
Brian O'Connor6dc44e92014-02-24 21:23:46 -08005import java.util.LinkedList;
Brian O'Connor12861f72014-02-19 20:40:32 -08006import java.util.List;
7import java.util.Map;
8import java.util.Set;
9import java.util.concurrent.BlockingQueue;
10import java.util.concurrent.LinkedBlockingQueue;
11
12import net.floodlightcontroller.core.IFloodlightProviderService;
13import net.floodlightcontroller.core.module.FloodlightModuleContext;
14import net.floodlightcontroller.core.module.FloodlightModuleException;
15import net.floodlightcontroller.core.module.IFloodlightModule;
16import net.floodlightcontroller.core.module.IFloodlightService;
17import net.onrc.onos.datagrid.IDatagridService;
Brian O'Connor5e73c012014-02-20 14:54:34 -080018import net.onrc.onos.datagrid.IEventChannel;
Brian O'Connor12861f72014-02-19 20:40:32 -080019import net.onrc.onos.datagrid.IEventChannelListener;
20import net.onrc.onos.intent.FlowEntry;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080021import net.onrc.onos.intent.Intent.IntentState;
22import net.onrc.onos.intent.IntentOperation;
Brian O'Connor12861f72014-02-19 20:40:32 -080023import net.onrc.onos.intent.IntentOperationList;
24import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Brian O'Connor135a95e2014-02-20 13:48:44 -080025import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080026//import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080027
Brian O'Connor5e73c012014-02-20 14:54:34 -080028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Brian O'Connor12861f72014-02-19 20:40:32 -080031public class PlanInstallModule implements IFloodlightModule {
32 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080033 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080034 protected volatile IDatagridService datagridService;
35 protected volatile IFlowPusherService flowPusher;
36 private PlanCalcRuntime planCalc;
37 private PlanInstallRuntime planInstall;
38 private EventListener eventListener;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080039 private IEventChannel<Long, IntentStateList> intentStateChannel;
Brian O'Connor9b712f62014-02-20 14:22:20 -080040 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
41
Brian O'Connor12861f72014-02-19 20:40:32 -080042
43 private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent";
Brian O'Connor488e5ed2014-02-20 19:50:01 -080044 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
45
Toshio Koide3a52ef32014-02-28 12:10:26 -080046
Brian O'Connor12861f72014-02-19 20:40:32 -080047 @Override
48 public void init(FloodlightModuleContext context)
49 throws FloodlightModuleException {
50 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080051 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080052 datagridService = context.getServiceImpl(IDatagridService.class);
53 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080054// NetworkGraph graph = networkGraph.getNetworkGraph();
55 planCalc = new PlanCalcRuntime();
56 planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080057 eventListener = new EventListener();
58 }
59
60 class EventListener extends Thread
61 implements IEventChannelListener<Long, IntentOperationList> {
Toshio Koide3a52ef32014-02-28 12:10:26 -080062
Brian O'Connor12861f72014-02-19 20:40:32 -080063 private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>();
Brian O'Connor488e5ed2014-02-20 19:50:01 -080064 private Long key = Long.valueOf(0);
Toshio Koide3a52ef32014-02-28 12:10:26 -080065
Brian O'Connor12861f72014-02-19 20:40:32 -080066 @Override
67 public void run() {
68 while(true) {
69 try {
70 IntentOperationList intents = intentQueue.take();
Toshio Koide3a52ef32014-02-28 12:10:26 -080071 //TODO: consider draining the remaining intent lists
Brian O'Connor067b95d2014-02-21 18:43:27 -080072 // and processing in one big batch
Brian O'Connor6dc44e92014-02-24 21:23:46 -080073// List<IntentOperationList> remaining = new LinkedList<>();
74// intentQueue.drainTo(remaining);
Toshio Koide3a52ef32014-02-28 12:10:26 -080075
Brian O'Connor12861f72014-02-19 20:40:32 -080076 processIntents(intents);
77 } catch (InterruptedException e) {
Brian O'Connor5e73c012014-02-20 14:54:34 -080078 log.warn("Error taking from intent queue: {}", e.getMessage());
Brian O'Connor12861f72014-02-19 20:40:32 -080079 }
80 }
81 }
Toshio Koide3a52ef32014-02-28 12:10:26 -080082
Brian O'Connor12861f72014-02-19 20:40:32 -080083 private void processIntents(IntentOperationList intents) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -080084 log("start_processIntents");
Brian O'Connor9b712f62014-02-20 14:22:20 -080085 log.debug("Processing OperationList {}", intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080086 log("begin_computePlan");
Brian O'Connor12861f72014-02-19 20:40:32 -080087 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080088 log("end_computePlan");
Brian O'Connor9b712f62014-02-20 14:22:20 -080089 log.debug("Plan: {}", plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080090 log("begin_installPlan");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080091 boolean success = planInstall.installPlan(plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080092 log("end_installPlan");
Toshio Koide3a52ef32014-02-28 12:10:26 -080093
Brian O'Connor85dd8f22014-02-25 11:43:07 -080094 log("begin_sendInstallNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080095 sendNotifications(intents, true, success);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080096 log("end_sendInstallNotif");
97 log("finish");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080098 }
Toshio Koide3a52ef32014-02-28 12:10:26 -080099
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800100 private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) {
101 IntentStateList states = new IntentStateList();
102 for(IntentOperation i : intents) {
103 IntentState newState;
104 switch(i.operator) {
105 case REMOVE:
106 if(installed) {
107 newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING;
108 }
109 else {
110 newState = IntentState.DEL_REQ;
111 }
112 break;
113 case ADD:
114 default:
115 if(installed) {
116 newState = success ? IntentState.INST_ACK : IntentState.INST_NACK;
117 }
118 else {
119 newState = IntentState.INST_REQ;
120 }
121 break;
122 }
123 states.put(i.intent.getId(), newState);
124 }
125 intentStateChannel.addEntry(key, states);
Toshio Koide3a52ef32014-02-28 12:10:26 -0800126 // XXX: Send notifications using the same key every time
127 // and receive them by entryAdded() and entryUpdated()
128 // key += 1;
Brian O'Connor12861f72014-02-19 20:40:32 -0800129 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800130
Brian O'Connor12861f72014-02-19 20:40:32 -0800131 @Override
132 public void entryAdded(IntentOperationList value) {
Toshio Koide3a52ef32014-02-28 12:10:26 -0800133 entryUpdated(value);
134 }
135
136 @Override
137 public void entryRemoved(IntentOperationList value) {
138 // This channel is a queue, so this method is not needed
139 }
140
141 @Override
142 public void entryUpdated(IntentOperationList value) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800143 log("start_intentNotifRecv");
144 log("begin_sendReceivedNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800145 sendNotifications(value, false, false);
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800146 log("end_sendReceivedNotif");
147 log("finish");
148
Brian O'Connor9b712f62014-02-20 14:22:20 -0800149 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -0800150 try {
151 intentQueue.put(value);
152 } catch (InterruptedException e) {
153 log.warn("Error putting to intent queue: {}", e.getMessage());
154 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800155 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800156 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800157
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800158 public static void log(String step) {
159 log.error("Time:{}, Step:{}", System.nanoTime(), step);
160 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800161
Brian O'Connor12861f72014-02-19 20:40:32 -0800162 @Override
163 public void startUp(FloodlightModuleContext context) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800164 // start subscriber
Toshio Koide3a52ef32014-02-28 12:10:26 -0800165 datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
166 eventListener,
167 Long.class,
Brian O'Connor5e73c012014-02-20 14:54:34 -0800168 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800169 eventListener.start();
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800170 // start publisher
Toshio Koide3a52ef32014-02-28 12:10:26 -0800171 intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME,
172 Long.class,
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800173 IntentStateList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800174 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800175
Brian O'Connor12861f72014-02-19 20:40:32 -0800176 @Override
177 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
178 Collection<Class<? extends IFloodlightService>> l =
179 new ArrayList<Class<? extends IFloodlightService>>();
180 l.add(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -0800181 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800182 l.add(IDatagridService.class);
183 l.add(IFlowPusherService.class);
184 return l;
185 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800186
Brian O'Connor12861f72014-02-19 20:40:32 -0800187 @Override
188 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800189 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800190 return null;
191 }
192
193 @Override
194 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800195 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800196 return null;
197 }
198
199}