blob: b410158e6ab1b5a98adf9e36a8b00334c9c7364a [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070017
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080018import com.google.common.collect.ArrayListMultimap;
alshabib64def642014-12-02 23:27:37 -080019import com.google.common.collect.Lists;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080020import com.google.common.collect.Multimap;
tom7ef8ff92014-09-17 13:08:06 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartbbd91d42015-02-27 11:18:04 -080024import org.apache.felix.scr.annotations.Modified;
Charles Chan3b00e1b2015-08-26 23:12:52 +080025import org.apache.felix.scr.annotations.Property;
alshabibb452fd72015-04-22 20:46:20 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
tom7ef8ff92014-09-17 13:08:06 -070028import org.apache.felix.scr.annotations.Service;
Charles Chan3b00e1b2015-08-26 23:12:52 +080029import org.onosproject.cfg.ComponentConfigService;
Charles Chanecfdfb72015-11-24 19:05:50 -080030import org.onosproject.core.CoreService;
Thomas Vachuska3358af22015-05-19 18:40:34 -070031import org.onosproject.net.driver.DefaultDriverProviderService;
alshabibb452fd72015-04-22 20:46:20 -070032import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext;
34import org.onosproject.openflow.controller.Dpid;
35import org.onosproject.openflow.controller.OpenFlowController;
36import org.onosproject.openflow.controller.OpenFlowEventListener;
37import org.onosproject.openflow.controller.OpenFlowPacketContext;
38import org.onosproject.openflow.controller.OpenFlowSwitch;
39import org.onosproject.openflow.controller.OpenFlowSwitchListener;
40import org.onosproject.openflow.controller.PacketListener;
41import org.onosproject.openflow.controller.RoleState;
42import org.onosproject.openflow.controller.driver.OpenFlowAgent;
Jonathan Hartbbd91d42015-02-27 11:18:04 -080043import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070044import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry;
45import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070046import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus;
47import org.projectfloodlight.openflow.protocol.OFExperimenter;
alshabib64def642014-12-02 23:27:37 -080048import org.projectfloodlight.openflow.protocol.OFFactories;
49import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
50import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070051import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
52import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
sangho6a0bb172015-02-05 12:24:48 -080053import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
54import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
55import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
56import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070057import org.projectfloodlight.openflow.protocol.OFMessage;
58import org.projectfloodlight.openflow.protocol.OFPacketIn;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070059import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho538108b2015-04-08 14:29:20 -070060import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
61import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070062import org.projectfloodlight.openflow.protocol.OFPortStatus;
Ayaka Koshibe38594c22014-10-22 13:36:12 -070063import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib64def642014-12-02 23:27:37 -080064import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
Marc De Leenheerb9311372015-07-09 11:36:49 -070065import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
66import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
tom7ef8ff92014-09-17 13:08:06 -070067import org.slf4j.Logger;
68import org.slf4j.LoggerFactory;
69
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080070import java.util.Collection;
Marc De Leenheerb9311372015-07-09 11:36:49 -070071import java.util.Collections;
Marc De Leenheerb9311372015-07-09 11:36:49 -070072import java.util.LinkedList;
73import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080074import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart6d44d192015-05-11 18:01:19 -070076import java.util.concurrent.CopyOnWriteArraySet;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080077import java.util.concurrent.ExecutorService;
78import java.util.concurrent.Executors;
79import java.util.concurrent.locks.Lock;
80import java.util.concurrent.locks.ReentrantLock;
81
82import static org.onlab.util.Tools.groupedThreads;
tom7ef8ff92014-09-17 13:08:06 -070083
84@Component(immediate = true)
85@Service
86public class OpenFlowControllerImpl implements OpenFlowController {
Charles Chanecfdfb72015-11-24 19:05:50 -080087 private static final String APP_ID = "org.onosproject.openflow-base";
Brian O'Connorff278502015-09-22 14:49:52 -070088 private static final String DEFAULT_OFPORT = "6633,6653";
Charles Chan3b00e1b2015-08-26 23:12:52 +080089 private static final int DEFAULT_WORKER_THREADS = 16;
tom7ef8ff92014-09-17 13:08:06 -070090
91 private static final Logger log =
92 LoggerFactory.getLogger(OpenFlowControllerImpl.class);
93
alshabibb452fd72015-04-22 20:46:20 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanecfdfb72015-11-24 19:05:50 -080095 protected CoreService coreService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibb452fd72015-04-22 20:46:20 -070098 protected DriverService driverService;
99
Thomas Vachuska3358af22015-05-19 18:40:34 -0700100 // References exists merely for sequencing purpose to assure drivers are loaded
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected DefaultDriverProviderService defaultDriverProviderService;
103
Charles Chan3b00e1b2015-08-26 23:12:52 +0800104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ComponentConfigService cfgService;
106
Brian O'Connore2a399e2015-09-22 15:32:50 -0700107 @Property(name = "openflowPorts", value = DEFAULT_OFPORT,
108 label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653")
109 private String openflowPorts = DEFAULT_OFPORT;
Charles Chan3b00e1b2015-08-26 23:12:52 +0800110
111 @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS,
112 label = "Number of controller worker threads; default is 16")
113 private int workerThreads = DEFAULT_WORKER_THREADS;
114
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800115 protected ExecutorService executorMsgs =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800116 Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d"));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800117
118 private final ExecutorService executorBarrier =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800119 Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d"));
alshabib8f1cf4a2014-09-17 14:44:48 -0700120
tom7ef8ff92014-09-17 13:08:06 -0700121 protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700122 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700123 protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700124 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700125 protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700126 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700127
128 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
Jonathan Hart6d44d192015-05-11 18:01:19 -0700129 protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700130
131 protected Multimap<Integer, PacketListener> ofPacketListener =
132 ArrayListMultimap.create();
133
Jonathan Hart6d44d192015-05-11 18:01:19 -0700134 protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700135
sangho6a0bb172015-02-05 12:24:48 -0800136 protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats =
137 ArrayListMultimap.create();
138
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700139 protected Multimap<Dpid, OFTableStatsEntry> fullTableStats =
140 ArrayListMultimap.create();
141
sangho6a0bb172015-02-05 12:24:48 -0800142 protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats =
143 ArrayListMultimap.create();
144
145 protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats =
alshabib64def642014-12-02 23:27:37 -0800146 ArrayListMultimap.create();
147
sangho538108b2015-04-08 14:29:20 -0700148 protected Multimap<Dpid, OFPortStatsEntry> fullPortStats =
149 ArrayListMultimap.create();
150
tom7ef8ff92014-09-17 13:08:06 -0700151 private final Controller ctrl = new Controller();
152
153 @Activate
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800154 public void activate(ComponentContext context) {
Charles Chanecfdfb72015-11-24 19:05:50 -0800155 coreService.registerApplication(APP_ID, this::preDeactivate);
Charles Chan3b00e1b2015-08-26 23:12:52 +0800156 cfgService.registerProperties(getClass());
Brian O'Connorff278502015-09-22 14:49:52 -0700157 ctrl.setConfigParams(context.getProperties());
alshabibb452fd72015-04-22 20:46:20 -0700158 ctrl.start(agent, driverService);
tom7ef8ff92014-09-17 13:08:06 -0700159 }
160
Charles Chanecfdfb72015-11-24 19:05:50 -0800161 private void preDeactivate() {
162 // Close listening channel and all OF channels before deactivating
163 ctrl.stop();
164 connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
165 }
166
tom7ef8ff92014-09-17 13:08:06 -0700167 @Deactivate
168 public void deactivate() {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800169 cfgService.unregisterProperties(getClass(), false);
Charles Chanecfdfb72015-11-24 19:05:50 -0800170 connectedSwitches.clear();
171 activeMasterSwitches.clear();
172 activeEqualSwitches.clear();
tom7ef8ff92014-09-17 13:08:06 -0700173 }
174
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800175 @Modified
176 public void modified(ComponentContext context) {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800177 ctrl.stop();
Brian O'Connorff278502015-09-22 14:49:52 -0700178 ctrl.setConfigParams(context.getProperties());
Charles Chan3b00e1b2015-08-26 23:12:52 +0800179 ctrl.start(agent, driverService);
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800180 }
181
tom7ef8ff92014-09-17 13:08:06 -0700182 @Override
183 public Iterable<OpenFlowSwitch> getSwitches() {
184 return connectedSwitches.values();
185 }
186
187 @Override
188 public Iterable<OpenFlowSwitch> getMasterSwitches() {
189 return activeMasterSwitches.values();
190 }
191
192 @Override
193 public Iterable<OpenFlowSwitch> getEqualSwitches() {
194 return activeEqualSwitches.values();
195 }
196
197 @Override
198 public OpenFlowSwitch getSwitch(Dpid dpid) {
199 return connectedSwitches.get(dpid);
200 }
201
202 @Override
203 public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
204 return activeMasterSwitches.get(dpid);
205 }
206
207 @Override
208 public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
209 return activeEqualSwitches.get(dpid);
210 }
211
212 @Override
213 public void addListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700214 if (!ofSwitchListener.contains(listener)) {
215 this.ofSwitchListener.add(listener);
tom7ef8ff92014-09-17 13:08:06 -0700216 }
217 }
218
219 @Override
220 public void removeListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700221 this.ofSwitchListener.remove(listener);
tom7ef8ff92014-09-17 13:08:06 -0700222 }
223
224 @Override
225 public void addPacketListener(int priority, PacketListener listener) {
226 ofPacketListener.put(priority, listener);
227 }
228
229 @Override
230 public void removePacketListener(PacketListener listener) {
231 ofPacketListener.values().remove(listener);
232 }
233
234 @Override
alshabibeec3a062014-09-17 18:01:26 -0700235 public void addEventListener(OpenFlowEventListener listener) {
236 ofEventListener.add(listener);
237 }
238
239 @Override
240 public void removeEventListener(OpenFlowEventListener listener) {
241 ofEventListener.remove(listener);
242 }
243
244 @Override
tom7ef8ff92014-09-17 13:08:06 -0700245 public void write(Dpid dpid, OFMessage msg) {
246 this.getSwitch(dpid).sendMsg(msg);
247 }
248
249 @Override
250 public void processPacket(Dpid dpid, OFMessage msg) {
sangho6a0bb172015-02-05 12:24:48 -0800251 Collection<OFFlowStatsEntry> flowStats;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700252 Collection<OFTableStatsEntry> tableStats;
sangho6a0bb172015-02-05 12:24:48 -0800253 Collection<OFGroupStatsEntry> groupStats;
254 Collection<OFGroupDescStatsEntry> groupDescStats;
sangho538108b2015-04-08 14:29:20 -0700255 Collection<OFPortStatsEntry> portStats;
sangho6a0bb172015-02-05 12:24:48 -0800256
tom7ef8ff92014-09-17 13:08:06 -0700257 switch (msg.getType()) {
258 case PORT_STATUS:
alshabib8f1cf4a2014-09-17 14:44:48 -0700259 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700260 l.portChanged(dpid, (OFPortStatus) msg);
261 }
262 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700263 case FEATURES_REPLY:
264 for (OpenFlowSwitchListener l : ofSwitchListener) {
265 l.switchChanged(dpid);
266 }
267 break;
tom7ef8ff92014-09-17 13:08:06 -0700268 case PACKET_IN:
269 OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext
270 .packetContextFromPacketIn(this.getSwitch(dpid),
271 (OFPacketIn) msg);
272 for (PacketListener p : ofPacketListener.values()) {
273 p.handlePacket(pktCtx);
274 }
275 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800276 // TODO: Consider using separate threadpool for sensitive messages.
277 // ie. Back to back error could cause us to starve.
278 case FLOW_REMOVED:
279 case ERROR:
280 executorMsgs.submit(new OFMessageHandler(dpid, msg));
281 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700282 case STATS_REPLY:
283 OFStatsReply reply = (OFStatsReply) msg;
sangho6a0bb172015-02-05 12:24:48 -0800284 switch (reply.getStatsType()) {
285 case PORT_DESC:
286 for (OpenFlowSwitchListener l : ofSwitchListener) {
287 l.switchChanged(dpid);
288 }
289 break;
290 case FLOW:
291 flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply);
292 if (flowStats != null) {
293 OFFlowStatsReply.Builder rep =
294 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
295 rep.setEntries(Lists.newLinkedList(flowStats));
ssyoon9030fbcd92015-08-17 10:42:07 +0900296 rep.setXid(reply.getXid());
sangho6a0bb172015-02-05 12:24:48 -0800297 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
298 }
299 break;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700300 case TABLE:
301 tableStats = publishTableStats(dpid, (OFTableStatsReply) reply);
302 if (tableStats != null) {
303 OFTableStatsReply.Builder rep =
304 OFFactories.getFactory(msg.getVersion()).buildTableStatsReply();
305 rep.setEntries(Lists.newLinkedList(tableStats));
306 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
307 }
308 break;
sangho6a0bb172015-02-05 12:24:48 -0800309 case GROUP:
310 groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply);
311 if (groupStats != null) {
312 OFGroupStatsReply.Builder rep =
313 OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply();
314 rep.setEntries(Lists.newLinkedList(groupStats));
315 rep.setXid(reply.getXid());
316 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
317 }
318 break;
319 case GROUP_DESC:
320 groupDescStats = publishGroupDescStats(dpid,
321 (OFGroupDescStatsReply) reply);
322 if (groupDescStats != null) {
323 OFGroupDescStatsReply.Builder rep =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700324 OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply();
sangho6a0bb172015-02-05 12:24:48 -0800325 rep.setEntries(Lists.newLinkedList(groupDescStats));
326 rep.setXid(reply.getXid());
327 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
328 }
329 break;
sangho538108b2015-04-08 14:29:20 -0700330 case PORT:
331 executorMsgs.submit(new OFMessageHandler(dpid, reply));
332 break;
alshabib5eb79392015-08-19 18:09:55 -0700333 case METER:
334 executorMsgs.submit(new OFMessageHandler(dpid, reply));
335 break;
Marc De Leenheerb9311372015-07-09 11:36:49 -0700336 case EXPERIMENTER:
337 if (reply instanceof OFCalientFlowStatsReply) {
338 // Convert Calient flow statistics to regular flow stats
339 // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API
340 OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply();
341 List<OFFlowStatsEntry> entries = new LinkedList<>();
342 for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) {
343
344 // Single instruction, i.e., output to port
345 OFActionOutput action = OFFactories
346 .getFactory(msg.getVersion())
347 .actions()
348 .buildOutput()
349 .setPort(entry.getOutPort())
350 .build();
351 OFInstruction instruction = OFFactories
352 .getFactory(msg.getVersion())
353 .instructions()
354 .applyActions(Collections.singletonList(action));
355 OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry()
356 .setMatch(entry.getMatch())
357 .setTableId(entry.getTableId())
358 .setDurationSec(entry.getDurationSec())
359 .setDurationNsec(entry.getDurationNsec())
360 .setPriority(entry.getPriority())
361 .setIdleTimeout(entry.getIdleTimeout())
362 .setHardTimeout(entry.getHardTimeout())
363 .setFlags(entry.getFlags())
364 .setCookie(entry.getCookie())
365 .setInstructions(Collections.singletonList(instruction))
366 .build();
367 entries.add(fs);
368 }
369 fsr.setEntries(entries);
370
371 flowStats = publishFlowStats(dpid, fsr.build());
372 if (flowStats != null) {
373 OFFlowStatsReply.Builder rep =
374 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
375 rep.setEntries(Lists.newLinkedList(flowStats));
376 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
377 }
378 } else {
Michael Bunimovich1b45e1f2015-08-27 07:49:25 -0400379 executorMsgs.submit(new OFMessageHandler(dpid, reply));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700380 }
381 break;
sangho6a0bb172015-02-05 12:24:48 -0800382 default:
alshabib5eb79392015-08-19 18:09:55 -0700383 log.warn("Discarding unknown stats reply type {}", reply.getStatsType());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700384 break;
alshabib64def642014-12-02 23:27:37 -0800385 }
386 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700387 case BARRIER_REPLY:
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800388 executorBarrier.submit(new OFMessageHandler(dpid, msg));
alshabib8f1cf4a2014-09-17 14:44:48 -0700389 break;
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700390 case EXPERIMENTER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700391 long experimenter = ((OFExperimenter) msg).getExperimenter();
392 if (experimenter == 0x748771) {
393 // LINC-OE port stats
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700394 OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg;
395 OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus();
396 OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc();
397 portDesc.setPortNo(circuitPortStatus.getPortNo())
398 .setHwAddr(circuitPortStatus.getHwAddr())
399 .setName(circuitPortStatus.getName())
400 .setConfig(circuitPortStatus.getConfig())
401 .setState(circuitPortStatus.getState());
402 portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build());
403 for (OpenFlowSwitchListener l : ofSwitchListener) {
404 l.portChanged(dpid, portStatus.build());
405 }
406 } else {
407 log.warn("Handling experimenter type {} not yet implemented",
408 ((OFExperimenter) msg).getExperimenter(), msg);
409 }
410 break;
tom7ef8ff92014-09-17 13:08:06 -0700411 default:
412 log.warn("Handling message type {} not yet implemented {}",
413 msg.getType(), msg);
414 }
415 }
416
sangho6a0bb172015-02-05 12:24:48 -0800417 private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid,
418 OFFlowStatsReply reply) {
alshabib64def642014-12-02 23:27:37 -0800419 //TODO: Get rid of synchronized
sangho6a0bb172015-02-05 12:24:48 -0800420 fullFlowStats.putAll(dpid, reply.getEntries());
alshabib64def642014-12-02 23:27:37 -0800421 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
sangho6a0bb172015-02-05 12:24:48 -0800422 return fullFlowStats.removeAll(dpid);
423 }
424 return null;
425 }
426
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700427 private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid,
428 OFTableStatsReply reply) {
429 //TODO: Get rid of synchronized
430 fullTableStats.putAll(dpid, reply.getEntries());
431 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
432 return fullTableStats.removeAll(dpid);
433 }
434 return null;
435 }
436
sangho6a0bb172015-02-05 12:24:48 -0800437 private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid,
438 OFGroupStatsReply reply) {
439 //TODO: Get rid of synchronized
440 fullGroupStats.putAll(dpid, reply.getEntries());
441 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
442 return fullGroupStats.removeAll(dpid);
443 }
444 return null;
445 }
446
447 private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid,
448 OFGroupDescStatsReply reply) {
449 //TODO: Get rid of synchronized
450 fullGroupDescStats.putAll(dpid, reply.getEntries());
451 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
452 return fullGroupDescStats.removeAll(dpid);
alshabib64def642014-12-02 23:27:37 -0800453 }
454 return null;
455 }
456
sangho538108b2015-04-08 14:29:20 -0700457 private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid,
458 OFPortStatsReply reply) {
459 fullPortStats.putAll(dpid, reply.getEntries());
460 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
461 return fullPortStats.removeAll(dpid);
462 }
463 return null;
464 }
465
tom7ef8ff92014-09-17 13:08:06 -0700466 @Override
467 public void setRole(Dpid dpid, RoleState role) {
Yuta HIGUCHI79cbd1c2014-10-02 16:57:57 -0700468 final OpenFlowSwitch sw = getSwitch(dpid);
469 if (sw == null) {
470 log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role);
471 return;
472 }
473 sw.setRole(role);
tom7ef8ff92014-09-17 13:08:06 -0700474 }
475
476 /**
477 * Implementation of an OpenFlow Agent which is responsible for
478 * keeping track of connected switches and the state in which
479 * they are.
480 */
481 public class OpenFlowSwitchAgent implements OpenFlowAgent {
482
483 private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class);
484 private final Lock switchLock = new ReentrantLock();
485
486 @Override
487 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
alshabib9eab22f2014-10-20 17:17:31 -0700488
tom7ef8ff92014-09-17 13:08:06 -0700489 if (connectedSwitches.get(dpid) != null) {
490 log.error("Trying to add connectedSwitch but found a previous "
491 + "value for dpid: {}", dpid);
492 return false;
493 } else {
Yuta HIGUCHIeb3f30b2014-10-22 11:34:49 -0700494 log.info("Added switch {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700495 connectedSwitches.put(dpid, sw);
alshabib8f1cf4a2014-09-17 14:44:48 -0700496 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700497 l.switchAdded(dpid);
498 }
499 return true;
500 }
501 }
502
503 @Override
504 public boolean validActivation(Dpid dpid) {
505 if (connectedSwitches.get(dpid) == null) {
506 log.error("Trying to activate switch but is not in "
507 + "connected switches: dpid {}. Aborting ..",
508 dpid);
509 return false;
510 }
511 if (activeMasterSwitches.get(dpid) != null ||
512 activeEqualSwitches.get(dpid) != null) {
513 log.error("Trying to activate switch but it is already "
514 + "activated: dpid {}. Found in activeMaster: {} "
Ray Milkey6bc43c22015-11-06 13:22:38 -0800515 + "Found in activeEqual: {}. Aborting ..",
516 dpid,
517 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
518 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y');
tom7ef8ff92014-09-17 13:08:06 -0700519 return false;
520 }
521 return true;
522 }
523
524
525 @Override
526 public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) {
527 switchLock.lock();
528 try {
529 if (!validActivation(dpid)) {
530 return false;
531 }
532 activeMasterSwitches.put(dpid, sw);
533 return true;
534 } finally {
535 switchLock.unlock();
536 }
537 }
538
539 @Override
540 public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) {
541 switchLock.lock();
542 try {
543 if (!validActivation(dpid)) {
544 return false;
545 }
546 activeEqualSwitches.put(dpid, sw);
547 log.info("Added Activated EQUAL Switch {}", dpid);
548 return true;
549 } finally {
550 switchLock.unlock();
551 }
552 }
553
554 @Override
555 public void transitionToMasterSwitch(Dpid dpid) {
556 switchLock.lock();
557 try {
558 if (activeMasterSwitches.containsKey(dpid)) {
559 return;
560 }
561 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
562 if (sw == null) {
563 sw = getSwitch(dpid);
564 if (sw == null) {
565 log.error("Transition to master called on sw {}, but switch "
566 + "was not found in controller-cache", dpid);
567 return;
568 }
569 }
570 log.info("Transitioned switch {} to MASTER", dpid);
571 activeMasterSwitches.put(dpid, sw);
572 } finally {
573 switchLock.unlock();
574 }
575 }
576
577
578 @Override
579 public void transitionToEqualSwitch(Dpid dpid) {
580 switchLock.lock();
581 try {
582 if (activeEqualSwitches.containsKey(dpid)) {
583 return;
584 }
585 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
586 if (sw == null) {
587 sw = getSwitch(dpid);
588 if (sw == null) {
589 log.error("Transition to equal called on sw {}, but switch "
590 + "was not found in controller-cache", dpid);
591 return;
592 }
593 }
594 log.info("Transitioned switch {} to EQUAL", dpid);
595 activeEqualSwitches.put(dpid, sw);
596 } finally {
597 switchLock.unlock();
598 }
599
600 }
601
602 @Override
603 public void removeConnectedSwitch(Dpid dpid) {
604 connectedSwitches.remove(dpid);
605 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
606 if (sw == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700607 log.debug("sw was null for {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700608 sw = activeEqualSwitches.remove(dpid);
609 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700610 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700611 l.switchRemoved(dpid);
612 }
613 }
614
615 @Override
616 public void processMessage(Dpid dpid, OFMessage m) {
617 processPacket(dpid, m);
618 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700619
620 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700621 public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700622 for (OpenFlowSwitchListener l : ofSwitchListener) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700623 l.receivedRoleReply(dpid, requested, response);
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700624 }
625 }
tom7ef8ff92014-09-17 13:08:06 -0700626 }
627
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800628 protected final class OFMessageHandler implements Runnable {
alshabib8f1cf4a2014-09-17 14:44:48 -0700629
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800630 protected final OFMessage msg;
631 protected final Dpid dpid;
alshabib8f1cf4a2014-09-17 14:44:48 -0700632
633 public OFMessageHandler(Dpid dpid, OFMessage msg) {
634 this.msg = msg;
635 this.dpid = dpid;
636 }
637
638 @Override
639 public void run() {
alshabibeec3a062014-09-17 18:01:26 -0700640 for (OpenFlowEventListener listener : ofEventListener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700641 listener.handleMessage(dpid, msg);
642 }
643 }
644
645 }
646
tom7ef8ff92014-09-17 13:08:06 -0700647}