Fixed a bug that failed specifying the value 0 on VLAN ID field for a match condition...
[controller.git] / opendaylight / protocol_plugins / openflow / src / test / java / org / opendaylight / controller / protocol_plugin / openflow / internal / FlowProgrammerServiceTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.protocol_plugin.openflow.internal.FlowConverter;
21 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match;
22 import org.openflow.protocol.OFMatch;
23 import org.openflow.protocol.action.OFAction;
24
25 import org.opendaylight.controller.sal.action.Action;
26 import org.opendaylight.controller.sal.action.Flood;
27 import org.opendaylight.controller.sal.action.FloodAll;
28 import org.opendaylight.controller.sal.action.HwPath;
29 import org.opendaylight.controller.sal.action.Loopback;
30 import org.opendaylight.controller.sal.action.Output;
31 import org.opendaylight.controller.sal.action.PopVlan;
32 import org.opendaylight.controller.sal.action.SetDlDst;
33 import org.opendaylight.controller.sal.action.SetDlSrc;
34 import org.opendaylight.controller.sal.action.SetNwDst;
35 import org.opendaylight.controller.sal.action.SetNwSrc;
36 import org.opendaylight.controller.sal.action.SetNwTos;
37 import org.opendaylight.controller.sal.action.SetTpDst;
38 import org.opendaylight.controller.sal.action.SetTpSrc;
39 import org.opendaylight.controller.sal.action.SetVlanId;
40 import org.opendaylight.controller.sal.action.SwPath;
41 import org.opendaylight.controller.sal.core.Node;
42 import org.opendaylight.controller.sal.core.NodeConnector;
43 import org.opendaylight.controller.sal.flowprogrammer.Flow;
44 import org.opendaylight.controller.sal.match.Match;
45 import org.opendaylight.controller.sal.match.MatchType;
46 import org.opendaylight.controller.sal.utils.EtherTypes;
47 import org.opendaylight.controller.sal.utils.IPProtocols;
48 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
49 import org.opendaylight.controller.sal.utils.NodeCreator;
50
51 public class FlowProgrammerServiceTest {
52
53     @Test
54     public void testSALtoOFFlowConverter() throws UnknownHostException {
55         Node node = NodeCreator.createOFNode(1000l);
56         NodeConnector port = NodeConnectorCreator.createNodeConnector(
57                 (short) 24, node);
58         NodeConnector oport = NodeConnectorCreator.createNodeConnector(
59                 (short) 30, node);
60         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
61                 (byte) 0x9a, (byte) 0xbc };
62         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
63                 (byte) 0x5e, (byte) 0x6f };
64         InetAddress srcIP = InetAddress.getByName("172.28.30.50");
65         InetAddress dstIP = InetAddress.getByName("171.71.9.52");
66         InetAddress ipMask = InetAddress.getByName("255.255.255.0");
67         short ethertype = EtherTypes.IPv4.shortValue();
68         short vlan = (short) 27;
69         byte vlanPr = 3;
70         Byte tos = 4;
71         byte proto = IPProtocols.TCP.byteValue();
72         short src = (short) 55000;
73         short dst = 80;
74
75         /*
76          * Create a SAL Flow aFlow
77          */
78         Match match = new Match();
79         match.setField(MatchType.IN_PORT, port);
80         match.setField(MatchType.DL_SRC, srcMac);
81         match.setField(MatchType.DL_DST, dstMac);
82         match.setField(MatchType.DL_TYPE, ethertype);
83         match.setField(MatchType.DL_VLAN, vlan);
84         match.setField(MatchType.DL_VLAN_PR, vlanPr);
85         match.setField(MatchType.NW_SRC, srcIP, ipMask);
86         match.setField(MatchType.NW_DST, dstIP, ipMask);
87         match.setField(MatchType.NW_TOS, tos);
88         match.setField(MatchType.NW_PROTO, proto);
89         match.setField(MatchType.TP_SRC, src);
90         match.setField(MatchType.TP_DST, dst);
91
92         Assert.assertTrue(match.isIPv4());
93
94         List<Action> actions = new ArrayList<Action>();
95         // Setting all the actions supported by of
96         actions.add(new PopVlan());
97         actions.add(new Output(oport));
98         actions.add(new Flood());
99         actions.add(new FloodAll());
100         actions.add(new SwPath());
101         actions.add(new HwPath());
102         actions.add(new Loopback());
103         byte mac[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
104         actions.add(new SetDlSrc(mac));
105         actions.add(new SetDlDst(mac));
106         actions.add(new SetNwSrc(dstIP));
107         actions.add(new SetNwDst(srcIP));
108         actions.add(new SetNwTos(3));
109         actions.add(new SetTpSrc(10));
110         actions.add(new SetTpDst(20));
111         actions.add(new SetVlanId(200));
112
113         Flow aFlow = new Flow(match, actions);
114
115         /*
116          * Convert the SAL aFlow to OF Flow
117          */
118         FlowConverter salToOF = new FlowConverter(aFlow);
119         OFMatch ofMatch = salToOF.getOFMatch();
120         List<OFAction> ofActions = salToOF.getOFActions();
121
122         /*
123          * Convert the OF Flow to SAL Flow bFlow
124          */
125         FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
126         Flow bFlow = ofToSal.getFlow(node);
127         Match bMatch = bFlow.getMatch();
128         List<Action> bActions = bFlow.getActions();
129
130         /*
131          * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
132          */
133         Assert.assertTrue(((NodeConnector) match.getField(MatchType.IN_PORT)
134                 .getValue()).equals(((NodeConnector) bMatch.getField(
135                 MatchType.IN_PORT).getValue())));
136         Assert.assertTrue(Arrays.equals((byte[]) match.getField(
137                 MatchType.DL_SRC).getValue(), (byte[]) bMatch.getField(
138                 MatchType.DL_SRC).getValue()));
139         Assert.assertTrue(Arrays.equals((byte[]) match.getField(
140                 MatchType.DL_DST).getValue(), (byte[]) bMatch.getField(
141                 MatchType.DL_DST).getValue()));
142         Assert
143                 .assertTrue(((Short) match.getField(MatchType.DL_TYPE)
144                         .getValue()).equals((Short) bMatch.getField(
145                         MatchType.DL_TYPE).getValue()));
146         Assert
147                 .assertTrue(((Short) match.getField(MatchType.DL_VLAN)
148                         .getValue()).equals((Short) bMatch.getField(
149                         MatchType.DL_VLAN).getValue()));
150         Assert.assertTrue(((Byte) match.getField(MatchType.DL_VLAN_PR)
151                 .getValue()).equals((Byte) bMatch
152                 .getField(MatchType.DL_VLAN_PR).getValue()));
153         Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_SRC)
154                 .getValue()).equals((InetAddress) bMatch.getField(
155                 MatchType.NW_SRC).getValue()));
156         Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_SRC)
157                 .getMask()).equals((InetAddress) bMatch.getField(
158                 MatchType.NW_SRC).getMask()));
159         Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_DST)
160                 .getValue()).equals((InetAddress) bMatch.getField(
161                 MatchType.NW_DST).getValue()));
162         Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_DST)
163                 .getMask()).equals((InetAddress) bMatch.getField(
164                 MatchType.NW_DST).getMask()));
165         Assert
166                 .assertTrue(((Byte) match.getField(MatchType.NW_PROTO)
167                         .getValue()).equals((Byte) bMatch.getField(
168                         MatchType.NW_PROTO).getValue()));
169         Assert.assertTrue(((Byte) match.getField(MatchType.NW_TOS).getValue())
170                 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
171         Assert.assertTrue(((Short) match.getField(MatchType.TP_SRC).getValue())
172                 .equals((Short) bMatch.getField(MatchType.TP_SRC).getValue()));
173         Assert.assertTrue(((Short) match.getField(MatchType.TP_DST).getValue())
174                 .equals((Short) bMatch.getField(MatchType.TP_DST).getValue()));
175
176         // FlowConverter parses and sets the actions in the same order for sal match and of match
177         for (short i = 0; i < actions.size(); i++) {
178             Assert.assertTrue(actions.get(i).equals(bActions.get(i)));
179         }
180     }
181
182     @Test
183     public void testVlanNoneIdFlowConversion() throws Exception {
184         Node node = NodeCreator.createOFNode(1000l);
185
186         /*
187          * The value 0 is used to indicate that no VLAN ID is set
188          * for SAL Flow.
189          */
190         short vlan = (short) 0;
191
192         /*
193          * Create a SAL Flow aFlow
194          */
195         Match match = new Match();
196         match.setField(MatchType.DL_VLAN, vlan);
197
198         List<Action> actions = new ArrayList<Action>();
199
200         Flow aFlow = new Flow(match, actions);
201
202         /*
203          * Convert the SAL aFlow to OF Flow
204          */
205         FlowConverter salToOF = new FlowConverter(aFlow);
206         OFMatch ofMatch = salToOF.getOFMatch();
207         List<OFAction> ofActions = salToOF.getOFActions();
208
209         /*
210          * The value 0xffff (OFP_VLAN_NONE) is used to indicate
211          * that no VLAN ID is set for OF Flow.
212          */
213         Assert.assertEquals((short) 0xffff, ofMatch.getDataLayerVirtualLan());
214
215         /*
216          * Convert the OF Flow to SAL Flow bFlow
217          */
218         FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
219         Flow bFlow = ofToSal.getFlow(node);
220         Match bMatch = bFlow.getMatch();
221
222         /*
223          * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
224          */
225         Assert
226                 .assertTrue(((Short) match.getField(MatchType.DL_VLAN)
227                         .getValue()).equals((Short) bMatch.getField(
228                         MatchType.DL_VLAN).getValue()));
229     }
230
231     @Test
232     public void testV6toSALFlowConversion() throws Exception {
233         Node node = NodeCreator.createOFNode(12l);
234         NodeConnector port = NodeConnectorCreator.createNodeConnector(
235                 (short) 34, node);
236         NodeConnector oport = NodeConnectorCreator.createNodeConnector(
237                 (short) 30, node);
238         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
239                 (byte) 0x9a, (byte) 0xbc };
240         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
241                 (byte) 0x5e, (byte) 0x6f };
242         InetAddress srcIP = InetAddress
243                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
244         InetAddress dstIP = InetAddress
245                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
246         InetAddress ipMask = InetAddress
247                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
248         short ethertype = EtherTypes.IPv6.shortValue();
249         short vlan = (short) 27;
250         byte vlanPr = 3;
251         Byte tos = 4;
252         byte proto = IPProtocols.TCP.byteValue();
253         short src = (short) 55000;
254         short dst = 80;
255
256         /*
257          * Create a SAL Flow aFlow
258          */
259         Match aMatch = new Match();
260
261         aMatch.setField(MatchType.IN_PORT, port);
262         aMatch.setField(MatchType.DL_SRC, srcMac);
263         aMatch.setField(MatchType.DL_DST, dstMac);
264         aMatch.setField(MatchType.DL_TYPE, ethertype);
265         aMatch.setField(MatchType.DL_VLAN, vlan);
266         aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
267         aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
268         aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
269         aMatch.setField(MatchType.NW_TOS, tos);
270         aMatch.setField(MatchType.NW_PROTO, proto);
271         aMatch.setField(MatchType.TP_SRC, src);
272         aMatch.setField(MatchType.TP_DST, dst);
273
274         Assert.assertTrue(aMatch.isIPv6());
275
276         List<Action> actions = new ArrayList<Action>();
277         // Setting all the actions supported by of for v6
278         actions.add(new PopVlan());
279         actions.add(new Output(oport));
280         actions.add(new Flood());
281         actions.add(new FloodAll());
282         actions.add(new SwPath());
283         actions.add(new HwPath());
284         actions.add(new Loopback());
285         byte mac[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
286         actions.add(new SetDlSrc(mac));
287         actions.add(new SetDlDst(mac));
288         //actions.add(new SetNwSrc(dstIP)); Nicira extensions do not provide IPv6 match addresses change
289         //actions.add(new SetNwDst(srcIP));
290         actions.add(new SetNwTos(3));
291         actions.add(new SetTpSrc(10));
292         actions.add(new SetTpDst(65535));
293         actions.add(new SetVlanId(200));
294
295         Flow aFlow = new Flow(aMatch, actions);
296
297         /*
298          * Convert the SAL aFlow to OF Flow
299          */
300         FlowConverter salToOF = new FlowConverter(aFlow);
301         V6Match v6Match = (V6Match) salToOF.getOFMatch();
302         List<OFAction> ofActions = salToOF.getOFActions();
303
304         /*
305          * Convert the OF Flow to SAL Flow bFlow
306          */
307         FlowConverter ofToSal = new FlowConverter(v6Match, ofActions);
308         Flow bFlow = ofToSal.getFlow(node);
309         Match bMatch = bFlow.getMatch();
310         List<Action> bActions = bFlow.getActions();
311
312         /*
313          * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
314          */
315         Assert.assertTrue(((NodeConnector) aMatch.getField(MatchType.IN_PORT)
316                 .getValue()).equals(((NodeConnector) bMatch.getField(
317                 MatchType.IN_PORT).getValue())));
318         Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
319                 MatchType.DL_SRC).getValue(), (byte[]) bMatch.getField(
320                 MatchType.DL_SRC).getValue()));
321         Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
322                 MatchType.DL_DST).getValue(), (byte[]) bMatch.getField(
323                 MatchType.DL_DST).getValue()));
324         Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_TYPE)
325                 .getValue()).equals((Short) bMatch.getField(MatchType.DL_TYPE)
326                 .getValue()));
327         Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_VLAN)
328                 .getValue()).equals((Short) bMatch.getField(MatchType.DL_VLAN)
329                 .getValue()));
330         Assert.assertTrue(((Byte) aMatch.getField(MatchType.DL_VLAN_PR)
331                 .getValue()).equals((Byte) bMatch
332                 .getField(MatchType.DL_VLAN_PR).getValue()));
333         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
334                 .getValue()).equals((InetAddress) bMatch.getField(
335                 MatchType.NW_SRC).getValue()));
336         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
337                 .getMask()).equals((InetAddress) bMatch.getField(
338                 MatchType.NW_SRC).getMask()));
339         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
340                 .getValue()).equals((InetAddress) bMatch.getField(
341                 MatchType.NW_DST).getValue()));
342         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
343                 .getMask()).equals((InetAddress) bMatch.getField(
344                 MatchType.NW_DST).getMask()));
345         Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_PROTO)
346                 .getValue()).equals((Byte) bMatch.getField(MatchType.NW_PROTO)
347                 .getValue()));
348         Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_TOS).getValue())
349                 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
350         Assert
351                 .assertTrue(((Short) aMatch.getField(MatchType.TP_SRC)
352                         .getValue()).equals((Short) bMatch.getField(
353                         MatchType.TP_SRC).getValue()));
354         Assert
355                 .assertTrue(((Short) aMatch.getField(MatchType.TP_DST)
356                         .getValue()).equals((Short) bMatch.getField(
357                         MatchType.TP_DST).getValue()));
358
359         // FlowConverter parses and sets the actions in the same order for sal match and of match
360         for (short i = 0; i < actions.size(); i++) {
361             Assert.assertTrue(actions.get(i).equals(bActions.get(i)));
362         }
363     }
364
365     @Test
366     public void testV6MatchToSALMatchToV6MatchConversion()
367             throws UnknownHostException {
368         NodeConnector port = NodeConnectorCreator.createNodeConnector(
369                 (short) 24, NodeCreator.createOFNode(6l));
370         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
371                 (byte) 0x9a, (byte) 0xbc };
372         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
373                 (byte) 0x5e, (byte) 0x6f };
374         InetAddress srcIP = InetAddress
375                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
376         InetAddress dstIP = InetAddress
377                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
378         InetAddress ipMask = null;//InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
379         short ethertype = EtherTypes.IPv6.shortValue();
380         short vlan = (short) 27;
381         byte vlanPr = 3;
382         Byte tos = 4;
383         byte proto = IPProtocols.TCP.byteValue();
384         short src = (short) 55000;
385         short dst = 80;
386
387         /*
388          * Create a SAL Flow aFlow
389          */
390         Match aMatch = new Match();
391
392         aMatch.setField(MatchType.IN_PORT, port);
393         aMatch.setField(MatchType.DL_SRC, srcMac);
394         aMatch.setField(MatchType.DL_DST, dstMac);
395         aMatch.setField(MatchType.DL_TYPE, ethertype);
396         aMatch.setField(MatchType.DL_VLAN, vlan);
397         aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
398         aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
399         aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
400         aMatch.setField(MatchType.NW_TOS, tos);
401         aMatch.setField(MatchType.NW_PROTO, proto);
402         aMatch.setField(MatchType.TP_SRC, src);
403         aMatch.setField(MatchType.TP_DST, dst);
404
405         Assert.assertTrue(aMatch.isIPv6());
406
407     }
408 }