Bug 639, Bug 641, Bug 642: This is MD-SAL based sample implementation of a learning...
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / test / java / org / opendaylight / controller / sample / l2switch / md / topology / NetworkGraphDijkstraTest.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sample.l2switch.md.topology;
9
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
13 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Destination;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import static junit.framework.Assert.assertEquals;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 /**
25  */
26 public class NetworkGraphDijkstraTest {
27   Link link1, link2, link3, link4, link5, link6, link7, link8, link9, link10,link11,link12;
28   Destination dest1, dest2, dest3, dest4, dest5, dest6,dest7,dest8,dest9,dest10,dest11,dest12;
29   Source src1, src2, src3, src4, src5, src6,src7,src8,src9,src10,src11,src12;
30   NodeId nodeId1 = new NodeId("openflow:1");
31   NodeId nodeId2 = new NodeId("openflow:2");
32   NodeId nodeId3 = new NodeId("openflow:3");
33   NodeId nodeId4 = new NodeId("openflow:4");
34   NodeId nodeId5 = new NodeId("openflow:5");
35   NodeId nodeId6 = new NodeId("openflow:6");
36   NodeId nodeId7 = new NodeId("openflow:7");
37   List<Link> links = new ArrayList<>();
38
39   @Before
40   public void init() {
41     link1 = mock(Link.class);
42     link2 = mock(Link.class);
43     link3 = mock(Link.class);
44     link4 = mock(Link.class);
45     link5 = mock(Link.class);
46     link6 = mock(Link.class);
47     link7 = mock(Link.class);
48     link8 = mock(Link.class);
49     link9 = mock(Link.class);
50     link10 = mock(Link.class);
51     link11 = mock(Link.class);
52     link12 = mock(Link.class);
53     dest1 = mock(Destination.class);
54     dest2 = mock(Destination.class);
55     dest3 = mock(Destination.class);
56     dest4 = mock(Destination.class);
57     dest5 = mock(Destination.class);
58     dest6 = mock(Destination.class);
59     dest7 = mock(Destination.class);
60     dest8 = mock(Destination.class);
61     dest9 = mock(Destination.class);
62     dest10 = mock(Destination.class);
63     dest11 = mock(Destination.class);
64     dest12 = mock(Destination.class);
65     src1 = mock(Source.class);
66     src2 = mock(Source.class);
67     src3 = mock(Source.class);
68     src4 = mock(Source.class);
69     src5 = mock(Source.class);
70     src6 = mock(Source.class);
71     src7 = mock(Source.class);
72     src8 = mock(Source.class);
73     src9 = mock(Source.class);
74     src10 = mock(Source.class);
75     src11 = mock(Source.class);
76     src12 = mock(Source.class);
77     when(link1.getSource()).thenReturn(src1);
78     when(link2.getSource()).thenReturn(src2);
79     when(link3.getSource()).thenReturn(src3);
80     when(link4.getSource()).thenReturn(src4);
81     when(link5.getSource()).thenReturn(src5);
82     when(link6.getSource()).thenReturn(src6);
83     when(link7.getSource()).thenReturn(src7);
84     when(link8.getSource()).thenReturn(src8);
85     when(link9.getSource()).thenReturn(src9);
86     when(link10.getSource()).thenReturn(src10);
87     when(link11.getSource()).thenReturn(src11);
88     when(link12.getSource()).thenReturn(src12);
89     when(link1.getDestination()).thenReturn(dest1);
90     when(link2.getDestination()).thenReturn(dest2);
91     when(link3.getDestination()).thenReturn(dest3);
92     when(link4.getDestination()).thenReturn(dest4);
93     when(link5.getDestination()).thenReturn(dest5);
94     when(link6.getDestination()).thenReturn(dest6);
95     when(link7.getDestination()).thenReturn(dest7);
96     when(link8.getDestination()).thenReturn(dest8);
97     when(link9.getDestination()).thenReturn(dest9);
98     when(link10.getDestination()).thenReturn(dest10);
99     when(link11.getDestination()).thenReturn(dest11);
100     when(link12.getDestination()).thenReturn(dest12);
101     when(src1.getSourceNode()).thenReturn(nodeId1);
102     when(dest1.getDestNode()).thenReturn(nodeId2);
103     when(src2.getSourceNode()).thenReturn(nodeId2);
104     when(dest2.getDestNode()).thenReturn(nodeId1);
105     when(src3.getSourceNode()).thenReturn(nodeId1);
106     when(dest3.getDestNode()).thenReturn(nodeId3);
107     when(src4.getSourceNode()).thenReturn(nodeId3);
108     when(dest4.getDestNode()).thenReturn(nodeId1);
109     when(src5.getSourceNode()).thenReturn(nodeId2);
110     when(dest5.getDestNode()).thenReturn(nodeId4);
111     when(src6.getSourceNode()).thenReturn(nodeId4);
112     when(dest6.getDestNode()).thenReturn(nodeId2);
113     when(src7.getSourceNode()).thenReturn(nodeId2);
114     when(dest7.getDestNode()).thenReturn(nodeId5);
115     when(src8.getSourceNode()).thenReturn(nodeId5);
116     when(dest8.getDestNode()).thenReturn(nodeId2);
117     when(src9.getSourceNode()).thenReturn(nodeId6);
118     when(dest9.getDestNode()).thenReturn(nodeId3);
119     when(src10.getSourceNode()).thenReturn(nodeId3);
120     when(dest10.getDestNode()).thenReturn(nodeId6);
121     when(src11.getSourceNode()).thenReturn(nodeId7);
122     when(dest11.getDestNode()).thenReturn(nodeId3);
123     when(src12.getSourceNode()).thenReturn(nodeId3);
124     when(dest12.getDestNode()).thenReturn(nodeId7);
125     links.add(link1);
126     links.add(link2);
127     links.add(link3);
128     links.add(link4);
129     links.add(link5);
130     links.add(link6);
131     links.add(link7);
132     links.add(link8);
133     links.add(link9);
134     links.add(link10);
135     links.add(link11);
136     links.add(link12);
137
138   }
139
140   @Test
141   public void testAddLinksAndGetPath() throws Exception {
142     NetworkGraphService networkGraphService = new NetworkGraphDijkstra();
143     networkGraphService.addLinks(links);
144     List<Link> path = networkGraphService.getPath(nodeId2, nodeId3);
145     assertEquals("path size is not as expected.", 2, path.size());
146     assertEquals("link source is not as expected.", nodeId2, path.get(0).getSource().getSourceNode());
147     assertEquals("link destination is not as expected.", nodeId1, path.get(0).getDestination().getDestNode());
148     path = networkGraphService.getPath(nodeId3, nodeId2);
149     assertEquals("path size is not as expected.", 2, path.size());
150     assertEquals("link source is not as expected.", nodeId3, path.get(0).getSource().getSourceNode());
151     assertEquals("link destination is not as expected.", nodeId1, path.get(0).getDestination().getDestNode());
152
153     path = networkGraphService.getPath(nodeId4, nodeId6);
154     assertEquals("path size is not as expected.", 4, path.size());
155     assertEquals("link source is not as expected.", nodeId4, path.get(0).getSource().getSourceNode());
156     assertEquals("link destination is not as expected.", nodeId2, path.get(0).getDestination().getDestNode());
157   }
158 }