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