OpenDaylight Controller functional modules.
[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 testV6toSALFlowConversion() throws Exception {
184         Node node = NodeCreator.createOFNode(12l);
185         NodeConnector port = NodeConnectorCreator.createNodeConnector(
186                 (short) 34, node);
187         NodeConnector oport = NodeConnectorCreator.createNodeConnector(
188                 (short) 30, node);
189         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
190                 (byte) 0x9a, (byte) 0xbc };
191         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
192                 (byte) 0x5e, (byte) 0x6f };
193         InetAddress srcIP = InetAddress
194                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
195         InetAddress dstIP = InetAddress
196                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
197         InetAddress ipMask = InetAddress
198                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
199         short ethertype = EtherTypes.IPv6.shortValue();
200         short vlan = (short) 27;
201         byte vlanPr = 3;
202         Byte tos = 4;
203         byte proto = IPProtocols.TCP.byteValue();
204         short src = (short) 55000;
205         short dst = 80;
206
207         /*
208          * Create a SAL Flow aFlow
209          */
210         Match aMatch = new Match();
211
212         aMatch.setField(MatchType.IN_PORT, port);
213         aMatch.setField(MatchType.DL_SRC, srcMac);
214         aMatch.setField(MatchType.DL_DST, dstMac);
215         aMatch.setField(MatchType.DL_TYPE, ethertype);
216         aMatch.setField(MatchType.DL_VLAN, vlan);
217         aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
218         aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
219         aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
220         aMatch.setField(MatchType.NW_TOS, tos);
221         aMatch.setField(MatchType.NW_PROTO, proto);
222         aMatch.setField(MatchType.TP_SRC, src);
223         aMatch.setField(MatchType.TP_DST, dst);
224
225         Assert.assertTrue(aMatch.isIPv6());
226
227         List<Action> actions = new ArrayList<Action>();
228         // Setting all the actions supported by of for v6
229         actions.add(new PopVlan());
230         actions.add(new Output(oport));
231         actions.add(new Flood());
232         actions.add(new FloodAll());
233         actions.add(new SwPath());
234         actions.add(new HwPath());
235         actions.add(new Loopback());
236         byte mac[] = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
237         actions.add(new SetDlSrc(mac));
238         actions.add(new SetDlDst(mac));
239         //actions.add(new SetNwSrc(dstIP)); Nicira extensions do not provide IPv6 match addresses change
240         //actions.add(new SetNwDst(srcIP));
241         actions.add(new SetNwTos(3));
242         actions.add(new SetTpSrc(10));
243         actions.add(new SetTpDst(65535));
244         actions.add(new SetVlanId(200));
245
246         Flow aFlow = new Flow(aMatch, actions);
247
248         /*
249          * Convert the SAL aFlow to OF Flow
250          */
251         FlowConverter salToOF = new FlowConverter(aFlow);
252         V6Match v6Match = (V6Match) salToOF.getOFMatch();
253         List<OFAction> ofActions = salToOF.getOFActions();
254
255         /*
256          * Convert the OF Flow to SAL Flow bFlow
257          */
258         FlowConverter ofToSal = new FlowConverter(v6Match, ofActions);
259         Flow bFlow = ofToSal.getFlow(node);
260         Match bMatch = bFlow.getMatch();
261         List<Action> bActions = bFlow.getActions();
262
263         /*
264          * Verify the converted SAL flow bFlow is equivalent to the original SAL Flow
265          */
266         Assert.assertTrue(((NodeConnector) aMatch.getField(MatchType.IN_PORT)
267                 .getValue()).equals(((NodeConnector) bMatch.getField(
268                 MatchType.IN_PORT).getValue())));
269         Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
270                 MatchType.DL_SRC).getValue(), (byte[]) bMatch.getField(
271                 MatchType.DL_SRC).getValue()));
272         Assert.assertTrue(Arrays.equals((byte[]) aMatch.getField(
273                 MatchType.DL_DST).getValue(), (byte[]) bMatch.getField(
274                 MatchType.DL_DST).getValue()));
275         Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_TYPE)
276                 .getValue()).equals((Short) bMatch.getField(MatchType.DL_TYPE)
277                 .getValue()));
278         Assert.assertTrue(((Short) aMatch.getField(MatchType.DL_VLAN)
279                 .getValue()).equals((Short) bMatch.getField(MatchType.DL_VLAN)
280                 .getValue()));
281         Assert.assertTrue(((Byte) aMatch.getField(MatchType.DL_VLAN_PR)
282                 .getValue()).equals((Byte) bMatch
283                 .getField(MatchType.DL_VLAN_PR).getValue()));
284         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
285                 .getValue()).equals((InetAddress) bMatch.getField(
286                 MatchType.NW_SRC).getValue()));
287         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_SRC)
288                 .getMask()).equals((InetAddress) bMatch.getField(
289                 MatchType.NW_SRC).getMask()));
290         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
291                 .getValue()).equals((InetAddress) bMatch.getField(
292                 MatchType.NW_DST).getValue()));
293         Assert.assertTrue(((InetAddress) aMatch.getField(MatchType.NW_DST)
294                 .getMask()).equals((InetAddress) bMatch.getField(
295                 MatchType.NW_DST).getMask()));
296         Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_PROTO)
297                 .getValue()).equals((Byte) bMatch.getField(MatchType.NW_PROTO)
298                 .getValue()));
299         Assert.assertTrue(((Byte) aMatch.getField(MatchType.NW_TOS).getValue())
300                 .equals((Byte) bMatch.getField(MatchType.NW_TOS).getValue()));
301         Assert
302                 .assertTrue(((Short) aMatch.getField(MatchType.TP_SRC)
303                         .getValue()).equals((Short) bMatch.getField(
304                         MatchType.TP_SRC).getValue()));
305         Assert
306                 .assertTrue(((Short) aMatch.getField(MatchType.TP_DST)
307                         .getValue()).equals((Short) bMatch.getField(
308                         MatchType.TP_DST).getValue()));
309
310         // FlowConverter parses and sets the actions in the same order for sal match and of match
311         for (short i = 0; i < actions.size(); i++) {
312             Assert.assertTrue(actions.get(i).equals(bActions.get(i)));
313         }
314     }
315
316     @Test
317     public void testV6MatchToSALMatchToV6MatchConversion()
318             throws UnknownHostException {
319         NodeConnector port = NodeConnectorCreator.createNodeConnector(
320                 (short) 24, NodeCreator.createOFNode(6l));
321         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
322                 (byte) 0x9a, (byte) 0xbc };
323         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
324                 (byte) 0x5e, (byte) 0x6f };
325         InetAddress srcIP = InetAddress
326                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
327         InetAddress dstIP = InetAddress
328                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
329         InetAddress ipMask = null;//InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
330         short ethertype = EtherTypes.IPv6.shortValue();
331         short vlan = (short) 27;
332         byte vlanPr = 3;
333         Byte tos = 4;
334         byte proto = IPProtocols.TCP.byteValue();
335         short src = (short) 55000;
336         short dst = 80;
337
338         /*
339          * Create a SAL Flow aFlow
340          */
341         Match aMatch = new Match();
342
343         aMatch.setField(MatchType.IN_PORT, port);
344         aMatch.setField(MatchType.DL_SRC, srcMac);
345         aMatch.setField(MatchType.DL_DST, dstMac);
346         aMatch.setField(MatchType.DL_TYPE, ethertype);
347         aMatch.setField(MatchType.DL_VLAN, vlan);
348         aMatch.setField(MatchType.DL_VLAN_PR, vlanPr);
349         aMatch.setField(MatchType.NW_SRC, srcIP, ipMask);
350         aMatch.setField(MatchType.NW_DST, dstIP, ipMask);
351         aMatch.setField(MatchType.NW_TOS, tos);
352         aMatch.setField(MatchType.NW_PROTO, proto);
353         aMatch.setField(MatchType.TP_SRC, src);
354         aMatch.setField(MatchType.TP_DST, dst);
355
356         Assert.assertTrue(aMatch.isIPv6());
357
358     }
359 }