learning switch code(Analysis)
PacketReceived.class
public interface PacketReceivedextends
DataObject,
Augmentable<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived>,
RawPacket,
Notification
{
public static final QName QNAME =
org.opendaylight.yangtools.yang.common.QName.create("urn:opendaylight:packet:service",
"2013-07-09", "packet-received").intern();
ConnectionCookie getConnectionCookie();
FlowCookie getFlowCookie();
TableId getTableId();
java.lang.Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason> getPacketInReason();
Match getMatch();
}
match
This class represents the following YANG schema fragment defined in module packet-processing(Source path: META-INF/yang/packet-processing.yang):
container match {
leaf in-port {
type node-connector-id;
}
leaf in-phy-port {
type node-connector-id;
}
container metadata {
leaf metadata {
type uint64;
}
leaf metadata-mask {
type uint64;
}
uses of-metadata;
}
RawPacket.class
/*** Basic packet structure.
*
* <p>This class represents the following YANG schema fragment defined in module <b>packet-processing</b>
* <br>(Source path: <i>META-INF/yang/packet-processing.yang</i>):
* <pre>
* grouping raw-packet {
* leaf ingress {
* type node-connector-ref;
* }
* leaf payload {
* type binary;
* }
* }
* </pre>
* The schema path to identify an instance is
* <i>packet-processing/raw-packet</i>
*
*/
public interface RawPacket extend DataObject
{
public static final QName QNAME = org.opendaylight.yangtools.yang.common.QName.create("urn:opendaylight:packet:service",
"2013-07-09", "raw-packet").intern();
NodeConnectorRef getIngress();
byte[] getPayload();
}
InventoryUtils.class
public final class InventoryUtils {
public static final String OPENFLOW_NODE_PREFIX = "openflow:";
public static NodeRef getNodeRef(NodeId nodeId) {
return new NodeRef(InstanceIdentifier.builder(Nodes.class)
.child(Node.class, new NodeKey(nodeId))
.build());
}
public static NodeRef getNodeRef(NodeConnectorRef nodeConnectorRef) {//get node reference providing node connector reference
InstanceIdentifier<Node> nodeIID = nodeConnectorRef.getValue()
.firstIdentifierOf(Node.class);
return new NodeRef(nodeIID);
}
public static NodeId getNodeId(NodeConnectorRef nodeConnectorRef) {
return nodeConnectorRef.getValue()
.firstKeyOf(Node.class, NodeKey.class)
.getId();
}
public static NodeId getNodeId(NodeConnectorId nodeConnectorId) {
if (nodeConnectorId == null)
return null;
String[] tokens = nodeConnectorId.getValue().split(":");
if (tokens.length == 3)
return new NodeId(OPENFLOW_NODE_PREFIX + Long.parseLong(tokens[1]));
else
return null;
}
public static NodeConnectorId getNodeConnectorId(NodeId nodeId, long portNumber) {
if (nodeId == null)
return null;
String nodeConnectorIdStr = nodeId.getValue() + ":" + portNumber;
return new NodeConnectorId(nodeConnectorIdStr);
}
public static NodeConnectorRef getNodeConnectorRef(NodeConnectorId nodeConnectorId) {
NodeId nodeId = getNodeId(nodeConnectorId);
return new NodeConnectorRef(InstanceIdentifier.builder(Nodes.class)
.child(Node.class, new NodeKey(nodeId))
.child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId))
.build());
}
public static NodeConnectorBuilder getNodeConnectorBuilder(NodeConnectorId nodeConnectorId) {
NodeConnectorBuilder builder = new NodeConnectorBuilder()
.setId(nodeConnectorId)
.setKey(new NodeConnectorKey(nodeConnectorId));
return builder;
}
public static NodeConnectorId getNodeConnectorId(NodeConnectorRef nodeConnectorRef) {
return nodeConnectorRef.getValue()
.firstKeyOf(NodeConnector.class, NodeConnectorKey.class)
.getId();
}
}
PacketParsingUtils
package org.sdnhub.odl.tutorial.utils;import java.util.Arrays;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
/**
*
*/
public abstract class PacketParsingUtils {
/**
* size of MAC address in octets (6*8 = 48 bits)
*/
private static final int MAC_ADDRESS_SIZE = 6;
/**
* start position of destination MAC address in array
*/
private static final int DST_MAC_START_POSITION = 0;
/**
* end position of destination MAC address in array
*/
private static final int DST_MAC_END_POSITION = 6;
/**
* start position of source MAC address in array
*/
private static final int SRC_MAC_START_POSITION = 6;
/**
* end position of source MAC address in array
*/
private static final int SRC_MAC_END_POSITION = 12;
/**
* start position of ethernet type in array
*/
private static final int ETHER_TYPE_START_POSITION = 12;
/**
* end position of ethernet type in array
*/
private static final int ETHER_TYPE_END_POSITION = 14;
private PacketParsingUtils() {
//prohibite to instantiate this class
}
/**
* @param payload
* @return destination MAC address
*/
public static byte[] extractDstMac(final byte[] payload) {
return Arrays.copyOfRange(payload, DST_MAC_START_POSITION, DST_MAC_END_POSITION);
}
/**
* @param payload
* @return source MAC address
*/
public static byte[] extractSrcMac(final byte[] payload) {
return Arrays.copyOfRange(payload, SRC_MAC_START_POSITION, SRC_MAC_END_POSITION);
}
/**
* @param payload
* @return source MAC address
*/
public static byte[] extractEtherType(final byte[] payload) {
return Arrays.copyOfRange(payload, ETHER_TYPE_START_POSITION, ETHER_TYPE_END_POSITION);
}
/**
* @param rawMac
* @return {@link MacAddress} wrapping string value, baked upon binary MAC
* address
*/
public static MacAddress rawMacToMac(final byte[] rawMac) {
MacAddress mac = null;
if (rawMac != null && rawMac.length == MAC_ADDRESS_SIZE) {
StringBuilder sb = new StringBuilder();
for (byte octet : rawMac) {
sb.append(String.format(":%02X", octet));
}
mac = new MacAddress(sb.substring(1));
}
return mac;
}
public static String rawMacToString(byte[] rawMac) {
if (rawMac != null && rawMac.length == 6) {
StringBuffer sb = new StringBuffer();
for (byte octet : rawMac) {
sb.append(String.format(":%02X", octet));
}
return sb.substring(1);
}
return null;
}
public static byte[] stringMacToRawMac(String address) {
String[] elements = address.split(":");
if (elements.length != MAC_ADDRESS_SIZE) {
throw new IllegalArgumentException(
"Specified MAC Address must contain 12 hex digits" +
" separated pairwise by :'s.");
}
byte[] addressInBytes = new byte[MAC_ADDRESS_SIZE];
for (int i = 0; i < MAC_ADDRESS_SIZE; i++) {
String element = elements[i];
addressInBytes[i] = (byte)Integer.parseInt(element, 16);
}
return addressInBytes;
}
}
TransmitPacketInputBuilder.class
public class TransmitPacketInputBuilder implements Builder <org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput> {
private List<Action> _action;
private java.lang.Long _bufferId;
private ConnectionCookie _connectionCookie;
private NodeConnectorRef _egress;
private NodeConnectorRef _ingress;
private NodeRef _node;
private byte[] _payload;
public TransmitPacketInputBuilder setIngress(final NodeConnectorRef value) {
this._ingress = value;
return this;
}
public TransmitPacketInputBuilder setNode(final NodeRef value) {
this._node = value;
return this;
}
public TransmitPacketInputBuilder setPayload(final byte[] value) {
this._payload = value;
return this;
}
public TransmitPacketInput build() {
return new TransmitPacketInputImpl(this);
}
PacketProcessingService.class
public interface PacketProcessingService
extends
RpcService
{
/**
* Sending packet out through openflow device.
*
*/
Future<RpcResult<java.lang.Void>> transmitPacket(TransmitPacketInput input);
}
评论
发表评论