Enable AD-SAL application to configure OF 1.3 PUSH_VLAN action.
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / action / ActionTest.java
1 /*
2  * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.action;
9
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;
15
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;
24
25 public class ActionTest {
26     protected static final Logger logger = LoggerFactory
27     .getLogger(ActionTest.class);
28     @Test
29     public void tesActionCreationValidation() {
30         Action action = new PopVlan();
31         Assert.assertTrue(action.isValid());
32
33         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
34                 (byte) 0x22, (byte) 0x33 };
35
36         action = new SetDlSrc(mac);
37         Assert.assertTrue(action.isValid());
38
39         action = new SetDlSrc(mac);
40         Assert.assertTrue(action.isValid());
41     }
42
43     @Test
44     public void testSetVlanActionCreation() {
45         Action action = null;
46
47         action = new SetVlanId(2);
48         Assert.assertTrue(action.isValid());
49
50         action = new SetVlanId(4095);
51         Assert.assertTrue(action.isValid());
52
53         action = new SetVlanId(0);
54         Assert.assertFalse(action.isValid());
55
56         action = new SetVlanId(1);
57         Assert.assertTrue(action.isValid());
58
59         action = new SetVlanId(4096);
60         Assert.assertFalse(action.isValid());
61     }
62
63     @Test
64     public void testPushVlanActionCreation() {
65         Action action = null;
66
67         action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
68         Assert.assertTrue(action.isValid());
69
70         action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
71         Assert.assertTrue(action.isValid());
72
73         action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
74         Assert.assertFalse(action.isValid());
75
76         action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
77         Assert.assertTrue(action.isValid());
78
79         action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
80         Assert.assertFalse(action.isValid());
81
82         action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
83         Assert.assertFalse(action.isValid());
84
85         action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
86         Assert.assertFalse(action.isValid());
87
88         action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
89         Assert.assertFalse(action.isValid());
90
91         // OF 1.3 PUSH_VLAN test.
92         for (EtherTypes tag: EtherTypes.values()) {
93             int t = tag.intValue();
94             boolean valid =
95                 (tag == EtherTypes.VLANTAGGED || tag == EtherTypes.QINQ);
96             PushVlan pv = new PushVlan(tag);
97             Assert.assertEquals(valid, pv.isValid());
98             if (valid) {
99                 Assert.assertEquals(t, pv.getTag());
100             }
101
102             pv = new PushVlan(t);
103             Assert.assertEquals(valid, pv.isValid());
104             if (valid) {
105                 Assert.assertEquals(t, pv.getTag());
106             }
107         }
108     }
109
110     @Test
111     public void testSetVlanPcpActionCreation() {
112         Action action = null;
113
114         action = new SetVlanPcp(0x4);
115         Assert.assertTrue(action.isValid());
116
117         action = new SetVlanPcp(0x8);
118         Assert.assertFalse(action.isValid());
119
120         action = new SetVlanPcp(-1);
121         Assert.assertFalse(action.isValid());
122     }
123
124     @Test
125     public void testSetVlanCfiActionCreation() {
126         Action action = null;
127
128         action = new SetVlanCfi(0x0);
129         Assert.assertTrue(action.isValid());
130
131         action = new SetVlanCfi(0x1);
132         Assert.assertTrue(action.isValid());
133
134         action = new SetVlanCfi(0x2);
135         Assert.assertFalse(action.isValid());
136
137         action = new SetVlanCfi(-1);
138         Assert.assertFalse(action.isValid());
139     }
140
141     @Test
142     public void testNetworkSetActionCreation() {
143         Action action = null;
144
145         InetAddress ip = null;
146         try {
147             ip = InetAddress.getByName("171.71.9.52");
148         } catch (UnknownHostException e) {
149             logger.error("",e);
150         }
151
152         action = new SetNwSrc(ip);
153         Assert.assertTrue(action.isValid());
154
155         action = new SetNwDst(ip);
156         Assert.assertTrue(action.isValid());
157
158         try {
159             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
160         } catch (UnknownHostException e) {
161             logger.error("", e);
162         }
163         action = new SetNwSrc(ip);
164         Assert.assertTrue(action.isValid());
165
166         action = new SetNwDst(ip);
167         Assert.assertTrue(action.isValid());
168
169         action = new SetNwTos(0xf);
170         Assert.assertTrue(action.isValid());
171
172         action = new SetNwTos(0x3f);
173         Assert.assertTrue(action.isValid());
174
175         action = new SetNwTos(0x40);
176         Assert.assertFalse(action.isValid());
177
178         action = new SetNwTos(0xff1);
179         Assert.assertFalse(action.isValid());
180
181         action = new SetNwTos(-1);
182         Assert.assertFalse(action.isValid());
183     }
184
185     @Test
186     public void testTransportSetActionCreation() {
187         Action action = null;
188
189         action = new SetTpSrc(50000);
190         Assert.assertTrue(action.isValid());
191
192         action = new SetTpDst(65535);
193         Assert.assertTrue(action.isValid());
194
195         action = new SetTpSrc(0);
196         Assert.assertTrue(action.isValid());
197
198         action = new SetTpDst(0);
199         Assert.assertTrue(action.isValid());
200
201         action = new SetTpSrc(-1);
202         Assert.assertFalse(action.isValid());
203
204         action = new SetTpDst(-1);
205         Assert.assertFalse(action.isValid());
206
207         action = new SetTpSrc(65536);
208         Assert.assertFalse(action.isValid());
209
210         action = new SetTpDst(65536);
211         Assert.assertFalse(action.isValid());
212     }
213
214     @Test
215     public void testNextHopActionCreation() {
216         SetNextHop action = null;
217
218         InetAddress ip = null;
219         try {
220             ip = InetAddress.getByName("171.71.9.52");
221         } catch (UnknownHostException e) {
222             logger.error("", e);
223         }
224
225         action = new SetNextHop(ip);
226         Assert.assertTrue(action.getAddress().equals(ip));
227
228         try {
229             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
230         } catch (UnknownHostException e) {
231             logger.error("", e);
232         }
233         action = new SetNextHop(ip);
234         Assert.assertTrue(action.getAddress().equals(ip));
235     }
236
237     @Test
238     public void testActionList() {
239         List<Action> actions = new ArrayList<Action>();
240         short portId = (short) 9;
241         Node node = null;
242         try {
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);
248         }
249         NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
250                 node);
251         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
252                 (byte) 0x22, (byte) 0x33 };
253         InetAddress ip = null;
254         try {
255             ip = InetAddress.getByName("1.1.1.1");
256         } catch (UnknownHostException e) {
257             logger.error("",e);
258         }
259
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());
265
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()));
271     }
272 }