blob: adef594f15054fcf1138b59946ef6263e962b831 [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;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080020import net.onrc.onos.intent.Intent.IntentState;
21import net.onrc.onos.intent.IntentOperation;
Brian O'Connor12861f72014-02-19 20:40:32 -080022import net.onrc.onos.intent.IntentOperationList;
23import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Brian O'Connor135a95e2014-02-20 13:48:44 -080024import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080025//import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080026
Brian O'Connor5e73c012014-02-20 14:54:34 -080027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Brian O'Connor12861f72014-02-19 20:40:32 -080030public class PlanInstallModule implements IFloodlightModule {
31 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connor135a95e2014-02-20 13:48:44 -080032 protected volatile INetworkGraphService networkGraph;
Brian O'Connor12861f72014-02-19 20:40:32 -080033 protected volatile IDatagridService datagridService;
34 protected volatile IFlowPusherService flowPusher;
35 private PlanCalcRuntime planCalc;
36 private PlanInstallRuntime planInstall;
37 private EventListener eventListener;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080038 private IEventChannel<Long, IntentStateList> intentStateChannel;
Brian O'Connor9b712f62014-02-20 14:22:20 -080039 private final static Logger log = LoggerFactory.getLogger(PlanInstallModule.class);
40
Brian O'Connor12861f72014-02-19 20:40:32 -080041
42 private static final String PATH_INTENT_CHANNEL_NAME = "onos.pathintent";
Brian O'Connor488e5ed2014-02-20 19:50:01 -080043 private static final String INTENT_STATE_EVENT_CHANNEL_NAME = "onos.pathintent_state";
44
Brian O'Connor12861f72014-02-19 20:40:32 -080045
46 @Override
47 public void init(FloodlightModuleContext context)
48 throws FloodlightModuleException {
49 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -080050 networkGraph = context.getServiceImpl(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -080051 datagridService = context.getServiceImpl(IDatagridService.class);
52 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080053// NetworkGraph graph = networkGraph.getNetworkGraph();
54 planCalc = new PlanCalcRuntime();
55 planInstall = new PlanInstallRuntime(floodlightProvider, flowPusher);
Brian O'Connor12861f72014-02-19 20:40:32 -080056 eventListener = new EventListener();
57 }
58
59 class EventListener extends Thread
60 implements IEventChannelListener<Long, IntentOperationList> {
61
62 private BlockingQueue<IntentOperationList> intentQueue = new LinkedBlockingQueue<>();
Brian O'Connor488e5ed2014-02-20 19:50:01 -080063 private Long key = Long.valueOf(0);
Brian O'Connor12861f72014-02-19 20:40:32 -080064
65 @Override
66 public void run() {
67 while(true) {
68 try {
69 IntentOperationList intents = intentQueue.take();
70 //TODO: drain the remaining intent lists
71 processIntents(intents);
72 } catch (InterruptedException e) {
Brian O'Connor5e73c012014-02-20 14:54:34 -080073 log.warn("Error taking from intent queue: {}", e.getMessage());
Brian O'Connor12861f72014-02-19 20:40:32 -080074 }
75 }
76 }
77
78 private void processIntents(IntentOperationList intents) {
Brian O'Connor9b712f62014-02-20 14:22:20 -080079 log.debug("Processing OperationList {}", intents);
Brian O'Connor12861f72014-02-19 20:40:32 -080080 List<Set<FlowEntry>> plan = planCalc.computePlan(intents);
Brian O'Connor9b712f62014-02-20 14:22:20 -080081 log.debug("Plan: {}", plan);
Brian O'Connor488e5ed2014-02-20 19:50:01 -080082 boolean success = planInstall.installPlan(plan);
83
84 sendNotifications(intents, true, success);
85 }
86
87 private void sendNotifications(IntentOperationList intents, boolean installed, boolean success) {
88 IntentStateList states = new IntentStateList();
89 for(IntentOperation i : intents) {
90 IntentState newState;
91 switch(i.operator) {
92 case REMOVE:
93 if(installed) {
94 newState = success ? IntentState.DEL_ACK : IntentState.DEL_PENDING;
95 }
96 else {
97 newState = IntentState.DEL_REQ;
98 }
99 break;
100 case ADD:
101 default:
102 if(installed) {
103 newState = success ? IntentState.INST_ACK : IntentState.INST_NACK;
104 }
105 else {
106 newState = IntentState.INST_REQ;
107 }
108 break;
109 }
110 states.put(i.intent.getId(), newState);
111 }
112 intentStateChannel.addEntry(key, states);
113 key += 1;
Brian O'Connor12861f72014-02-19 20:40:32 -0800114 }
115
116 @Override
117 public void entryAdded(IntentOperationList value) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800118 sendNotifications(value, false, false);
119
Brian O'Connor9b712f62014-02-20 14:22:20 -0800120 log.debug("Added OperationList {}", value);
Brian O'Connor5e73c012014-02-20 14:54:34 -0800121 try {
122 intentQueue.put(value);
123 } catch (InterruptedException e) {
124 log.warn("Error putting to intent queue: {}", e.getMessage());
125 }
Brian O'Connor12861f72014-02-19 20:40:32 -0800126 }
127
128 @Override
129 public void entryRemoved(IntentOperationList value) {
130 // This channel is a queue, so this method is not needed
131 }
132
133 @Override
134 public void entryUpdated(IntentOperationList value) {
135 // This channel is a queue, so this method is not needed
136 }
137 }
138 @Override
139 public void startUp(FloodlightModuleContext context) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800140 // start subscriber
141 datagridService.addListener(PATH_INTENT_CHANNEL_NAME,
Brian O'Connor5e73c012014-02-20 14:54:34 -0800142 eventListener,
143 Long.class,
144 IntentOperationList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800145 eventListener.start();
Brian O'Connor488e5ed2014-02-20 19:50:01 -0800146 // start publisher
147 intentStateChannel = datagridService.createChannel(INTENT_STATE_EVENT_CHANNEL_NAME,
148 Long.class,
149 IntentStateList.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800150 }
151
152 @Override
153 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
154 Collection<Class<? extends IFloodlightService>> l =
155 new ArrayList<Class<? extends IFloodlightService>>();
156 l.add(IFloodlightProviderService.class);
Brian O'Connor135a95e2014-02-20 13:48:44 -0800157 l.add(INetworkGraphService.class);
Brian O'Connor12861f72014-02-19 20:40:32 -0800158 l.add(IDatagridService.class);
159 l.add(IFlowPusherService.class);
160 return l;
161 }
162
163 @Override
164 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
165 // TODO Auto-generated method stub
166 return null;
167 }
168
169 @Override
170 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
171 // TODO Auto-generated method stub
172 return null;
173 }
174
175}