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