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