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.action;
12 import org.opendaylight.controller.sal.core.ConstructionException;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.junit.Test;
19 import org.junit.Assert;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.utils.EtherTypes;
23 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class ActionTest {
28 protected static final Logger logger = LoggerFactory
29 .getLogger(ActionTest.class);
31 public void tesActionCreationValidation() {
32 Action action = new PopVlan();
33 Assert.assertTrue(action.isValid());
35 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
36 (byte) 0x22, (byte) 0x33 };
38 action = new SetDlSrc(mac);
39 Assert.assertTrue(action.isValid());
41 action = new SetDlSrc(mac);
42 Assert.assertTrue(action.isValid());
46 public void testSetVlanActionCreation() {
49 action = new SetVlanId(2);
50 Assert.assertTrue(action.isValid());
52 action = new SetVlanId(4095);
53 Assert.assertTrue(action.isValid());
55 action = new SetVlanId(0);
56 Assert.assertFalse(action.isValid());
58 action = new SetVlanId(1);
59 Assert.assertTrue(action.isValid());
61 action = new SetVlanId(4096);
62 Assert.assertFalse(action.isValid());
66 public void testPushVlanActionCreation() {
69 action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
70 Assert.assertTrue(action.isValid());
72 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
73 Assert.assertTrue(action.isValid());
75 action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
76 Assert.assertFalse(action.isValid());
78 action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
79 Assert.assertTrue(action.isValid());
81 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
82 Assert.assertFalse(action.isValid());
84 action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
85 Assert.assertFalse(action.isValid());
87 action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
88 Assert.assertFalse(action.isValid());
90 action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
91 Assert.assertFalse(action.isValid());
95 public void testSetVlanPcpActionCreation() {
98 action = new SetVlanPcp(0x4);
99 Assert.assertTrue(action.isValid());
101 action = new SetVlanPcp(0x8);
102 Assert.assertFalse(action.isValid());
104 action = new SetVlanPcp(-1);
105 Assert.assertFalse(action.isValid());
109 public void testSetVlanCfiActionCreation() {
110 Action action = null;
112 action = new SetVlanCfi(0x0);
113 Assert.assertTrue(action.isValid());
115 action = new SetVlanCfi(0x1);
116 Assert.assertTrue(action.isValid());
118 action = new SetVlanCfi(0x2);
119 Assert.assertFalse(action.isValid());
121 action = new SetVlanCfi(-1);
122 Assert.assertFalse(action.isValid());
126 public void testNetworkSetActionCreation() {
127 Action action = null;
129 InetAddress ip = null;
131 ip = InetAddress.getByName("171.71.9.52");
132 } catch (UnknownHostException e) {
136 action = new SetNwSrc(ip);
137 Assert.assertTrue(action.isValid());
139 action = new SetNwDst(ip);
140 Assert.assertTrue(action.isValid());
143 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
144 } catch (UnknownHostException e) {
147 action = new SetNwSrc(ip);
148 Assert.assertTrue(action.isValid());
150 action = new SetNwDst(ip);
151 Assert.assertTrue(action.isValid());
153 action = new SetNwTos(0xf);
154 Assert.assertTrue(action.isValid());
156 action = new SetNwTos(0x3f);
157 Assert.assertTrue(action.isValid());
159 action = new SetNwTos(0x40);
160 Assert.assertFalse(action.isValid());
162 action = new SetNwTos(0xff1);
163 Assert.assertFalse(action.isValid());
165 action = new SetNwTos(-1);
166 Assert.assertFalse(action.isValid());
170 public void testTransportSetActionCreation() {
171 Action action = null;
173 action = new SetTpSrc(50000);
174 Assert.assertTrue(action.isValid());
176 action = new SetTpDst(65535);
177 Assert.assertTrue(action.isValid());
179 action = new SetTpDst(0);
180 Assert.assertFalse(action.isValid());
182 action = new SetTpSrc(-1);
183 Assert.assertFalse(action.isValid());
185 action = new SetTpDst(-1);
186 Assert.assertFalse(action.isValid());
188 action = new SetTpSrc(65536);
189 Assert.assertFalse(action.isValid());
191 action = new SetTpDst(65536);
192 Assert.assertFalse(action.isValid());
196 public void testNextHopActionCreation() {
197 SetNextHop action = null;
199 InetAddress ip = null;
201 ip = InetAddress.getByName("171.71.9.52");
202 } catch (UnknownHostException e) {
206 action = new SetNextHop(ip);
207 Assert.assertTrue(action.getAddress().equals(ip));
210 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
211 } catch (UnknownHostException e) {
214 action = new SetNextHop(ip);
215 Assert.assertTrue(action.getAddress().equals(ip));
219 public void testActionList() {
220 List<Action> actions = new ArrayList<Action>();
221 short portId = (short) 9;
224 node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
225 } catch (ConstructionException e) {
226 // If we reach this point the exception was raised
227 // which is not expected
228 Assert.assertTrue(false);
230 NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
232 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
233 (byte) 0x22, (byte) 0x33 };
234 InetAddress ip = null;
236 ip = InetAddress.getByName("1.1.1.1");
237 } catch (UnknownHostException e) {
241 actions.add(new SetDlSrc(mac));
242 actions.add(new SetNwSrc(ip));
243 actions.add(new Output(nc));
244 Assert.assertTrue(actions.size() == 3);
245 Assert.assertTrue(actions.get(0).isValid());
247 Action probe = new Output(nc);
248 Assert.assertTrue(actions.contains(probe));
249 Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
250 .createNodeConnector((short) 5, node))));
251 Assert.assertFalse(actions.contains(new Controller()));