776f139aebc33cdec84f1cf9050484377c518e2f
[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
9 package org.opendaylight.openflowplugin.learningswitch;
10
11 import java.util.Arrays;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.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;
20
21 /**
22  *
23  */
24 public abstract class PacketUtils {
25
26     /**
27      * size of MAC address in octets (6*8 = 48 bits)
28      */
29     private static final int MAC_ADDRESS_SIZE = 6;
30
31     /**
32      * start position of destination MAC address in array
33      */
34     private static final int DST_MAC_START_POSITION = 0;
35
36     /**
37      * end position of destination MAC address in array
38      */
39     private static final int DST_MAC_END_POSITION = 6;
40
41     /**
42      * start position of source MAC address in array
43      */
44     private static final int SRC_MAC_START_POSITION = 6;
45
46     /**
47      * end position of source MAC address in array
48      */
49     private static final int SRC_MAC_END_POSITION = 12;
50
51     /**
52      * start position of ethernet type in array
53      */
54     private static final int ETHER_TYPE_START_POSITION = 12;
55
56     /**
57      * end position of ethernet type in array
58      */
59     private static final int ETHER_TYPE_END_POSITION = 14;
60
61     private PacketUtils() {
62         //prohibite to instantiate this class
63     }
64
65     /**
66      * @param payload
67      * @return destination MAC address
68      */
69     public static byte[] extractDstMac(byte[] payload) {
70         return Arrays.copyOfRange(payload, DST_MAC_START_POSITION, DST_MAC_END_POSITION);
71     }
72
73     /**
74      * @param payload
75      * @return source MAC address
76      */
77     public static byte[] extractSrcMac(byte[] payload) {
78         return Arrays.copyOfRange(payload, SRC_MAC_START_POSITION, SRC_MAC_END_POSITION);
79     }
80
81     /**
82      * @param payload
83      * @return source MAC address
84      */
85     public static byte[] extractEtherType(byte[] payload) {
86         return Arrays.copyOfRange(payload, ETHER_TYPE_START_POSITION, ETHER_TYPE_END_POSITION);
87     }
88
89     /**
90      * @param rawMac
91      * @return {@link MacAddress} wrapping string value, baked upon binary MAC
92      *         address
93      */
94     public static MacAddress rawMacToMac(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));
100             }
101             mac = new MacAddress(sb.substring(1));
102         }
103         return mac;
104     }
105
106     /**
107      * @param nodeInstId
108      * @param nodeKey
109      * @param port
110      * @return port wrapped into {@link NodeConnectorRef}
111      */
112     public static NodeConnectorRef createNodeConnRef(InstanceIdentifier<Node> nodeInstId, NodeKey nodeKey, 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 = InstanceIdentifier.builder(nodeInstId)
116                 .child(NodeConnector.class, nConKey).build();
117         return new NodeConnectorRef(portPath);
118     }
119 }