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.utils.EtherTypes;
21 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class ActionTest {
26 protected static final Logger logger = LoggerFactory
27 .getLogger(ActionTest.class);
29 public void tesActionCreationValidation() {
30 Action action = new PopVlan();
31 Assert.assertTrue(action.isValid());
33 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
34 (byte) 0x22, (byte) 0x33 };
36 action = new SetDlSrc(mac);
37 Assert.assertTrue(action.isValid());
39 action = new SetDlSrc(mac);
40 Assert.assertTrue(action.isValid());
44 public void testSetVlanActionCreation() {
47 action = new SetVlanId(2);
48 Assert.assertTrue(action.isValid());
50 action = new SetVlanId(4095);
51 Assert.assertTrue(action.isValid());
53 action = new SetVlanId(0);
54 Assert.assertFalse(action.isValid());
56 action = new SetVlanId(1);
57 Assert.assertTrue(action.isValid());
59 action = new SetVlanId(4096);
60 Assert.assertFalse(action.isValid());
64 public void testPushVlanActionCreation() {
67 action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
68 Assert.assertTrue(action.isValid());
70 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
71 Assert.assertTrue(action.isValid());
73 action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
74 Assert.assertFalse(action.isValid());
76 action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
77 Assert.assertTrue(action.isValid());
79 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
80 Assert.assertFalse(action.isValid());
82 action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
83 Assert.assertFalse(action.isValid());
85 action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
86 Assert.assertFalse(action.isValid());
88 action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
89 Assert.assertFalse(action.isValid());
91 // OF 1.3 PUSH_VLAN test.
92 for (EtherTypes tag: EtherTypes.values()) {
93 int t = tag.intValue();
95 (tag == EtherTypes.VLANTAGGED || tag == EtherTypes.QINQ);
96 PushVlan pv = new PushVlan(tag);
97 Assert.assertEquals(valid, pv.isValid());
99 Assert.assertEquals(t, pv.getTag());
102 pv = new PushVlan(t);
103 Assert.assertEquals(valid, pv.isValid());
105 Assert.assertEquals(t, pv.getTag());
111 public void testSetVlanPcpActionCreation() {
112 Action action = null;
114 action = new SetVlanPcp(0x4);
115 Assert.assertTrue(action.isValid());
117 action = new SetVlanPcp(0x8);
118 Assert.assertFalse(action.isValid());
120 action = new SetVlanPcp(-1);
121 Assert.assertFalse(action.isValid());
125 public void testSetVlanCfiActionCreation() {
126 Action action = null;
128 action = new SetVlanCfi(0x0);
129 Assert.assertTrue(action.isValid());
131 action = new SetVlanCfi(0x1);
132 Assert.assertTrue(action.isValid());
134 action = new SetVlanCfi(0x2);
135 Assert.assertFalse(action.isValid());
137 action = new SetVlanCfi(-1);
138 Assert.assertFalse(action.isValid());
142 public void testNetworkSetActionCreation() {
143 Action action = null;
145 InetAddress ip = null;
147 ip = InetAddress.getByName("171.71.9.52");
148 } catch (UnknownHostException e) {
152 action = new SetNwSrc(ip);
153 Assert.assertTrue(action.isValid());
155 action = new SetNwDst(ip);
156 Assert.assertTrue(action.isValid());
159 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
160 } catch (UnknownHostException e) {
163 action = new SetNwSrc(ip);
164 Assert.assertTrue(action.isValid());
166 action = new SetNwDst(ip);
167 Assert.assertTrue(action.isValid());
169 action = new SetNwTos(0xf);
170 Assert.assertTrue(action.isValid());
172 action = new SetNwTos(0x3f);
173 Assert.assertTrue(action.isValid());
175 action = new SetNwTos(0x40);
176 Assert.assertFalse(action.isValid());
178 action = new SetNwTos(0xff1);
179 Assert.assertFalse(action.isValid());
181 action = new SetNwTos(-1);
182 Assert.assertFalse(action.isValid());
186 public void testTransportSetActionCreation() {
187 Action action = null;
189 action = new SetTpSrc(50000);
190 Assert.assertTrue(action.isValid());
192 action = new SetTpDst(65535);
193 Assert.assertTrue(action.isValid());
195 action = new SetTpSrc(0);
196 Assert.assertTrue(action.isValid());
198 action = new SetTpDst(0);
199 Assert.assertTrue(action.isValid());
201 action = new SetTpSrc(-1);
202 Assert.assertFalse(action.isValid());
204 action = new SetTpDst(-1);
205 Assert.assertFalse(action.isValid());
207 action = new SetTpSrc(65536);
208 Assert.assertFalse(action.isValid());
210 action = new SetTpDst(65536);
211 Assert.assertFalse(action.isValid());
215 public void testNextHopActionCreation() {
216 SetNextHop action = null;
218 InetAddress ip = null;
220 ip = InetAddress.getByName("171.71.9.52");
221 } catch (UnknownHostException e) {
225 action = new SetNextHop(ip);
226 Assert.assertTrue(action.getAddress().equals(ip));
229 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
230 } catch (UnknownHostException e) {
233 action = new SetNextHop(ip);
234 Assert.assertTrue(action.getAddress().equals(ip));
238 public void testActionList() {
239 List<Action> actions = new ArrayList<Action>();
240 short portId = (short) 9;
243 node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
244 } catch (ConstructionException e) {
245 // If we reach this point the exception was raised
246 // which is not expected
247 Assert.assertTrue(false);
249 NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
251 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
252 (byte) 0x22, (byte) 0x33 };
253 InetAddress ip = null;
255 ip = InetAddress.getByName("1.1.1.1");
256 } catch (UnknownHostException e) {
260 actions.add(new SetDlSrc(mac));
261 actions.add(new SetNwSrc(ip));
262 actions.add(new Output(nc));
263 Assert.assertTrue(actions.size() == 3);
264 Assert.assertTrue(actions.get(0).isValid());
266 Action probe = new Output(nc);
267 Assert.assertTrue(actions.contains(probe));
268 Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
269 .createNodeConnector((short) 5, node))));
270 Assert.assertFalse(actions.contains(new Controller()));