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.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;
38 public class FlowTest {
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);
47 // Check Match equality
48 Assert.assertTrue(flow1.getMatch().equals(flow2.getMatch()));
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));
58 Assert.assertTrue(flow1.equals(flow2));
59 Assert.assertFalse(flow2.equals(flow3));
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));
71 public void testFlowCloning() throws UnknownHostException {
72 Node node = NodeCreator.createOFNode(55L);
73 Flow flow1 = getSampleFlowV6(node);
74 Flow flow2 = flow1.clone();
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()));
83 public void testFlowActions() throws UnknownHostException {
84 Node node = NodeCreator.createOFNode(55L);
85 Flow flow = getSampleFlowV6(node);
87 List<Action> actions = flow.getActions();
88 actions.add(new Loopback());
90 Assert.assertTrue(flow.getActions() != actions);
91 Assert.assertTrue(!flow.getActions().equals(actions));
93 flow.addAction(new Loopback());
94 Assert.assertTrue(flow.getActions().equals(actions));
96 actions.remove(new Loopback());
97 flow.removeAction(new Loopback());
98 Assert.assertTrue(flow.getActions().equals(actions));
100 // Add a malformed action
101 Assert.assertFalse(flow.addAction(new PushVlan(EtherTypes.CISCOQINQ, 3,
105 private Flow getSampleFlow(Node node) throws UnknownHostException {
106 NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
108 NodeConnector oport = NodeConnectorCreator.createOFNodeConnector(
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;
123 byte proto = IPProtocols.TCP.byteValue();
124 short src = (short) 55000;
128 * Create a SAL Flow aFlow
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);
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());
151 Flow flow = new Flow(match, actions);
152 flow.setPriority((short) 100);
153 flow.setHardTimeout((short) 360);
158 private Flow getSampleFlowV6(Node node) throws UnknownHostException {
159 NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
161 NodeConnector oport = NodeConnectorCreator.createOFNodeConnector(
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;
182 byte proto = IPProtocols.UDP.byteValue();
183 short src = (short) 5500;
187 * Create a SAL Flow aFlow
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);
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());
212 actions.add(new Controller());
214 Flow flow = new Flow(match, actions);
215 flow.setPriority((short) 300);
216 flow.setHardTimeout((short) 240);