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;
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;
20 * Serializes ofp_match (OpenFlow v1.0) structure
21 * @author michal.polkorab
23 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
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;
31 * Serializes ofp_match (OpenFlow v1.0)
32 * @param outBuffer output ByteBuf
33 * @param match match to be serialized
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());
54 private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
55 int bitmask = ByteBufUtils.fillBitMask(0,
61 wildcards.isNWPROTO(),
64 bitmask |= ByteBufUtils.fillBitMask(20,
65 wildcards.isDLVLANPCP(),
67 bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
68 bitmask |= ((32 - dstMask) << NW_DST_SHIFT);