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