blob: 7bd75d220be884f9fe29a42b87c9cd10e661020d [file] [log] [blame]
Teru68076a42013-06-27 14:57:49 -07001package net.onrc.onos.ofcontroller.core;
2
Jonathan Hart56d15772013-11-03 19:09:59 -08003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertTrue;
Teru68076a42013-06-27 14:57:49 -07005
6import java.util.HashMap;
7
8import net.onrc.onos.graph.GraphDBConnection;
9import net.onrc.onos.graph.GraphDBOperation;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart56d15772013-11-03 19:09:59 -080011import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
Teru68076a42013-06-27 14:57:49 -070012import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
14import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
15import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
Jonathan Hart56d15772013-11-03 19:09:59 -080016
Teru68076a42013-06-27 14:57:49 -070017import org.easymock.EasyMock;
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.powermock.api.easymock.PowerMock;
23import org.powermock.core.classloader.annotations.PrepareForTest;
Teru68076a42013-06-27 14:57:49 -070024import org.powermock.modules.junit4.PowerMockRunner;
Jonathan Hart56d15772013-11-03 19:09:59 -080025import org.slf4j.LoggerFactory;
Teru68076a42013-06-27 14:57:49 -070026
Jonathan Hart56d15772013-11-03 19:09:59 -080027import com.google.common.net.InetAddresses;
Teru68076a42013-06-27 14:57:49 -070028import com.thinkaurelius.titan.core.TitanFactory;
29import com.thinkaurelius.titan.core.TitanGraph;
30
31//Add Powermock preparation
32@RunWith(PowerMockRunner.class)
33@PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, SwitchStorageImpl.class})
34public 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 Hart56d15772013-11-03 19:09:59 -080096 int ipaddr = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.0.1"));
Teru68076a42013-06-27 14:57:49 -070097 IDeviceObject devObj = ope.newDevice();
Jonathan Hart56d15772013-11-03 19:09:59 -080098 IIpv4Address ipv4Address = ope.newIpv4Address();
99 ipv4Address.setIpv4Address(ipaddr);
100 devObj.addIpv4Address(ipv4Address);
101 assertEquals(devObj.getIpv4Address(ipaddr), ipv4Address);
Teru68076a42013-06-27 14:57:49 -0700102 }
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
Teru68076a42013-06-27 14:57:49 -0700120 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;
Teru68076a42013-06-27 14:57:49 -0700149 IPortObject portObj = ope.newPort(dpid, number);
Teru68076a42013-06-27 14:57:49 -0700150
Teru68076a42013-06-27 14:57:49 -0700151 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}