Merge "Cleanup RpcRoutingStrategy definition"
[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.core.Property;
21 import org.opendaylight.controller.sal.core.Tables;
22 import org.opendaylight.controller.sal.core.Tier;
23 import org.opendaylight.controller.sal.utils.EtherTypes;
24 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class ActionTest {
29     protected static final Logger logger = LoggerFactory
30     .getLogger(ActionTest.class);
31     @Test
32     public void tesActionCreationValidation() {
33         Action action = new PopVlan();
34         Assert.assertTrue(action.isValid());
35
36         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
37                 (byte) 0x22, (byte) 0x33 };
38
39         action = new SetDlSrc(mac);
40         Assert.assertTrue(action.isValid());
41
42         action = new SetDlSrc(mac);
43         Assert.assertTrue(action.isValid());
44     }
45
46     @Test
47     public void testSetVlanActionCreation() {
48         Action action = null;
49
50         action = new SetVlanId(2);
51         Assert.assertTrue(action.isValid());
52
53         action = new SetVlanId(4095);
54         Assert.assertTrue(action.isValid());
55
56         action = new SetVlanId(0);
57         Assert.assertFalse(action.isValid());
58
59         action = new SetVlanId(1);
60         Assert.assertTrue(action.isValid());
61
62         action = new SetVlanId(4096);
63         Assert.assertFalse(action.isValid());
64     }
65
66     @Test
67     public void testPushVlanActionCreation() {
68         Action action = null;
69
70         action = new PushVlan(EtherTypes.QINQ, 0x4, 0x1, 2000);
71         Assert.assertTrue(action.isValid());
72
73         action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 2000);
74         Assert.assertTrue(action.isValid());
75
76         action = new PushVlan(EtherTypes.OLDQINQ, 0x4, 2, 2000);
77         Assert.assertFalse(action.isValid());
78
79         action = new PushVlan(EtherTypes.VLANTAGGED, 0x4, 0, 2000);
80         Assert.assertTrue(action.isValid());
81
82         action = new PushVlan(EtherTypes.QINQ.intValue(), 0x4, 0x1, 5000);
83         Assert.assertFalse(action.isValid());
84
85         action = new PushVlan(EtherTypes.LLDP, 0x4, 0x1, 2000);
86         Assert.assertFalse(action.isValid());
87
88         action = new PushVlan(EtherTypes.PVSTP, 0x4, 2, 2000);
89         Assert.assertFalse(action.isValid());
90
91         action = new PushVlan(EtherTypes.QINQ, 0x4, -1, 2000);
92         Assert.assertFalse(action.isValid());
93
94         // OF 1.3 PUSH_VLAN test.
95         for (EtherTypes tag: EtherTypes.values()) {
96             int t = tag.intValue();
97             boolean valid =
98                 (tag == EtherTypes.VLANTAGGED || tag == EtherTypes.QINQ);
99             PushVlan pv = new PushVlan(tag);
100             Assert.assertEquals(valid, pv.isValid());
101             if (valid) {
102                 Assert.assertEquals(t, pv.getTag());
103             }
104
105             pv = new PushVlan(t);
106             Assert.assertEquals(valid, pv.isValid());
107             if (valid) {
108                 Assert.assertEquals(t, pv.getTag());
109             }
110         }
111     }
112
113     @Test
114     public void testSetVlanPcpActionCreation() {
115         Action action = null;
116
117         action = new SetVlanPcp(0x4);
118         Assert.assertTrue(action.isValid());
119
120         action = new SetVlanPcp(0x8);
121         Assert.assertFalse(action.isValid());
122
123         action = new SetVlanPcp(-1);
124         Assert.assertFalse(action.isValid());
125     }
126
127     @Test
128     public void testSetVlanCfiActionCreation() {
129         Action action = null;
130
131         action = new SetVlanCfi(0x0);
132         Assert.assertTrue(action.isValid());
133
134         action = new SetVlanCfi(0x1);
135         Assert.assertTrue(action.isValid());
136
137         action = new SetVlanCfi(0x2);
138         Assert.assertFalse(action.isValid());
139
140         action = new SetVlanCfi(-1);
141         Assert.assertFalse(action.isValid());
142     }
143
144     @Test
145     public void testNetworkSetActionCreation() {
146         Action action = null;
147
148         InetAddress ip = null;
149         try {
150             ip = InetAddress.getByName("171.71.9.52");
151         } catch (UnknownHostException e) {
152             logger.error("",e);
153         }
154
155         action = new SetNwSrc(ip);
156         Assert.assertTrue(action.isValid());
157
158         action = new SetNwDst(ip);
159         Assert.assertTrue(action.isValid());
160
161         try {
162             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
163         } catch (UnknownHostException e) {
164             logger.error("", e);
165         }
166         action = new SetNwSrc(ip);
167         Assert.assertTrue(action.isValid());
168
169         action = new SetNwDst(ip);
170         Assert.assertTrue(action.isValid());
171
172         action = new SetNwTos(0xf);
173         Assert.assertTrue(action.isValid());
174
175         action = new SetNwTos(0x3f);
176         Assert.assertTrue(action.isValid());
177
178         action = new SetNwTos(0x40);
179         Assert.assertFalse(action.isValid());
180
181         action = new SetNwTos(0xff1);
182         Assert.assertFalse(action.isValid());
183
184         action = new SetNwTos(-1);
185         Assert.assertFalse(action.isValid());
186     }
187
188     @Test
189     public void testTransportSetActionCreation() {
190         Action action = null;
191
192         action = new SetTpSrc(50000);
193         Assert.assertTrue(action.isValid());
194
195         action = new SetTpDst(65535);
196         Assert.assertTrue(action.isValid());
197
198         action = new SetTpSrc(0);
199         Assert.assertTrue(action.isValid());
200
201         action = new SetTpDst(0);
202         Assert.assertTrue(action.isValid());
203
204         action = new SetTpSrc(-1);
205         Assert.assertFalse(action.isValid());
206
207         action = new SetTpDst(-1);
208         Assert.assertFalse(action.isValid());
209
210         action = new SetTpSrc(65536);
211         Assert.assertFalse(action.isValid());
212
213         action = new SetTpDst(65536);
214         Assert.assertFalse(action.isValid());
215     }
216
217     @Test
218     public void testNextHopActionCreation() {
219         SetNextHop action = null;
220
221         InetAddress ip = null;
222         try {
223             ip = InetAddress.getByName("171.71.9.52");
224         } catch (UnknownHostException e) {
225             logger.error("", e);
226         }
227
228         action = new SetNextHop(ip);
229         Assert.assertTrue(action.getAddress().equals(ip));
230
231         try {
232             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
233         } catch (UnknownHostException e) {
234             logger.error("", e);
235         }
236         action = new SetNextHop(ip);
237         Assert.assertTrue(action.getAddress().equals(ip));
238     }
239
240     @Test
241     public void testActionList() {
242         List<Action> actions = new ArrayList<Action>();
243         short portId = (short) 9;
244         Node node = null;
245         try {
246             node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
247         } catch (ConstructionException e) {
248             // If we reach this point the exception was raised
249             // which is not expected
250             Assert.assertTrue(false);
251         }
252         NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
253                 node);
254         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
255                 (byte) 0x22, (byte) 0x33 };
256         InetAddress ip = null;
257         try {
258             ip = InetAddress.getByName("1.1.1.1");
259         } catch (UnknownHostException e) {
260             logger.error("",e);
261         }
262
263         actions.add(new SetDlSrc(mac));
264         actions.add(new SetNwSrc(ip));
265         actions.add(new Output(nc));
266         Assert.assertTrue(actions.size() == 3);
267         Assert.assertTrue(actions.get(0).isValid());
268
269         Action probe = new Output(nc);
270         Assert.assertTrue(actions.contains(probe));
271         Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
272                 .createNodeConnector((short) 5, node))));
273         Assert.assertFalse(actions.contains(new Controller()));
274     }
275
276     @Test
277     public void testMetadata() {
278         Property tier1 = new Tier(1);
279         Property tier2 = new Tier(2);
280         Property table1 = new Tables((byte)0x7f);
281         Action a1 = new PopVlan();
282         List<Property> resprops = null;
283         resprops = a1.getMetadatas();
284         // This should be an empty list
285         Assert.assertTrue(resprops.isEmpty());
286         a1.setMetadata("tier1", tier1);
287         a1.setMetadata("tier2", tier2);
288         a1.setMetadata("table1", table1);
289         resprops = a1.getMetadatas();
290         // Check for the number of elements in it
291         Assert.assertTrue(resprops.size() == 3);
292         // Check if the elements are in it
293         Assert.assertTrue(resprops.contains(tier1));
294         Assert.assertTrue(resprops.contains(tier2));
295         Assert.assertTrue(resprops.contains(table1));
296         // Check for single elements retrieve
297         Assert.assertTrue(a1.getMetadata("tier1").equals(tier1));
298         Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
299         Assert.assertTrue(a1.getMetadata("table1").equals(table1));
300         // Now remove an element and make sure the remaining are
301         // correct
302         a1.removeMetadata("tier1");
303
304         resprops = a1.getMetadatas();
305         // Check for the number of elements in it
306         Assert.assertTrue(resprops.size() == 2);
307         // Check if the elements are in it
308         Assert.assertFalse(resprops.contains(tier1));
309         Assert.assertTrue(resprops.contains(tier2));
310         Assert.assertTrue(resprops.contains(table1));
311         // Check for single elements retrieve
312         Assert.assertTrue(a1.getMetadata("table1").equals(table1));
313         Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
314         Assert.assertNull(a1.getMetadata("tier1"));
315
316         // Check for an element never existed
317         Assert.assertNull(a1.getMetadata("table100"));
318
319         // Remove them all
320         a1.removeMetadata("tier2");
321         a1.removeMetadata("table1");
322
323         // Remove also a non-existent one
324         a1.removeMetadata("table100");
325
326         resprops = a1.getMetadatas();
327         // Check there are no elements left
328         Assert.assertTrue(resprops.size() == 0);
329
330         // Now check for exception on setting null values
331         try {
332             a1.setMetadata("foo", null);
333             // The line below should never be reached
334             Assert.assertTrue(false);
335         } catch (NullPointerException nue) {
336             // NPE should be raised for null value
337             Assert.assertTrue(true);
338         }
339
340         // Now check on using null key
341         try {
342             a1.setMetadata(null, table1);
343             // The line below should never be reached
344             Assert.assertTrue(false);
345         } catch (NullPointerException nue) {
346             // NPE should be raised for null value
347             Assert.assertTrue(true);
348         }
349
350         // Now check on using null key and null value
351         try {
352             a1.setMetadata(null, null);
353             // The line below should never be reached
354             Assert.assertTrue(false);
355         } catch (NullPointerException nue) {
356             // NPE should be raised for null value
357             Assert.assertTrue(true);
358         }
359     }
360 }