blob: 69b0fc2fe268aa07cf6d74915e47cf93958880a1 [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;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070016import net.onrc.onos.core.util.Dpid;
17import net.onrc.onos.core.util.PortNumber;
Toshio Koide122e5702014-02-21 17:53:51 -080018
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22
23public class PathIntentMapTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070024 Link link12, link23, link24;
25 Switch sw1, sw2, sw3, sw4;
26 Port port11, port22, port21, port23, port31, port41;
27 Path path1, path2;
28 PathIntent intent1, intent2;
Toshio Koide122e5702014-02-21 17:53:51 -080029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 @Before
31 public void setUp() throws Exception {
32 sw1 = createMock(Switch.class);
33 sw2 = createMock(Switch.class);
34 sw3 = createMock(Switch.class);
35 sw4 = createMock(Switch.class);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070036 expect(sw1.getDpid()).andReturn(new Dpid(1L)).anyTimes();
37 expect(sw2.getDpid()).andReturn(new Dpid(2L)).anyTimes();
38 expect(sw3.getDpid()).andReturn(new Dpid(3L)).anyTimes();
39 expect(sw4.getDpid()).andReturn(new Dpid(4L)).anyTimes();
Ray Milkey269ffb92014-04-03 14:43:30 -070040 replay(sw1);
41 replay(sw2);
42 replay(sw3);
43 replay(sw4);
Toshio Koide122e5702014-02-21 17:53:51 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 port11 = createMock(Port.class);
46 port22 = createMock(Port.class);
47 port21 = createMock(Port.class);
48 port23 = createMock(Port.class);
49 port31 = createMock(Port.class);
50 port41 = createMock(Port.class);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070051 expect(port11.getNumber()).andReturn(new PortNumber((short) 1)).anyTimes();
52 expect(port22.getNumber()).andReturn(new PortNumber((short) 2)).anyTimes();
53 expect(port21.getNumber()).andReturn(new PortNumber((short) 1)).anyTimes();
54 expect(port23.getNumber()).andReturn(new PortNumber((short) 3)).anyTimes();
55 expect(port31.getNumber()).andReturn(new PortNumber((short) 1)).anyTimes();
56 expect(port41.getNumber()).andReturn(new PortNumber((short) 1)).anyTimes();
Ray Milkey269ffb92014-04-03 14:43:30 -070057 replay(port11);
58 replay(port22);
59 replay(port21);
60 replay(port23);
61 replay(port31);
62 replay(port41);
Toshio Koide122e5702014-02-21 17:53:51 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 link12 = createMock(Link.class);
65 link23 = createMock(Link.class);
66 link24 = createMock(Link.class);
67 expect(link12.getCapacity()).andReturn(1000.0).anyTimes();
68 expect(link23.getCapacity()).andReturn(1000.0).anyTimes();
69 expect(link24.getCapacity()).andReturn(1000.0).anyTimes();
70 expect(link12.getSrcSwitch()).andReturn(sw1).anyTimes();
71 expect(link23.getSrcSwitch()).andReturn(sw2).anyTimes();
72 expect(link24.getSrcSwitch()).andReturn(sw2).anyTimes();
73 expect(link12.getSrcPort()).andReturn(port11).anyTimes();
74 expect(link23.getSrcPort()).andReturn(port21).anyTimes();
75 expect(link24.getSrcPort()).andReturn(port23).anyTimes();
76 expect(link12.getDstSwitch()).andReturn(sw2).anyTimes();
77 expect(link23.getDstSwitch()).andReturn(sw3).anyTimes();
78 expect(link24.getDstSwitch()).andReturn(sw4).anyTimes();
79 expect(link12.getDstPort()).andReturn(port22).anyTimes();
80 expect(link23.getDstPort()).andReturn(port31).anyTimes();
81 expect(link24.getDstPort()).andReturn(port41).anyTimes();
82 replay(link12);
83 replay(link23);
84 replay(link24);
Toshio Koide122e5702014-02-21 17:53:51 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 path1 = new Path();
87 path1.add(new LinkEvent(link12));
88 path1.add(new LinkEvent(link23));
Toshio Koide122e5702014-02-21 17:53:51 -080089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 path2 = new Path();
91 path2.add(new LinkEvent(link12));
92 path2.add(new LinkEvent(link24));
Toshio Koide122e5702014-02-21 17:53:51 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 intent1 = new PathIntent("1", path1, 400.0, new Intent("_1"));
95 intent2 = new PathIntent("2", path2, 400.0, new Intent("_2"));
96 }
Toshio Koide122e5702014-02-21 17:53:51 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 @After
99 public void tearDown() throws Exception {
100 }
Toshio Koide122e5702014-02-21 17:53:51 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 @Test
103 public void testCreate() {
104 PathIntentMap intents = new PathIntentMap();
105 assertEquals(0, intents.getAllIntents().size());
106 }
Toshio Koide122e5702014-02-21 17:53:51 -0800107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 @Test
109 public void testGetIntentsByDpid() {
110 IntentOperationList operations = new IntentOperationList();
111 operations.add(Operator.ADD, intent1);
112 operations.add(Operator.ADD, intent2);
113 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800114
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 PathIntentMap intents = new PathIntentMap();
116 intents.executeOperations(operations);
117 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800118
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 Collection<PathIntent> pathIntents = intents.getIntentsByDpid(1L);
120 assertEquals(2, pathIntents.size());
121 assertTrue(pathIntents.contains(intent1));
122 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800123
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 pathIntents = intents.getIntentsByDpid(2L);
125 assertEquals(2, pathIntents.size());
126 assertTrue(pathIntents.contains(intent1));
127 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800128
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 pathIntents = intents.getIntentsByDpid(3L);
130 assertEquals(1, pathIntents.size());
131 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800132
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 pathIntents = intents.getIntentsByDpid(4L);
134 assertEquals(1, pathIntents.size());
135 assertTrue(pathIntents.contains(intent2));
136 }
Toshio Koide122e5702014-02-21 17:53:51 -0800137
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 @Test
139 public void testGetPathIntentsByPort() {
140 IntentOperationList operations = new IntentOperationList();
141 operations.add(Operator.ADD, intent1);
142 operations.add(Operator.ADD, intent2);
143 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800144
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 PathIntentMap intents = new PathIntentMap();
146 intents.executeOperations(operations);
147 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800148
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 Collection<PathIntent> pathIntents = intents.getIntentsByPort(1L, 1L);
150 assertEquals(2, pathIntents.size());
151 assertTrue(pathIntents.contains(intent1));
152 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800153
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 pathIntents = intents.getIntentsByPort(2L, 1L);
155 assertEquals(1, pathIntents.size());
156 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800157
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 pathIntents = intents.getIntentsByPort(2L, 3L);
159 assertEquals(1, pathIntents.size());
160 assertTrue(pathIntents.contains(intent2));
161 }
Toshio Koide122e5702014-02-21 17:53:51 -0800162
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 @Test
164 public void testGetPathIntentsByLink() {
165 IntentOperationList operations = new IntentOperationList();
166 operations.add(Operator.ADD, intent1);
167 operations.add(Operator.ADD, intent2);
168 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800169
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 PathIntentMap intents = new PathIntentMap();
171 intents.executeOperations(operations);
172 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800173
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 Collection<PathIntent> pathIntents = intents.getIntentsByLink(new LinkEvent(link12));
175 assertEquals(2, pathIntents.size());
176 assertTrue(pathIntents.contains(intent1));
177 assertTrue(pathIntents.contains(intent2));
Toshio Koide122e5702014-02-21 17:53:51 -0800178
Ray Milkey269ffb92014-04-03 14:43:30 -0700179 pathIntents = intents.getIntentsByLink(new LinkEvent(link23));
180 assertEquals(1, pathIntents.size());
181 assertTrue(pathIntents.contains(intent1));
Toshio Koide122e5702014-02-21 17:53:51 -0800182
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 pathIntents = intents.getIntentsByLink(new LinkEvent(link24));
184 assertEquals(1, pathIntents.size());
185 assertTrue(pathIntents.contains(intent2));
186 }
Toshio Koide122e5702014-02-21 17:53:51 -0800187
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 @Test
189 public void testGetAvailableBandwidth() {
190 IntentOperationList operations = new IntentOperationList();
191 operations.add(Operator.ADD, intent1);
192 operations.add(Operator.ADD, intent2);
193 assertEquals(2, operations.size());
Toshio Koide122e5702014-02-21 17:53:51 -0800194
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 PathIntentMap intents = new PathIntentMap();
196 intents.executeOperations(operations);
197 assertEquals(2, intents.getAllIntents().size());
Toshio Koide122e5702014-02-21 17:53:51 -0800198
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 assertEquals(200.0, intents.getAvailableBandwidth(link12), 0.0);
200 }
Toshio Koide122e5702014-02-21 17:53:51 -0800201}