Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core; |
| 2 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 5 | |
| 6 | import java.util.HashMap; |
| 7 | |
| 8 | import net.onrc.onos.graph.GraphDBConnection; |
| 9 | import net.onrc.onos.graph.GraphDBOperation; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 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; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [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; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 24 | import org.powermock.modules.junit4.PowerMockRunner; |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 25 | import org.slf4j.LoggerFactory; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 26 | |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 27 | import com.google.common.net.InetAddresses; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 28 | import com.thinkaurelius.titan.core.TitanFactory; |
| 29 | import com.thinkaurelius.titan.core.TitanGraph; |
| 30 | |
| 31 | //Add Powermock preparation |
| 32 | @RunWith(PowerMockRunner.class) |
| 33 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class}) |
| 34 | public class INetMapTopologyObjectsIDeviceObjectTest { |
| 35 | |
| 36 | //The test network is ./titan/schema/test-network.xml |
| 37 | |
| 38 | protected static org.slf4j.Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 39 | |
| 40 | String conf; |
| 41 | private GraphDBConnection conn = null; |
| 42 | private GraphDBOperation ope = null; |
| 43 | private TitanGraph titanGraph = null; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() throws Exception { |
| 47 | conf = "/dummy/path/to/db"; |
| 48 | |
| 49 | // Make mock cassandra DB |
| 50 | // Replace TitanFactory.open() to return mock DB |
| 51 | TestDatabaseManager.deleteTestDatabase(); |
| 52 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 53 | //TestDatabaseManager.populateTestData(titanGraph); |
| 54 | PowerMock.mockStatic(TitanFactory.class); |
| 55 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 56 | PowerMock.replay(TitanFactory.class); |
| 57 | |
| 58 | conn = GraphDBConnection.getInstance(conf); |
| 59 | ope = new GraphDBOperation(conn); |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | titanGraph.shutdown(); |
| 65 | TestDatabaseManager.deleteTestDatabase(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Desc: |
| 70 | * Test method for get and set MacAddress method. |
| 71 | * Condition: |
| 72 | * N/A |
| 73 | * Expect: |
| 74 | * 1. Should set the mac address. |
| 75 | * 2. Should get the mac address. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testSetGetMacAddress() { |
| 79 | String macaddr = "00:00:00:00:00:00:0a:07"; |
| 80 | IDeviceObject devObj = ope.newDevice(); |
| 81 | devObj.setMACAddress(macaddr); |
| 82 | assertEquals(devObj.getMACAddress(), macaddr); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Desc: |
| 87 | * Test method for get and set IPAddress method. |
| 88 | * Condition: |
| 89 | * N/A |
| 90 | * Expect: |
| 91 | * 1. Should set the ip address. |
| 92 | * 2. Should get the ip address. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testSetGetIPAddress() { |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 96 | int ipaddr = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.0.1")); |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 97 | IDeviceObject devObj = ope.newDevice(); |
Jonathan Hart | 56d1577 | 2013-11-03 19:09:59 -0800 | [diff] [blame] | 98 | IIpv4Address ipv4Address = ope.newIpv4Address(); |
| 99 | ipv4Address.setIpv4Address(ipaddr); |
| 100 | devObj.addIpv4Address(ipv4Address); |
| 101 | assertEquals(devObj.getIpv4Address(ipaddr), ipv4Address); |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Desc: |
| 106 | * Test method for get attached port. |
| 107 | * Condition: |
| 108 | * N/A |
| 109 | * Expect: |
| 110 | * 1. Should get the attached ports. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testGetAttachedPort() { |
| 114 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 115 | Short number = 1; |
| 116 | Short number2 = 2; |
| 117 | IPortObject portObj = ope.newPort(dpid, number); |
| 118 | IPortObject portObj2 = ope.newPort(dpid, number2); |
| 119 | |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 120 | IDeviceObject devObj = ope.newDevice(); |
| 121 | |
| 122 | portObj.setDevice(devObj); |
| 123 | portObj2.setDevice(devObj); |
| 124 | |
| 125 | HashMap<Short, IPortObject> portObjectList = new HashMap<Short, IPortObject>(); |
| 126 | for(IPortObject port : devObj.getAttachedPorts()) |
| 127 | { |
| 128 | portObjectList.put(port.getNumber(), port); |
| 129 | } |
| 130 | |
| 131 | assertTrue(portObjectList.containsValue(portObj)); |
| 132 | assertTrue(portObjectList.containsValue(portObj2)); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Desc: |
| 138 | * Test method for set and remove host port method. |
| 139 | * Condition: |
| 140 | * N/A |
| 141 | * Expect: |
| 142 | * 1. Should set host port from the device. |
| 143 | * 2. Should remove host port from the device. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testSetRemoveHostPort() { |
| 147 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 148 | Short number = 1; |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 149 | IPortObject portObj = ope.newPort(dpid, number); |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 150 | |
Teru | 68076a4 | 2013-06-27 14:57:49 -0700 | [diff] [blame] | 151 | IDeviceObject devObj = ope.newDevice(); |
| 152 | |
| 153 | devObj.setHostPort(portObj); |
| 154 | |
| 155 | HashMap<String, IDeviceObject> portObjectList = new HashMap<String, IDeviceObject>(); |
| 156 | for(IDeviceObject dev : portObj.getDevices()) |
| 157 | { |
| 158 | portObjectList.put(dev.getMACAddress(), dev); |
| 159 | } |
| 160 | assertTrue(portObjectList.containsValue(devObj)); |
| 161 | |
| 162 | devObj.removeHostPort(portObj); |
| 163 | |
| 164 | HashMap<String, IDeviceObject> portObjectList2 = new HashMap<String, IDeviceObject>(); |
| 165 | for(IDeviceObject dev : portObj.getDevices()) |
| 166 | { |
| 167 | portObjectList2.put(dev.getMACAddress(), dev); |
| 168 | } |
| 169 | assertTrue(!portObjectList2.containsValue(devObj)); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Desc: |
| 174 | * Test method for getSwitch method. |
| 175 | * Condition: |
| 176 | * N/A |
| 177 | * Expect: |
| 178 | * 1. Should get the switch connected to the device. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testGetSwitches() { |
| 182 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 183 | String dpid2 = "00:00:00:00:00:00:0a:08"; |
| 184 | Short number = 1; |
| 185 | Short number2 = 2; |
| 186 | ISwitchObject swObj = ope.newSwitch(dpid); |
| 187 | ISwitchObject swObj2 = ope.newSwitch(dpid2); |
| 188 | IPortObject portObj = ope.newPort(dpid, number); |
| 189 | IPortObject portObj2 = ope.newPort(dpid2, number2); |
| 190 | swObj.addPort(portObj); |
| 191 | swObj2.addPort(portObj2); |
| 192 | IDeviceObject devObj = ope.newDevice(); |
| 193 | portObj.setDevice(devObj); |
| 194 | portObj2.setDevice(devObj); |
| 195 | |
| 196 | HashMap<String, ISwitchObject> switchObjectList = new HashMap<String, ISwitchObject>(); |
| 197 | for(ISwitchObject sw : devObj.getSwitch()) |
| 198 | { |
| 199 | switchObjectList.put(sw.getDPID(), sw); |
| 200 | } |
| 201 | assertTrue(switchObjectList.containsValue(swObj)); |
| 202 | assertTrue(switchObjectList.containsValue(swObj2)); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | } |