Merge "Bumped to SNAPSHOT dependencies - some dependencies moved to <dependencyManag...
[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     
30     /**
31      * Encodes ofp_match (OpenFlow v1.0)
32      * @param out output ByteBuf that match will be written into
33      * @param match match to be encoded
34      */
35     public static void encodeMatchV10(ByteBuf out, MatchV10 match) {
36         out.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
37         out.writeShort(match.getInPort());
38         out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
39         out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
40         out.writeShort(match.getDlVlan());
41         out.writeByte(match.getDlVlanPcp());
42         ByteBufUtils.padBuffer(PADDING_IN_MATCH, out);
43         out.writeShort(match.getDlType());
44         out.writeByte(match.getNwTos());
45         out.writeByte(match.getNwProto());
46         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, out);
47         String[] srcGroups = match.getNwSrc().getValue().split("\\.");
48         for (int i = 0; i < srcGroups.length; i++) {
49             out.writeByte(Integer.parseInt(srcGroups[i]));
50         }
51         String[] dstGroups = match.getNwDst().getValue().split("\\.");
52         for (int i = 0; i < dstGroups.length; i++) {
53             out.writeByte(Integer.parseInt(dstGroups[i]));
54         }
55         out.writeShort(match.getTpSrc());
56         out.writeShort(match.getTpDst());
57     }
58     
59     private static int encodeWildcards(FlowWildcardsV10 wildcards, short srcMask, short dstMask) {
60         int bitmask = 0;
61         Map<Integer, Boolean> wildcardsMap = new HashMap<>();
62         wildcardsMap.put(0, wildcards.isINPORT());
63         wildcardsMap.put(1, wildcards.isDLVLAN());
64         wildcardsMap.put(2, wildcards.isDLSRC());
65         wildcardsMap.put(3, wildcards.isDLDST());
66         wildcardsMap.put(4, wildcards.isDLTYPE());
67         wildcardsMap.put(5, wildcards.isNWPROTO());
68         wildcardsMap.put(6, wildcards.isTPSRC());
69         wildcardsMap.put(7, wildcards.isTPDST());
70         wildcardsMap.put(20, wildcards.isDLVLANPCP());
71         wildcardsMap.put(21, wildcards.isNWTOS());
72         bitmask = ByteBufUtils.fillBitMaskFromMap(wildcardsMap);
73         bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
74         bitmask |= ((32 - dstMask) << NW_DST_SHIFT);
75         return bitmask;
76     }
77     
78 }