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 java.util.HashMap;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
20 import com.google.common.base.Splitter;
23 * Serializes ofp_match (OpenFlow v1.0) structure
24 * @author michal.polkorab
26 public class OF10MatchSerializer implements OFSerializer<MatchV10> {
28 private static final byte PADDING_IN_MATCH = 1;
29 private static final byte PADDING_IN_MATCH_2 = 2;
30 private static final byte NW_SRC_SHIFT = 8;
31 private static final byte NW_DST_SHIFT = 14;
34 * Serializes ofp_match (OpenFlow v1.0)
35 * @param outBuffer output ByteBuf
36 * @param match match to be serialized
39 public void serialize(MatchV10 match, ByteBuf outBuffer) {
40 outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
41 outBuffer.writeShort(match.getInPort());
42 outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
43 outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
44 outBuffer.writeShort(match.getDlVlan());
45 outBuffer.writeByte(match.getDlVlanPcp());
46 ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
47 outBuffer.writeShort(match.getDlType());
48 outBuffer.writeByte(match.getNwTos());
49 outBuffer.writeByte(match.getNwProto());
50 ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
51 Iterable<String> srcGroups = Splitter.on(".").split(match.getNwSrc().getValue());
52 for (String group : srcGroups) {
53 outBuffer.writeByte(Short.parseShort(group));
55 Iterable<String> dstGroups = Splitter.on(".").split(match.getNwDst().getValue());
56 for (String group : dstGroups) {
57 outBuffer.writeByte(Short.parseShort(group));
59 outBuffer.writeShort(match.getTpSrc());
60 outBuffer.writeShort(match.getTpDst());
63 private static int encodeWildcards(FlowWildcardsV10 wildcards, short srcMask, short dstMask) {
65 Map<Integer, Boolean> wildcardsMap = new HashMap<>();
66 wildcardsMap.put(0, wildcards.isINPORT());
67 wildcardsMap.put(1, wildcards.isDLVLAN());
68 wildcardsMap.put(2, wildcards.isDLSRC());
69 wildcardsMap.put(3, wildcards.isDLDST());
70 wildcardsMap.put(4, wildcards.isDLTYPE());
71 wildcardsMap.put(5, wildcards.isNWPROTO());
72 wildcardsMap.put(6, wildcards.isTPSRC());
73 wildcardsMap.put(7, wildcards.isTPDST());
74 wildcardsMap.put(20, wildcards.isDLVLANPCP());
75 wildcardsMap.put(21, wildcards.isNWTOS());
76 bitmask = ByteBufUtils.fillBitMaskFromMap(wildcardsMap);
77 bitmask |= ((32 - srcMask) << NW_SRC_SHIFT);
78 bitmask |= ((32 - dstMask) << NW_DST_SHIFT);