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