Refactor ForwardingRulesmanager
[controller.git] / opendaylight / forwardingrulesmanager / api / src / test / java / org / opendaylight / controller / forwardingrulesmanager / frmTest.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.forwardingrulesmanager;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
23 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
24 import org.opendaylight.controller.sal.action.Action;
25 import org.opendaylight.controller.sal.action.ActionType;
26 import org.opendaylight.controller.sal.action.Controller;
27 import org.opendaylight.controller.sal.action.Flood;
28 import org.opendaylight.controller.sal.action.Output;
29 import org.opendaylight.controller.sal.action.PopVlan;
30 import org.opendaylight.controller.sal.action.SetDlDst;
31 import org.opendaylight.controller.sal.action.SetNwDst;
32 import org.opendaylight.controller.sal.action.SetVlanId;
33 import org.opendaylight.controller.sal.core.ContainerFlow;
34 import org.opendaylight.controller.sal.core.Node;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.flowprogrammer.Flow;
37 import org.opendaylight.controller.sal.match.Match;
38 import org.opendaylight.controller.sal.match.MatchType;
39 import org.opendaylight.controller.sal.utils.EtherTypes;
40 import org.opendaylight.controller.sal.utils.IPProtocols;
41 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
42 import org.opendaylight.controller.sal.utils.NodeCreator;
43 import org.opendaylight.controller.sal.utils.Status;
44
45 public class frmTest {
46
47     @Test
48     public void testFlowEntryInstall() throws UnknownHostException {
49         Node node = NodeCreator.createOFNode(1L);
50         FlowEntry pol = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
51         FlowEntry pol2 = new FlowEntry("polTest2", null, getSampleFlowV6(node), node);
52         FlowEntryInstall fei = new FlowEntryInstall(pol.clone(), null);
53         FlowEntryInstall fei2 = new FlowEntryInstall(pol.clone(), null);
54         FlowEntryInstall fei3 = new FlowEntryInstall(pol2.clone(), null);
55         Assert.assertTrue(fei.getOriginal().equals(pol));
56         Assert.assertTrue(fei.getInstall().equals(pol));
57         Assert.assertTrue(fei.getFlowName().equals(pol.getFlowName()));
58         Assert.assertTrue(fei.getGroupName().equals(pol.getGroupName()));
59         Assert.assertTrue(fei.getNode().equals(pol.getNode()));
60         Assert.assertFalse(fei.isDeletePending());
61         fei.toBeDeleted();
62         Assert.assertTrue(fei.isDeletePending());
63         Assert.assertNull(fei.getContainerFlow());
64         Assert.assertTrue(fei.equalsByNodeAndName(pol.getNode(), pol.getFlowName()));
65
66         Assert.assertTrue(fei.equals(fei2));
67         Assert.assertFalse(fei.equals(null));
68         Assert.assertTrue(fei.equals(fei3));
69
70     }
71
72     @Test
73     public void testFlowEntryCreation() throws UnknownHostException {
74         Node node = NodeCreator.createOFNode(1L);
75         FlowEntry pol = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
76         Assert.assertTrue(pol.getFlow().equals(getSampleFlowV6(node)));
77     }
78
79     @Test
80     public void testFlowEntrySetGet() throws UnknownHostException {
81         Node node = NodeCreator.createOFNode(1L);
82         Node node2 = NodeCreator.createOFNode(2L);
83         FlowEntry pol = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
84         pol.setGroupName("polTest2");
85         pol.setFlowName("flowName");
86         Assert.assertTrue(pol.getFlowName().equals("flowName"));
87         Assert.assertTrue(pol.getGroupName().equals("polTest2"));
88         pol.setNode(node2);
89         Assert.assertTrue(pol.getNode().equals(node2));
90         Assert.assertTrue(pol.equalsByNodeAndName(node2, "flowName"));
91     }
92
93     @Test
94     public void testFlowEntryEquality() throws UnknownHostException {
95         Node node = NodeCreator.createOFNode(1L);
96         Node node2 = NodeCreator.createOFNode(1L);
97         FlowEntry pol = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
98         FlowEntry pol2 = new FlowEntry("polTest", null, getSampleFlowV6(node), node2);
99         Assert.assertTrue(pol.equals(pol2));
100     }
101
102     @Test
103     public void testFlowEntryCollision() throws UnknownHostException {
104         // Create 2 equal FlowEntry objects
105         Node node1 = NodeCreator.createOFNode(1L);
106         Node node2 = NodeCreator.createOFNode(1L);
107         FlowEntry fe1 = new FlowEntry("Junit", "flow1", getSampleFlowV6(node1), node1);
108         FlowEntry fe2 = new FlowEntry("Junit", "flow2", getSampleFlowV6(node2), node1);
109
110         // Check equality in FlowEntry and parameters
111         Assert.assertTrue(fe1.getFlow().getMatch().equals(fe2.getFlow().getMatch()));
112         Assert.assertTrue(fe1.getFlow().getMatch().getMatches() == fe2.getFlow().getMatch().getMatches());
113         Assert.assertTrue(fe1.getFlow().getMatch().hashCode() == fe2.getFlow().getMatch().hashCode());
114         Assert.assertTrue(fe1.getFlow().hashCode() == fe2.getFlow().hashCode());
115         Assert.assertTrue(fe1.equals(fe2));
116         Assert.assertTrue(fe1.hashCode() == fe2.hashCode());
117
118         // Change priority field for fe2, verify inequality
119         fe2.getFlow().setPriority((short)1000);
120
121         // Verify FlowEntry works as key in collection
122         ConcurrentMap<FlowEntry, FlowEntry> map = new ConcurrentHashMap<FlowEntry, FlowEntry>();
123         Assert.assertTrue(null == map.put(fe1, fe1));
124         Assert.assertTrue(fe1.clone().equals(map.put(fe1.clone(), fe1.clone())));
125         Assert.assertTrue(map.get(fe1.clone()).equals(fe1.clone()));
126         Assert.assertTrue(map.keySet().contains(fe1.clone()));
127         Assert.assertTrue(map.containsKey(fe1));
128
129         // Remove key
130         map.remove(fe1);
131         Assert.assertTrue(map.isEmpty());
132         Assert.assertFalse(map.containsKey(fe1));
133
134         // Verify cloned object as key
135         map.put(fe1.clone(), fe1.clone());
136         Assert.assertTrue(map.containsKey(fe1));
137
138         // Verify different key is not present
139         Assert.assertFalse(map.containsKey(fe2));
140
141         // Add different key
142         map.put(fe2.clone(), fe2.clone());
143         Assert.assertTrue(map.size() == 2);
144         Assert.assertTrue(map.containsKey(fe1));
145         Assert.assertTrue(map.containsKey(fe2));
146
147         // Make fe2 equal to fe1 again
148         fe2.getFlow().setPriority((short)300);
149         Assert.assertTrue(fe2.equals(fe1));
150         Assert.assertTrue(map.containsKey(fe2));
151
152         // Clean up
153         map.clear();
154     }
155
156     @Test
157     public void testFlowEntryInstallCollision() throws UnknownHostException {
158         // Create 2 equal FlowEntryInstall objects
159         Node node1 = NodeCreator.createOFNode(1L);
160         Node node2 = NodeCreator.createOFNode(1L);
161         FlowEntry fe1 = new FlowEntry("Junit", "flow1", getSampleFlowV6(node1), node1);
162         FlowEntry fe2 = new FlowEntry("Junit", "flow2", getSampleFlowV6(node2), node1);
163         ContainerFlow cf1 = null;
164         ContainerFlow cf2 = null;
165         FlowEntryInstall fei1 = new FlowEntryInstall(fe1, cf1);
166         FlowEntryInstall fei2 = new FlowEntryInstall(fe2, cf2);
167
168         // Check equality in FlowEntry and parameters
169         Assert.assertTrue(fei1.equals(fei2));
170         Assert.assertTrue(fei1.hashCode() == fei2.hashCode());
171
172         // Verify FlowEntryInstall works as key in collection
173         ConcurrentMap<FlowEntryInstall, FlowEntryInstall> map =
174                 new ConcurrentHashMap<FlowEntryInstall, FlowEntryInstall>();
175         Assert.assertTrue(null == map.put(fei1, fei1));
176         Assert.assertTrue(map.get(fei1).equals(fei2));
177         Assert.assertTrue(map.keySet().contains(fei1));
178         Assert.assertTrue(map.keySet().contains(fei2));
179         Assert.assertTrue(map.containsKey(fei1));
180
181         // Remove key
182         map.remove(fei1);
183         Assert.assertTrue(map.isEmpty());
184         Assert.assertFalse(map.containsKey(fei1));
185
186         // Verify cloned object as key
187         map.put(fei1, fei1);
188         Assert.assertTrue(map.containsKey(fei1));
189
190         // Change fei2, change relevant hashcode info
191         fei2.getInstall().getFlow().setPriority((short)301);
192         Assert.assertFalse(fei1.equals(fei2));
193         Assert.assertFalse(fei1.hashCode() == fei2.hashCode());
194
195
196         // Verify different key is not present
197         Assert.assertFalse(map.containsKey(fei2));
198
199         // Add different key
200         map.put(fei2, fei2);
201         Assert.assertTrue(map.size() == 2);
202         Assert.assertTrue(map.containsKey(fei1));
203         Assert.assertTrue(map.containsKey(fei2));
204
205         // Make fei2 equal to fei1 again
206         fei2.getInstall().getFlow().setPriority((short)300);
207         Assert.assertTrue(fei2.equals(fei1));
208         Assert.assertTrue(map.containsKey(fei2));
209
210         // Clean up
211         map.clear();
212     }
213
214     @Test
215     public void testFlowEntryCloning() throws UnknownHostException {
216         Node node = NodeCreator.createOFNode(1L);
217         FlowEntry pol = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
218         FlowEntry pol2 = pol.clone();
219         Assert.assertTrue(pol.equals(pol2));
220     }
221
222     @Test
223     public void testFlowEntrySet() throws UnknownHostException {
224         Set<FlowEntry> set = new HashSet<FlowEntry>();
225
226         Node node1 = NodeCreator.createOFNode(1L);
227         Node node2 = NodeCreator.createOFNode(2L);
228         Node node3 = NodeCreator.createOFNode(3L);
229
230         Match match = new Match();
231         match.setField(MatchType.NW_SRC, InetAddress.getAllByName("1.1.1.1"));
232         match.setField(MatchType.NW_DST, InetAddress.getAllByName("2.2.2.2"));
233         match.setField(MatchType.DL_TYPE, EtherTypes.IPv4.shortValue());
234
235         List<Action> actionList = new ArrayList<Action>();
236         // actionList.add(new Drop());
237
238         Flow flow = new Flow(match, actionList);
239         FlowEntry pol1 = new FlowEntry("m1", "same", flow, node1);
240         FlowEntry pol2 = new FlowEntry("m2", "same", flow, node2);
241         FlowEntry pol3 = new FlowEntry("m3", "same", flow, node3);
242
243         set.add(pol1);
244         set.add(pol2);
245         set.add(pol3);
246
247         Assert.assertTrue(set.contains(pol1));
248         Assert.assertTrue(set.contains(pol2));
249         Assert.assertTrue(set.contains(pol3));
250
251         Assert.assertTrue(set.contains(pol1.clone()));
252         Assert.assertTrue(set.contains(pol2.clone()));
253         Assert.assertTrue(set.contains(pol3.clone()));
254
255     }
256
257     @Test
258     public void testInternalFlow() {
259         FlowConfig flowConfig = new FlowConfig();
260         Assert.assertFalse(flowConfig.isInternalFlow());
261         flowConfig.setName("**Internal");
262         Assert.assertTrue(flowConfig.isInternalFlow());
263         flowConfig.setName("External");
264         Assert.assertFalse(flowConfig.isInternalFlow());
265     }
266
267     @Test
268     public void testFlowConfigCreateSet() throws UnknownHostException {
269         FlowConfig frmC = new FlowConfig();
270         FlowConfig frmC3 = new FlowConfig();
271         Node node = NodeCreator.createOFNode(1L);
272         FlowEntry entry = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
273
274         // testing equal function
275         Assert.assertFalse(frmC.equals(null));
276         Assert.assertTrue(frmC.equals(frmC));
277         Assert.assertTrue(frmC.equals(frmC3));
278         Assert.assertFalse(frmC.equals(entry));
279         FlowConfig flowC = createSampleFlowConfig();
280         Assert.assertFalse(frmC.equals(flowC));
281         // testing installInHW
282         Assert.assertTrue(frmC.installInHw());
283         frmC.setInstallInHw(false);
284         Assert.assertFalse(frmC.installInHw());
285         frmC.setInstallInHw(true);
286         Assert.assertTrue(frmC.installInHw());
287
288         // testing general set and get methods
289         ArrayList<String> actions = createSampleActionList();
290         frmC.setActions(actions);
291         Assert.assertFalse(frmC.equals(frmC3));
292         frmC3.setActions(actions);
293
294         Assert.assertFalse(frmC.equals(flowC));
295         frmC.setCookie("0");
296         Assert.assertTrue(frmC.getCookie().equals("0"));
297         Assert.assertFalse(frmC.equals(frmC3));
298         frmC3.setCookie("0");
299
300         Assert.assertFalse(frmC.equals(flowC));
301         frmC.setDstMac("00:A0:C9:22:AB:11");
302         Assert.assertTrue(frmC.getDstMac().equals("00:A0:C9:22:AB:11"));
303         Assert.assertFalse(frmC.equals(frmC3));
304         frmC3.setDstMac("00:A0:C9:22:AB:11");
305
306         Assert.assertFalse(frmC.equals(flowC));
307         frmC.setSrcMac("00:A0:C9:14:C8:29");
308         Assert.assertTrue(frmC.getSrcMac().equals("00:A0:C9:14:C8:29"));
309         Assert.assertFalse(frmC.equals(frmC3));
310         frmC3.setSrcMac("00:A0:C9:14:C8:29");
311
312         Assert.assertFalse(frmC.equals(flowC));
313         frmC.setDynamic(true);
314         Assert.assertTrue(frmC.isDynamic());
315         Assert.assertFalse(frmC.equals(frmC3));
316         frmC3.setDynamic(true);
317         flowC.setDynamic(true);
318
319         Assert.assertFalse(frmC.equals(flowC));
320         frmC.setEtherType("0x0800");
321         Assert.assertTrue(frmC.getEtherType().equals("0x0800"));
322         Assert.assertFalse(frmC.equals(frmC3));
323         frmC3.setEtherType("0x0800");
324
325         Assert.assertFalse(frmC.equals(flowC));
326         frmC.setIngressPort("60");
327         Assert.assertTrue(frmC.getIngressPort().equals("60"));
328         Assert.assertFalse(frmC.equals(frmC3));
329         frmC3.setIngressPort("60");
330
331         Assert.assertFalse(frmC.equals(flowC));
332         frmC.setName("Config1");
333         Assert.assertTrue(frmC.getName().equals("Config1"));
334         Assert.assertFalse(frmC.equals(frmC3));
335         frmC3.setName("Config1");
336
337         Assert.assertFalse(frmC.equals(flowC));
338         frmC.setDstIp("2.2.2.2");
339         Assert.assertTrue(frmC.getDstIp().equals("2.2.2.2"));
340         Assert.assertFalse(frmC.equals(frmC3));
341         frmC3.setDstIp("2.2.2.2");
342
343         Assert.assertFalse(frmC.equals(flowC));
344         frmC.setSrcIp("1.2.3.4");
345         Assert.assertTrue(frmC.getSrcIp().equals("1.2.3.4"));
346         Assert.assertFalse(frmC.equals(frmC3));
347         frmC3.setSrcIp("1.2.3.4");
348
349         Assert.assertFalse(frmC.equals(flowC));
350         Assert.assertFalse(frmC.isPortGroupEnabled());
351         frmC.setPortGroup("2");
352         Assert.assertTrue(frmC.isPortGroupEnabled());
353         Assert.assertTrue(frmC.getPortGroup().equals("2"));
354         Assert.assertFalse(frmC.equals(frmC3));
355         frmC3.setPortGroup("2");
356
357         Assert.assertFalse(frmC.equals(flowC));
358         frmC.setPriority("100");
359         Assert.assertTrue(frmC.getPriority().equals("100"));
360         Assert.assertFalse(frmC.equals(frmC3));
361         frmC3.setPriority("100");
362
363         Assert.assertFalse(frmC.equals(flowC));
364         frmC.setProtocol(IPProtocols.TCP.toString());
365         Assert.assertTrue(frmC.getProtocol().equals(IPProtocols.TCP.toString()));
366         Assert.assertFalse(frmC.equals(frmC3));
367         frmC3.setProtocol(IPProtocols.TCP.toString());
368
369         Assert.assertFalse(frmC.equals(flowC));
370         frmC.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
371         Assert.assertTrue(frmC.getNode().equals(Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
372         Assert.assertFalse(frmC.equals(frmC3));
373         frmC3.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
374
375         Assert.assertFalse(frmC.equals(flowC));
376         frmC.setTosBits("0");
377         Assert.assertTrue(frmC.getTosBits().equals("0"));
378         Assert.assertFalse(frmC.equals(frmC3));
379         frmC3.setTosBits("0");
380
381         Assert.assertFalse(frmC.equals(flowC));
382         frmC.setDstPort("100");
383         Assert.assertTrue(frmC.getDstPort().equals("100"));
384         Assert.assertFalse(frmC.equals(frmC3));
385         frmC3.setDstPort("100");
386
387         Assert.assertFalse(frmC.equals(flowC));
388         frmC.setSrcPort("8080");
389         Assert.assertTrue(frmC.getSrcPort().equals("8080"));
390         Assert.assertFalse(frmC.equals(frmC3));
391         frmC3.setSrcPort("8080");
392
393         Assert.assertFalse(frmC.equals(flowC));
394         frmC.setVlanId("100");
395         Assert.assertTrue(frmC.getVlanId().equals("100"));
396         Assert.assertFalse(frmC.equals(frmC3));
397         frmC3.setVlanId("100");
398
399         Assert.assertFalse(frmC.equals(flowC));
400         frmC.setVlanPriority("0");
401         Assert.assertTrue(frmC.getVlanPriority().equals("0"));
402         Assert.assertFalse(frmC.equals(frmC3));
403         frmC3.setVlanPriority("0");
404
405         Assert.assertFalse(frmC.equals(flowC));
406         frmC.setIdleTimeout("300");
407         Assert.assertTrue(frmC.getIdleTimeout().equals("300"));
408         Assert.assertFalse(frmC.equals(frmC3));
409         frmC3.setIdleTimeout("300");
410
411         Assert.assertFalse(frmC.equals(flowC));
412         frmC.setHardTimeout("1000");
413         Assert.assertTrue(frmC.getHardTimeout().equals("1000"));
414         Assert.assertFalse(frmC.equals(frmC3));
415         frmC3.setHardTimeout("1000");
416
417         // Assert.assertFalse(frmC.equals(flowC));
418         Assert.assertTrue(actions.equals(frmC.getActions()));
419
420         FlowConfig frmC2 = new FlowConfig(frmC);
421
422         Assert.assertFalse(frmC2.equals(frmC));
423         frmC2.setDynamic(false);
424         Assert.assertFalse(frmC2.equals(frmC));
425         frmC2.setDynamic(true);
426         Assert.assertTrue(frmC2.equals(frmC));
427         // Assert.assertFalse(frmC2.equals(frmC3));
428         flowC.setDynamic(true);
429         Assert.assertTrue(flowC.equals(frmC));
430         Assert.assertTrue(flowC.isStatusSuccessful());
431         flowC.setStatus("Invalid");
432         Assert.assertFalse(flowC.isStatusSuccessful());
433
434         flowC.getActions().add(ActionType.DROP.toString());
435         Assert.assertFalse(flowC.equals(frmC));
436         Assert.assertFalse(flowC.isIPv6());
437         flowC.setDstIp("2001:420:281:1004:407a:57f4:4d15:c355");
438         Assert.assertTrue(flowC.isIPv6());
439         flowC.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
440         Assert.assertTrue(flowC.isIPv6());
441
442         Long id = (Long) flowC.getNode().getID();
443         Assert.assertTrue(id.toString().equals("1"));
444
445     }
446
447     @Test
448     public void testFlowConfigEqualities() throws UnknownHostException {
449         FlowConfig fc = new FlowConfig();
450         FlowConfig fc2 = new FlowConfig();
451         fc.setName("flow1");
452         fc.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
453         Assert.assertFalse(fc.onNode(Node.fromString(Node.NodeIDType.OPENFLOW, "0")));
454         Assert.assertTrue(fc.onNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
455
456         Assert.assertTrue(fc.isByNameAndNodeIdEqual("flow1", Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
457         Assert.assertFalse(fc.isByNameAndNodeIdEqual("flow1", Node.fromString(Node.NodeIDType.OPENFLOW, "0")));
458         Assert.assertFalse(fc.isByNameAndNodeIdEqual("flow2", Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
459
460         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
461         fc2.setName("flow1");
462         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
463         fc2.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "0"));
464         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
465         fc2.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
466         Assert.assertTrue(fc.isByNameAndNodeIdEqual(fc2));
467     }
468
469     @Test
470     public void testStatusToggle() throws UnknownHostException {
471         FlowConfig fc = new FlowConfig();
472         fc.toggleInstallation();
473         Assert.assertTrue(fc.installInHw());
474         fc.toggleInstallation();
475         Assert.assertFalse(fc.installInHw());
476         fc.toggleInstallation();
477         Assert.assertTrue(fc.installInHw());
478
479     }
480
481     @Test
482     public void testGetFlowEntry() throws UnknownHostException {
483         FlowConfig fc2 = createSampleFlowConfig();
484         FlowEntry fe = fc2.getFlowEntry();
485         Assert.assertNotNull(fe);
486     }
487
488     @Test
489     public void testGetFlow() throws UnknownHostException {
490         FlowConfig fc = new FlowConfig();
491         fc.setActions(createSampleActionList());
492         Flow flow = fc.getFlow();
493         Assert.assertNotNull(flow);
494     }
495
496     @Test
497     public void testL2AddressValid() {
498         FlowConfig fc = new FlowConfig();
499         Assert.assertFalse(fc.isL2AddressValid(null));
500         Assert.assertFalse(fc.isL2AddressValid("11"));
501         Assert.assertFalse(fc.isL2AddressValid("00:A0:C9:14:C8:"));
502         Assert.assertFalse(fc.isL2AddressValid("000:A01:C9:14:C8:211"));
503
504         Assert.assertTrue(fc.isL2AddressValid("00:A0:C9:14:C8:29"));
505     }
506
507     @Test
508     public void testValid() throws UnknownHostException {
509         FlowConfig fc2 = createSampleFlowConfig();
510         Assert.assertTrue(fc2.validate(null).isSuccess());
511
512         FlowConfig fc = new FlowConfig();
513         Status status = fc.validate(null);
514         Assert.assertFalse(status.isSuccess());
515         Assert.assertTrue(status.getDescription().contains("Invalid name"));
516
517         fc.setName("Config");
518         status = fc.validate(null);
519         Assert.assertFalse(status.isSuccess());
520         Assert.assertTrue(status.getDescription().contains("Node is null"));
521
522         fc.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
523         Assert.assertTrue(fc.validate(null).isSuccess());
524
525         fc.setPriority("-1");
526         status = fc.validate(null);
527         Assert.assertFalse(status.isSuccess());
528         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 65535"));
529
530         fc.setPriority("100000");
531         status = fc.validate(null);
532         Assert.assertFalse(status.isSuccess());
533         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 65535"));
534
535         fc.setPriority("2000");
536         Assert.assertTrue(fc.validate(null).isSuccess());
537
538         fc.setCookie("100");
539         Assert.assertTrue(fc.validate(null).isSuccess());
540
541         fc.setIngressPort("-1");
542         status = fc.validate(null);
543         Assert.assertFalse(status.isSuccess());
544         Assert.assertTrue(status.getDescription().contains("is not valid for the Switch"));
545
546         fc.setIngressPort("100");
547         Assert.assertTrue(fc.validate(null).isSuccess());
548
549         fc.setVlanId(("-1"));
550         status = fc.validate(null);
551         Assert.assertFalse(status.isSuccess());
552         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 4095"));
553
554         fc.setVlanId("5000");
555         status = fc.validate(null);
556         Assert.assertFalse(status.isSuccess());
557         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 4095"));
558
559         fc.setVlanId("100");
560         Assert.assertTrue(fc.validate(null).isSuccess());
561
562         fc.setVlanPriority("-1");
563         status = fc.validate(null);
564         Assert.assertFalse(status.isSuccess());
565         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 7"));
566
567         fc.setVlanPriority("9");
568         status = fc.validate(null);
569         Assert.assertFalse(status.isSuccess());
570         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 7"));
571
572         fc.setVlanPriority("5");
573         Assert.assertTrue(fc.validate(null).isSuccess());
574
575         fc.setEtherType("-1");
576         status = fc.validate(null);
577         Assert.assertFalse(status.isSuccess());
578         Assert.assertTrue(status.getDescription().contains("Ethernet type"));
579
580         fc.setEtherType("0xfffff");
581         status = fc.validate(null);
582         Assert.assertFalse(status.isSuccess());
583         Assert.assertTrue(status.getDescription().contains("Ethernet type"));
584
585         fc.setEtherType("0x800");
586         Assert.assertTrue(fc.validate(null).isSuccess());
587
588         fc.setTosBits("-1");
589         status = fc.validate(null);
590         Assert.assertFalse(status.isSuccess());
591         Assert.assertTrue(status.getDescription().contains("IP ToS bits"));
592
593         fc.setTosBits("65");
594         status = fc.validate(null);
595         Assert.assertFalse(status.isSuccess());
596         Assert.assertTrue(status.getDescription().contains("IP ToS bits"));
597
598         fc.setTosBits("60");
599         Assert.assertTrue(fc.validate(null).isSuccess());
600
601         fc.setSrcPort("-1");
602         status = fc.validate(null);
603         Assert.assertFalse(status.isSuccess());
604         Assert.assertTrue(status.getDescription().contains("Transport source port"));
605
606         fc.setSrcPort("0xfffff");
607         status = fc.validate(null);
608         Assert.assertFalse(status.isSuccess());
609         Assert.assertTrue(status.getDescription().contains("Transport source port"));
610
611         fc.setSrcPort("0x00ff");
612         Assert.assertTrue(fc.validate(null).isSuccess());
613
614         fc.setDstPort("-1");
615         status = fc.validate(null);
616         Assert.assertFalse(status.isSuccess());
617         Assert.assertTrue(status.getDescription().contains("Transport destination port"));
618
619         fc.setDstPort("0xfffff");
620         status = fc.validate(null);
621         Assert.assertFalse(status.isSuccess());
622         Assert.assertTrue(status.getDescription().contains("Transport destination port"));
623
624         fc.setDstPort("0x00ff");
625         Assert.assertTrue(fc.validate(null).isSuccess());
626
627         fc.setSrcMac("abc");
628         status = fc.validate(null);
629         Assert.assertFalse(status.isSuccess());
630         Assert.assertTrue(status.getDescription().contains("Ethernet source address"));
631
632         fc.setSrcMac("00:A0:C9:14:C8:29");
633         Assert.assertTrue(fc.validate(null).isSuccess());
634
635         fc.setDstMac("abc");
636         status = fc.validate(null);
637         Assert.assertFalse(status.isSuccess());
638         Assert.assertTrue(status.getDescription().contains("Ethernet destination address"));
639
640         fc.setDstMac("00:A0:C9:22:AB:11");
641         Assert.assertTrue(fc.validate(null).isSuccess());
642
643         fc.setSrcIp("-1");
644         status = fc.validate(null);
645         Assert.assertFalse(status.isSuccess());
646         Assert.assertTrue(status.getDescription().contains("IP source address"));
647
648         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
649         status = fc.validate(null);
650         Assert.assertFalse(status.isSuccess());
651         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Src IP"));
652
653         fc.setEtherType("0x86dd");
654         Assert.assertTrue(fc.validate(null).isSuccess());
655
656         fc.setSrcIp("1.1.1.1");
657         status = fc.validate(null);
658         Assert.assertFalse(status.isSuccess());
659         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Src IP"));
660
661         fc.setEtherType("0x800");
662         Assert.assertTrue(fc.validate(null).isSuccess());
663
664         fc.setDstIp("-1");
665         status = fc.validate(null);
666         Assert.assertFalse(status.isSuccess());
667         Assert.assertTrue(status.getDescription().contains("IP destination address"));
668
669         fc.setDstIp("2001:420:281:1004:407a:57f4:4d15:c355");
670         status = fc.validate(null);
671         Assert.assertFalse(status.isSuccess());
672         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Dst IP"));
673
674         fc.setEtherType("0x86dd");
675         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
676         Assert.assertTrue(fc.validate(null).isSuccess());
677
678         fc.setDstIp("2.2.2.2");
679         status = fc.validate(null);
680         Assert.assertFalse(status.isSuccess());
681         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Dst IP"));
682
683         fc.setEtherType("0x800");
684         fc.setSrcIp("1.1.1.1");
685         Assert.assertTrue(fc.validate(null).isSuccess());
686
687         fc.setEtherType(null);
688         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
689         status = fc.validate(null);
690         Assert.assertFalse(status.isSuccess());
691         Assert.assertTrue(status.getDescription().contains("IP Src Dest Type mismatch"));
692
693         fc.setSrcIp("1.1.1.1");
694         fc.setIdleTimeout("-1");
695         status = fc.validate(null);
696         Assert.assertFalse(status.isSuccess());
697         Assert.assertTrue(status.getDescription().contains("Idle Timeout value"));
698
699         fc.setIdleTimeout("0xfffff");
700         status = fc.validate(null);
701         Assert.assertFalse(status.isSuccess());
702         Assert.assertTrue(status.getDescription().contains("Idle Timeout value"));
703
704         fc.setIdleTimeout("10");
705         Assert.assertTrue(fc.validate(null).isSuccess());
706
707         fc.setHardTimeout("-1");
708         status = fc.validate(null);
709         Assert.assertFalse(status.isSuccess());
710         Assert.assertTrue(status.getDescription().contains("Hard Timeout value"));
711
712         fc.setHardTimeout("0xfffff");
713         status = fc.validate(null);
714         Assert.assertFalse(status.isSuccess());
715         Assert.assertTrue(status.getDescription().contains("Hard Timeout value"));
716
717         fc.setHardTimeout("10");
718         Assert.assertTrue(fc.validate(null).isSuccess());
719
720     }
721
722     private FlowConfig createSampleFlowConfig() throws UnknownHostException {
723         ArrayList<String> actions;
724         actions = createSampleActionList();
725         // actions.add(ActionType.CONTROLLER.toString());
726         FlowConfig flowConfig = new FlowConfig("true", "Config1", Node.fromString(Node.NodeIDType.OPENFLOW, "1"),
727                 "100", "0", "60", "2", "100", "0", "0x0800", "00:A0:C9:14:C8:29", "00:A0:C9:22:AB:11",
728                 IPProtocols.TCP.toString(), "0", "1.2.3.4", "2.2.2.2", "8080", "100", "300", "1000", actions);
729         return flowConfig;
730
731     }
732
733     private ArrayList<String> createSampleActionList() {
734         ArrayList<String> actions = new ArrayList<String>();
735         actions.add(ActionType.DROP.toString());
736         actions.add(ActionType.LOOPBACK.toString());
737         actions.add(ActionType.FLOOD.toString());
738         actions.add(ActionType.SW_PATH.toString());
739         actions.add(ActionType.HW_PATH.toString());
740         actions.add(ActionType.SET_VLAN_PCP.toString() + "=1");
741         actions.add(ActionType.SET_VLAN_ID.toString() + "=1");
742         actions.add(ActionType.POP_VLAN.toString());
743         actions.add(ActionType.SET_DL_SRC.toString() + "=00:A0:C1:AB:22:11");
744         actions.add(ActionType.SET_DL_DST.toString() + "=00:B1:C1:00:AA:BB");
745         actions.add(ActionType.SET_NW_SRC.toString() + "=1.1.1.1");
746         actions.add(ActionType.SET_NW_DST.toString() + "=2.2.2.2");
747         actions.add(ActionType.CONTROLLER.toString());
748         actions.add(ActionType.SET_NW_TOS.toString() + "1");
749         actions.add(ActionType.SET_TP_SRC.toString() + "60");
750         actions.add(ActionType.SET_TP_DST.toString() + "8080");
751         actions.add(ActionType.SET_NEXT_HOP.toString() + "=1.1.1.1");
752
753         return actions;
754     }
755
756     private Flow getSampleFlowV6(Node node) throws UnknownHostException {
757         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
758         NodeConnector oport = NodeConnectorCreator.createOFNodeConnector((short) 30, node);
759         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
760         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
761         byte newMac[] = { (byte) 0x11, (byte) 0xaa, (byte) 0xbb, (byte) 0x34, (byte) 0x9a, (byte) 0xee };
762         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
763         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
764         InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
765         InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
766         InetAddress newIP = InetAddress.getByName("2056:650::a1b0");
767         short ethertype = EtherTypes.IPv6.shortValue();
768         short vlan = (short) 27;
769         byte vlanPr = (byte) 3;
770         Byte tos = 4;
771         byte proto = IPProtocols.UDP.byteValue();
772         short src = (short) 5500;
773         short dst = 80;
774
775         /*
776          * Create a SAL Flow aFlow
777          */
778         Match match = new Match();
779         match.setField(MatchType.IN_PORT, port);
780         match.setField(MatchType.DL_SRC, srcMac);
781         match.setField(MatchType.DL_DST, dstMac);
782         match.setField(MatchType.DL_TYPE, ethertype);
783         match.setField(MatchType.DL_VLAN, vlan);
784         match.setField(MatchType.DL_VLAN_PR, vlanPr);
785         match.setField(MatchType.NW_SRC, srcIP, ipMask);
786         match.setField(MatchType.NW_DST, dstIP, ipMask2);
787         match.setField(MatchType.NW_TOS, tos);
788         match.setField(MatchType.NW_PROTO, proto);
789         match.setField(MatchType.TP_SRC, src);
790         match.setField(MatchType.TP_DST, dst);
791
792         List<Action> actions = new ArrayList<Action>();
793         actions.add(new Controller());
794         actions.add(new SetVlanId(5));
795         actions.add(new SetDlDst(newMac));
796         actions.add(new SetNwDst(newIP));
797         actions.add(new Output(oport));
798         actions.add(new PopVlan());
799         actions.add(new Flood());
800
801         Flow flow = new Flow(match, actions);
802         flow.setPriority((short) 300);
803         flow.setHardTimeout((short) 240);
804
805         return flow;
806     }
807 }