Extensibility support (deserialization part)
[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 java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;
22
23 import com.google.common.base.Joiner;
24
25 /**
26  * Deserializes ofp_match (OpenFlow v1.0) structure
27  * @author michal.polkorab
28  */
29 public class OF10MatchDeserializer implements OFDeserializer<MatchV10> {
30
31     private static final byte PADDING_IN_MATCH = 1;
32     private static final byte PADDING_IN_MATCH_2 = 2;
33     private static final byte NW_SRC_BITS = 6;
34     private static final byte NW_SRC_SHIFT = 8;
35     private static final int NW_SRC_MASK = ((1 << NW_SRC_BITS) - 1) << NW_SRC_SHIFT;
36     private static final byte NW_DST_BITS = 6;
37     private static final byte NW_DST_SHIFT = 14;
38     private static final int NW_DST_MASK = ((1 << NW_DST_BITS) - 1) << NW_DST_SHIFT;
39
40     @Override
41     public MatchV10 deserialize(ByteBuf input) {
42         MatchV10Builder builder = new MatchV10Builder();
43         long wildcards = input.readUnsignedInt();
44         builder.setWildcards(createWildcards(wildcards));
45         builder.setNwSrcMask(decodeNwSrcMask(wildcards));
46         builder.setNwDstMask(decodeNwDstMask(wildcards));
47         builder.setInPort(input.readUnsignedShort());
48         byte[] dlSrc = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
49         input.readBytes(dlSrc);
50         builder.setDlSrc(new MacAddress(ByteBufUtils.macAddressToString(dlSrc)));
51         byte[] dlDst = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
52         input.readBytes(dlDst);
53         builder.setDlDst(new MacAddress(ByteBufUtils.macAddressToString(dlDst)));
54
55         builder.setDlVlan(input.readUnsignedShort());
56         builder.setDlVlanPcp(input.readUnsignedByte());
57         input.skipBytes(PADDING_IN_MATCH);
58         builder.setDlType(input.readUnsignedShort());
59         builder.setNwTos(input.readUnsignedByte());
60         builder.setNwProto(input.readUnsignedByte());
61         input.skipBytes(PADDING_IN_MATCH_2);
62         List<String> srcGroups = new ArrayList<>();
63         for (int i = 0; i < EncodeConstants.GROUPS_IN_IPV4_ADDRESS; i++) {
64             srcGroups.add(Short.toString(input.readUnsignedByte()));
65         }
66         Joiner joiner = Joiner.on(".");
67         builder.setNwSrc(new Ipv4Address(joiner.join(srcGroups)));
68         List<String> dstGroups = new ArrayList<>();
69         for (int i = 0; i < EncodeConstants.GROUPS_IN_IPV4_ADDRESS; i++) {
70             dstGroups.add(Short.toString(input.readUnsignedByte()));
71         }
72         builder.setNwDst(new Ipv4Address(joiner.join(dstGroups)));
73         builder.setTpSrc(input.readUnsignedShort());
74         builder.setTpDst(input.readUnsignedShort());
75         return builder.build();
76     }
77
78     /**
79      * Decodes FlowWildcards
80      * @param input input ByteBuf
81      * @return decoded FlowWildcardsV10
82      */
83     public static FlowWildcardsV10 createWildcards(long input) {
84         boolean _iNPORT = (input & (1 << 0)) != 0;
85         boolean _dLVLAN = (input & (1 << 1)) != 0;
86         boolean _dLSRC = (input & (1 << 2)) != 0;
87         boolean _dLDST = (input & (1 << 3)) != 0;
88         boolean _dLTYPE = (input & (1 << 4)) != 0;
89         boolean _nWPROTO = (input & (1 << 5)) != 0;
90         boolean _tPSRC = (input & (1 << 6)) != 0;
91         boolean _tPDST = (input & (1 << 7)) != 0;
92         boolean _dLVLANPCP = (input & (1 << 20)) != 0;
93         boolean _nWTOS = (input & (1 << 21)) != 0;
94         return new FlowWildcardsV10(_dLDST, _dLSRC, _dLTYPE, _dLVLAN,
95                 _dLVLANPCP, _iNPORT, _nWPROTO, _nWTOS, _tPDST, _tPSRC);
96     }
97     
98     /**
99      * Decodes NwSrcMask from FlowWildcards (represented as uint32)
100      * @param input binary FlowWildcards
101      * @return decoded NwSrcMask
102      */
103     public static short decodeNwSrcMask(long input) {
104         return (short) Math.max(32 - ((input & NW_SRC_MASK) >> NW_SRC_SHIFT), 0);
105     }
106     
107     /**
108      * Decodes NwDstMask from FlowWildcards (represented as uint32)
109      * @param input binary FlowWildcards
110      * @return decoded NwDstMask
111      */
112     public static short decodeNwDstMask(long input) {
113         return (short) Math.max(32 - ((input & NW_DST_MASK) >> NW_DST_SHIFT), 0);
114     }
115 }