blob: 2f3cac42362995e3dfd3cc397f061b9f87807f72 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koide122e5702014-02-21 17:53:51 -08002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.easymock.EasyMock.createMock;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.replay;
6import static org.junit.Assert.assertEquals;
7import static org.junit.Assert.assertTrue;
Toshio Koide122e5702014-02-21 17:53:51 -08008
9import java.util.Collection;
10
Jonathan Hartaa380972014-04-03 10:24:46 -070011import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hart472062d2014-04-03 10:56:48 -070012import net.onrc.onos.core.topology.Link;
13import net.onrc.onos.core.topology.LinkEvent;
Jonathan Hart472062d2014-04-03 10:56:48 -070014import net.onrc.onos.core.topology.Port;
15import net.onrc.onos.core.topology.Switch;
Toshio Koide122e5702014-02-21 17:53:51 -080016
17import org.junit.After;
18import org.junit.Before;
19import org.junit.Test;
20
21public class PathIntentMapTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070022 Link link12, link23, link24;
23 Switch sw1, sw2, sw3, sw4;
24 Port port11, port22, port21, port23, port31, port41;
25 Path path1, path2;
26 PathIntent intent1, intent2;
Toshio Koide122e5702014-02-21 17:53:51 -080027
Ray Milkey269ffb92014-04-03 14:43:30 -070028 @Before
29 public void setUp() throws Exception {
30 sw1 = createMock(Switch.class);
31 sw2 = createMock(Switch.class);
32 sw3 = createMock(Switch.class);
33 sw4 = createMock(Switch.class);
34 expect(sw1.getDpid()).andReturn(1L).anyTimes();
35 expect(sw2.getDpid()).andReturn(2L).anyTimes();
36 expect(sw3.getDpid()).andReturn(3L).anyTimes();
37 expect(sw4.getDpid()).andReturn(4L).anyTimes();
38 replay(sw1);
39 replay(sw2);
40 replay(sw3);
41 replay(sw4);
Toshio Koide122e5702014-02-21 17:53:51 -080042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 port11 = createMock(Port.class);
44 port22 = createMock(Port.class);
45 port21 = createMock(Port.class);
46 port23 = createMock(Port.class);
47 port31 = createMock(Port.class);
48 port41 = createMock(Port.class);
49 expect(port11.getNumber()).andReturn(1L).anyTimes();
50 expect(port22.getNumber()).andReturn(2L).anyTimes();
51 expect(port21.getNumber()).andReturn(1L).anyTimes();
52 expect(port23.getNumber()).andReturn(3L).anyTimes();
53 expect(port31.getNumber()).andReturn(1L).anyTimes();
54 expect(port41.getNumber()).andReturn(1L).anyTimes();
55 replay(port11);
56 replay(port22);
57 replay(port21);
58 replay(port23);
59 replay(port31);
60 replay(port41);
Toshio Koide122e5702014-02-21 17:53:51 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 link12 = createMock(Link.class);
63 link23 = createMock(Link.class);
64 link24 = createMock(Link.class);
65 expect(link12.getCapacity()).andReturn(1000.0).anyTimes();
66 expect(link23.getCapacity()).andReturn(1000.0).anyTimes();
67 expect(link24.getCapacity()).andReturn(1000.0).anyTimes();
68 expect(link12.getSrcSwitch()).andReturn(sw1).anyTimes();
69 expect(link23.getSrcSwitch()).andReturn(sw2).anyTimes();
70 expect(link24.getSrcSwitch()).andReturn(sw2).anyTimes();
71 expect(link12.getSrcPort()).andReturn(port11).anyTimes();
72 expect(link23.getSrcPort()).andReturn(port21).anyTimes();
73 expect(link24.getSrcPort()).andReturn(port23).anyTimes();
74 expect(link12.getDstSwitch()).andReturn(sw2).anyTimes();
75 expect(link23.getDstSwitch()).andReturn(sw3).anyTimes();
76 expect(link24.getDstSwitch()).andReturn(sw4).anyTimes();
77 expect(link12.getDstPort()).andReturn(port22).anyTimes();
78 expect(link23.getDstPort()).andReturn(port31).anyTimes();
79 expect(link24.getDstPort()).andReturn(port41).anyTimes();
80 replay(link12);
81 replay(link23);
82 replay(link24);
Toshio Koide122e5702014-02-21 17:53:51 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 path1 = new Path();
85 path1.add(new LinkEvent(link12));
86 path1.add(new LinkEvent(link23));
Toshio Koide122e5702014-02-21 17:53:51 -080087
Ray Milkey269ffb92014-04-03 14:43:30 -070088 path2 = new Path();
89 path2.add(new LinkEvent(link12));
90 path2.add(new LinkEvent(link24));
Toshio Koide122e5702014-02-21 17:53:51 -080091
Ray Milkey269ffb92014-04-03 14:43:30 -070092 intent1 = new PathIntent("1", path1, 400.0, new Intent("_1"));
93 intent2 = new PathIntent("2", path2, 400.0, new Intent("_2"));
94 }
Toshio Koide122e5702014-02-21 17:53:51 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @After
97 public void tearDown() throws Exception {
98 }
Toshio Koide122e5702014-02-21 17:53:51 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 @Test
101 public void testCreate() {
102 PathIntentMap intents = new PathIntentMap();
103 assertEquals(0, intents.getAllIntents().size());
104 }
Toshio Koide122e5702014-02-21 17:53:51 -0800105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 @Test
107 public void testGetIntentsByDpid() {
108 IntentOperationList operations = new IntentOperationList();
109 operations.add(Operator.ADD, intent1);
110 operations.add(Operator.ADD, intent2);
111 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 PathIntentMap intents = new PathIntentMap();
114 intents.executeOperations(operations);
115 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 Collection<PathIntent> pathIntents = intents.getIntentsByDpid(1L);
118 assertEquals(2, pathIntents.size());
119 assertTrue(pathIntents.contains(intent1));
120 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800121
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 pathIntents = intents.getIntentsByDpid(2L);
123 assertEquals(2, pathIntents.size());
124 assertTrue(pathIntents.contains(intent1));
125 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800126
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 pathIntents = intents.getIntentsByDpid(3L);
128 assertEquals(1, pathIntents.size());
129 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 pathIntents = intents.getIntentsByDpid(4L);
132 assertEquals(1, pathIntents.size());
133 assertTrue(pathIntents.contains(intent2));
134 }
Toshio Koide122e5702014-02-21 17:53:51 -0800135
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 @Test
137 public void testGetPathIntentsByPort() {
138 IntentOperationList operations = new IntentOperationList();
139 operations.add(Operator.ADD, intent1);
140 operations.add(Operator.ADD, intent2);
141 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800142
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 PathIntentMap intents = new PathIntentMap();
144 intents.executeOperations(operations);
145 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800146
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 Collection<PathIntent> pathIntents = intents.getIntentsByPort(1L, 1L);
148 assertEquals(2, pathIntents.size());
149 assertTrue(pathIntents.contains(intent1));
150 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800151
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 pathIntents = intents.getIntentsByPort(2L, 1L);
153 assertEquals(1, pathIntents.size());
154 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800155
Ray Milkey269ffb92014-04-03 14:43:30 -0700156 pathIntents = intents.getIntentsByPort(2L, 3L);
157 assertEquals(1, pathIntents.size());
158 assertTrue(pathIntents.contains(intent2));
159 }
Toshio Koide122e5702014-02-21 17:53:51 -0800160
Ray Milkey269ffb92014-04-03 14:43:30 -0700161 @Test
162 public void testGetPathIntentsByLink() {
163 IntentOperationList operations = new IntentOperationList();
164 operations.add(Operator.ADD, intent1);
165 operations.add(Operator.ADD, intent2);
166 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800167
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 PathIntentMap intents = new PathIntentMap();
169 intents.executeOperations(operations);
170 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800171
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 Collection<PathIntent> pathIntents = intents.getIntentsByLink(new LinkEvent(link12));
173 assertEquals(2, pathIntents.size());
174 assertTrue(pathIntents.contains(intent1));
175 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 pathIntents = intents.getIntentsByLink(new LinkEvent(link23));
178 assertEquals(1, pathIntents.size());
179 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800180
Ray Milkey269ffb92014-04-03 14:43:30 -0700181 pathIntents = intents.getIntentsByLink(new LinkEvent(link24));
182 assertEquals(1, pathIntents.size());
183 assertTrue(pathIntents.contains(intent2));
184 }
Toshio Koide122e5702014-02-21 17:53:51 -0800185
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 @Test
187 public void testGetAvailableBandwidth() {
188 IntentOperationList operations = new IntentOperationList();
189 operations.add(Operator.ADD, intent1);
190 operations.add(Operator.ADD, intent2);
191 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800192
Ray Milkey269ffb92014-04-03 14:43:30 -0700193 PathIntentMap intents = new PathIntentMap();
194 intents.executeOperations(operations);
195 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800196
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 assertEquals(200.0, intents.getAvailableBandwidth(link12), 0.0);
198 }
Toshio Koide122e5702014-02-21 17:53:51 -0800199}