Bug 2756 - Match model update
[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.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.rev150225.match.v10.grouping.MatchV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
21
22 /**
23  * Deserializes ofp_match (OpenFlow v1.0) structure
24  * @author michal.polkorab
25  */
26 public class OF10MatchDeserializer implements OFDeserializer<MatchV10> {
27
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;
36
37     @Override
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)));
51
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();
64     }
65
66     /**
67      * Decodes FlowWildcards
68      * @param input input ByteBuf
69      * @return decoded FlowWildcardsV10
70      */
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);
84     }
85
86     /**
87      * Decodes NwSrcMask from FlowWildcards (represented as uint32)
88      * @param input binary FlowWildcards
89      * @return decoded NwSrcMask
90      */
91     public static short decodeNwSrcMask(final long input) {
92         return (short) Math.max(32 - ((input & NW_SRC_MASK) >> NW_SRC_SHIFT), 0);
93     }
94
95     /**
96      * Decodes NwDstMask from FlowWildcards (represented as uint32)
97      * @param input binary FlowWildcards
98      * @return decoded NwDstMask
99      */
100     public static short decodeNwDstMask(final long input) {
101         return (short) Math.max(32 - ((input & NW_DST_MASK) >> NW_DST_SHIFT), 0);
102     }
103 }