Add method to register listener for unknown msg
[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 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
13 import org.opendaylight.openflowjava.util.ByteBufUtils;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
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.rev150225.match.v10.grouping.MatchV10;
18
19 /**
20  * Serializes ofp_match (OpenFlow v1.0) structure
21  * @author michal.polkorab
22  */
23 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
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      * Serializes ofp_match (OpenFlow v1.0)
32      * @param outBuffer output ByteBuf
33      * @param match match to be serialized
34      */
35     @Override
36     public void serialize(final MatchV10 match, final ByteBuf outBuffer) {
37         outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
38         outBuffer.writeShort(match.getInPort());
39         outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(match.getDlSrc()));
40         outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(match.getDlDst()));
41         outBuffer.writeShort(match.getDlVlan());
42         outBuffer.writeByte(match.getDlVlanPcp());
43         outBuffer.writeZero(PADDING_IN_MATCH);
44         outBuffer.writeShort(match.getDlType());
45         outBuffer.writeByte(match.getNwTos());
46         outBuffer.writeByte(match.getNwProto());
47         outBuffer.writeZero(PADDING_IN_MATCH_2);
48         outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressBytes(match.getNwSrc()));
49         outBuffer.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressBytes(match.getNwDst()));
50         outBuffer.writeShort(match.getTpSrc());
51         outBuffer.writeShort(match.getTpDst());
52     }
53
54     private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
55         int bitmask = ByteBufUtils.fillBitMask(0,
56                 wildcards.isINPORT(),
57                 wildcards.isDLVLAN(),
58                 wildcards.isDLSRC(),
59                 wildcards.isDLDST(),
60                 wildcards.isDLTYPE(),
61                 wildcards.isNWPROTO(),
62                 wildcards.isTPSRC(),
63                 wildcards.isTPDST());
64         bitmask |= ByteBufUtils.fillBitMask(20,
65                 wildcards.isDLVLANPCP(),
66                 wildcards.isNWTOS());
67         bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
68         bitmask |= ((32 - dstMask) << NW_DST_SHIFT);
69         return bitmask;
70     }
71
72 }