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.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;
23 * Deserializes ofp_match (OpenFlow v1.0) structure
24 * @author michal.polkorab
26 public class OF10MatchDeserializer implements OFDeserializer<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_BITS = 6;
31 private static final byte NW_SRC_SHIFT = 8;
32 private static final int NW_SRC_MASK = ((1 << NW_SRC_BITS) - 1) << NW_SRC_SHIFT;
33 private static final byte NW_DST_BITS = 6;
34 private static final byte NW_DST_SHIFT = 14;
35 private static final int NW_DST_MASK = ((1 << NW_DST_BITS) - 1) << NW_DST_SHIFT;
38 public MatchV10 deserialize(final ByteBuf input) {
39 MatchV10Builder builder = new MatchV10Builder();
40 long wildcards = input.readUnsignedInt();
41 builder.setWildcards(createWildcards(wildcards));
42 builder.setNwSrcMask(decodeNwSrcMask(wildcards));
43 builder.setNwDstMask(decodeNwDstMask(wildcards));
44 builder.setInPort(input.readUnsignedShort());
45 byte[] dlSrc = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
46 input.readBytes(dlSrc);
47 builder.setDlSrc(new MacAddress(ByteBufUtils.macAddressToString(dlSrc)));
48 byte[] dlDst = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
49 input.readBytes(dlDst);
50 builder.setDlDst(new MacAddress(ByteBufUtils.macAddressToString(dlDst)));
52 builder.setDlVlan(input.readUnsignedShort());
53 builder.setDlVlanPcp(input.readUnsignedByte());
54 input.skipBytes(PADDING_IN_MATCH);
55 builder.setDlType(input.readUnsignedShort());
56 builder.setNwTos(input.readUnsignedByte());
57 builder.setNwProto(input.readUnsignedByte());
58 input.skipBytes(PADDING_IN_MATCH_2);
59 builder.setNwSrc(new Ipv4Address(ByteBufUtils.readIpv4Address(input)));
60 builder.setNwDst(new Ipv4Address(ByteBufUtils.readIpv4Address(input)));
61 builder.setTpSrc(input.readUnsignedShort());
62 builder.setTpDst(input.readUnsignedShort());
63 return builder.build();
67 * Decodes FlowWildcards
68 * @param input input ByteBuf
69 * @return decoded FlowWildcardsV10
71 public static FlowWildcardsV10 createWildcards(final long input) {
72 boolean _iNPORT = (input & (1 << 0)) != 0;
73 boolean _dLVLAN = (input & (1 << 1)) != 0;
74 boolean _dLSRC = (input & (1 << 2)) != 0;
75 boolean _dLDST = (input & (1 << 3)) != 0;
76 boolean _dLTYPE = (input & (1 << 4)) != 0;
77 boolean _nWPROTO = (input & (1 << 5)) != 0;
78 boolean _tPSRC = (input & (1 << 6)) != 0;
79 boolean _tPDST = (input & (1 << 7)) != 0;
80 boolean _dLVLANPCP = (input & (1 << 20)) != 0;
81 boolean _nWTOS = (input & (1 << 21)) != 0;
82 return new FlowWildcardsV10(_dLDST, _dLSRC, _dLTYPE, _dLVLAN,
83 _dLVLANPCP, _iNPORT, _nWPROTO, _nWTOS, _tPDST, _tPSRC);
87 * Decodes NwSrcMask from FlowWildcards (represented as uint32)
88 * @param input binary FlowWildcards
89 * @return decoded NwSrcMask
91 public static short decodeNwSrcMask(final long input) {
92 return (short) Math.max(32 - ((input & NW_SRC_MASK) >> NW_SRC_SHIFT), 0);
96 * Decodes NwDstMask from FlowWildcards (represented as uint32)
97 * @param input binary FlowWildcards
98 * @return decoded NwDstMask
100 public static short decodeNwDstMask(final long input) {
101 return (short) Math.max(32 - ((input & NW_DST_MASK) >> NW_DST_SHIFT), 0);