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