2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.eclipse.osgi.framework.console.CommandInterpreter;
21 import org.eclipse.osgi.framework.console.CommandProvider;
22 import org.opendaylight.controller.protocol_plugin.openflow.IFlowProgrammerNotifier;
23 import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener;
24 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
25 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageListener;
26 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
27 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Error;
28 import org.openflow.protocol.OFError;
29 import org.openflow.protocol.OFFlowMod;
30 import org.openflow.protocol.OFFlowRemoved;
31 import org.openflow.protocol.OFMessage;
32 import org.openflow.protocol.OFPort;
33 import org.openflow.protocol.OFType;
34 import org.openflow.protocol.action.OFAction;
36 import org.opendaylight.controller.sal.core.ContainerFlow;
37 import org.opendaylight.controller.sal.core.IContainerListener;
38 import org.opendaylight.controller.sal.core.Node;
39 import org.opendaylight.controller.sal.core.Node.NodeIDType;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.core.Property;
42 import org.opendaylight.controller.sal.core.UpdateType;
43 import org.opendaylight.controller.sal.flowprogrammer.Flow;
44 import org.opendaylight.controller.sal.flowprogrammer.IPluginInFlowProgrammerService;
45 import org.opendaylight.controller.sal.match.Match;
46 import org.opendaylight.controller.sal.match.MatchType;
47 import org.opendaylight.controller.sal.utils.GlobalConstants;
48 import org.opendaylight.controller.sal.utils.HexEncode;
49 import org.opendaylight.controller.sal.utils.NodeCreator;
50 import org.opendaylight.controller.sal.utils.StatusCode;
51 import org.opendaylight.controller.sal.utils.Status;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * Represents the openflow plugin component in charge of programming the flows
59 * the flow programming and relay them to functional modules above SAL.
61 public class FlowProgrammerService implements IPluginInFlowProgrammerService,
62 IMessageListener, IContainerListener, IInventoryShimExternalListener,
64 private static final Logger log = LoggerFactory
65 .getLogger(FlowProgrammerService.class);
66 private IController controller;
67 private ConcurrentMap<String, IFlowProgrammerNotifier> flowProgrammerNotifiers;
68 private Map<String, Set<NodeConnector>> containerToNc;
69 private ConcurrentMap<Long, Map<Integer, Long>> xid2rid;
70 private int barrierMessagePriorCount = getBarrierMessagePriorCount();
72 public FlowProgrammerService() {
74 flowProgrammerNotifiers = new ConcurrentHashMap<String, IFlowProgrammerNotifier>();
75 containerToNc = new HashMap<String, Set<NodeConnector>>();
76 xid2rid = new ConcurrentHashMap<Long, Map<Integer, Long>>();
79 public void setController(IController core) {
80 this.controller = core;
83 public void unsetController(IController core) {
84 if (this.controller == core) {
85 this.controller = null;
89 public void setFlowProgrammerNotifier(Map<String, ?> props,
90 IFlowProgrammerNotifier s) {
91 if (props == null || props.get("containerName") == null) {
92 log.error("Didn't receive the service correct properties");
95 String containerName = (String) props.get("containerName");
96 this.flowProgrammerNotifiers.put(containerName, s);
99 public void unsetFlowProgrammerNotifier(Map<String, ?> props,
100 IFlowProgrammerNotifier s) {
101 if (props == null || props.get("containerName") == null) {
102 log.error("Didn't receive the service correct properties");
105 String containerName = (String) props.get("containerName");
106 if (this.flowProgrammerNotifiers != null
107 && this.flowProgrammerNotifiers.containsKey(containerName)
108 && this.flowProgrammerNotifiers.get(containerName) == s) {
109 this.flowProgrammerNotifiers.remove(containerName);
114 * Function called by the dependency manager when all the required
115 * dependencies are satisfied
119 this.controller.addMessageListener(OFType.FLOW_REMOVED, this);
120 registerWithOSGIConsole();
124 * Function called by the dependency manager when at least one dependency
125 * become unsatisfied or when the component is shutting down because for
126 * example bundle is being stopped.
133 * Function called by dependency manager after "init ()" is called and after
134 * the services provided by the class are registered in the service registry
141 * Function called by the dependency manager before the services exported by
142 * the component are unregistered, this will be followed by a "destroy ()"
150 public Status addFlow(Node node, Flow flow) {
151 return addFlowInternal(node, flow, 0);
155 public Status modifyFlow(Node node, Flow oldFlow, Flow newFlow) {
156 return modifyFlowInternal(node, oldFlow, newFlow, 0);
160 public Status removeFlow(Node node, Flow flow) {
161 return removeFlowInternal(node, flow, 0);
165 public Status addFlowAsync(Node node, Flow flow, long rid) {
166 return addFlowInternal(node, flow, rid);
170 public Status modifyFlowAsync(Node node, Flow oldFlow, Flow newFlow,
172 return modifyFlowInternal(node, oldFlow, newFlow, rid);
176 public Status removeFlowAsync(Node node, Flow flow, long rid) {
177 return removeFlowInternal(node, flow, rid);
180 private Status addFlowInternal(Node node, Flow flow, long rid) {
181 String action = "add";
182 if (!node.getType().equals(NodeIDType.OPENFLOW)) {
183 return new Status(StatusCode.NOTACCEPTABLE, errorString("send",
184 action, "Invalid node type"));
187 if (controller != null) {
188 ISwitch sw = controller.getSwitch((Long) node.getID());
190 FlowConverter x = new FlowConverter(flow);
191 OFMessage msg = x.getOFFlowMod(OFFlowMod.OFPFC_ADD, null);
196 * Synchronous message send. Each message is followed by a
199 result = sw.syncSend(msg);
202 * Message will be sent asynchronously. A Barrier message
203 * will be inserted automatically to synchronize the
206 result = asyncMsgSend(node, sw, msg, rid);
208 if (result instanceof Boolean) {
209 return ((Boolean) result == Boolean.TRUE) ? new Status(
210 StatusCode.SUCCESS, null) : new Status(
211 StatusCode.TIMEOUT, errorString(null, action,
212 "Request Timed Out"));
213 } else if (result instanceof OFError) {
214 OFError res = (OFError) result;
215 if (res.getErrorType() == V6Error.NICIRA_VENDOR_ERRORTYPE) {
216 V6Error er = new V6Error(res);
217 byte[] b = res.getError();
218 ByteBuffer bb = ByteBuffer.allocate(b.length);
222 return new Status(StatusCode.INTERNALERROR,
223 errorString("program", action,
224 "Vendor Extension Internal Error"));
226 return new Status(StatusCode.INTERNALERROR, errorString(
227 "program", action, Utils.getOFErrorString(res)));
229 return new Status(StatusCode.INTERNALERROR, errorString(
230 "send", action, "Internal Error"));
233 return new Status(StatusCode.GONE, errorString("send", action,
234 "Switch is not available"));
237 return new Status(StatusCode.INTERNALERROR, errorString("send", action,
238 "Internal plugin error"));
241 private Status modifyFlowInternal(Node node, Flow oldFlow, Flow newFlow, long rid) {
242 String action = "modify";
243 if (!node.getType().equals(NodeIDType.OPENFLOW)) {
244 return new Status(StatusCode.NOTACCEPTABLE, errorString("send",
245 action, "Invalid node type"));
247 if (controller != null) {
248 ISwitch sw = controller.getSwitch((Long) node.getID());
250 OFMessage msg1 = null, msg2 = null;
252 // If priority and match portion are the same, send a
253 // modification message
254 if (oldFlow.getPriority() != newFlow.getPriority()
255 || !oldFlow.getMatch().equals(newFlow.getMatch())) {
256 msg1 = new FlowConverter(oldFlow).getOFFlowMod(
257 OFFlowMod.OFPFC_DELETE_STRICT, OFPort.OFPP_NONE);
258 msg2 = new FlowConverter(newFlow).getOFFlowMod(
259 OFFlowMod.OFPFC_ADD, null);
261 msg1 = new FlowConverter(newFlow).getOFFlowMod(
262 OFFlowMod.OFPFC_MODIFY_STRICT, null);
265 * Synchronous message send
267 action = (msg2 == null) ? "modify" : "delete";
271 * Synchronous message send. Each message is followed by a
274 result = sw.syncSend(msg1);
277 * Message will be sent asynchronously. A Barrier message
278 * will be inserted automatically to synchronize the
281 result = asyncMsgSend(node, sw, msg1, rid);
283 if (result instanceof Boolean) {
284 if ((Boolean) result == Boolean.FALSE) {
285 return new Status(StatusCode.TIMEOUT, errorString(null,
286 action, "Request Timed Out"));
287 } else if (msg2 == null) {
288 return new Status(StatusCode.SUCCESS, null);
290 } else if (result instanceof OFError) {
291 return new Status(StatusCode.INTERNALERROR, errorString(
293 Utils.getOFErrorString((OFError) result)));
295 return new Status(StatusCode.INTERNALERROR, errorString(
296 "send", action, "Internal Error"));
303 * Synchronous message send. Each message is followed by a
306 result = sw.syncSend(msg2);
309 * Message will be sent asynchronously. A Barrier message
310 * will be inserted automatically to synchronize the
313 result = asyncMsgSend(node, sw, msg2, rid);
315 if (result instanceof Boolean) {
316 return ((Boolean) result == Boolean.TRUE) ? new Status(
317 StatusCode.SUCCESS, null) : new Status(
318 StatusCode.TIMEOUT, errorString(null, action,
319 "Request Timed Out"));
320 } else if (result instanceof OFError) {
321 return new Status(StatusCode.INTERNALERROR,
322 errorString("program", action, Utils
323 .getOFErrorString((OFError) result)));
325 return new Status(StatusCode.INTERNALERROR,
326 errorString("send", action, "Internal Error"));
330 return new Status(StatusCode.GONE, errorString("send", action,
331 "Switch is not available"));
334 return new Status(StatusCode.INTERNALERROR, errorString("send", action,
335 "Internal plugin error"));
338 private Status removeFlowInternal(Node node, Flow flow, long rid) {
339 String action = "remove";
340 if (!node.getType().equals(NodeIDType.OPENFLOW)) {
341 return new Status(StatusCode.NOTACCEPTABLE, errorString("send",
342 action, "Invalid node type"));
344 if (controller != null) {
345 ISwitch sw = controller.getSwitch((Long) node.getID());
347 OFMessage msg = new FlowConverter(flow).getOFFlowMod(
348 OFFlowMod.OFPFC_DELETE_STRICT, OFPort.OFPP_NONE);
352 * Synchronous message send. Each message is followed by a
355 result = sw.syncSend(msg);
358 * Message will be sent asynchronously. A Barrier message
359 * will be inserted automatically to synchronize the
362 result = asyncMsgSend(node, sw, msg, rid);
364 if (result instanceof Boolean) {
365 return ((Boolean) result == Boolean.TRUE) ? new Status(
366 StatusCode.SUCCESS, null) : new Status(
367 StatusCode.TIMEOUT, errorString(null, action,
368 "Request Timed Out"));
369 } else if (result instanceof OFError) {
370 return new Status(StatusCode.INTERNALERROR, errorString(
372 Utils.getOFErrorString((OFError) result)));
374 return new Status(StatusCode.INTERNALERROR, errorString(
375 "send", action, "Internal Error"));
378 return new Status(StatusCode.GONE, errorString("send", action,
379 "Switch is not available"));
382 return new Status(StatusCode.INTERNALERROR, errorString("send", action,
383 "Internal plugin error"));
387 public Status removeAllFlows(Node node) {
388 return new Status(StatusCode.SUCCESS, null);
391 private String errorString(String phase, String action, String cause) {
393 + ((phase != null) ? phase + " the " + action
394 + " flow message: " : action + " the flow: ") + cause;
398 public void receive(ISwitch sw, OFMessage msg) {
399 if (msg instanceof OFFlowRemoved) {
400 handleFlowRemovedMessage(sw, (OFFlowRemoved) msg);
404 private void handleFlowRemovedMessage(ISwitch sw, OFFlowRemoved msg) {
405 Node node = NodeCreator.createOFNode(sw.getId());
406 Flow flow = new FlowConverter(msg.getMatch(),
407 new ArrayList<OFAction>(0)).getFlow(node);
408 flow.setPriority(msg.getPriority());
409 flow.setIdleTimeout(msg.getIdleTimeout());
410 flow.setId(msg.getCookie());
412 Match match = flow.getMatch();
413 NodeConnector inPort = match.isPresent(MatchType.IN_PORT) ? (NodeConnector) match
414 .getField(MatchType.IN_PORT).getValue() : null;
416 for (Map.Entry<String, IFlowProgrammerNotifier> containerNotifier : flowProgrammerNotifiers
418 String container = containerNotifier.getKey();
419 IFlowProgrammerNotifier notifier = containerNotifier.getValue();
421 * Switch only provide us with the match information. For now let's
422 * try to identify the container membership only from the input port
423 * match field. In any case, upper layer consumers can derive
424 * whether the notification was not for them. More sophisticated
425 * filtering can be added later on.
428 || container.equals(GlobalConstants.DEFAULT.toString())
429 || this.containerToNc.get(container).contains(inPort)) {
430 notifier.flowRemoved(node, flow);
436 public void tagUpdated(String containerName, Node n, short oldTag,
437 short newTag, UpdateType t) {
442 public void containerFlowUpdated(String containerName,
443 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
447 public void nodeConnectorUpdated(String containerName, NodeConnector p,
449 Set<NodeConnector> target = null;
453 if (!containerToNc.containsKey(containerName)) {
454 containerToNc.put(containerName, new HashSet<NodeConnector>());
456 containerToNc.get(containerName).add(p);
461 target = containerToNc.get(containerName);
462 if (target != null) {
471 public void containerModeUpdated(UpdateType t) {
476 public Status sendBarrierMessage(Node node) {
477 if (!node.getType().equals(NodeIDType.OPENFLOW)) {
478 return new Status(StatusCode.NOTACCEPTABLE,
479 "The node does not support Barrier message.");
482 if (controller != null) {
483 long swid = (Long) node.getID();
484 ISwitch sw = controller.getSwitch(swid);
486 sw.sendBarrierMessage();
488 return (new Status(StatusCode.SUCCESS, null));
490 return new Status(StatusCode.GONE,
491 "The node does not have a valid Switch reference.");
494 return new Status(StatusCode.INTERNALERROR,
495 "Failed to send Barrier message.");
499 * This method sends the message asynchronously until the number of messages
500 * sent reaches a threshold. Then a Barrier message is sent automatically
501 * for sync purpose. An unique Request ID associated with the message is
502 * passed down by the caller. The Request ID will be returned to the caller
503 * when an error message is received from the switch.
510 * The OF message to be sent
515 private Object asyncMsgSend(Node node, ISwitch sw, OFMessage msg, long rid) {
516 Object result = Boolean.TRUE;
517 long swid = (Long) node.getID();
520 xid = sw.asyncSend(msg);
521 addXid2Rid(swid, xid, rid);
523 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
524 if (swxid2rid == null) {
528 int size = swxid2rid.size();
529 if (size % barrierMessagePriorCount == 0) {
530 result = sendBarrierMessage(node);
537 * A number of async messages are sent followed by a synchronous Barrier
538 * message. This method returns the maximum async messages that can be sent
539 * before the Barrier message.
541 * @return The max count of async messages sent prior to Barrier message
543 private int getBarrierMessagePriorCount() {
544 String count = System.getProperty("of.barrierMessagePriorCount");
549 rv = Integer.parseInt(count);
550 } catch (Exception e) {
558 * This method returns the message Request ID previously assigned by the
559 * caller for a given OF message xid
565 * @return The Request ID
567 public long getMessageRid(long swid, int xid) {
568 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
571 if (swxid2rid != null) {
572 rid = swxid2rid.get(xid);
578 * This method returns a copy of outstanding xid to rid mappings.for a given
583 * @return a copy of xid2rid mappings
585 public Map<Integer, Long> getSwXid2Rid(long swid) {
586 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
588 if (swxid2rid != null) {
589 return new HashMap<Integer, Long>(swxid2rid);
591 return new HashMap<Integer, Long>();
596 * Adds xid to rid mapping to the local DB
603 * The message Request ID
605 private void addXid2Rid(long swid, int xid, long rid) {
606 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
607 if (swxid2rid != null) {
608 swxid2rid.put(xid, rid);
613 * When an Error message is received, this method will be invoked to remove
614 * the offending xid from the local DB.
621 private void removeXid2Rid(long swid, int xid) {
622 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
623 if (swxid2rid != null) {
624 swxid2rid.remove(xid);
629 * When a Barrier reply is received, this method will be invoked to clear
635 private void clearXid2Rid(long swid) {
636 Map<Integer, Long> swxid2rid = this.xid2rid.get(swid);
637 if (swxid2rid != null) {
643 public void updateNode(Node node, UpdateType type, Set<Property> props) {
644 long swid = (Long)node.getID();
648 Map<Integer, Long> swxid2rid = new HashMap<Integer, Long>();
649 this.xid2rid.put(swid, swxid2rid);
654 this.xid2rid.remove(swid);
661 public void updateNodeConnector(NodeConnector nodeConnector,
662 UpdateType type, Set<Property> props) {
665 private void registerWithOSGIConsole() {
666 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
668 bundleContext.registerService(CommandProvider.class.getName(), this,
673 public String getHelp() {
674 StringBuffer help = new StringBuffer();
675 help.append("-- Flow Programmer Service --\n");
676 help.append("\t px2r <node id> - Print outstanding xid2rid mappings for a given node id\n");
677 help.append("\t px2rc - Print max num of async msgs prior to the Barrier\n");
678 return help.toString();
681 public void _px2r(CommandInterpreter ci) {
682 String st = ci.nextArgument();
684 ci.println("Please enter a valid node id");
690 sid = HexEncode.stringToLong(st);
691 } catch (NumberFormatException e) {
692 ci.println("Please enter a valid node id");
696 Map<Integer, Long> swxid2rid = this.xid2rid.get(sid);
697 if (swxid2rid == null) {
698 ci.println("The node id entered does not exist");
702 ci.println("xid rid");
704 Set<Integer> xidSet = swxid2rid.keySet();
705 if (xidSet == null) {
709 for (Integer xid : xidSet) {
710 ci.println(xid + " " + swxid2rid.get(xid));
714 public void _px2rc(CommandInterpreter ci) {
715 ci.println("Max num of async messages sent prior to the Barrier message is "
716 + barrierMessagePriorCount);