Optimize mask creation utilities
[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 io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
16
17 /**
18  * Serializes ofp_match (OpenFlow v1.0) structure
19  * @author michal.polkorab
20  */
21 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
22
23     private static final byte PADDING_IN_MATCH = 1;
24     private static final byte PADDING_IN_MATCH_2 = 2;
25     private static final byte NW_SRC_SHIFT = 8;
26     private static final byte NW_DST_SHIFT = 14;
27
28     /**
29      * Serializes ofp_match (OpenFlow v1.0)
30      * @param outBuffer output ByteBuf
31      * @param match match to be serialized
32      */
33     @Override
34     public void serialize(final MatchV10 match, final ByteBuf outBuffer) {
35         outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
36         outBuffer.writeShort(match.getInPort());
37         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
38         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
39         outBuffer.writeShort(match.getDlVlan());
40         outBuffer.writeByte(match.getDlVlanPcp());
41         ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
42         outBuffer.writeShort(match.getDlType());
43         outBuffer.writeByte(match.getNwTos());
44         outBuffer.writeByte(match.getNwProto());
45         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
46         Iterable<String> srcGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwSrc().getValue());
47         for (String group : srcGroups) {
48             outBuffer.writeByte(Short.parseShort(group));
49         }
50         Iterable<String> dstGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwDst().getValue());
51         for (String group : dstGroups) {
52             outBuffer.writeByte(Short.parseShort(group));
53         }
54         outBuffer.writeShort(match.getTpSrc());
55         outBuffer.writeShort(match.getTpDst());
56     }
57
58     private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
59         int bitmask = ByteBufUtils.fillBitMask(0,
60                 wildcards.isINPORT(),
61                 wildcards.isDLVLAN(),
62                 wildcards.isDLSRC(),
63                 wildcards.isDLDST(),
64                 wildcards.isDLTYPE(),
65                 wildcards.isNWPROTO(),
66                 wildcards.isTPSRC(),
67                 wildcards.isTPDST());
68         bitmask |= ByteBufUtils.fillBitMask(20,
69                 wildcards.isDLVLANPCP(),
70                 wildcards.isNWTOS());
71         bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
72         bitmask |= ((32 - dstMask) << NW_DST_SHIFT);
73         return bitmask;
74     }
75
76 }