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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
18 * Serializes ofp_match (OpenFlow v1.0) structure
19 * @author michal.polkorab
21 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
23 private static final byte PADDING_IN_MATCH = 1;
24 private static final byte PADDING_IN_MATCH_2 = 2;
25 private static final byte NW_SRC_SHIFT = 8;
26 private static final byte NW_DST_SHIFT = 14;
29 * Serializes ofp_match (OpenFlow v1.0)
30 * @param outBuffer output ByteBuf
31 * @param match match to be serialized
34 public void serialize(final MatchV10 match, final ByteBuf outBuffer) {
35 outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
36 outBuffer.writeShort(match.getInPort());
37 outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
38 outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
39 outBuffer.writeShort(match.getDlVlan());
40 outBuffer.writeByte(match.getDlVlanPcp());
41 ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
42 outBuffer.writeShort(match.getDlType());
43 outBuffer.writeByte(match.getNwTos());
44 outBuffer.writeByte(match.getNwProto());
45 ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
46 Iterable<String> srcGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwSrc().getValue());
47 for (String group : srcGroups) {
48 outBuffer.writeByte(Short.parseShort(group));
50 Iterable<String> dstGroups = ByteBufUtils.DOT_SPLITTER.split(match.getNwDst().getValue());
51 for (String group : dstGroups) {
52 outBuffer.writeByte(Short.parseShort(group));
54 outBuffer.writeShort(match.getTpSrc());
55 outBuffer.writeShort(match.getTpDst());
58 private static int encodeWildcards(final FlowWildcardsV10 wildcards, final short srcMask, final short dstMask) {
59 int bitmask = ByteBufUtils.fillBitMask(0,
65 wildcards.isNWPROTO(),
68 bitmask |= ByteBufUtils.fillBitMask(20,
69 wildcards.isDLVLANPCP(),
71 bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
72 bitmask |= ((32 - dstMask) << NW_DST_SHIFT);