OF1.0 fixes
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchSerializer.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.util;\r
3 \r
4 import java.util.HashMap;\r
5 import java.util.Map;\r
6 \r
7 import io.netty.buffer.ByteBuf;\r
8 \r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;\r
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;\r
11 \r
12 /**\r
13  * Serializes ofp_match (OpenFlow v1.0) structure\r
14  * @author michal.polkorab\r
15  */\r
16 public abstract class OF10MatchSerializer {\r
17 \r
18     private static final byte PADDING_IN_MATCH = 1;\r
19     private static final byte PADDING_IN_MATCH_2 = 2;\r
20     private static final byte NW_SRC_SHIFT = 8;\r
21     private static final byte NW_DST_SHIFT = 14;\r
22     private static final int ALL = ((1 << 22) - 1);\r
23     \r
24     /**\r
25      * Encodes ofp_match (OpenFlow v1.0)\r
26      * @param out output ByteBuf that match will be written into\r
27      * @param match match to be encoded\r
28      */\r
29     public static void encodeMatchV10(ByteBuf out, MatchV10 match) {\r
30         out.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));\r
31         out.writeShort(match.getInPort());\r
32         out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));\r
33         out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));\r
34         out.writeShort(match.getDlVlan());\r
35         out.writeByte(match.getDlVlanPcp());\r
36         ByteBufUtils.padBuffer(PADDING_IN_MATCH, out);\r
37         out.writeShort(match.getDlType());\r
38         out.writeByte(match.getNwTos());\r
39         out.writeByte(match.getNwProto());\r
40         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, out);\r
41         String[] srcGroups = match.getNwSrc().getValue().split("\\.");\r
42         for (int i = 0; i < srcGroups.length; i++) {\r
43             out.writeByte(Integer.parseInt(srcGroups[i]));\r
44         }\r
45         String[] dstGroups = match.getNwDst().getValue().split("\\.");\r
46         for (int i = 0; i < dstGroups.length; i++) {\r
47             out.writeByte(Integer.parseInt(dstGroups[i]));\r
48         }\r
49         out.writeShort(match.getTpSrc());\r
50         out.writeShort(match.getTpDst());\r
51     }\r
52     \r
53     private static int encodeWildcards(FlowWildcardsV10 wildcards, short srcMask, short dstMask) {\r
54         int bitmask = 0;\r
55         if (wildcards.isALL()) {\r
56             bitmask |= ALL;\r
57         } else {\r
58             Map<Integer, Boolean> wildcardsMap = new HashMap<>();\r
59             wildcardsMap.put(0, wildcards.isINPORT());\r
60             wildcardsMap.put(1, wildcards.isDLVLAN());\r
61             wildcardsMap.put(2, wildcards.isDLSRC());\r
62             wildcardsMap.put(3, wildcards.isDLDST());\r
63             wildcardsMap.put(4, wildcards.isDLTYPE());\r
64             wildcardsMap.put(5, wildcards.isNWPROTO());\r
65             wildcardsMap.put(6, wildcards.isTPSRC());\r
66             wildcardsMap.put(7, wildcards.isTPDST());\r
67             wildcardsMap.put(20, wildcards.isDLVLANPCP());\r
68             wildcardsMap.put(21, wildcards.isNWTOS());\r
69             bitmask = ByteBufUtils.fillBitMaskFromMap(wildcardsMap);\r
70             bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);\r
71             bitmask |= ((32 - dstMask) << NW_DST_SHIFT);\r
72         }\r
73         return bitmask;\r
74     }\r
75     \r
76 }\r