blob: d9f1b0f01f2d407338dd9e30f626ff909c27ada7 [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
Brian O'Connor12861f72014-02-19 20:40:32 -080046
47 @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> {
62
63 private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>();
Brian O'Connor488e5ed2014-02-20 19:50:01 -080064 private Long key = Long.valueOf(0);
Brian O'Connor12861f72014-02-19 20:40:32 -080065
66 @Override
67 public void run() {
68 while(true) {
69 try {
70 IntentOperationList intents = intentQueue.take();
Brian O'Connor067b95d2014-02-21 18:43:27 -080071 //TODO: consider draining the remaining intent lists
72 // and processing in one big batch
Brian O'Connor6dc44e92014-02-24 21:23:46 -080073// List<IntentOperationList> remaining = new LinkedList<>();
74// intentQueue.drainTo(remaining);
75
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 }
82
83 private void processIntents(IntentOperationList intents) {
Brian O'Connor9b712f62014-02-20 14:22:20 -080084 log.debug("Processing OperationList {}", intents);
Brian O'Connor12861f72014-02-19 20:40:32 -080085 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor9b712f62014-02-20 14:22:20 -080086 log.debug("Plan: {}", plan);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080087 boolean success = planInstall.installPlan(plan);
88
89 sendNotifications(intents, true, success);
90 }
91
92 private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) {
93 IntentStateList states = new IntentStateList();
94 for(IntentOperation i : intents) {
95 IntentState newState;
96 switch(i.operator) {
97 case REMOVE:
98 if(installed) {
99 newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING;
100 }
101 else {
102 newState = IntentState.DEL_REQ;
103 }
104 break;
105 case ADD:
106 default:
107 if(installed) {
108 newState = success ? IntentState.INST_ACK : IntentState.INST_NACK;
109 }
110 else {
111 newState = IntentState.INST_REQ;
112 }
113 break;
114 }
115 states.put(i.intent.getId(), newState);
116 }
117 intentStateChannel.addEntry(key, states);
118 key += 1;
Brian O'Connor12861f72014-02-19 20:40:32 -0800119 }
120
121 @Override
122 public void entryAdded(IntentOperationList value) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800123 sendNotifications(value, false, false);
124
Brian O'Connor9b712f62014-02-20 14:22:20 -0800125 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -0800126 try {
127 intentQueue.put(value);
128 } catch (InterruptedException e) {
129 log.warn("Error putting to intent queue: {}", e.getMessage());
130 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800131 }
132
133 @Override
134 public void entryRemoved(IntentOperationList value) {
135 // This channel is a queue, so this method is not needed
136 }
137
138 @Override
139 public void entryUpdated(IntentOperationList value) {
140 // This channel is a queue, so this method is not needed
141 }
142 }
143 @Override
144 public void startUp(FloodlightModuleContext context) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800145 // start subscriber
146 datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
Brian O'Connor5e73c012014-02-20 14:54:34 -0800147 eventListener,
148 Long.class,
149 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800150 eventListener.start();
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800151 // start publisher
152 intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME,
153 Long.class,
154 IntentStateList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800155 }
156
157 @Override
158 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
159 Collection<Class<? extends IFloodlightService>> l =
160 new ArrayList<Class<? extends IFloodlightService>>();
161 l.add(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -0800162 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800163 l.add(IDatagridService.class);
164 l.add(IFlowPusherService.class);
165 return l;
166 }
167
168 @Override
169 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800170 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800171 return null;
172 }
173
174 @Override
175 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Brian O'Connor067b95d2014-02-21 18:43:27 -0800176 // no services, for now
Brian O'Connor12861f72014-02-19 20:40:32 -0800177 return null;
178 }
179
180}