2 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.action;
10 import org.opendaylight.controller.sal.core.ConstructionException;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.junit.Test;
17 import org.junit.Assert;
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.core.NodeConnector;
20 import org.opendaylight.controller.sal.core.Property;
21 import org.opendaylight.controller.sal.core.Tables;
22 import org.opendaylight.controller.sal.core.Tier;
23 import org.opendaylight.controller.sal.utils.EtherTypes;
24 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class ActionTest {
29 protected static final Logger logger = LoggerFactory
30 .getLogger(ActionTest.class);
32 public void tesActionCreationValidation() {
33 Action action = new PopVlan();
34 Assert.assertTrue(action.isValid());
36 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
37 (byte) 0x22, (byte) 0x33 };
39 action = new SetDlSrc(mac);
40 Assert.assertTrue(action.isValid());
42 action = new SetDlSrc(mac);
43 Assert.assertTrue(action.isValid());
47 public void testSetVlanActionCreation() {
50 action = new SetVlanId(2);
51 Assert.assertTrue(action.isValid());
53 action = new SetVlanId(4095);
54 Assert.assertTrue(action.isValid());
56 action = new SetVlanId(0);
57 Assert.assertFalse(action.isValid());
59 action = new SetVlanId(1);
60 Assert.assertTrue(action.isValid());
62 action = new SetVlanId(4096);
63 Assert.assertFalse(action.isValid());
67 public void testPushVlanActionCreation() {
70 action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
71 Assert.assertTrue(action.isValid());
73 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
74 Assert.assertTrue(action.isValid());
76 action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
77 Assert.assertFalse(action.isValid());
79 action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
80 Assert.assertTrue(action.isValid());
82 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
83 Assert.assertFalse(action.isValid());
85 action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
86 Assert.assertFalse(action.isValid());
88 action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
89 Assert.assertFalse(action.isValid());
91 action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
92 Assert.assertFalse(action.isValid());
94 // OF 1.3 PUSH_VLAN test.
95 for (EtherTypes tag: EtherTypes.values()) {
96 int t = tag.intValue();
98 (tag == EtherTypes.VLANTAGGED || tag == EtherTypes.QINQ);
99 PushVlan pv = new PushVlan(tag);
100 Assert.assertEquals(valid, pv.isValid());
102 Assert.assertEquals(t, pv.getTag());
105 pv = new PushVlan(t);
106 Assert.assertEquals(valid, pv.isValid());
108 Assert.assertEquals(t, pv.getTag());
114 public void testSetVlanPcpActionCreation() {
115 Action action = null;
117 action = new SetVlanPcp(0x4);
118 Assert.assertTrue(action.isValid());
120 action = new SetVlanPcp(0x8);
121 Assert.assertFalse(action.isValid());
123 action = new SetVlanPcp(-1);
124 Assert.assertFalse(action.isValid());
128 public void testSetVlanCfiActionCreation() {
129 Action action = null;
131 action = new SetVlanCfi(0x0);
132 Assert.assertTrue(action.isValid());
134 action = new SetVlanCfi(0x1);
135 Assert.assertTrue(action.isValid());
137 action = new SetVlanCfi(0x2);
138 Assert.assertFalse(action.isValid());
140 action = new SetVlanCfi(-1);
141 Assert.assertFalse(action.isValid());
145 public void testNetworkSetActionCreation() {
146 Action action = null;
148 InetAddress ip = null;
150 ip = InetAddress.getByName("171.71.9.52");
151 } catch (UnknownHostException e) {
155 action = new SetNwSrc(ip);
156 Assert.assertTrue(action.isValid());
158 action = new SetNwDst(ip);
159 Assert.assertTrue(action.isValid());
162 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
163 } catch (UnknownHostException e) {
166 action = new SetNwSrc(ip);
167 Assert.assertTrue(action.isValid());
169 action = new SetNwDst(ip);
170 Assert.assertTrue(action.isValid());
172 action = new SetNwTos(0xf);
173 Assert.assertTrue(action.isValid());
175 action = new SetNwTos(0x3f);
176 Assert.assertTrue(action.isValid());
178 action = new SetNwTos(0x40);
179 Assert.assertFalse(action.isValid());
181 action = new SetNwTos(0xff1);
182 Assert.assertFalse(action.isValid());
184 action = new SetNwTos(-1);
185 Assert.assertFalse(action.isValid());
189 public void testTransportSetActionCreation() {
190 Action action = null;
192 action = new SetTpSrc(50000);
193 Assert.assertTrue(action.isValid());
195 action = new SetTpDst(65535);
196 Assert.assertTrue(action.isValid());
198 action = new SetTpSrc(0);
199 Assert.assertTrue(action.isValid());
201 action = new SetTpDst(0);
202 Assert.assertTrue(action.isValid());
204 action = new SetTpSrc(-1);
205 Assert.assertFalse(action.isValid());
207 action = new SetTpDst(-1);
208 Assert.assertFalse(action.isValid());
210 action = new SetTpSrc(65536);
211 Assert.assertFalse(action.isValid());
213 action = new SetTpDst(65536);
214 Assert.assertFalse(action.isValid());
218 public void testNextHopActionCreation() {
219 SetNextHop action = null;
221 InetAddress ip = null;
223 ip = InetAddress.getByName("171.71.9.52");
224 } catch (UnknownHostException e) {
228 action = new SetNextHop(ip);
229 Assert.assertTrue(action.getAddress().equals(ip));
232 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
233 } catch (UnknownHostException e) {
236 action = new SetNextHop(ip);
237 Assert.assertTrue(action.getAddress().equals(ip));
241 public void testActionList() {
242 List<Action> actions = new ArrayList<Action>();
243 short portId = (short) 9;
246 node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
247 } catch (ConstructionException e) {
248 // If we reach this point the exception was raised
249 // which is not expected
250 Assert.assertTrue(false);
252 NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
254 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
255 (byte) 0x22, (byte) 0x33 };
256 InetAddress ip = null;
258 ip = InetAddress.getByName("1.1.1.1");
259 } catch (UnknownHostException e) {
263 actions.add(new SetDlSrc(mac));
264 actions.add(new SetNwSrc(ip));
265 actions.add(new Output(nc));
266 Assert.assertTrue(actions.size() == 3);
267 Assert.assertTrue(actions.get(0).isValid());
269 Action probe = new Output(nc);
270 Assert.assertTrue(actions.contains(probe));
271 Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
272 .createNodeConnector((short) 5, node))));
273 Assert.assertFalse(actions.contains(new Controller()));
277 public void testMetadata() {
278 Property tier1 = new Tier(1);
279 Property tier2 = new Tier(2);
280 Property table1 = new Tables((byte)0x7f);
281 Action a1 = new PopVlan();
282 List<Property> resprops = null;
283 resprops = a1.getMetadatas();
284 // This should be an empty list
285 Assert.assertTrue(resprops.isEmpty());
286 a1.setMetadata("tier1", tier1);
287 a1.setMetadata("tier2", tier2);
288 a1.setMetadata("table1", table1);
289 resprops = a1.getMetadatas();
290 // Check for the number of elements in it
291 Assert.assertTrue(resprops.size() == 3);
292 // Check if the elements are in it
293 Assert.assertTrue(resprops.contains(tier1));
294 Assert.assertTrue(resprops.contains(tier2));
295 Assert.assertTrue(resprops.contains(table1));
296 // Check for single elements retrieve
297 Assert.assertTrue(a1.getMetadata("tier1").equals(tier1));
298 Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
299 Assert.assertTrue(a1.getMetadata("table1").equals(table1));
300 // Now remove an element and make sure the remaining are
302 a1.removeMetadata("tier1");
304 resprops = a1.getMetadatas();
305 // Check for the number of elements in it
306 Assert.assertTrue(resprops.size() == 2);
307 // Check if the elements are in it
308 Assert.assertFalse(resprops.contains(tier1));
309 Assert.assertTrue(resprops.contains(tier2));
310 Assert.assertTrue(resprops.contains(table1));
311 // Check for single elements retrieve
312 Assert.assertTrue(a1.getMetadata("table1").equals(table1));
313 Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
314 Assert.assertNull(a1.getMetadata("tier1"));
316 // Check for an element never existed
317 Assert.assertNull(a1.getMetadata("table100"));
320 a1.removeMetadata("tier2");
321 a1.removeMetadata("table1");
323 // Remove also a non-existent one
324 a1.removeMetadata("table100");
326 resprops = a1.getMetadatas();
327 // Check there are no elements left
328 Assert.assertTrue(resprops.size() == 0);
330 // Now check for exception on setting null values
332 a1.setMetadata("foo", null);
333 // The line below should never be reached
334 Assert.assertTrue(false);
335 } catch (NullPointerException nue) {
336 // NPE should be raised for null value
337 Assert.assertTrue(true);
340 // Now check on using null key
342 a1.setMetadata(null, table1);
343 // The line below should never be reached
344 Assert.assertTrue(false);
345 } catch (NullPointerException nue) {
346 // NPE should be raised for null value
347 Assert.assertTrue(true);
350 // Now check on using null key and null value
352 a1.setMetadata(null, null);
353 // The line below should never be reached
354 Assert.assertTrue(false);
355 } catch (NullPointerException nue) {
356 // NPE should be raised for null value
357 Assert.assertTrue(true);