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