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