Used Splitter instead of String.split() - performance improvement
[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 java.util.HashMap;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
19
20 import com.google.common.base.Splitter;
21
22 /**
23  * Serializes ofp_match (OpenFlow v1.0) structure
24  * @author michal.polkorab
25  */
26 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
27
28     private static final byte PADDING_IN_MATCH = 1;
29     private static final byte PADDING_IN_MATCH_2 = 2;
30     private static final byte NW_SRC_SHIFT = 8;
31     private static final byte NW_DST_SHIFT = 14;
32
33     /**
34      * Serializes ofp_match (OpenFlow v1.0)
35      * @param outBuffer output ByteBuf
36      * @param match match to be serialized
37      */
38     @Override
39     public void serialize(MatchV10 match, ByteBuf outBuffer) {
40         outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
41         outBuffer.writeShort(match.getInPort());
42         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
43         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
44         outBuffer.writeShort(match.getDlVlan());
45         outBuffer.writeByte(match.getDlVlanPcp());
46         ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
47         outBuffer.writeShort(match.getDlType());
48         outBuffer.writeByte(match.getNwTos());
49         outBuffer.writeByte(match.getNwProto());
50         ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
51         Iterable<String> srcGroups = Splitter.on(".").split(match.getNwSrc().getValue());
52         for (String group : srcGroups) {
53             outBuffer.writeByte(Short.parseShort(group));
54         }
55         Iterable<String> dstGroups = Splitter.on(".").split(match.getNwDst().getValue());
56         for (String group : dstGroups) {
57             outBuffer.writeByte(Short.parseShort(group));
58         }
59         outBuffer.writeShort(match.getTpSrc());
60         outBuffer.writeShort(match.getTpDst());
61     }
62
63     private static int encodeWildcards(FlowWildcardsV10 wildcards, short srcMask, short dstMask) {
64         int bitmask = 0;
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         return bitmask;
80     }
81
82 }