2 * Copyright (c) 2014 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
8 package org.opendaylight.openflowplugin.learningswitch;
10 import java.util.Arrays;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 public abstract class PacketUtils {
23 * size of MAC address in octets (6*8 = 48 bits).
25 private static final int MAC_ADDRESS_SIZE = 6;
28 * start position of destination MAC address in array.
30 private static final int DST_MAC_START_POSITION = 0;
33 * end position of destination MAC address in array.
35 private static final int DST_MAC_END_POSITION = 6;
38 * start position of source MAC address in array.
40 private static final int SRC_MAC_START_POSITION = 6;
43 * end position of source MAC address in array.
45 private static final int SRC_MAC_END_POSITION = 12;
48 * start position of ethernet type in array.
50 private static final int ETHER_TYPE_START_POSITION = 12;
53 * end position of ethernet type in array.
55 private static final int ETHER_TYPE_END_POSITION = 14;
57 private PacketUtils() {
58 //prohibite to instantiate this class
62 * Extracts the destination MAC address.
64 * @param payload the payload bytes
65 * @return destination MAC address
67 public static byte[] extractDstMac(final byte[] payload) {
68 return Arrays.copyOfRange(payload, DST_MAC_START_POSITION, DST_MAC_END_POSITION);
72 * Extracts the source MAC address.
74 * @param payload the payload bytes
75 * @return source MAC address
77 public static byte[] extractSrcMac(final byte[] payload) {
78 return Arrays.copyOfRange(payload, SRC_MAC_START_POSITION, SRC_MAC_END_POSITION);
82 * Extracts the ethernet type.
84 * @param payload the payload bytes
85 * @return source MAC address
87 public static byte[] extractEtherType(final byte[] payload) {
88 return Arrays.copyOfRange(payload, ETHER_TYPE_START_POSITION, ETHER_TYPE_END_POSITION);
92 * Converts a raw MAC bytes to a MacAddress.
94 * @param rawMac the raw bytes
95 * @return {@link MacAddress} wrapping string value, baked upon binary MAC address
97 public static MacAddress rawMacToMac(final byte[] rawMac) {
98 MacAddress mac = null;
99 if (rawMac != null && rawMac.length == MAC_ADDRESS_SIZE) {
100 StringBuilder sb = new StringBuilder();
101 for (byte octet : rawMac) {
102 sb.append(String.format(":%02X", octet));
104 mac = new MacAddress(sb.substring(1));
110 * Returns the port wrapped into {@link NodeConnectorRef}.
112 public static NodeConnectorRef createNodeConnRef(final InstanceIdentifier<Node> nodeInstId, final NodeKey nodeKey,
114 StringBuilder builder = new StringBuilder(nodeKey.getId().getValue()).append(':').append(port);
115 NodeConnectorKey connKey = new NodeConnectorKey(new NodeConnectorId(builder.toString()));
116 InstanceIdentifier<NodeConnector> portPath = nodeInstId.child(NodeConnector.class, connKey);
117 return new NodeConnectorRef(portPath);