Fixed minimum values for TP_SRC/TP_DST match fields.
[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         Status status = flowConfig.validate(null);
263         Assert.assertFalse(status.isSuccess());
264         Assert.assertTrue(status.getDescription().contains("name"));
265         Assert.assertTrue(flowConfig.isInternalFlow());
266         flowConfig.setName("External");
267         Assert.assertFalse(flowConfig.isInternalFlow());
268     }
269
270     @Test
271     public void testFlowConfigCreateSet() throws UnknownHostException {
272         FlowConfig frmC = new FlowConfig();
273         FlowConfig frmC3 = new FlowConfig();
274         Node node = NodeCreator.createOFNode(1L);
275         FlowEntry entry = new FlowEntry("polTest", null, getSampleFlowV6(node), node);
276
277         // testing equal function
278         Assert.assertFalse(frmC.equals(null));
279         Assert.assertTrue(frmC.equals(frmC));
280         Assert.assertTrue(frmC.equals(frmC3));
281         Assert.assertFalse(frmC.equals(entry));
282         FlowConfig flowC = createSampleFlowConfig();
283         Assert.assertFalse(frmC.equals(flowC));
284         // testing installInHW
285         Assert.assertTrue(frmC.installInHw());
286         frmC.setInstallInHw(false);
287         Assert.assertFalse(frmC.installInHw());
288         frmC.setInstallInHw(true);
289         Assert.assertTrue(frmC.installInHw());
290
291         // testing general set and get methods
292         ArrayList<String> actions = createSampleActionList();
293         frmC.setActions(actions);
294         Assert.assertFalse(frmC.equals(frmC3));
295         frmC3.setActions(actions);
296
297         Assert.assertFalse(frmC.equals(flowC));
298         frmC.setCookie("0");
299         Assert.assertTrue(frmC.getCookie().equals("0"));
300         Assert.assertFalse(frmC.equals(frmC3));
301         frmC3.setCookie("0");
302
303         Assert.assertFalse(frmC.equals(flowC));
304         frmC.setDstMac("00:A0:C9:22:AB:11");
305         Assert.assertTrue(frmC.getDstMac().equals("00:A0:C9:22:AB:11"));
306         Assert.assertFalse(frmC.equals(frmC3));
307         frmC3.setDstMac("00:A0:C9:22:AB:11");
308
309         Assert.assertFalse(frmC.equals(flowC));
310         frmC.setSrcMac("00:A0:C9:14:C8:29");
311         Assert.assertTrue(frmC.getSrcMac().equals("00:A0:C9:14:C8:29"));
312         Assert.assertFalse(frmC.equals(frmC3));
313         frmC3.setSrcMac("00:A0:C9:14:C8:29");
314
315         Assert.assertFalse(frmC.equals(flowC));
316         frmC.setDynamic(true);
317         Assert.assertTrue(frmC.isDynamic());
318         Assert.assertFalse(frmC.equals(frmC3));
319         frmC3.setDynamic(true);
320         flowC.setDynamic(true);
321
322         Assert.assertFalse(frmC.equals(flowC));
323         frmC.setEtherType("0x0800");
324         Assert.assertTrue(frmC.getEtherType().equals("0x0800"));
325         Assert.assertFalse(frmC.equals(frmC3));
326         frmC3.setEtherType("0x0800");
327
328         Assert.assertFalse(frmC.equals(flowC));
329         frmC.setIngressPort("60");
330         Assert.assertTrue(frmC.getIngressPort().equals("60"));
331         Assert.assertFalse(frmC.equals(frmC3));
332         frmC3.setIngressPort("60");
333
334         Assert.assertFalse(frmC.equals(flowC));
335         frmC.setName("Config1");
336         Assert.assertTrue(frmC.getName().equals("Config1"));
337         Assert.assertFalse(frmC.equals(frmC3));
338         frmC3.setName("Config1");
339
340         Assert.assertFalse(frmC.equals(flowC));
341         frmC.setDstIp("2.2.2.2");
342         Assert.assertTrue(frmC.getDstIp().equals("2.2.2.2"));
343         Assert.assertFalse(frmC.equals(frmC3));
344         frmC3.setDstIp("2.2.2.2");
345
346         Assert.assertFalse(frmC.equals(flowC));
347         frmC.setSrcIp("1.2.3.4");
348         Assert.assertTrue(frmC.getSrcIp().equals("1.2.3.4"));
349         Assert.assertFalse(frmC.equals(frmC3));
350         frmC3.setSrcIp("1.2.3.4");
351
352         Assert.assertFalse(frmC.equals(flowC));
353         Assert.assertFalse(frmC.isPortGroupEnabled());
354         frmC.setPortGroup("2");
355         Assert.assertTrue(frmC.isPortGroupEnabled());
356         Assert.assertTrue(frmC.getPortGroup().equals("2"));
357         Assert.assertFalse(frmC.equals(frmC3));
358         frmC3.setPortGroup("2");
359
360         Assert.assertFalse(frmC.equals(flowC));
361         frmC.setPriority("100");
362         Assert.assertTrue(frmC.getPriority().equals("100"));
363         Assert.assertFalse(frmC.equals(frmC3));
364         frmC3.setPriority("100");
365
366         Assert.assertFalse(frmC.equals(flowC));
367         frmC.setProtocol(IPProtocols.TCP.toString());
368         Assert.assertTrue(frmC.getProtocol().equals(IPProtocols.TCP.toString()));
369         Assert.assertFalse(frmC.equals(frmC3));
370         frmC3.setProtocol(IPProtocols.TCP.toString());
371
372         Assert.assertFalse(frmC.equals(flowC));
373         frmC.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
374         Assert.assertTrue(frmC.getNode().equals(Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
375         Assert.assertFalse(frmC.equals(frmC3));
376         frmC3.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
377
378         Assert.assertFalse(frmC.equals(flowC));
379         frmC.setTosBits("0");
380         Assert.assertTrue(frmC.getTosBits().equals("0"));
381         Assert.assertFalse(frmC.equals(frmC3));
382         frmC3.setTosBits("0");
383
384         Assert.assertFalse(frmC.equals(flowC));
385         frmC.setDstPort("100");
386         Assert.assertTrue(frmC.getDstPort().equals("100"));
387         Assert.assertFalse(frmC.equals(frmC3));
388         frmC3.setDstPort("100");
389
390         Assert.assertFalse(frmC.equals(flowC));
391         frmC.setSrcPort("8080");
392         Assert.assertTrue(frmC.getSrcPort().equals("8080"));
393         Assert.assertFalse(frmC.equals(frmC3));
394         frmC3.setSrcPort("8080");
395
396         Assert.assertFalse(frmC.equals(flowC));
397         frmC.setVlanId("100");
398         Assert.assertTrue(frmC.getVlanId().equals("100"));
399         Assert.assertFalse(frmC.equals(frmC3));
400         frmC3.setVlanId("100");
401
402         Assert.assertFalse(frmC.equals(flowC));
403         frmC.setVlanPriority("0");
404         Assert.assertTrue(frmC.getVlanPriority().equals("0"));
405         Assert.assertFalse(frmC.equals(frmC3));
406         frmC3.setVlanPriority("0");
407
408         Assert.assertFalse(frmC.equals(flowC));
409         frmC.setIdleTimeout("300");
410         Assert.assertTrue(frmC.getIdleTimeout().equals("300"));
411         Assert.assertFalse(frmC.equals(frmC3));
412         frmC3.setIdleTimeout("300");
413
414         Assert.assertFalse(frmC.equals(flowC));
415         frmC.setHardTimeout("1000");
416         Assert.assertTrue(frmC.getHardTimeout().equals("1000"));
417         Assert.assertFalse(frmC.equals(frmC3));
418         frmC3.setHardTimeout("1000");
419
420         // Assert.assertFalse(frmC.equals(flowC));
421         Assert.assertTrue(actions.equals(frmC.getActions()));
422
423         FlowConfig frmC2 = new FlowConfig(frmC);
424
425         Assert.assertFalse(frmC2.equals(frmC));
426         frmC2.setDynamic(false);
427         Assert.assertFalse(frmC2.equals(frmC));
428         frmC2.setDynamic(true);
429         Assert.assertTrue(frmC2.equals(frmC));
430         // Assert.assertFalse(frmC2.equals(frmC3));
431         flowC.setDynamic(true);
432         Assert.assertTrue(flowC.equals(frmC));
433         Assert.assertTrue(flowC.isStatusSuccessful());
434         flowC.setStatus("Invalid");
435         Assert.assertFalse(flowC.isStatusSuccessful());
436
437         flowC.getActions().add(ActionType.DROP.toString());
438         Assert.assertFalse(flowC.equals(frmC));
439         Assert.assertFalse(flowC.isIPv6());
440         flowC.setDstIp("2001:420:281:1004:407a:57f4:4d15:c355");
441         Assert.assertTrue(flowC.isIPv6());
442         flowC.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
443         Assert.assertTrue(flowC.isIPv6());
444
445         Long id = (Long) flowC.getNode().getID();
446         Assert.assertTrue(id.toString().equals("1"));
447
448     }
449
450     @Test
451     public void testFlowConfigEqualities() throws UnknownHostException {
452         FlowConfig fc = new FlowConfig();
453         FlowConfig fc2 = new FlowConfig();
454         fc.setName("flow1");
455         fc.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
456         Assert.assertFalse(fc.onNode(Node.fromString(Node.NodeIDType.OPENFLOW, "0")));
457         Assert.assertTrue(fc.onNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
458
459         Assert.assertTrue(fc.isByNameAndNodeIdEqual("flow1", Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
460         Assert.assertFalse(fc.isByNameAndNodeIdEqual("flow1", Node.fromString(Node.NodeIDType.OPENFLOW, "0")));
461         Assert.assertFalse(fc.isByNameAndNodeIdEqual("flow2", Node.fromString(Node.NodeIDType.OPENFLOW, "1")));
462
463         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
464         fc2.setName("flow1");
465         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
466         fc2.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "0"));
467         Assert.assertFalse(fc.isByNameAndNodeIdEqual(fc2));
468         fc2.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
469         Assert.assertTrue(fc.isByNameAndNodeIdEqual(fc2));
470     }
471
472     @Test
473     public void testStatusToggle() throws UnknownHostException {
474         FlowConfig fc = new FlowConfig();
475         fc.toggleInstallation();
476         Assert.assertTrue(fc.installInHw());
477         fc.toggleInstallation();
478         Assert.assertFalse(fc.installInHw());
479         fc.toggleInstallation();
480         Assert.assertTrue(fc.installInHw());
481
482     }
483
484     @Test
485     public void testGetFlowEntry() throws UnknownHostException {
486         FlowConfig fc2 = createSampleFlowConfig();
487         FlowEntry fe = fc2.getFlowEntry();
488         Assert.assertNotNull(fe);
489     }
490
491     @Test
492     public void testGetFlow() throws UnknownHostException {
493         FlowConfig fc = new FlowConfig();
494         fc.setActions(createSampleActionList());
495         Flow flow = fc.getFlow();
496         Assert.assertNotNull(flow);
497     }
498
499     @Test
500     public void testL2AddressValid() {
501         FlowConfig fc = new FlowConfig();
502         Assert.assertFalse(fc.isL2AddressValid(null));
503         Assert.assertFalse(fc.isL2AddressValid("11"));
504         Assert.assertFalse(fc.isL2AddressValid("00:A0:C9:14:C8:"));
505         Assert.assertFalse(fc.isL2AddressValid("000:A01:C9:14:C8:211"));
506
507         Assert.assertTrue(fc.isL2AddressValid("00:A0:C9:14:C8:29"));
508     }
509
510     @Test
511     public void testValid() throws UnknownHostException {
512         FlowConfig fc2 = createSampleFlowConfig();
513         Assert.assertTrue(fc2.validate(null).isSuccess());
514
515         FlowConfig fc = new FlowConfig();
516         Status status = fc.validate(null);
517         Assert.assertFalse(status.isSuccess());
518         Assert.assertTrue(status.getDescription().contains("Invalid name"));
519
520         fc.setName("Config");
521         status = fc.validate(null);
522         Assert.assertFalse(status.isSuccess());
523         Assert.assertTrue(status.getDescription().contains("Node is null"));
524
525         fc.setNode(Node.fromString(Node.NodeIDType.OPENFLOW, "1"));
526         Assert.assertTrue(fc.validate(null).isSuccess());
527
528         fc.setPriority("-1");
529         status = fc.validate(null);
530         Assert.assertFalse(status.isSuccess());
531         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 65535"));
532
533         fc.setPriority("100000");
534         status = fc.validate(null);
535         Assert.assertFalse(status.isSuccess());
536         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 65535"));
537
538         fc.setPriority("2000");
539         Assert.assertTrue(fc.validate(null).isSuccess());
540
541         fc.setCookie("100");
542         Assert.assertTrue(fc.validate(null).isSuccess());
543
544         fc.setIngressPort("-1");
545         status = fc.validate(null);
546         Assert.assertFalse(status.isSuccess());
547         Assert.assertTrue(status.getDescription().contains("is not valid for the Switch"));
548
549         fc.setIngressPort("100");
550         Assert.assertTrue(fc.validate(null).isSuccess());
551
552         fc.setVlanId(("-1"));
553         status = fc.validate(null);
554         Assert.assertFalse(status.isSuccess());
555         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 4095"));
556
557         fc.setVlanId("5000");
558         status = fc.validate(null);
559         Assert.assertFalse(status.isSuccess());
560         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 4095"));
561
562         fc.setVlanId("100");
563         Assert.assertTrue(fc.validate(null).isSuccess());
564
565         fc.setVlanPriority("-1");
566         status = fc.validate(null);
567         Assert.assertFalse(status.isSuccess());
568         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 7"));
569
570         fc.setVlanPriority("9");
571         status = fc.validate(null);
572         Assert.assertFalse(status.isSuccess());
573         Assert.assertTrue(status.getDescription().contains("is not in the range 0 - 7"));
574
575         fc.setVlanPriority("5");
576         Assert.assertTrue(fc.validate(null).isSuccess());
577
578         fc.setEtherType("-1");
579         status = fc.validate(null);
580         Assert.assertFalse(status.isSuccess());
581         Assert.assertTrue(status.getDescription().contains("Ethernet type"));
582
583         fc.setEtherType("0xfffff");
584         status = fc.validate(null);
585         Assert.assertFalse(status.isSuccess());
586         Assert.assertTrue(status.getDescription().contains("Ethernet type"));
587
588         fc.setEtherType("0x800");
589         Assert.assertTrue(fc.validate(null).isSuccess());
590
591         fc.setTosBits("-1");
592         status = fc.validate(null);
593         Assert.assertFalse(status.isSuccess());
594         Assert.assertTrue(status.getDescription().contains("IP ToS bits"));
595
596         fc.setTosBits("65");
597         status = fc.validate(null);
598         Assert.assertFalse(status.isSuccess());
599         Assert.assertTrue(status.getDescription().contains("IP ToS bits"));
600
601         fc.setTosBits("60");
602         Assert.assertTrue(fc.validate(null).isSuccess());
603
604         fc.setSrcPort("-1");
605         status = fc.validate(null);
606         Assert.assertFalse(status.isSuccess());
607         Assert.assertTrue(status.getDescription().contains("Transport source port"));
608
609         fc.setSrcPort("0xfffff");
610         status = fc.validate(null);
611         Assert.assertFalse(status.isSuccess());
612         Assert.assertTrue(status.getDescription().contains("Transport source port"));
613
614         fc.setSrcPort("0");
615         Assert.assertTrue(fc.validate(null).isSuccess());
616
617         fc.setSrcPort("0x00ff");
618         Assert.assertTrue(fc.validate(null).isSuccess());
619
620         fc.setSrcPort("0xffff");
621         Assert.assertTrue(fc.validate(null).isSuccess());
622
623         fc.setDstPort("-1");
624         status = fc.validate(null);
625         Assert.assertFalse(status.isSuccess());
626         Assert.assertTrue(status.getDescription().contains("Transport destination port"));
627
628         fc.setDstPort("0xfffff");
629         status = fc.validate(null);
630         Assert.assertFalse(status.isSuccess());
631         Assert.assertTrue(status.getDescription().contains("Transport destination port"));
632
633         fc.setDstPort("0");
634         Assert.assertTrue(fc.validate(null).isSuccess());
635
636         fc.setDstPort("0x00ff");
637         Assert.assertTrue(fc.validate(null).isSuccess());
638
639         fc.setDstPort("0xffff");
640         Assert.assertTrue(fc.validate(null).isSuccess());
641
642         fc.setSrcMac("abc");
643         status = fc.validate(null);
644         Assert.assertFalse(status.isSuccess());
645         Assert.assertTrue(status.getDescription().contains("Ethernet source address"));
646
647         fc.setSrcMac("00:A0:C9:14:C8:29");
648         Assert.assertTrue(fc.validate(null).isSuccess());
649
650         fc.setDstMac("abc");
651         status = fc.validate(null);
652         Assert.assertFalse(status.isSuccess());
653         Assert.assertTrue(status.getDescription().contains("Ethernet destination address"));
654
655         fc.setDstMac("00:A0:C9:22:AB:11");
656         Assert.assertTrue(fc.validate(null).isSuccess());
657
658         fc.setSrcIp("-1");
659         status = fc.validate(null);
660         Assert.assertFalse(status.isSuccess());
661         Assert.assertTrue(status.getDescription().contains("IP source address"));
662
663         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
664         status = fc.validate(null);
665         Assert.assertFalse(status.isSuccess());
666         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Src IP"));
667
668         fc.setEtherType("0x86dd");
669         Assert.assertTrue(fc.validate(null).isSuccess());
670
671         fc.setSrcIp("1.1.1.1");
672         status = fc.validate(null);
673         Assert.assertFalse(status.isSuccess());
674         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Src IP"));
675
676         fc.setEtherType("0x800");
677         Assert.assertTrue(fc.validate(null).isSuccess());
678
679         fc.setDstIp("-1");
680         status = fc.validate(null);
681         Assert.assertFalse(status.isSuccess());
682         Assert.assertTrue(status.getDescription().contains("IP destination address"));
683
684         fc.setDstIp("2001:420:281:1004:407a:57f4:4d15:c355");
685         status = fc.validate(null);
686         Assert.assertFalse(status.isSuccess());
687         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Dst IP"));
688
689         fc.setEtherType("0x86dd");
690         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
691         Assert.assertTrue(fc.validate(null).isSuccess());
692
693         fc.setDstIp("2.2.2.2");
694         status = fc.validate(null);
695         Assert.assertFalse(status.isSuccess());
696         Assert.assertTrue(status.getDescription().contains("Type mismatch between Ethernet & Dst IP"));
697
698         fc.setEtherType("0x800");
699         fc.setSrcIp("1.1.1.1");
700         Assert.assertTrue(fc.validate(null).isSuccess());
701
702         fc.setEtherType(null);
703         fc.setSrcIp("2001:420:281:1004:407a:57f4:4d15:c355");
704         status = fc.validate(null);
705         Assert.assertFalse(status.isSuccess());
706         Assert.assertTrue(status.getDescription().contains("IP Src Dest Type mismatch"));
707
708         fc.setSrcIp("1.1.1.1");
709         fc.setIdleTimeout("-1");
710         status = fc.validate(null);
711         Assert.assertFalse(status.isSuccess());
712         Assert.assertTrue(status.getDescription().contains("Idle Timeout value"));
713
714         fc.setIdleTimeout("0xfffff");
715         status = fc.validate(null);
716         Assert.assertFalse(status.isSuccess());
717         Assert.assertTrue(status.getDescription().contains("Idle Timeout value"));
718
719         fc.setIdleTimeout("10");
720         Assert.assertTrue(fc.validate(null).isSuccess());
721
722         fc.setHardTimeout("-1");
723         status = fc.validate(null);
724         Assert.assertFalse(status.isSuccess());
725         Assert.assertTrue(status.getDescription().contains("Hard Timeout value"));
726
727         fc.setHardTimeout("0xfffff");
728         status = fc.validate(null);
729         Assert.assertFalse(status.isSuccess());
730         Assert.assertTrue(status.getDescription().contains("Hard Timeout value"));
731
732         fc.setHardTimeout("10");
733         Assert.assertTrue(fc.validate(null).isSuccess());
734
735     }
736
737     private FlowConfig createSampleFlowConfig() throws UnknownHostException {
738         ArrayList<String> actions;
739         actions = createSampleActionList();
740         // actions.add(ActionType.CONTROLLER.toString());
741         FlowConfig flowConfig = new FlowConfig("true", "Config1", Node.fromString(Node.NodeIDType.OPENFLOW, "1"),
742                 "100", "0", "60", "2", "100", "0", "0x0800", "00:A0:C9:14:C8:29", "00:A0:C9:22:AB:11",
743                 IPProtocols.TCP.toString(), "0", "1.2.3.4", "2.2.2.2", "8080", "100", "300", "1000", actions);
744         return flowConfig;
745
746     }
747
748     private ArrayList<String> createSampleActionList() {
749         ArrayList<String> actions = new ArrayList<String>();
750         actions.add(ActionType.DROP.toString());
751         actions.add(ActionType.LOOPBACK.toString());
752         actions.add(ActionType.FLOOD.toString());
753         actions.add(ActionType.SW_PATH.toString());
754         actions.add(ActionType.HW_PATH.toString());
755         actions.add(ActionType.SET_VLAN_PCP.toString() + "=1");
756         actions.add(ActionType.SET_VLAN_ID.toString() + "=1");
757         actions.add(ActionType.POP_VLAN.toString());
758         actions.add(ActionType.SET_DL_SRC.toString() + "=00:A0:C1:AB:22:11");
759         actions.add(ActionType.SET_DL_DST.toString() + "=00:B1:C1:00:AA:BB");
760         actions.add(ActionType.SET_NW_SRC.toString() + "=1.1.1.1");
761         actions.add(ActionType.SET_NW_DST.toString() + "=2.2.2.2");
762         actions.add(ActionType.CONTROLLER.toString());
763         actions.add(ActionType.SET_NW_TOS.toString() + "1");
764         actions.add(ActionType.SET_TP_SRC.toString() + "60");
765         actions.add(ActionType.SET_TP_DST.toString() + "8080");
766         actions.add(ActionType.SET_NEXT_HOP.toString() + "=1.1.1.1");
767
768         return actions;
769     }
770
771     private Flow getSampleFlowV6(Node node) throws UnknownHostException {
772         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
773         NodeConnector oport = NodeConnectorCreator.createOFNodeConnector((short) 30, node);
774         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
775         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
776         byte newMac[] = { (byte) 0x11, (byte) 0xaa, (byte) 0xbb, (byte) 0x34, (byte) 0x9a, (byte) 0xee };
777         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
778         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
779         InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
780         InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
781         InetAddress newIP = InetAddress.getByName("2056:650::a1b0");
782         short ethertype = EtherTypes.IPv6.shortValue();
783         short vlan = (short) 27;
784         byte vlanPr = (byte) 3;
785         Byte tos = 4;
786         byte proto = IPProtocols.UDP.byteValue();
787         short src = (short) 5500;
788         short dst = 80;
789
790         /*
791          * Create a SAL Flow aFlow
792          */
793         Match match = new Match();
794         match.setField(MatchType.IN_PORT, port);
795         match.setField(MatchType.DL_SRC, srcMac);
796         match.setField(MatchType.DL_DST, dstMac);
797         match.setField(MatchType.DL_TYPE, ethertype);
798         match.setField(MatchType.DL_VLAN, vlan);
799         match.setField(MatchType.DL_VLAN_PR, vlanPr);
800         match.setField(MatchType.NW_SRC, srcIP, ipMask);
801         match.setField(MatchType.NW_DST, dstIP, ipMask2);
802         match.setField(MatchType.NW_TOS, tos);
803         match.setField(MatchType.NW_PROTO, proto);
804         match.setField(MatchType.TP_SRC, src);
805         match.setField(MatchType.TP_DST, dst);
806
807         List<Action> actions = new ArrayList<Action>();
808         actions.add(new Controller());
809         actions.add(new SetVlanId(5));
810         actions.add(new SetDlDst(newMac));
811         actions.add(new SetNwDst(newIP));
812         actions.add(new Output(oport));
813         actions.add(new PopVlan());
814         actions.add(new Flood());
815
816         Flow flow = new Flow(match, actions);
817         flow.setPriority((short) 300);
818         flow.setHardTimeout((short) 240);
819
820         return flow;
821     }
822 }