blob: b397554577005b4a3382e375911545dbe0cc6fb2 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent.runtime;
Brian O'Connor12861f72014-02-19 20:40:32 -08002
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;
Jonathan Hart6df90172014-04-03 10:13:11 -070017import net.onrc.onos.core.datagrid.IDatagridService;
18import net.onrc.onos.core.datagrid.IEventChannel;
19import net.onrc.onos.core.datagrid.IEventChannelListener;
Jonathan Hart23701d12014-04-03 10:45:48 -070020import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
Jonathan Hartaa380972014-04-03 10:24:46 -070021import net.onrc.onos.core.intent.FlowEntry;
22import net.onrc.onos.core.intent.IntentOperation;
23import net.onrc.onos.core.intent.IntentOperationList;
24import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hart472062d2014-04-03 10:56:48 -070025import net.onrc.onos.core.topology.INetworkGraphService;
26//import net.onrc.onos.core.topology.NetworkGraph;
27
Brian O'Connor12861f72014-02-19 20:40:32 -080028
Jonathan Hart6df90172014-04-03 10:13:11 -070029
Jonathan Hartaa380972014-04-03 10:24:46 -070030
Jonathan Hart23701d12014-04-03 10:45:48 -070031
Brian O'Connor5e73c012014-02-20 14:54:34 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Brian O'Connor12861f72014-02-19 20:40:32 -080035public class PlanInstallModule implements IFloodlightModule {
36 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080037 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080038 protected volatile IDatagridService datagridService;
39 protected volatile IFlowPusherService flowPusher;
40 private PlanCalcRuntime planCalc;
41 private PlanInstallRuntime planInstall;
42 private EventListener eventListener;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080043 private IEventChannel<Long, IntentStateList> intentStateChannel;
Brian O'Connor9b712f62014-02-20 14:22:20 -080044 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
45
Brian O'Connor12861f72014-02-19 20:40:32 -080046
47 private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent";
Brian O'Connor488e5ed2014-02-20 19:50:01 -080048 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
49
Toshio Koide3a52ef32014-02-28 12:10:26 -080050
Brian O'Connor12861f72014-02-19 20:40:32 -080051 @Override
52 public void init(FloodlightModuleContext context)
53 throws FloodlightModuleException {
54 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080055 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080056 datagridService = context.getServiceImpl(IDatagridService.class);
57 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080058// NetworkGraph graph = networkGraph.getNetworkGraph();
59 planCalc = new PlanCalcRuntime();
60 planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080061 eventListener = new EventListener();
62 }
63
64 class EventListener extends Thread
65 implements IEventChannelListener<Long, IntentOperationList> {
Toshio Koide3a52ef32014-02-28 12:10:26 -080066
Brian O'Connor12861f72014-02-19 20:40:32 -080067 private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>();
Brian O'Connor488e5ed2014-02-20 19:50:01 -080068 private Long key = Long.valueOf(0);
Toshio Koide3a52ef32014-02-28 12:10:26 -080069
Brian O'Connor12861f72014-02-19 20:40:32 -080070 @Override
71 public void run() {
72 while(true) {
73 try {
74 IntentOperationList intents = intentQueue.take();
Toshio Koide3a52ef32014-02-28 12:10:26 -080075 //TODO: consider draining the remaining intent lists
Brian O'Connor067b95d2014-02-21 18:43:27 -080076 // and processing in one big batch
Brian O'Connor6dc44e92014-02-24 21:23:46 -080077// List<IntentOperationList> remaining = new LinkedList<>();
78// intentQueue.drainTo(remaining);
Toshio Koide3a52ef32014-02-28 12:10:26 -080079
Brian O'Connor12861f72014-02-19 20:40:32 -080080 processIntents(intents);
81 } catch (InterruptedException e) {
Brian O'Connor5e73c012014-02-20 14:54:34 -080082 log.warn("Error taking from intent queue: {}", e.getMessage());
Brian O'Connor12861f72014-02-19 20:40:32 -080083 }
84 }
85 }
Toshio Koide3a52ef32014-02-28 12:10:26 -080086
Brian O'Connor12861f72014-02-19 20:40:32 -080087 private void processIntents(IntentOperationList intents) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -080088 log("start_processIntents");
Brian O'Connor9b712f62014-02-20 14:22:20 -080089 log.debug("Processing OperationList {}", intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080090 log("begin_computePlan");
Brian O'Connor12861f72014-02-19 20:40:32 -080091 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080092 log("end_computePlan");
Brian O'Connor9b712f62014-02-20 14:22:20 -080093 log.debug("Plan: {}", plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080094 log("begin_installPlan");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080095 boolean success = planInstall.installPlan(plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080096 log("end_installPlan");
Toshio Koide3a52ef32014-02-28 12:10:26 -080097
Brian O'Connor85dd8f22014-02-25 11:43:07 -080098 log("begin_sendInstallNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080099 sendNotifications(intents, true, success);
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800100 log("end_sendInstallNotif");
101 log("finish");
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800102 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800103
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800104 private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) {
105 IntentStateList states = new IntentStateList();
106 for(IntentOperation i : intents) {
107 IntentState newState;
108 switch(i.operator) {
109 case REMOVE:
110 if(installed) {
111 newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING;
112 }
113 else {
114 newState = IntentState.DEL_REQ;
115 }
116 break;
117 case ADD:
118 default:
119 if(installed) {
120 newState = success ? IntentState.INST_ACK : IntentState.INST_NACK;
121 }
122 else {
123 newState = IntentState.INST_REQ;
124 }
125 break;
126 }
127 states.put(i.intent.getId(), newState);
128 }
129 intentStateChannel.addEntry(key, states);
Toshio Koide3a52ef32014-02-28 12:10:26 -0800130 // XXX: Send notifications using the same key every time
131 // and receive them by entryAdded() and entryUpdated()
132 // key += 1;
Brian O'Connor12861f72014-02-19 20:40:32 -0800133 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800134
Brian O'Connor12861f72014-02-19 20:40:32 -0800135 @Override
136 public void entryAdded(IntentOperationList value) {
Toshio Koide3a52ef32014-02-28 12:10:26 -0800137 entryUpdated(value);
138 }
139
140 @Override
141 public void entryRemoved(IntentOperationList value) {
142 // This channel is a queue, so this method is not needed
143 }
144
145 @Override
146 public void entryUpdated(IntentOperationList value) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800147 log("start_intentNotifRecv");
148 log("begin_sendReceivedNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800149 sendNotifications(value, false, false);
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800150 log("end_sendReceivedNotif");
151 log("finish");
152
Brian O'Connor9b712f62014-02-20 14:22:20 -0800153 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -0800154 try {
155 intentQueue.put(value);
156 } catch (InterruptedException e) {
157 log.warn("Error putting to intent queue: {}", e.getMessage());
158 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800159 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800160 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800161
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800162 public static void log(String step) {
163 log.error("Time:{}, Step:{}", System.nanoTime(), step);
164 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800165
Brian O'Connor12861f72014-02-19 20:40:32 -0800166 @Override
167 public void startUp(FloodlightModuleContext context) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800168 // start subscriber
Toshio Koide3a52ef32014-02-28 12:10:26 -0800169 datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
170 eventListener,
171 Long.class,
Brian O'Connor5e73c012014-02-20 14:54:34 -0800172 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800173 eventListener.start();
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800174 // start publisher
Toshio Koide3a52ef32014-02-28 12:10:26 -0800175 intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME,
176 Long.class,
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800177 IntentStateList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800178 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800179
Brian O'Connor12861f72014-02-19 20:40:32 -0800180 @Override
181 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
182 Collection<Class<? extends IFloodlightService>> l =
183 new ArrayList<Class<? extends IFloodlightService>>();
184 l.add(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -0800185 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800186 l.add(IDatagridService.class);
187 l.add(IFlowPusherService.class);
188 return l;
189 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800190
Brian O'Connor12861f72014-02-19 20:40:32 -0800191 @Override
192 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800193 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800194 return null;
195 }
196
197 @Override
198 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800199 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800200 return null;
201 }
202
203}