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