blob: bbc303d55fc3c4212884cad8355f6b0d8ee9e5d3 [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 Hartaa380972014-04-03 10:24:46 -070020import net.onrc.onos.core.intent.FlowEntry;
21import net.onrc.onos.core.intent.IntentOperation;
22import net.onrc.onos.core.intent.IntentOperationList;
23import net.onrc.onos.core.intent.Intent.IntentState;
Brian O'Connor12861f72014-02-19 20:40:32 -080024import 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
Jonathan Hart6df90172014-04-03 10:13:11 -070028
Jonathan Hartaa380972014-04-03 10:24:46 -070029
Brian O'Connor5e73c012014-02-20 14:54:34 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Brian O'Connor12861f72014-02-19 20:40:32 -080033public class PlanInstallModule implements IFloodlightModule {
34 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080035 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080036 protected volatile IDatagridService datagridService;
37 protected volatile IFlowPusherService flowPusher;
38 private PlanCalcRuntime planCalc;
39 private PlanInstallRuntime planInstall;
40 private EventListener eventListener;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080041 private IEventChannel<Long, IntentStateList> intentStateChannel;
Brian O'Connor9b712f62014-02-20 14:22:20 -080042 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
43
Brian O'Connor12861f72014-02-19 20:40:32 -080044
45 private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent";
Brian O'Connor488e5ed2014-02-20 19:50:01 -080046 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
47
Toshio Koide3a52ef32014-02-28 12:10:26 -080048
Brian O'Connor12861f72014-02-19 20:40:32 -080049 @Override
50 public void init(FloodlightModuleContext context)
51 throws FloodlightModuleException {
52 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080053 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080054 datagridService = context.getServiceImpl(IDatagridService.class);
55 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080056// NetworkGraph graph = networkGraph.getNetworkGraph();
57 planCalc = new PlanCalcRuntime();
58 planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080059 eventListener = new EventListener();
60 }
61
62 class EventListener extends Thread
63 implements IEventChannelListener<Long, IntentOperationList> {
Toshio Koide3a52ef32014-02-28 12:10:26 -080064
Brian O'Connor12861f72014-02-19 20:40:32 -080065 private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>();
Brian O'Connor488e5ed2014-02-20 19:50:01 -080066 private Long key = Long.valueOf(0);
Toshio Koide3a52ef32014-02-28 12:10:26 -080067
Brian O'Connor12861f72014-02-19 20:40:32 -080068 @Override
69 public void run() {
70 while(true) {
71 try {
72 IntentOperationList intents = intentQueue.take();
Toshio Koide3a52ef32014-02-28 12:10:26 -080073 //TODO: consider draining the remaining intent lists
Brian O'Connor067b95d2014-02-21 18:43:27 -080074 // and processing in one big batch
Brian O'Connor6dc44e92014-02-24 21:23:46 -080075// List<IntentOperationList> remaining = new LinkedList<>();
76// intentQueue.drainTo(remaining);
Toshio Koide3a52ef32014-02-28 12:10:26 -080077
Brian O'Connor12861f72014-02-19 20:40:32 -080078 processIntents(intents);
79 } catch (InterruptedException e) {
Brian O'Connor5e73c012014-02-20 14:54:34 -080080 log.warn("Error taking from intent queue: {}", e.getMessage());
Brian O'Connor12861f72014-02-19 20:40:32 -080081 }
82 }
83 }
Toshio Koide3a52ef32014-02-28 12:10:26 -080084
Brian O'Connor12861f72014-02-19 20:40:32 -080085 private void processIntents(IntentOperationList intents) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -080086 log("start_processIntents");
Brian O'Connor9b712f62014-02-20 14:22:20 -080087 log.debug("Processing OperationList {}", intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080088 log("begin_computePlan");
Brian O'Connor12861f72014-02-19 20:40:32 -080089 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080090 log("end_computePlan");
Brian O'Connor9b712f62014-02-20 14:22:20 -080091 log.debug("Plan: {}", plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080092 log("begin_installPlan");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080093 boolean success = planInstall.installPlan(plan);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080094 log("end_installPlan");
Toshio Koide3a52ef32014-02-28 12:10:26 -080095
Brian O'Connor85dd8f22014-02-25 11:43:07 -080096 log("begin_sendInstallNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -080097 sendNotifications(intents, true, success);
Brian O'Connor85dd8f22014-02-25 11:43:07 -080098 log("end_sendInstallNotif");
99 log("finish");
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800100 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800101
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800102 private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) {
103 IntentStateList states = new IntentStateList();
104 for(IntentOperation i : intents) {
105 IntentState newState;
106 switch(i.operator) {
107 case REMOVE:
108 if(installed) {
109 newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING;
110 }
111 else {
112 newState = IntentState.DEL_REQ;
113 }
114 break;
115 case ADD:
116 default:
117 if(installed) {
118 newState = success ? IntentState.INST_ACK : IntentState.INST_NACK;
119 }
120 else {
121 newState = IntentState.INST_REQ;
122 }
123 break;
124 }
125 states.put(i.intent.getId(), newState);
126 }
127 intentStateChannel.addEntry(key, states);
Toshio Koide3a52ef32014-02-28 12:10:26 -0800128 // XXX: Send notifications using the same key every time
129 // and receive them by entryAdded() and entryUpdated()
130 // key += 1;
Brian O'Connor12861f72014-02-19 20:40:32 -0800131 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800132
Brian O'Connor12861f72014-02-19 20:40:32 -0800133 @Override
134 public void entryAdded(IntentOperationList value) {
Toshio Koide3a52ef32014-02-28 12:10:26 -0800135 entryUpdated(value);
136 }
137
138 @Override
139 public void entryRemoved(IntentOperationList value) {
140 // This channel is a queue, so this method is not needed
141 }
142
143 @Override
144 public void entryUpdated(IntentOperationList value) {
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800145 log("start_intentNotifRecv");
146 log("begin_sendReceivedNotif");
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800147 sendNotifications(value, false, false);
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800148 log("end_sendReceivedNotif");
149 log("finish");
150
Brian O'Connor9b712f62014-02-20 14:22:20 -0800151 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -0800152 try {
153 intentQueue.put(value);
154 } catch (InterruptedException e) {
155 log.warn("Error putting to intent queue: {}", e.getMessage());
156 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800157 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800158 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800159
Brian O'Connor85dd8f22014-02-25 11:43:07 -0800160 public static void log(String step) {
161 log.error("Time:{}, Step:{}", System.nanoTime(), step);
162 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800163
Brian O'Connor12861f72014-02-19 20:40:32 -0800164 @Override
165 public void startUp(FloodlightModuleContext context) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800166 // start subscriber
Toshio Koide3a52ef32014-02-28 12:10:26 -0800167 datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
168 eventListener,
169 Long.class,
Brian O'Connor5e73c012014-02-20 14:54:34 -0800170 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800171 eventListener.start();
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800172 // start publisher
Toshio Koide3a52ef32014-02-28 12:10:26 -0800173 intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME,
174 Long.class,
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800175 IntentStateList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800176 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800177
Brian O'Connor12861f72014-02-19 20:40:32 -0800178 @Override
179 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
180 Collection<Class<? extends IFloodlightService>> l =
181 new ArrayList<Class<? extends IFloodlightService>>();
182 l.add(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -0800183 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800184 l.add(IDatagridService.class);
185 l.add(IFlowPusherService.class);
186 return l;
187 }
Toshio Koide3a52ef32014-02-28 12:10:26 -0800188
Brian O'Connor12861f72014-02-19 20:40:32 -0800189 @Override
190 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800191 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800192 return null;
193 }
194
195 @Override
196 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800197 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800198 return null;
199 }
200
201}