2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
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;
19 * Serializes ofp_match (OpenFlow v1.0) structure
20 * @author michal.polkorab
22 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
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;
30 * Serializes ofp_match (OpenFlow v1.0)
31 * @param outBuffer output ByteBuf
32 * @param match match to be serialized
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));
51 Iterable<String> dstGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwDst().getValue());
52 for (String group : dstGroups) {
53 outBuffer.writeByte(Short.parseShort(group));
55 outBuffer.writeShort(match.getTpSrc());
56 outBuffer.writeShort(match.getTpDst());
59 private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
60 int bitmask = ByteBufUtils.fillBitMask(0,
66 wildcards.isNWPROTO(),
69 bitmask |= ByteBufUtils.fillBitMask(20,
70 wildcards.isDLVLANPCP(),
72 bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
73 bitmask |= ((32 - dstMask) << NW_DST_SHIFT);