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());
93 public void testSetVlanPcpActionCreation() {
96 action = new SetVlanPcp(0x4);
97 Assert.assertTrue(action.isValid());
99 action = new SetVlanPcp(0x8);
100 Assert.assertFalse(action.isValid());
102 action = new SetVlanPcp(-1);
103 Assert.assertFalse(action.isValid());
107 public void testSetVlanCfiActionCreation() {
108 Action action = null;
110 action = new SetVlanCfi(0x0);
111 Assert.assertTrue(action.isValid());
113 action = new SetVlanCfi(0x1);
114 Assert.assertTrue(action.isValid());
116 action = new SetVlanCfi(0x2);
117 Assert.assertFalse(action.isValid());
119 action = new SetVlanCfi(-1);
120 Assert.assertFalse(action.isValid());
124 public void testNetworkSetActionCreation() {
125 Action action = null;
127 InetAddress ip = null;
129 ip = InetAddress.getByName("171.71.9.52");
130 } catch (UnknownHostException e) {
134 action = new SetNwSrc(ip);
135 Assert.assertTrue(action.isValid());
137 action = new SetNwDst(ip);
138 Assert.assertTrue(action.isValid());
141 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
142 } catch (UnknownHostException e) {
145 action = new SetNwSrc(ip);
146 Assert.assertTrue(action.isValid());
148 action = new SetNwDst(ip);
149 Assert.assertTrue(action.isValid());
151 action = new SetNwTos(0xf);
152 Assert.assertTrue(action.isValid());
154 action = new SetNwTos(0x3f);
155 Assert.assertTrue(action.isValid());
157 action = new SetNwTos(0x40);
158 Assert.assertFalse(action.isValid());
160 action = new SetNwTos(0xff1);
161 Assert.assertFalse(action.isValid());
163 action = new SetNwTos(-1);
164 Assert.assertFalse(action.isValid());
168 public void testTransportSetActionCreation() {
169 Action action = null;
171 action = new SetTpSrc(50000);
172 Assert.assertTrue(action.isValid());
174 action = new SetTpDst(65535);
175 Assert.assertTrue(action.isValid());
177 action = new SetTpSrc(0);
178 Assert.assertTrue(action.isValid());
180 action = new SetTpDst(0);
181 Assert.assertTrue(action.isValid());
183 action = new SetTpSrc(-1);
184 Assert.assertFalse(action.isValid());
186 action = new SetTpDst(-1);
187 Assert.assertFalse(action.isValid());
189 action = new SetTpSrc(65536);
190 Assert.assertFalse(action.isValid());
192 action = new SetTpDst(65536);
193 Assert.assertFalse(action.isValid());
197 public void testNextHopActionCreation() {
198 SetNextHop action = null;
200 InetAddress ip = null;
202 ip = InetAddress.getByName("171.71.9.52");
203 } catch (UnknownHostException e) {
207 action = new SetNextHop(ip);
208 Assert.assertTrue(action.getAddress().equals(ip));
211 ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
212 } catch (UnknownHostException e) {
215 action = new SetNextHop(ip);
216 Assert.assertTrue(action.getAddress().equals(ip));
220 public void testActionList() {
221 List<Action> actions = new ArrayList<Action>();
222 short portId = (short) 9;
225 node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
226 } catch (ConstructionException e) {
227 // If we reach this point the exception was raised
228 // which is not expected
229 Assert.assertTrue(false);
231 NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
233 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
234 (byte) 0x22, (byte) 0x33 };
235 InetAddress ip = null;
237 ip = InetAddress.getByName("1.1.1.1");
238 } catch (UnknownHostException e) {
242 actions.add(new SetDlSrc(mac));
243 actions.add(new SetNwSrc(ip));
244 actions.add(new Output(nc));
245 Assert.assertTrue(actions.size() == 3);
246 Assert.assertTrue(actions.get(0).isValid());
248 Action probe = new Output(nc);
249 Assert.assertTrue(actions.contains(probe));
250 Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
251 .createNodeConnector((short) 5, node))));
252 Assert.assertFalse(actions.contains(new Controller()));