Merge "OPNFLWPLUG-1076 Migrate lldp-speaker, forwardingrules-sync and arbitratorrecon...
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / PacketUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.learningswitch;
9
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;
19
20 public abstract class PacketUtils {
21
22     /**
23      * size of MAC address in octets (6*8 = 48 bits).
24      */
25     private static final int MAC_ADDRESS_SIZE = 6;
26
27     /**
28      * start position of destination MAC address in array.
29      */
30     private static final int DST_MAC_START_POSITION = 0;
31
32     /**
33      * end position of destination MAC address in array.
34      */
35     private static final int DST_MAC_END_POSITION = 6;
36
37     /**
38      * start position of source MAC address in array.
39      */
40     private static final int SRC_MAC_START_POSITION = 6;
41
42     /**
43      * end position of source MAC address in array.
44      */
45     private static final int SRC_MAC_END_POSITION = 12;
46
47     /**
48      * start position of ethernet type in array.
49      */
50     private static final int ETHER_TYPE_START_POSITION = 12;
51
52     /**
53      * end position of ethernet type in array.
54      */
55     private static final int ETHER_TYPE_END_POSITION = 14;
56
57     private PacketUtils() {
58         //prohibite to instantiate this class
59     }
60
61     /**
62      * Extracts the destination MAC address.
63      *
64      * @param payload the payload bytes
65      * @return destination MAC address
66      */
67     public static byte[] extractDstMac(final byte[] payload) {
68         return Arrays.copyOfRange(payload, DST_MAC_START_POSITION, DST_MAC_END_POSITION);
69     }
70
71     /**
72      * Extracts the source MAC address.
73      *
74      * @param payload the payload bytes
75      * @return source MAC address
76      */
77     public static byte[] extractSrcMac(final byte[] payload) {
78         return Arrays.copyOfRange(payload, SRC_MAC_START_POSITION, SRC_MAC_END_POSITION);
79     }
80
81     /**
82      * Extracts the ethernet type.
83      *
84      * @param payload the payload bytes
85      * @return source MAC address
86      */
87     public static byte[] extractEtherType(final byte[] payload) {
88         return Arrays.copyOfRange(payload, ETHER_TYPE_START_POSITION, ETHER_TYPE_END_POSITION);
89     }
90
91     /**
92      * Converts a raw MAC bytes to a MacAddress.
93      *
94      * @param rawMac the raw bytes
95      * @return {@link MacAddress} wrapping string value, baked upon binary MAC address
96      */
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));
103             }
104             mac = new MacAddress(sb.substring(1));
105         }
106         return mac;
107     }
108
109     /**
110      * Returns the port wrapped into {@link NodeConnectorRef}.
111      */
112     public static NodeConnectorRef createNodeConnRef(final InstanceIdentifier<Node> nodeInstId, final NodeKey nodeKey,
113             final String port) {
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);
118     }
119 }