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.action.Action;
21 import org.opendaylight.controller.sal.action.Controller;
22 import org.opendaylight.controller.sal.action.Output;
23 import org.opendaylight.controller.sal.action.PopVlan;
24 import org.opendaylight.controller.sal.action.PushVlan;
25 import org.opendaylight.controller.sal.action.SetDlSrc;
26 import org.opendaylight.controller.sal.action.SetNwDst;
27 import org.opendaylight.controller.sal.action.SetNwSrc;
28 import org.opendaylight.controller.sal.action.SetNwTos;
29 import org.opendaylight.controller.sal.action.SetTpDst;
30 import org.opendaylight.controller.sal.action.SetTpSrc;
31 import org.opendaylight.controller.sal.action.SetVlanCfi;
32 import org.opendaylight.controller.sal.action.SetVlanId;
33 import org.opendaylight.controller.sal.action.SetVlanPcp;
34 import org.opendaylight.controller.sal.core.Node;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.utils.EtherTypes;
37 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class ActionTest {
42 protected static final Logger logger = LoggerFactory
43 .getLogger(ActionTest.class);
45 public void tesActionCreationValidation() {
46 Action action = new PopVlan();
47 Assert.assertTrue(action.isValid());
49 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
50 (byte) 0x22, (byte) 0x33 };
52 action = new SetDlSrc(mac);
53 Assert.assertTrue(action.isValid());
55 action = new SetDlSrc(mac);
56 Assert.assertTrue(action.isValid());
60 public void testSetVlanActionCreation() {
63 action = new SetVlanId(2);
64 Assert.assertTrue(action.isValid());
66 action = new SetVlanId(4095);
67 Assert.assertTrue(action.isValid());
69 action = new SetVlanId(0);
70 Assert.assertFalse(action.isValid());
72 action = new SetVlanId(1);
73 Assert.assertTrue(action.isValid());
75 action = new SetVlanId(4096);
76 Assert.assertFalse(action.isValid());
80 public void testPushVlanActionCreation() {
83 action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
84 Assert.assertTrue(action.isValid());
86 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
87 Assert.assertTrue(action.isValid());
89 action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
90 Assert.assertFalse(action.isValid());
92 action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
93 Assert.assertTrue(action.isValid());
95 action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
96 Assert.assertFalse(action.isValid());
98 action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
99 Assert.assertFalse(action.isValid());
101 action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
102 Assert.assertFalse(action.isValid());
104 action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
105 Assert.assertFalse(action.isValid());
109 public void testSetVlanPcpActionCreation() {
110 Action action = null;
112 action = new SetVlanPcp(0x4);
113 Assert.assertTrue(action.isValid());
115 action = new SetVlanPcp(0x8);
116 Assert.assertFalse(action.isValid());
118 action = new SetVlanPcp(-1);
119 Assert.assertFalse(action.isValid());
123 public void testSetVlanCfiActionCreation() {
124 Action action = null;
126 action = new SetVlanCfi(0x0);
127 Assert.assertTrue(action.isValid());
129 action = new SetVlanCfi(0x1);
130 Assert.assertTrue(action.isValid());
132 action = new SetVlanCfi(0x2);
133 Assert.assertFalse(action.isValid());
135 action = new SetVlanCfi(-1);
136 Assert.assertFalse(action.isValid());
140 public void testNetworkSetActionCreation() {
141 Action action = null;
143 InetAddress ip = null;
145 ip = InetAddress.getByName("171.71.9.52");
146 } catch (UnknownHostException e) {
150 action = new SetNwSrc(ip);
151 Assert.assertTrue(action.isValid());
153 action = new SetNwDst(ip);
154 Assert.assertTrue(action.isValid());
157 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
158 } catch (UnknownHostException e) {
161 action = new SetNwSrc(ip);
162 Assert.assertTrue(action.isValid());
164 action = new SetNwDst(ip);
165 Assert.assertTrue(action.isValid());
167 action = new SetNwTos(0xf);
168 Assert.assertTrue(action.isValid());
170 action = new SetNwTos(0x3f);
171 Assert.assertTrue(action.isValid());
173 action = new SetNwTos(0x40);
174 Assert.assertFalse(action.isValid());
176 action = new SetNwTos(0xff1);
177 Assert.assertFalse(action.isValid());
179 action = new SetNwTos(-1);
180 Assert.assertFalse(action.isValid());
184 public void testTransportSetActionCreation() {
185 Action action = null;
187 action = new SetTpSrc(50000);
188 Assert.assertTrue(action.isValid());
190 action = new SetTpDst(65535);
191 Assert.assertTrue(action.isValid());
193 action = new SetTpDst(0);
194 Assert.assertFalse(action.isValid());
196 action = new SetTpSrc(-1);
197 Assert.assertFalse(action.isValid());
199 action = new SetTpDst(-1);
200 Assert.assertFalse(action.isValid());
202 action = new SetTpSrc(65536);
203 Assert.assertFalse(action.isValid());
205 action = new SetTpDst(65536);
206 Assert.assertFalse(action.isValid());
210 public void testActionList() {
211 List<Action> actions = new ArrayList<Action>();
212 short portId = (short) 9;
215 node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
216 } catch (ConstructionException e) {
217 // If we reach this point the exception was raised
218 // which is not expected
219 Assert.assertTrue(false);
221 NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
223 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
224 (byte) 0x22, (byte) 0x33 };
225 InetAddress ip = null;
227 ip = InetAddress.getByName("1.1.1.1");
228 } catch (UnknownHostException e) {
232 actions.add(new SetDlSrc(mac));
233 actions.add(new SetNwSrc(ip));
234 actions.add(new Output(nc));
235 Assert.assertTrue(actions.size() == 3);
236 Assert.assertTrue(actions.get(0).isValid());
238 Action probe = new Output(nc);
239 Assert.assertTrue(actions.contains(probe));
240 Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
241 .createNodeConnector((short) 5, node))));
242 Assert.assertFalse(actions.contains(new Controller()));