Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / TableFeaturesMatchFieldDeserializerTest.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.UnpooledByteBufAllocator;
14 import java.util.Optional;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpOp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSha;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpSpa;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTha;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.ArpTpa;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthDst;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.EthSrc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Code;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv4Type;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Code;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Icmpv6Type;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPhyPort;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.InPort;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpDscp;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpEcn;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.IpProto;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Dst;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv4Src;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Dst;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Exthdr;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Flabel;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdSll;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTarget;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6NdTll;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Ipv6Src;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.Metadata;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsBos;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsLabel;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.MplsTc;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.PbbIsid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpDst;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.SctpSrc;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpDst;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TcpSrc;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TunnelId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpDst;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.UdpSrc;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanPcp;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.VlanVid;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.set.field.match.SetFieldMatch;
58
59 public class TableFeaturesMatchFieldDeserializerTest {
60     private final TableFeaturesMatchFieldDeserializer deserializer = new TableFeaturesMatchFieldDeserializer();
61
62     @Test
63     public void deserialize() {
64         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
65
66         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ARP_OP);
67         Optional<SetFieldMatch> fieldMatch = deserializer.deserialize(buffer);
68         assertEquals(ArpOp.VALUE, fieldMatch.orElseThrow().key().getMatchType());
69
70         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ARP_SHA);
71         fieldMatch = deserializer.deserialize(buffer);
72         assertEquals(ArpSha.VALUE, fieldMatch.orElseThrow().key().getMatchType());
73
74         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ARP_SPA);
75         fieldMatch = deserializer.deserialize(buffer);
76         assertEquals(ArpSpa.VALUE, fieldMatch.orElseThrow().key().getMatchType());
77
78         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ARP_THA);
79         fieldMatch = deserializer.deserialize(buffer);
80         assertEquals(ArpTha.VALUE, fieldMatch.orElseThrow().key().getMatchType());
81
82         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ARP_TPA);
83         fieldMatch = deserializer.deserialize(buffer);
84         assertEquals(ArpTpa.VALUE, fieldMatch.orElseThrow().key().getMatchType());
85
86         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ETH_DST);
87         fieldMatch = deserializer.deserialize(buffer);
88         assertEquals(EthDst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
89
90         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ETH_SRC);
91         fieldMatch = deserializer.deserialize(buffer);
92         assertEquals(EthSrc.VALUE, fieldMatch.orElseThrow().key().getMatchType());
93
94         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ICMPV4_CODE);
95         fieldMatch = deserializer.deserialize(buffer);
96         assertEquals(Icmpv4Code.VALUE, fieldMatch.orElseThrow().key().getMatchType());
97
98         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ICMPV4_TYPE);
99         fieldMatch = deserializer.deserialize(buffer);
100         assertEquals(Icmpv4Type.VALUE, fieldMatch.orElseThrow().key().getMatchType());
101
102         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ICMPV6_CODE);
103         fieldMatch = deserializer.deserialize(buffer);
104         assertEquals(Icmpv6Code.VALUE, fieldMatch.orElseThrow().key().getMatchType());
105
106         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.ICMPV6_TYPE);
107         fieldMatch = deserializer.deserialize(buffer);
108         assertEquals(Icmpv6Type.VALUE, fieldMatch.orElseThrow().key().getMatchType());
109
110         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IN_PHY_PORT);
111         fieldMatch = deserializer.deserialize(buffer);
112         assertEquals(InPhyPort.VALUE, fieldMatch.orElseThrow().key().getMatchType());
113
114         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IN_PORT);
115         fieldMatch = deserializer.deserialize(buffer);
116         assertEquals(InPort.VALUE, fieldMatch.orElseThrow().key().getMatchType());
117
118         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IP_DSCP);
119         fieldMatch = deserializer.deserialize(buffer);
120         assertEquals(IpDscp.VALUE, fieldMatch.orElseThrow().key().getMatchType());
121
122         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IP_ECN);
123         fieldMatch = deserializer.deserialize(buffer);
124         assertEquals(IpEcn.VALUE, fieldMatch.orElseThrow().key().getMatchType());
125
126         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IP_PROTO);
127         fieldMatch = deserializer.deserialize(buffer);
128         assertEquals(IpProto.VALUE, fieldMatch.orElseThrow().key().getMatchType());
129
130         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV4_SRC);
131         fieldMatch = deserializer.deserialize(buffer);
132         assertEquals(Ipv4Src.VALUE, fieldMatch.orElseThrow().key().getMatchType());
133
134         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV4_DST);
135         fieldMatch = deserializer.deserialize(buffer);
136         assertEquals(Ipv4Dst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
137
138         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_SRC);
139         fieldMatch = deserializer.deserialize(buffer);
140         assertEquals(Ipv6Src.VALUE, fieldMatch.orElseThrow().key().getMatchType());
141
142         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_DST);
143         fieldMatch = deserializer.deserialize(buffer);
144         assertEquals(Ipv6Dst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
145
146         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_EXTHDR);
147         fieldMatch = deserializer.deserialize(buffer);
148         assertEquals(Ipv6Exthdr.VALUE, fieldMatch.orElseThrow().key().getMatchType());
149
150         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_FLABEL);
151         fieldMatch = deserializer.deserialize(buffer);
152         assertEquals(Ipv6Flabel.VALUE, fieldMatch.orElseThrow().key().getMatchType());
153
154         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_ND_SLL);
155         fieldMatch = deserializer.deserialize(buffer);
156         assertEquals(Ipv6NdSll.VALUE, fieldMatch.orElseThrow().key().getMatchType());
157
158         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_ND_TLL);
159         fieldMatch = deserializer.deserialize(buffer);
160         assertEquals(Ipv6NdTll.VALUE, fieldMatch.orElseThrow().key().getMatchType());
161
162         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.IPV6_ND_TARGET);
163         fieldMatch = deserializer.deserialize(buffer);
164         assertEquals(Ipv6NdTarget.VALUE, fieldMatch.orElseThrow().key().getMatchType());
165
166         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.METADATA);
167         fieldMatch = deserializer.deserialize(buffer);
168         assertEquals(Metadata.VALUE, fieldMatch.orElseThrow().key().getMatchType());
169
170         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.MPLS_BOS);
171         fieldMatch = deserializer.deserialize(buffer);
172         assertEquals(MplsBos.VALUE, fieldMatch.orElseThrow().key().getMatchType());
173
174         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.MPLS_LABEL);
175         fieldMatch = deserializer.deserialize(buffer);
176         assertEquals(MplsLabel.VALUE, fieldMatch.orElseThrow().key().getMatchType());
177
178         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.MPLS_TC);
179         fieldMatch = deserializer.deserialize(buffer);
180         assertEquals(MplsTc.VALUE, fieldMatch.orElseThrow().key().getMatchType());
181
182         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.PBB_ISID);
183         fieldMatch = deserializer.deserialize(buffer);
184         assertEquals(PbbIsid.VALUE, fieldMatch.orElseThrow().key().getMatchType());
185
186         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.SCTP_SRC);
187         fieldMatch = deserializer.deserialize(buffer);
188         assertEquals(SctpSrc.VALUE, fieldMatch.orElseThrow().key().getMatchType());
189
190         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.SCTP_DST);
191         fieldMatch = deserializer.deserialize(buffer);
192         assertEquals(SctpDst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
193
194         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.TCP_SRC);
195         fieldMatch = deserializer.deserialize(buffer);
196         assertEquals(TcpSrc.VALUE, fieldMatch.orElseThrow().key().getMatchType());
197
198         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.TCP_DST);
199         fieldMatch = deserializer.deserialize(buffer);
200         assertEquals(TcpDst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
201
202         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.TUNNEL_ID);
203         fieldMatch = deserializer.deserialize(buffer);
204         assertEquals(TunnelId.VALUE, fieldMatch.orElseThrow().key().getMatchType());
205
206         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.UDP_SRC);
207         fieldMatch = deserializer.deserialize(buffer);
208         assertEquals(UdpSrc.VALUE, fieldMatch.orElseThrow().key().getMatchType());
209
210         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.UDP_DST);
211         fieldMatch = deserializer.deserialize(buffer);
212         assertEquals(UdpDst.VALUE, fieldMatch.orElseThrow().key().getMatchType());
213
214         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.VLAN_PCP);
215         fieldMatch = deserializer.deserialize(buffer);
216         assertEquals(VlanPcp.VALUE, fieldMatch.orElseThrow().key().getMatchType());
217
218         addValues(buffer, OxmMatchConstants.OPENFLOW_BASIC_CLASS, OxmMatchConstants.VLAN_VID);
219         fieldMatch = deserializer.deserialize(buffer);
220         assertEquals(VlanVid.VALUE, fieldMatch.orElseThrow().key().getMatchType());
221
222         assertEquals(0, buffer.readableBytes());
223     }
224
225     private static void addValues(final ByteBuf buffer, final int oxmClass, final int oxmField) {
226         buffer.clear();
227         buffer.writeShort(oxmClass);
228         buffer.writeByte(oxmField << 1);
229         buffer.writeByte(EncodeConstants.EMPTY_LENGTH);
230     }
231 }