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
9 package org.opendaylight.openflowplugin.learningswitch;
11 import java.util.Arrays;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 public abstract class PacketUtils {
27 * size of MAC address in octets (6*8 = 48 bits)
29 private static final int MAC_ADDRESS_SIZE = 6;
32 * start position of destination MAC address in array
34 private static final int DST_MAC_START_POSITION = 0;
37 * end position of destination MAC address in array
39 private static final int DST_MAC_END_POSITION = 6;
42 * start position of source MAC address in array
44 private static final int SRC_MAC_START_POSITION = 6;
47 * end position of source MAC address in array
49 private static final int SRC_MAC_END_POSITION = 12;
52 * start position of ethernet type in array
54 private static final int ETHER_TYPE_START_POSITION = 12;
57 * end position of ethernet type in array
59 private static final int ETHER_TYPE_END_POSITION = 14;
61 private PacketUtils() {
62 //prohibite to instantiate this class
67 * @return destination MAC address
69 public static byte[] extractDstMac(final byte[] payload) {
70 return Arrays.copyOfRange(payload, DST_MAC_START_POSITION, DST_MAC_END_POSITION);
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);
83 * @return source MAC address
85 public static byte[] extractEtherType(final byte[] payload) {
86 return Arrays.copyOfRange(payload, ETHER_TYPE_START_POSITION, ETHER_TYPE_END_POSITION);
91 * @return {@link MacAddress} wrapping string value, baked upon binary MAC
94 public static MacAddress rawMacToMac(final byte[] rawMac) {
95 MacAddress mac = null;
96 if (rawMac != null && rawMac.length == MAC_ADDRESS_SIZE) {
97 StringBuilder sb = new StringBuilder();
98 for (byte octet : rawMac) {
99 sb.append(String.format(":%02X", octet));
101 mac = new MacAddress(sb.substring(1));
110 * @return port wrapped into {@link NodeConnectorRef}
112 public static NodeConnectorRef createNodeConnRef(final InstanceIdentifier<Node> nodeInstId, final NodeKey nodeKey, final String port) {
113 StringBuilder sBuild = new StringBuilder(nodeKey.getId().getValue()).append(':').append(port);
114 NodeConnectorKey nConKey = new NodeConnectorKey(new NodeConnectorId(sBuild.toString()));
115 InstanceIdentifier<NodeConnector> portPath = nodeInstId.child(NodeConnector.class, nConKey);
116 return new NodeConnectorRef(portPath);