Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchDeserializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
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;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;
20
21 /**
22  * Deserializes ofp_match (OpenFlow v1.0) structure
23  * @author michal.polkorab
24  */
25 public class OF10MatchDeserializer implements OFDeserializer<MatchV10> {
26
27     private static final byte PADDING_IN_MATCH = 1;
28     private static final byte PADDING_IN_MATCH_2 = 2;
29     private static final byte NW_SRC_BITS = 6;
30     private static final byte NW_SRC_SHIFT = 8;
31     private static final int NW_SRC_MASK = ((1 << NW_SRC_BITS) - 1) << NW_SRC_SHIFT;
32     private static final byte NW_DST_BITS = 6;
33     private static final byte NW_DST_SHIFT = 14;
34     private static final int NW_DST_MASK = ((1 << NW_DST_BITS) - 1) << NW_DST_SHIFT;
35
36     @Override
37     public MatchV10 deserialize(final ByteBuf input) {
38         MatchV10Builder builder = new MatchV10Builder();
39         long wildcards = input.readUnsignedInt();
40         builder.setWildcards(createWildcards(wildcards));
41         builder.setNwSrcMask(decodeNwSrcMask(wildcards));
42         builder.setNwDstMask(decodeNwDstMask(wildcards));
43         builder.setInPort(input.readUnsignedShort());
44         byte[] dlSrc = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
45         input.readBytes(dlSrc);
46         builder.setDlSrc(new MacAddress(ByteBufUtils.macAddressToString(dlSrc)));
47         byte[] dlDst = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
48         input.readBytes(dlDst);
49         builder.setDlDst(new MacAddress(ByteBufUtils.macAddressToString(dlDst)));
50
51         builder.setDlVlan(input.readUnsignedShort());
52         builder.setDlVlanPcp(input.readUnsignedByte());
53         input.skipBytes(PADDING_IN_MATCH);
54         builder.setDlType(input.readUnsignedShort());
55         builder.setNwTos(input.readUnsignedByte());
56         builder.setNwProto(input.readUnsignedByte());
57         input.skipBytes(PADDING_IN_MATCH_2);
58         builder.setNwSrc(new Ipv4Address(ByteBufUtils.readIpv4Address(input)));
59         builder.setNwDst(new Ipv4Address(ByteBufUtils.readIpv4Address(input)));
60         builder.setTpSrc(input.readUnsignedShort());
61         builder.setTpDst(input.readUnsignedShort());
62         return builder.build();
63     }
64
65     /**
66      * Decodes FlowWildcards
67      * @param input input ByteBuf
68      * @return decoded FlowWildcardsV10
69      */
70     public static FlowWildcardsV10 createWildcards(final long input) {
71         boolean _iNPORT = (input & (1 << 0)) != 0;
72         boolean _dLVLAN = (input & (1 << 1)) != 0;
73         boolean _dLSRC = (input & (1 << 2)) != 0;
74         boolean _dLDST = (input & (1 << 3)) != 0;
75         boolean _dLTYPE = (input & (1 << 4)) != 0;
76         boolean _nWPROTO = (input & (1 << 5)) != 0;
77         boolean _tPSRC = (input & (1 << 6)) != 0;
78         boolean _tPDST = (input & (1 << 7)) != 0;
79         boolean _dLVLANPCP = (input & (1 << 20)) != 0;
80         boolean _nWTOS = (input & (1 << 21)) != 0;
81         return new FlowWildcardsV10(_dLDST, _dLSRC, _dLTYPE, _dLVLAN,
82                 _dLVLANPCP, _iNPORT, _nWPROTO, _nWTOS, _tPDST, _tPSRC);
83     }
84
85     /**
86      * Decodes NwSrcMask from FlowWildcards (represented as uint32)
87      * @param input binary FlowWildcards
88      * @return decoded NwSrcMask
89      */
90     public static short decodeNwSrcMask(final long input) {
91         return (short) Math.max(32 - ((input & NW_SRC_MASK) >> NW_SRC_SHIFT), 0);
92     }
93
94     /**
95      * Decodes NwDstMask from FlowWildcards (represented as uint32)
96      * @param input binary FlowWildcards
97      * @return decoded NwDstMask
98      */
99     public static short decodeNwDstMask(final long input) {
100         return (short) Math.max(32 - ((input & NW_DST_MASK) >> NW_DST_SHIFT), 0);
101     }
102 }