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.openflow.protocol.OFMatch;
21 import org.openflow.protocol.action.OFAction;
23 import org.opendaylight.controller.sal.action.Action;
24 import org.opendaylight.controller.sal.action.Flood;
25 import org.opendaylight.controller.sal.action.FloodAll;
26 import org.opendaylight.controller.sal.action.HwPath;
27 import org.opendaylight.controller.sal.action.Loopback;
28 import org.opendaylight.controller.sal.action.Output;
29 import org.opendaylight.controller.sal.action.PopVlan;
30 import org.opendaylight.controller.sal.action.SetDlDst;
31 import org.opendaylight.controller.sal.action.SetDlSrc;
32 import org.opendaylight.controller.sal.action.SetNwDst;
33 import org.opendaylight.controller.sal.action.SetNwSrc;
34 import org.opendaylight.controller.sal.action.SetNwTos;
35 import org.opendaylight.controller.sal.action.SetTpDst;
36 import org.opendaylight.controller.sal.action.SetTpSrc;
37 import org.opendaylight.controller.sal.action.SetVlanId;
38 import org.opendaylight.controller.sal.action.SwPath;
39 import org.opendaylight.controller.sal.core.Node;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.flowprogrammer.Flow;
42 import org.opendaylight.controller.sal.match.Match;
43 import org.opendaylight.controller.sal.match.MatchType;
44 import org.opendaylight.controller.sal.utils.EtherTypes;
45 import org.opendaylight.controller.sal.utils.IPProtocols;
46 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
47 import org.opendaylight.controller.sal.utils.NodeCreator;
48 import org.opendaylight.openflowplugin.openflow.internal.FlowConverter;
49 import org.opendaylight.openflowplugin.openflow.vendorextension.v6extension.V6Match;
51 public class FlowProgrammerServiceTest {
54 public void testSALtoOFFlowConverter() throws UnknownHostException {
55 Node node = NodeCreator.createOFNode(1000l);
56 NodeConnector port = NodeConnectorCreator.createNodeConnector(
58 NodeConnector oport = NodeConnectorCreator.createNodeConnector(
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;
71 byte proto = IPProtocols.TCP.byteValue();
72 short src = (short) 55000;
76 * Create a SAL Flow aFlow
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);
92 Assert.assertTrue(match.isIPv4());
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));
113 Flow aFlow = new Flow(match, actions);
116 * Convert the SAL aFlow to OF Flow
118 FlowConverter salToOF = new FlowConverter(aFlow);
119 OFMatch ofMatch = salToOF.getOFMatch();
120 List<OFAction> ofActions = salToOF.getOFActions();
123 * Convert the OF Flow to SAL Flow bFlow
125 FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
126 Flow bFlow = ofToSal.getFlow(node);
127 Match bMatch = bFlow.getMatch();
128 List<Action> bActions = bFlow.getActions();
131 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
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()));
143 .assertTrue(((Short) match.getField(MatchType.DL_TYPE)
144 .getValue()).equals((Short) bMatch.getField(
145 MatchType.DL_TYPE).getValue()));
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()));
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()));
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)));
183 public void testVlanNoneIdFlowConversion() throws Exception {
184 Node node = NodeCreator.createOFNode(1000l);
187 * The value 0 is used to indicate that no VLAN ID is set
190 short vlan = (short) 0;
193 * Create a SAL Flow aFlow
195 Match match = new Match();
196 match.setField(MatchType.DL_VLAN, vlan);
198 List<Action> actions = new ArrayList<Action>();
200 Flow aFlow = new Flow(match, actions);
203 * Convert the SAL aFlow to OF Flow
205 FlowConverter salToOF = new FlowConverter(aFlow);
206 OFMatch ofMatch = salToOF.getOFMatch();
207 List<OFAction> ofActions = salToOF.getOFActions();
210 * The value 0xffff (OFP_VLAN_NONE) is used to indicate
211 * that no VLAN ID is set for OF Flow.
213 Assert.assertEquals((short) 0xffff, ofMatch.getDataLayerVirtualLan());
216 * Convert the OF Flow to SAL Flow bFlow
218 FlowConverter ofToSal = new FlowConverter(ofMatch, ofActions);
219 Flow bFlow = ofToSal.getFlow(node);
220 Match bMatch = bFlow.getMatch();
223 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
226 .assertTrue(((Short) match.getField(MatchType.DL_VLAN)
227 .getValue()).equals((Short) bMatch.getField(
228 MatchType.DL_VLAN).getValue()));
232 public void testV6toSALFlowConversion() throws Exception {
233 Node node = NodeCreator.createOFNode(12l);
234 NodeConnector port = NodeConnectorCreator.createNodeConnector(
236 NodeConnector oport = NodeConnectorCreator.createNodeConnector(
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;
252 byte proto = IPProtocols.TCP.byteValue();
253 short src = (short) 55000;
257 * Create a SAL Flow aFlow
259 Match aMatch = new Match();
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);
274 Assert.assertTrue(aMatch.isIPv6());
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));
295 Flow aFlow = new Flow(aMatch, actions);
298 * Convert the SAL aFlow to OF Flow
300 FlowConverter salToOF = new FlowConverter(aFlow);
301 V6Match v6Match = (V6Match) salToOF.getOFMatch();
302 List<OFAction> ofActions = salToOF.getOFActions();
305 * Convert the OF Flow to SAL Flow bFlow
307 FlowConverter ofToSal = new FlowConverter(v6Match, ofActions);
308 Flow bFlow = ofToSal.getFlow(node);
309 Match bMatch = bFlow.getMatch();
310 List<Action> bActions = bFlow.getActions();
313 * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
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)
327 Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_VLAN)
328 .getValue()).equals((Short) bMatch.getField(MatchType.DL_VLAN)
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)
348 Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_TOS).getValue())
349 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
351 .assertTrue(((Short) aMatch.getField(MatchType.TP_SRC)
352 .getValue()).equals((Short) bMatch.getField(
353 MatchType.TP_SRC).getValue()));
355 .assertTrue(((Short) aMatch.getField(MatchType.TP_DST)
356 .getValue()).equals((Short) bMatch.getField(
357 MatchType.TP_DST).getValue()));
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)));
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;
383 byte proto = IPProtocols.TCP.byteValue();
384 short src = (short) 55000;
388 * Create a SAL Flow aFlow
390 Match aMatch = new Match();
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);
405 Assert.assertTrue(aMatch.isIPv6());