3fe9a18b3fa545cbdcf279092c272c43a34d4cc1
[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
92     @Test
93     public void testSetVlanPcpActionCreation() {
94         Action action = null;
95
96         action = new SetVlanPcp(0x4);
97         Assert.assertTrue(action.isValid());
98
99         action = new SetVlanPcp(0x8);
100         Assert.assertFalse(action.isValid());
101
102         action = new SetVlanPcp(-1);
103         Assert.assertFalse(action.isValid());
104     }
105
106     @Test
107     public void testSetVlanCfiActionCreation() {
108         Action action = null;
109
110         action = new SetVlanCfi(0x0);
111         Assert.assertTrue(action.isValid());
112
113         action = new SetVlanCfi(0x1);
114         Assert.assertTrue(action.isValid());
115
116         action = new SetVlanCfi(0x2);
117         Assert.assertFalse(action.isValid());
118
119         action = new SetVlanCfi(-1);
120         Assert.assertFalse(action.isValid());
121     }
122
123     @Test
124     public void testNetworkSetActionCreation() {
125         Action action = null;
126
127         InetAddress ip = null;
128         try {
129             ip = InetAddress.getByName("171.71.9.52");
130         } catch (UnknownHostException e) {
131             logger.error("",e);
132         }
133
134         action = new SetNwSrc(ip);
135         Assert.assertTrue(action.isValid());
136
137         action = new SetNwDst(ip);
138         Assert.assertTrue(action.isValid());
139
140         try {
141             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
142         } catch (UnknownHostException e) {
143             logger.error("", e);
144         }
145         action = new SetNwSrc(ip);
146         Assert.assertTrue(action.isValid());
147
148         action = new SetNwDst(ip);
149         Assert.assertTrue(action.isValid());
150
151         action = new SetNwTos(0xf);
152         Assert.assertTrue(action.isValid());
153
154         action = new SetNwTos(0x3f);
155         Assert.assertTrue(action.isValid());
156
157         action = new SetNwTos(0x40);
158         Assert.assertFalse(action.isValid());
159
160         action = new SetNwTos(0xff1);
161         Assert.assertFalse(action.isValid());
162
163         action = new SetNwTos(-1);
164         Assert.assertFalse(action.isValid());
165     }
166
167     @Test
168     public void testTransportSetActionCreation() {
169         Action action = null;
170
171         action = new SetTpSrc(50000);
172         Assert.assertTrue(action.isValid());
173
174         action = new SetTpDst(65535);
175         Assert.assertTrue(action.isValid());
176
177         action = new SetTpSrc(0);
178         Assert.assertTrue(action.isValid());
179
180         action = new SetTpDst(0);
181         Assert.assertTrue(action.isValid());
182
183         action = new SetTpSrc(-1);
184         Assert.assertFalse(action.isValid());
185
186         action = new SetTpDst(-1);
187         Assert.assertFalse(action.isValid());
188
189         action = new SetTpSrc(65536);
190         Assert.assertFalse(action.isValid());
191
192         action = new SetTpDst(65536);
193         Assert.assertFalse(action.isValid());
194     }
195
196     @Test
197     public void testNextHopActionCreation() {
198         SetNextHop action = null;
199
200         InetAddress ip = null;
201         try {
202             ip = InetAddress.getByName("171.71.9.52");
203         } catch (UnknownHostException e) {
204             logger.error("", e);
205         }
206
207         action = new SetNextHop(ip);
208         Assert.assertTrue(action.getAddress().equals(ip));
209
210         try {
211             ip = InetAddress.getByName("2001:420:281:1003:f2de:f1ff:fe71:728d");
212         } catch (UnknownHostException e) {
213             logger.error("", e);
214         }
215         action = new SetNextHop(ip);
216         Assert.assertTrue(action.getAddress().equals(ip));
217     }
218
219     @Test
220     public void testActionList() {
221         List<Action> actions = new ArrayList<Action>();
222         short portId = (short) 9;
223         Node node = null;
224         try {
225             node = new Node(Node.NodeIDType.OPENFLOW, new Long(0x55667788L));
226         } catch (ConstructionException e) {
227             // If we reach this point the exception was raised
228             // which is not expected
229             Assert.assertTrue(false);
230         }
231         NodeConnector nc = NodeConnectorCreator.createNodeConnector(portId,
232                 node);
233         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0x11,
234                 (byte) 0x22, (byte) 0x33 };
235         InetAddress ip = null;
236         try {
237             ip = InetAddress.getByName("1.1.1.1");
238         } catch (UnknownHostException e) {
239             logger.error("",e);
240         }
241
242         actions.add(new SetDlSrc(mac));
243         actions.add(new SetNwSrc(ip));
244         actions.add(new Output(nc));
245         Assert.assertTrue(actions.size() == 3);
246         Assert.assertTrue(actions.get(0).isValid());
247
248         Action probe = new Output(nc);
249         Assert.assertTrue(actions.contains(probe));
250         Assert.assertFalse(actions.contains(new Output(NodeConnectorCreator
251                 .createNodeConnector((short) 5, node))));
252         Assert.assertFalse(actions.contains(new Controller()));
253     }
254 }