3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
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;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match;
21 import org.openflow.protocol.OFMatch;
22 import org.openflow.protocol.action.OFAction;
24 import org.opendaylight.controller.sal.action.Action;
25 import org.opendaylight.controller.sal.action.Flood;
26 import org.opendaylight.controller.sal.action.FloodAll;
27 import org.opendaylight.controller.sal.action.HwPath;
28 import org.opendaylight.controller.sal.action.Loopback;
29 import org.opendaylight.controller.sal.action.Output;
30 import org.opendaylight.controller.sal.action.PopVlan;
31 import org.opendaylight.controller.sal.action.SetDlDst;
32 import org.opendaylight.controller.sal.action.SetDlSrc;
33 import org.opendaylight.controller.sal.action.SetNwDst;
34 import org.opendaylight.controller.sal.action.SetNwSrc;
35 import org.opendaylight.controller.sal.action.SetNwTos;
36 import org.opendaylight.controller.sal.action.SetTpDst;
37 import org.opendaylight.controller.sal.action.SetTpSrc;
38 import org.opendaylight.controller.sal.action.SetVlanId;
39 import org.opendaylight.controller.sal.action.SwPath;
40 import org.opendaylight.controller.sal.core.Node;
41 import org.opendaylight.controller.sal.core.NodeConnector;
42 import org.opendaylight.controller.sal.flowprogrammer.Flow;
43 import org.opendaylight.controller.sal.match.Match;
44 import org.opendaylight.controller.sal.match.MatchType;
45 import org.opendaylight.controller.sal.utils.EtherTypes;
46 import org.opendaylight.controller.sal.utils.IPProtocols;
47 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
48 import org.opendaylight.controller.sal.utils.NodeCreator;
50 public class FlowProgrammerServiceTest {
53 public void testSALtoOFFlowConverter() throws UnknownHostException {
54 Node node = NodeCreator.createOFNode(1000L);
55 NodeConnector port = NodeConnectorCreator.createNodeConnector(
57 NodeConnector oport = NodeConnectorCreator.createNodeConnector(
59 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
60 (byte) 0x9a, (byte) 0xbc };
61 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
62 (byte) 0x5e, (byte) 0x6f };
63 InetAddress srcIP = InetAddress.getByName("172.28.30.50");
64 InetAddress dstIP = InetAddress.getByName("171.71.9.52");
65 InetAddress ipMask = InetAddress.getByName("255.255.255.0");
66 short ethertype = EtherTypes.IPv4.shortValue();
67 short vlan = (short) 27;
70 byte proto = IPProtocols.TCP.byteValue();
71 short src = (short) 55000;
75 * Create a SAL Flow aFlow
77 Match match = new Match();
78 match.setField(MatchType.IN_PORT, port);
79 match.setField(MatchType.DL_SRC, srcMac);
80 match.setField(MatchType.DL_DST, dstMac);
81 match.setField(MatchType.DL_TYPE, ethertype);
82 match.setField(MatchType.DL_VLAN, vlan);
83 match.setField(MatchType.DL_VLAN_PR, vlanPr);
84 match.setField(MatchType.NW_SRC, srcIP, ipMask);
85 match.setField(MatchType.NW_DST, dstIP, ipMask);
86 match.setField(MatchType.NW_TOS, tos);
87 match.setField(MatchType.NW_PROTO, proto);
88 match.setField(MatchType.TP_SRC, src);
89 match.setField(MatchType.TP_DST, dst);
91 Assert.assertTrue(match.isIPv4());
93 List<Action> actions = new ArrayList<Action>();
94 // Setting all the actions supported by of
95 actions.add(new PopVlan());
96 actions.add(new Output(oport));
97 actions.add(new Flood());
98 actions.add(new FloodAll());
99 actions.add(new SwPath());
100 actions.add(new HwPath());
101 actions.add(new Loopback());
102 byte mac[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
103 actions.add(new SetDlSrc(mac));
104 actions.add(new SetDlDst(mac));
105 actions.add(new SetNwSrc(dstIP));
106 actions.add(new SetNwDst(srcIP));
107 actions.add(new SetNwTos(3));
108 actions.add(new SetTpSrc(10));
109 actions.add(new SetTpDst(20));
110 actions.add(new SetVlanId(200));
112 Flow aFlow = new Flow(match, actions);
115 * Convert the SAL aFlow to OF Flow
117 FlowConverter salToOF = new FlowConverter(aFlow);
118 OFMatch ofMatch = salToOF.getOFMatch();
119 List<OFAction> ofActions = salToOF.getOFActions();
122 * Convert the OF Flow to SAL Flow bFlow
124 FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
125 Flow bFlow = ofToSal.getFlow(node);
126 Match bMatch = bFlow.getMatch();
127 List<Action> bActions = bFlow.getActions();
130 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
132 Assert.assertTrue(((NodeConnector) match.getField(MatchType.IN_PORT)
133 .getValue()).equals(((NodeConnector) bMatch.getField(
134 MatchType.IN_PORT).getValue())));
135 Assert.assertTrue(Arrays.equals((byte[]) match.getField(
136 MatchType.DL_SRC).getValue(), (byte[]) bMatch.getField(
137 MatchType.DL_SRC).getValue()));
138 Assert.assertTrue(Arrays.equals((byte[]) match.getField(
139 MatchType.DL_DST).getValue(), (byte[]) bMatch.getField(
140 MatchType.DL_DST).getValue()));
142 .assertTrue(((Short) match.getField(MatchType.DL_TYPE)
143 .getValue()).equals((Short) bMatch.getField(
144 MatchType.DL_TYPE).getValue()));
146 .assertTrue(((Short) match.getField(MatchType.DL_VLAN)
147 .getValue()).equals((Short) bMatch.getField(
148 MatchType.DL_VLAN).getValue()));
149 Assert.assertTrue(((Byte) match.getField(MatchType.DL_VLAN_PR)
150 .getValue()).equals((Byte) bMatch
151 .getField(MatchType.DL_VLAN_PR).getValue()));
152 Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_SRC)
153 .getValue()).equals((InetAddress) bMatch.getField(
154 MatchType.NW_SRC).getValue()));
155 Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_SRC)
156 .getMask()).equals((InetAddress) bMatch.getField(
157 MatchType.NW_SRC).getMask()));
158 Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_DST)
159 .getValue()).equals((InetAddress) bMatch.getField(
160 MatchType.NW_DST).getValue()));
161 Assert.assertTrue(((InetAddress) match.getField(MatchType.NW_DST)
162 .getMask()).equals((InetAddress) bMatch.getField(
163 MatchType.NW_DST).getMask()));
165 .assertTrue(((Byte) match.getField(MatchType.NW_PROTO)
166 .getValue()).equals((Byte) bMatch.getField(
167 MatchType.NW_PROTO).getValue()));
168 Assert.assertTrue(((Byte) match.getField(MatchType.NW_TOS).getValue())
169 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
170 Assert.assertTrue(((Short) match.getField(MatchType.TP_SRC).getValue())
171 .equals((Short) bMatch.getField(MatchType.TP_SRC).getValue()));
172 Assert.assertTrue(((Short) match.getField(MatchType.TP_DST).getValue())
173 .equals((Short) bMatch.getField(MatchType.TP_DST).getValue()));
175 // FlowConverter parses and sets the actions in the same order for sal match and of match
176 for (short i = 0; i < actions.size(); i++) {
177 Assert.assertTrue(actions.get(i).equals(bActions.get(i)));
182 public void testVlanNoneIdFlowConversion() throws Exception {
183 Node node = NodeCreator.createOFNode(1000L);
186 * The value 0 is used to indicate that no VLAN ID is set
189 short vlan = (short) 0;
192 * Create a SAL Flow aFlow
194 Match match = new Match();
195 match.setField(MatchType.DL_VLAN, vlan);
197 List<Action> actions = new ArrayList<Action>();
199 Flow aFlow = new Flow(match, actions);
202 * Convert the SAL aFlow to OF Flow
204 FlowConverter salToOF = new FlowConverter(aFlow);
205 OFMatch ofMatch = salToOF.getOFMatch();
206 List<OFAction> ofActions = salToOF.getOFActions();
209 * The value 0xffff (OFP_VLAN_NONE) is used to indicate
210 * that no VLAN ID is set for OF Flow.
212 Assert.assertEquals((short) 0xffff, ofMatch.getDataLayerVirtualLan());
215 * Convert the OF Flow to SAL Flow bFlow
217 FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
218 Flow bFlow = ofToSal.getFlow(node);
219 Match bMatch = bFlow.getMatch();
222 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
225 .assertTrue(((Short) match.getField(MatchType.DL_VLAN)
226 .getValue()).equals((Short) bMatch.getField(
227 MatchType.DL_VLAN).getValue()));
231 public void testV6toSALFlowConversion() throws Exception {
232 Node node = NodeCreator.createOFNode(12L);
233 NodeConnector port = NodeConnectorCreator.createNodeConnector(
235 NodeConnector oport = NodeConnectorCreator.createNodeConnector(
237 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
238 (byte) 0x9a, (byte) 0xbc };
239 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
240 (byte) 0x5e, (byte) 0x6f };
241 InetAddress srcIP = InetAddress
242 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
243 InetAddress dstIP = InetAddress
244 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
245 InetAddress ipMask = InetAddress
246 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
247 short ethertype = EtherTypes.IPv6.shortValue();
248 short vlan = (short) 27;
251 byte proto = IPProtocols.TCP.byteValue();
252 short src = (short) 55000;
256 * Create a SAL Flow aFlow
258 Match aMatch = new Match();
260 aMatch.setField(MatchType.IN_PORT, port);
261 aMatch.setField(MatchType.DL_SRC, srcMac);
262 aMatch.setField(MatchType.DL_DST, dstMac);
263 aMatch.setField(MatchType.DL_TYPE, ethertype);
264 aMatch.setField(MatchType.DL_VLAN, vlan);
265 aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
266 aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
267 aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
268 aMatch.setField(MatchType.NW_TOS, tos);
269 aMatch.setField(MatchType.NW_PROTO, proto);
270 aMatch.setField(MatchType.TP_SRC, src);
271 aMatch.setField(MatchType.TP_DST, dst);
273 Assert.assertTrue(aMatch.isIPv6());
275 List<Action> actions = new ArrayList<Action>();
276 // Setting all the actions supported by of for v6
277 actions.add(new PopVlan());
278 actions.add(new Output(oport));
279 actions.add(new Flood());
280 actions.add(new FloodAll());
281 actions.add(new SwPath());
282 actions.add(new HwPath());
283 actions.add(new Loopback());
284 byte mac[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
285 actions.add(new SetDlSrc(mac));
286 actions.add(new SetDlDst(mac));
287 //actions.add(new SetNwSrc(dstIP)); Nicira extensions do not provide IPv6 match addresses change
288 //actions.add(new SetNwDst(srcIP));
289 actions.add(new SetNwTos(3));
290 actions.add(new SetTpSrc(10));
291 actions.add(new SetTpDst(65535));
292 actions.add(new SetVlanId(200));
294 Flow aFlow = new Flow(aMatch, actions);
297 * Convert the SAL aFlow to OF Flow
299 FlowConverter salToOF = new FlowConverter(aFlow);
300 V6Match v6Match = (V6Match) salToOF.getOFMatch();
301 List<OFAction> ofActions = salToOF.getOFActions();
304 * Convert the OF Flow to SAL Flow bFlow
306 FlowConverter ofToSal = new FlowConverter(v6Match, ofActions);
307 Flow bFlow = ofToSal.getFlow(node);
308 Match bMatch = bFlow.getMatch();
309 List<Action> bActions = bFlow.getActions();
312 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
314 Assert.assertTrue(((NodeConnector) aMatch.getField(MatchType.IN_PORT)
315 .getValue()).equals(((NodeConnector) bMatch.getField(
316 MatchType.IN_PORT).getValue())));
317 Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
318 MatchType.DL_SRC).getValue(), (byte[]) bMatch.getField(
319 MatchType.DL_SRC).getValue()));
320 Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
321 MatchType.DL_DST).getValue(), (byte[]) bMatch.getField(
322 MatchType.DL_DST).getValue()));
323 Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_TYPE)
324 .getValue()).equals((Short) bMatch.getField(MatchType.DL_TYPE)
326 Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_VLAN)
327 .getValue()).equals((Short) bMatch.getField(MatchType.DL_VLAN)
329 Assert.assertTrue(((Byte) aMatch.getField(MatchType.DL_VLAN_PR)
330 .getValue()).equals((Byte) bMatch
331 .getField(MatchType.DL_VLAN_PR).getValue()));
332 Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
333 .getValue()).equals((InetAddress) bMatch.getField(
334 MatchType.NW_SRC).getValue()));
335 Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
336 .getMask()).equals((InetAddress) bMatch.getField(
337 MatchType.NW_SRC).getMask()));
338 Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
339 .getValue()).equals((InetAddress) bMatch.getField(
340 MatchType.NW_DST).getValue()));
341 Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
342 .getMask()).equals((InetAddress) bMatch.getField(
343 MatchType.NW_DST).getMask()));
344 Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_PROTO)
345 .getValue()).equals((Byte) bMatch.getField(MatchType.NW_PROTO)
347 Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_TOS).getValue())
348 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
350 .assertTrue(((Short) aMatch.getField(MatchType.TP_SRC)
351 .getValue()).equals((Short) bMatch.getField(
352 MatchType.TP_SRC).getValue()));
354 .assertTrue(((Short) aMatch.getField(MatchType.TP_DST)
355 .getValue()).equals((Short) bMatch.getField(
356 MatchType.TP_DST).getValue()));
358 // FlowConverter parses and sets the actions in the same order for sal match and of match
359 for (short i = 0; i < actions.size(); i++) {
360 Assert.assertTrue(actions.get(i).equals(bActions.get(i)));
365 public void testV6MatchToSALMatchToV6MatchConversion()
366 throws UnknownHostException {
367 NodeConnector port = NodeConnectorCreator.createNodeConnector(
368 (short) 24, NodeCreator.createOFNode(6L));
369 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
370 (byte) 0x9a, (byte) 0xbc };
371 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
372 (byte) 0x5e, (byte) 0x6f };
373 InetAddress srcIP = InetAddress
374 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
375 InetAddress dstIP = InetAddress
376 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
377 InetAddress ipMask = null;//InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
378 short ethertype = EtherTypes.IPv6.shortValue();
379 short vlan = (short) 27;
382 byte proto = IPProtocols.TCP.byteValue();
383 short src = (short) 55000;
387 * Create a SAL Flow aFlow
389 Match aMatch = new Match();
391 aMatch.setField(MatchType.IN_PORT, port);
392 aMatch.setField(MatchType.DL_SRC, srcMac);
393 aMatch.setField(MatchType.DL_DST, dstMac);
394 aMatch.setField(MatchType.DL_TYPE, ethertype);
395 aMatch.setField(MatchType.DL_VLAN, vlan);
396 aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
397 aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
398 aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
399 aMatch.setField(MatchType.NW_TOS, tos);
400 aMatch.setField(MatchType.NW_PROTO, proto);
401 aMatch.setField(MatchType.TP_SRC, src);
402 aMatch.setField(MatchType.TP_DST, dst);
404 Assert.assertTrue(aMatch.isIPv6());