8f09de9aef52dc9d1f01c247d12419b500020c4a
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / flowprogrammer / FlowTest.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.sal.flowprogrammer;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.action.Action;
20 import org.opendaylight.controller.sal.action.Controller;
21 import org.opendaylight.controller.sal.action.Flood;
22 import org.opendaylight.controller.sal.action.Loopback;
23 import org.opendaylight.controller.sal.action.Output;
24 import org.opendaylight.controller.sal.action.PopVlan;
25 import org.opendaylight.controller.sal.action.PushVlan;
26 import org.opendaylight.controller.sal.action.SetDlDst;
27 import org.opendaylight.controller.sal.action.SetNwDst;
28 import org.opendaylight.controller.sal.action.SetVlanId;
29 import org.opendaylight.controller.sal.core.Node;
30 import org.opendaylight.controller.sal.core.NodeConnector;
31 import org.opendaylight.controller.sal.match.Match;
32 import org.opendaylight.controller.sal.match.MatchType;
33 import org.opendaylight.controller.sal.utils.EtherTypes;
34 import org.opendaylight.controller.sal.utils.IPProtocols;
35 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
36 import org.opendaylight.controller.sal.utils.NodeCreator;
37
38 public class FlowTest {
39
40     @Test
41     public void testFlowEquality() throws Exception {
42         Node node = NodeCreator.createOFNode(1055l);
43         Flow flow1 = getSampleFlowV6(node);
44         Flow flow2 = getSampleFlowV6(node);
45         Flow flow3 = getSampleFlow(node);
46
47         // Check Match equality
48         Assert.assertTrue(flow1.getMatch().equals(flow2.getMatch()));
49
50         // Check Actions equality
51         for (int i = 0; i < flow1.getActions().size(); i++) {
52             Action a = flow1.getActions().get(i);
53             Action b = flow2.getActions().get(i);
54             Assert.assertTrue(a != b);
55             Assert.assertTrue(a.equals(b));
56         }
57
58         Assert.assertTrue(flow1.equals(flow2));
59         Assert.assertFalse(flow2.equals(flow3));
60
61         // Check Flow equality where Flow has null action list (pure match)
62         List<Action> emptyList = new ArrayList<Action>(1);
63         Flow x = flow1.clone();
64         x.setActions(emptyList);
65         Assert.assertFalse(flow1.equals(x));
66         flow1.setActions(emptyList);
67         Assert.assertTrue(flow1.equals(x));
68     }
69
70     @Test
71     public void testFlowCloning() throws UnknownHostException {
72         Node node = NodeCreator.createOFNode(55l);
73         Flow flow1 = getSampleFlowV6(node);
74         Flow flow2 = flow1.clone();
75
76         Assert.assertTrue(flow1.equals(flow2));
77         Assert.assertTrue(flow1.getMatch().equals(flow2.getMatch()));
78         Assert.assertTrue(flow1.getActions() != flow2.getActions());
79         Assert.assertTrue(flow1.getActions().equals(flow2.getActions()));
80     }
81
82     @Test
83     public void testFlowActions() throws UnknownHostException {
84         Node node = NodeCreator.createOFNode(55l);
85         Flow flow = getSampleFlowV6(node);
86
87         List<Action> actions = flow.getActions();
88         actions.add(new Loopback());
89
90         Assert.assertTrue(flow.getActions() != actions);
91         Assert.assertTrue(!flow.getActions().equals(actions));
92
93         flow.addAction(new Loopback());
94         Assert.assertTrue(flow.getActions().equals(actions));
95
96         actions.remove(new Loopback());
97         flow.removeAction(new Loopback());
98         Assert.assertTrue(flow.getActions().equals(actions));
99
100         // Add a malformed action
101         Assert.assertFalse(flow.addAction(new PushVlan(EtherTypes.CISCOQINQ, 3,
102                 3, 8000)));
103     }
104
105     private Flow getSampleFlow(Node node) throws UnknownHostException {
106         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
107                 (short) 24, node);
108         NodeConnector oport = NodeConnectorCreator.createOFNodeConnector(
109                 (short) 30, node);
110         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
111                 (byte) 0x9a, (byte) 0xbc };
112         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
113                 (byte) 0x5e, (byte) 0x6f };
114         InetAddress srcIP = InetAddress.getByName("172.28.30.50");
115         InetAddress dstIP = InetAddress.getByName("171.71.9.52");
116         InetAddress newIP = InetAddress.getByName("200.200.100.1");
117         InetAddress ipMask = InetAddress.getByName("255.255.255.0");
118         InetAddress ipMask2 = InetAddress.getByName("255.240.0.0");
119         short ethertype = EtherTypes.IPv4.shortValue();
120         short vlan = (short) 27;
121         byte vlanPr = 3;
122         Byte tos = 4;
123         byte proto = IPProtocols.TCP.byteValue();
124         short src = (short) 55000;
125         short dst = 80;
126
127         /*
128          * Create a SAL Flow aFlow
129          */
130         Match match = new Match();
131         match.setField(MatchType.IN_PORT, port);
132         match.setField(MatchType.DL_SRC, srcMac);
133         match.setField(MatchType.DL_DST, dstMac);
134         match.setField(MatchType.DL_TYPE, ethertype);
135         match.setField(MatchType.DL_VLAN, vlan);
136         match.setField(MatchType.DL_VLAN_PR, vlanPr);
137         match.setField(MatchType.NW_SRC, srcIP, ipMask);
138         match.setField(MatchType.NW_DST, dstIP, ipMask2);
139         match.setField(MatchType.NW_TOS, tos);
140         match.setField(MatchType.NW_PROTO, proto);
141         match.setField(MatchType.TP_SRC, src);
142         match.setField(MatchType.TP_DST, dst);
143
144         List<Action> actions = new ArrayList<Action>();
145         actions.add(new SetNwDst(newIP));
146         actions.add(new Output(oport));
147         actions.add(new PopVlan());
148         actions.add(new Flood());
149         actions.add(new Controller());
150
151         Flow flow = new Flow(match, actions);
152         flow.setPriority((short) 100);
153         flow.setHardTimeout((short) 360);
154
155         return flow;
156     }
157
158     private Flow getSampleFlowV6(Node node) throws UnknownHostException {
159         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
160                 (short) 24, node);
161         NodeConnector oport = NodeConnectorCreator.createOFNodeConnector(
162                 (short) 30, node);
163         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
164                 (byte) 0x9a, (byte) 0xbc };
165         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
166                 (byte) 0x5e, (byte) 0x6f };
167         byte newMac[] = { (byte) 0x11, (byte) 0xaa, (byte) 0xbb, (byte) 0x34,
168                 (byte) 0x9a, (byte) 0xee };
169         InetAddress srcIP = InetAddress
170                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
171         InetAddress dstIP = InetAddress
172                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
173         InetAddress ipMask = InetAddress
174                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
175         InetAddress ipMask2 = InetAddress
176                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
177         InetAddress newIP = InetAddress.getByName("2056:650::a1b0");
178         short ethertype = EtherTypes.IPv6.shortValue();
179         short vlan = (short) 27;
180         byte vlanPr = (byte) 3;
181         Byte tos = 4;
182         byte proto = IPProtocols.UDP.byteValue();
183         short src = (short) 5500;
184         short dst = 80;
185
186         /*
187          * Create a SAL Flow aFlow
188          */
189         Match match = new Match();
190         match.setField(MatchType.IN_PORT, port);
191         match.setField(MatchType.DL_SRC, srcMac);
192         match.setField(MatchType.DL_DST, dstMac);
193         match.setField(MatchType.DL_TYPE, ethertype);
194         match.setField(MatchType.DL_VLAN, vlan);
195         match.setField(MatchType.DL_VLAN_PR, vlanPr);
196         match.setField(MatchType.NW_SRC, srcIP, ipMask);
197         match.setField(MatchType.NW_DST, dstIP, ipMask2);
198         match.setField(MatchType.NW_TOS, tos);
199         match.setField(MatchType.NW_PROTO, proto);
200         match.setField(MatchType.TP_SRC, src);
201         match.setField(MatchType.TP_DST, dst);
202
203         List<Action> actions = new ArrayList<Action>();
204         actions.add(new Controller());
205         actions.add(new SetVlanId(5));
206         actions.add(new SetDlDst(newMac));
207         actions.add(new SetNwDst(newIP));
208         actions.add(new Output(oport));
209         actions.add(new PopVlan());
210         actions.add(new Flood());
211
212         actions.add(new Controller());
213
214         Flow flow = new Flow(match, actions);
215         flow.setPriority((short) 300);
216         flow.setHardTimeout((short) 240);
217
218         return flow;
219     }
220 }