Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent; |
| 2 | |
| 3 | import static org.easymock.EasyMock.*; |
| 4 | import static org.junit.Assert.*; |
| 5 | |
| 6 | import java.util.Collection; |
| 7 | |
| 8 | import net.onrc.onos.intent.IntentOperation.Operator; |
| 9 | import net.onrc.onos.ofcontroller.networkgraph.Link; |
| 10 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
| 11 | import net.onrc.onos.ofcontroller.networkgraph.Path; |
| 12 | import net.onrc.onos.ofcontroller.networkgraph.Port; |
| 13 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 14 | |
| 15 | import org.junit.After; |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| 18 | |
| 19 | public class PathIntentMapTest { |
| 20 | Link link12, link23, link24; |
| 21 | Switch sw1, sw2, sw3, sw4; |
| 22 | Port port11, port22, port21, port23, port31, port41; |
| 23 | Path path1, path2; |
| 24 | PathIntent intent1, intent2; |
| 25 | |
| 26 | @Before |
| 27 | public void setUp() throws Exception { |
| 28 | sw1 = createMock(Switch.class); |
| 29 | sw2 = createMock(Switch.class); |
| 30 | sw3 = createMock(Switch.class); |
| 31 | sw4 = createMock(Switch.class); |
| 32 | expect(sw1.getDpid()).andReturn(1L).anyTimes(); |
| 33 | expect(sw2.getDpid()).andReturn(2L).anyTimes(); |
| 34 | expect(sw3.getDpid()).andReturn(3L).anyTimes(); |
| 35 | expect(sw4.getDpid()).andReturn(4L).anyTimes(); |
| 36 | replay(sw1); |
| 37 | replay(sw2); |
| 38 | replay(sw3); |
| 39 | replay(sw4); |
| 40 | |
| 41 | port11 = createMock(Port.class); |
| 42 | port22 = createMock(Port.class); |
| 43 | port21 = createMock(Port.class); |
| 44 | port23 = createMock(Port.class); |
| 45 | port31 = createMock(Port.class); |
| 46 | port41 = createMock(Port.class); |
| 47 | expect(port11.getNumber()).andReturn(1L).anyTimes(); |
| 48 | expect(port22.getNumber()).andReturn(2L).anyTimes(); |
| 49 | expect(port21.getNumber()).andReturn(1L).anyTimes(); |
| 50 | expect(port23.getNumber()).andReturn(3L).anyTimes(); |
| 51 | expect(port31.getNumber()).andReturn(1L).anyTimes(); |
| 52 | expect(port41.getNumber()).andReturn(1L).anyTimes(); |
| 53 | replay(port11); |
| 54 | replay(port22); |
| 55 | replay(port21); |
| 56 | replay(port23); |
| 57 | replay(port31); |
| 58 | replay(port41); |
| 59 | |
| 60 | link12 = createMock(Link.class); |
| 61 | link23 = createMock(Link.class); |
| 62 | link24 = createMock(Link.class); |
| 63 | expect(link12.getCapacity()).andReturn(1000.0).anyTimes(); |
| 64 | expect(link23.getCapacity()).andReturn(1000.0).anyTimes(); |
| 65 | expect(link24.getCapacity()).andReturn(1000.0).anyTimes(); |
| 66 | expect(link12.getSrcSwitch()).andReturn(sw1).anyTimes(); |
| 67 | expect(link23.getSrcSwitch()).andReturn(sw2).anyTimes(); |
| 68 | expect(link24.getSrcSwitch()).andReturn(sw2).anyTimes(); |
| 69 | expect(link12.getSrcPort()).andReturn(port11).anyTimes(); |
| 70 | expect(link23.getSrcPort()).andReturn(port21).anyTimes(); |
| 71 | expect(link24.getSrcPort()).andReturn(port23).anyTimes(); |
| 72 | expect(link12.getDstSwitch()).andReturn(sw2).anyTimes(); |
| 73 | expect(link23.getDstSwitch()).andReturn(sw3).anyTimes(); |
| 74 | expect(link24.getDstSwitch()).andReturn(sw4).anyTimes(); |
| 75 | expect(link12.getDstPort()).andReturn(port22).anyTimes(); |
| 76 | expect(link23.getDstPort()).andReturn(port31).anyTimes(); |
| 77 | expect(link24.getDstPort()).andReturn(port41).anyTimes(); |
| 78 | replay(link12); |
| 79 | replay(link23); |
| 80 | replay(link24); |
| 81 | |
| 82 | path1 = new Path(); |
| 83 | path1.add(new LinkEvent(link12)); |
| 84 | path1.add(new LinkEvent(link23)); |
| 85 | |
| 86 | path2 = new Path(); |
| 87 | path2.add(new LinkEvent(link12)); |
| 88 | path2.add(new LinkEvent(link24)); |
| 89 | |
| 90 | intent1 = new PathIntent("1", path1, 400.0, new Intent("_1")); |
| 91 | intent2 = new PathIntent("2", path2, 400.0, new Intent("_2")); |
| 92 | } |
| 93 | |
| 94 | @After |
| 95 | public void tearDown() throws Exception { |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testCreate() { |
| 100 | PathIntentMap intents = new PathIntentMap(); |
| 101 | assertEquals(0, intents.getAllIntents().size()); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testGetIntentsByDpid() { |
| 106 | IntentOperationList operations = new IntentOperationList(); |
| 107 | operations.add(Operator.ADD, intent1); |
| 108 | operations.add(Operator.ADD, intent2); |
| 109 | assertEquals(2, operations.size()); |
| 110 | |
| 111 | PathIntentMap intents = new PathIntentMap(); |
| 112 | intents.executeOperations(operations); |
| 113 | assertEquals(2, intents.getAllIntents().size()); |
| 114 | |
| 115 | Collection<PathIntent> pathIntents = intents.getIntentsByDpid(1L); |
| 116 | assertEquals(2, pathIntents.size()); |
| 117 | assertTrue(pathIntents.contains(intent1)); |
| 118 | assertTrue(pathIntents.contains(intent2)); |
| 119 | |
| 120 | pathIntents = intents.getIntentsByDpid(2L); |
| 121 | assertEquals(2, pathIntents.size()); |
| 122 | assertTrue(pathIntents.contains(intent1)); |
| 123 | assertTrue(pathIntents.contains(intent2)); |
| 124 | |
| 125 | pathIntents = intents.getIntentsByDpid(3L); |
| 126 | assertEquals(1, pathIntents.size()); |
| 127 | assertTrue(pathIntents.contains(intent1)); |
| 128 | |
| 129 | pathIntents = intents.getIntentsByDpid(4L); |
| 130 | assertEquals(1, pathIntents.size()); |
| 131 | assertTrue(pathIntents.contains(intent2)); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testGetPathIntentsByPort() { |
| 136 | IntentOperationList operations = new IntentOperationList(); |
| 137 | operations.add(Operator.ADD, intent1); |
| 138 | operations.add(Operator.ADD, intent2); |
| 139 | assertEquals(2, operations.size()); |
| 140 | |
| 141 | PathIntentMap intents = new PathIntentMap(); |
| 142 | intents.executeOperations(operations); |
| 143 | assertEquals(2, intents.getAllIntents().size()); |
| 144 | |
| 145 | Collection<PathIntent> pathIntents = intents.getIntentsByPort(1L, 1L); |
| 146 | assertEquals(2, pathIntents.size()); |
| 147 | assertTrue(pathIntents.contains(intent1)); |
| 148 | assertTrue(pathIntents.contains(intent2)); |
| 149 | |
| 150 | pathIntents = intents.getIntentsByPort(2L, 1L); |
| 151 | assertEquals(1, pathIntents.size()); |
| 152 | assertTrue(pathIntents.contains(intent1)); |
| 153 | |
| 154 | pathIntents = intents.getIntentsByPort(2L, 3L); |
| 155 | assertEquals(1, pathIntents.size()); |
| 156 | assertTrue(pathIntents.contains(intent2)); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testGetPathIntentsByLink() { |
| 161 | IntentOperationList operations = new IntentOperationList(); |
| 162 | operations.add(Operator.ADD, intent1); |
| 163 | operations.add(Operator.ADD, intent2); |
| 164 | assertEquals(2, operations.size()); |
| 165 | |
| 166 | PathIntentMap intents = new PathIntentMap(); |
| 167 | intents.executeOperations(operations); |
| 168 | assertEquals(2, intents.getAllIntents().size()); |
| 169 | |
| 170 | Collection<PathIntent> pathIntents = intents.getIntentsByLink(new LinkEvent(link12)); |
| 171 | assertEquals(2, pathIntents.size()); |
| 172 | assertTrue(pathIntents.contains(intent1)); |
| 173 | assertTrue(pathIntents.contains(intent2)); |
| 174 | |
| 175 | pathIntents = intents.getIntentsByLink(new LinkEvent(link23)); |
| 176 | assertEquals(1, pathIntents.size()); |
| 177 | assertTrue(pathIntents.contains(intent1)); |
| 178 | |
| 179 | pathIntents = intents.getIntentsByLink(new LinkEvent(link24)); |
| 180 | assertEquals(1, pathIntents.size()); |
| 181 | assertTrue(pathIntents.contains(intent2)); |
| 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testGetAvailableBandwidth() { |
| 186 | IntentOperationList operations = new IntentOperationList(); |
| 187 | operations.add(Operator.ADD, intent1); |
| 188 | operations.add(Operator.ADD, intent2); |
| 189 | assertEquals(2, operations.size()); |
| 190 | |
| 191 | PathIntentMap intents = new PathIntentMap(); |
| 192 | intents.executeOperations(operations); |
| 193 | assertEquals(2, intents.getAllIntents().size()); |
| 194 | |
| 195 | assertEquals(200.0, intents.getAvailableBandwidth(link12), 0.0); |
| 196 | } |
| 197 | } |