Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.util.HashMap; |
| 6 | |
| 7 | import net.onrc.onos.graph.GraphDBConnection; |
| 8 | import net.onrc.onos.graph.GraphDBOperation; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 13 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 14 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
| 15 | import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager; |
Yuta HIGUCHI | ad4a6cf | 2013-10-09 16:51:09 -0700 | [diff] [blame] | 16 | |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 17 | import org.easymock.EasyMock; |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.powermock.api.easymock.PowerMock; |
| 23 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | import org.powermock.modules.junit4.PowerMockRunner; |
| 26 | |
| 27 | import com.thinkaurelius.titan.core.TitanFactory; |
| 28 | import com.thinkaurelius.titan.core.TitanGraph; |
| 29 | |
| 30 | //Add Powermock preparation |
| 31 | @RunWith(PowerMockRunner.class) |
| 32 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 33 | public class INetMapTopologyObjectsISwitchObjectTest { |
| 34 | |
| 35 | //The test network is ./titan/schema/test-network.xml |
| 36 | |
| 37 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 38 | |
| 39 | String conf; |
| 40 | private GraphDBConnection conn = null; |
| 41 | private GraphDBOperation ope = null; |
| 42 | private TitanGraph titanGraph = null; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() throws Exception { |
| 46 | conf = "/dummy/path/to/db"; |
| 47 | |
| 48 | // Make mock cassandra DB |
| 49 | // Replace TitanFactory.open() to return mock DB |
| 50 | TestDatabaseManager.deleteTestDatabase(); |
| 51 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 52 | //TestDatabaseManager.populateTestData(titanGraph); |
| 53 | PowerMock.mockStatic(TitanFactory.class); |
| 54 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 55 | PowerMock.replay(TitanFactory.class); |
| 56 | |
| 57 | conn = GraphDBConnection.getInstance(conf); |
| 58 | ope = new GraphDBOperation(conn); |
| 59 | } |
| 60 | |
| 61 | @After |
| 62 | public void tearDown() throws Exception { |
| 63 | titanGraph.shutdown(); |
| 64 | TestDatabaseManager.deleteTestDatabase(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Desc: |
| 69 | * Test method for get and set state method. |
| 70 | * Condition: |
| 71 | * N/A |
| 72 | * Expect: |
| 73 | * 1. Should set the status of the switch. |
| 74 | * 2. Should get the status of the switch. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testSetGetState() { |
| 78 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 79 | String state = "ACTIVE"; |
| 80 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 81 | swObj.setState(state); |
| 82 | assertEquals(swObj.getState(), state); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Desc: |
| 87 | * Test method for get and set Type method. |
| 88 | * Condition: |
| 89 | * N/A |
| 90 | * Expect: |
| 91 | * 1. Should set the Type of the switch. |
| 92 | * 2. Should get the Type of the switch. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testSetGetType() { |
| 96 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 97 | String type = "Switch"; |
| 98 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 99 | swObj.setType("Switch"); |
| 100 | assertEquals(swObj.getType(), type); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Desc: |
| 105 | * Test method for getDPID method. |
| 106 | * Condition: |
| 107 | * N/A |
| 108 | * Expect: |
| 109 | * 1. Should get the dpid of the switch. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testGetDPID() { |
| 113 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 114 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 115 | |
| 116 | assertEquals(swObj.getDPID(), dpid); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Desc: |
| 121 | * Test method for setDPID method. |
| 122 | * Condition: |
| 123 | * N/A |
| 124 | * Expect: |
| 125 | * 1. Should set the dpid of the switch. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testSetDPID() { |
| 129 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 130 | String dpid2 = "00:00:00:00:00:00:0a:08"; |
| 131 | ISwitchObject obj = ope.newSwitch(dpid); |
| 132 | assertEquals(obj.getDPID(), dpid); |
| 133 | |
| 134 | obj.setDPID(dpid2); |
| 135 | assertEquals(obj.getDPID(), dpid2); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Desc: |
| 140 | * Test method for getPorts method. |
| 141 | * Condition: |
| 142 | * N/A |
| 143 | * Expect: |
| 144 | * 1. Should get all of ports taken by the switch. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testGetPorts() { |
| 148 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 149 | Short portNumber = 1; |
Yuta HIGUCHI | ad4a6cf | 2013-10-09 16:51:09 -0700 | [diff] [blame] | 150 | final int testSwitchPortNumber = 1; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 151 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 152 | IPortObject portObj = ope.newPort(dpid, portNumber); |
| 153 | |
| 154 | swObj.addPort(portObj); |
| 155 | int i = 0; |
Yuta HIGUCHI | ad4a6cf | 2013-10-09 16:51:09 -0700 | [diff] [blame] | 156 | for(@SuppressWarnings("unused") IPortObject port : swObj.getPorts()){ |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 157 | i++; |
| 158 | } |
Yuta HIGUCHI | ad4a6cf | 2013-10-09 16:51:09 -0700 | [diff] [blame] | 159 | assertEquals(testSwitchPortNumber, i); |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Desc: |
| 164 | * Test method for add and getPort method. |
| 165 | * Condition: |
| 166 | * N/A |
| 167 | * Expect: |
| 168 | * 1. Should add the port. |
| 169 | * 1. Should get the port. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testGetPort() { |
| 173 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 174 | Short portNumber = 1; |
| 175 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 176 | IPortObject portObj = ope.newPort(dpid, portNumber); |
| 177 | |
| 178 | swObj.addPort(portObj); |
| 179 | IPortObject portObj2 = swObj.getPort(portNumber); |
| 180 | assertEquals(portObj, portObj2); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Desc: |
| 185 | * Test method for add and removePort method. |
| 186 | * Condition: |
| 187 | * N/A |
| 188 | * Expect: |
| 189 | * 1. Should add a port to the switch. |
| 190 | * 1. Should remove a port from the switch. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testAddRemovePorts() { |
| 194 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 195 | Short portNum = 1; |
| 196 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 197 | IPortObject portObj = ope.newPort(dpid, portNum); |
| 198 | swObj.addPort(portObj); |
| 199 | |
| 200 | IPortObject portObj2 = swObj.getPort(portNum); |
| 201 | assertEquals(portObj2, portObj); |
| 202 | swObj.removePort(portObj); |
| 203 | assertNull(swObj.getPort(portNum)); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Desc: |
| 208 | * Test method for getDevices method. |
| 209 | * Condition: |
| 210 | * N/A |
| 211 | * Expect: |
| 212 | * 1. Should get all devices attached to the switch. |
| 213 | */ |
| 214 | @Test |
| 215 | public void testGetDevices() { |
| 216 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 217 | Short portNum = 1; |
| 218 | String devMac = "00:00:00:00:00:11"; |
| 219 | int numOfDev = 1; |
| 220 | |
| 221 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 222 | IPortObject portObj = ope.newPort(dpid, portNum); |
| 223 | IDeviceObject devObj = ope.newDevice(); |
| 224 | devObj.setMACAddress(devMac); |
| 225 | swObj.addPort(portObj); |
| 226 | portObj.setDevice(devObj); |
| 227 | |
| 228 | int i = 0; |
Yuta HIGUCHI | e2eb7fc | 2013-10-14 10:00:55 -0700 | [diff] [blame] | 229 | for(@SuppressWarnings("unused") IDeviceObject dev : swObj.getDevices()){ |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 230 | i++; |
| 231 | } |
| 232 | assertEquals(i, numOfDev); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Desc: |
| 237 | * Test method for getFlowEntries method. |
| 238 | * Condition: |
| 239 | * N/A |
| 240 | * Expect: |
| 241 | * 1. Should get all flowEntries attached to the switch. |
| 242 | */ |
| 243 | @Test |
| 244 | public void testGetFlowEntries() { |
| 245 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 246 | Short number = 1; |
| 247 | Short number2 = 2; |
| 248 | Short number3 = 3; |
| 249 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 250 | IPortObject portObj = ope.newPort(dpid, number); |
| 251 | IPortObject portObj2 = ope.newPort(dpid, number2); |
| 252 | IPortObject portObj3 = ope.newPort(dpid, number3); |
| 253 | |
| 254 | swObj.addPort(portObj); |
| 255 | swObj.addPort(portObj2); |
| 256 | swObj.addPort(portObj3); |
| 257 | |
| 258 | IFlowPath flowPathObj = ope.newFlowPath(); |
| 259 | |
| 260 | String flowEId = "1"; |
| 261 | IFlowEntry flowEntryObj = ope.newFlowEntry(); |
| 262 | flowEntryObj.setFlowEntryId(flowEId); |
| 263 | flowEntryObj.setInPort(portObj); |
| 264 | flowEntryObj.setOutPort(portObj2); |
| 265 | flowEntryObj.setSwitch(swObj); |
| 266 | flowEntryObj.setFlow(flowPathObj); |
| 267 | |
| 268 | String flowEId2 = "2"; |
| 269 | IFlowEntry flowEntryObj2 = ope.newFlowEntry(); |
| 270 | flowEntryObj2.setFlowEntryId(flowEId2); |
| 271 | flowEntryObj2.setInPort(portObj); |
| 272 | flowEntryObj2.setOutPort(portObj3); |
| 273 | flowEntryObj2.setSwitch(swObj); |
| 274 | flowEntryObj2.setFlow(flowPathObj); |
| 275 | |
| 276 | HashMap<String, IFlowEntry> flowEntryList = new HashMap<String, IFlowEntry>(); |
| 277 | for(IFlowEntry flowEnt : swObj.getFlowEntries()) |
| 278 | { |
| 279 | flowEntryList.put(flowEnt.getFlowEntryId(), flowEnt); |
| 280 | } |
| 281 | |
| 282 | assertTrue(flowEntryList.containsValue(flowEntryObj)); |
| 283 | assertTrue(flowEntryList.containsValue(flowEntryObj2)); |
| 284 | } |
| 285 | |
| 286 | } |