blob: e915614b518bcf62d54f147a316cfdeba7aa698a [file] [log] [blame]
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berdeb6031342013-02-19 18:51:51 -08002
Teru87cd2da2013-06-15 20:33:08 -07003import java.util.ArrayList;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07004import java.util.Arrays;
Jonathan Hart18ad9502013-12-15 18:28:00 -08005import java.util.Iterator;
Pankaj Berdeb6031342013-02-19 18:51:51 -08006import java.util.List;
Pankaj Berdeb6031342013-02-19 18:51:51 -08007
Pankaj Berdeb6031342013-02-19 18:51:51 -08008import net.floodlightcontroller.devicemanager.IDevice;
Pankaj Berdeb6031342013-02-19 18:51:51 -08009import net.floodlightcontroller.devicemanager.SwitchPort;
Teru87cd2da2013-06-15 20:33:08 -070010import net.floodlightcontroller.packet.IPv4;
yoshi2fd4c7e2013-11-22 15:47:55 -080011import net.onrc.onos.graph.DBOperation;
12import net.onrc.onos.graph.GraphDBManager;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070013import net.onrc.onos.ofcontroller.core.IDeviceStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070014import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080017import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
18import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Pankaj Berdeb6031342013-02-19 18:51:51 -080019
Pankaj Berdeb6031342013-02-19 18:51:51 -080020import org.openflow.util.HexString;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import com.google.common.collect.Lists;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080025import com.google.common.net.InetAddresses;
Pankaj Berdeb6031342013-02-19 18:51:51 -080026import com.thinkaurelius.titan.core.TitanException;
Teru87cd2da2013-06-15 20:33:08 -070027/**
28 * This is the class for storing the information of devices into CassandraDB
yoshi2fd4c7e2013-11-22 15:47:55 -080029 *
Teru87cd2da2013-06-15 20:33:08 -070030 * @author Pankaj
31 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080032public class DeviceStorageImpl implements IDeviceStorage {
Pankaj Berdeb6031342013-02-19 18:51:51 -080033
yoshi0fee3de2013-11-23 09:13:37 -080034 private DBOperation ope;
35 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
36 /**
37 * *
38 * Initialize function. Before you use this class, please call this method
39 *
40 * @param conf configuration file for Cassandra DB
41 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080042 @Override
yoshi0fee3de2013-11-23 09:13:37 -080043 public void init(final String dbStore, final String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070044 try {
yoshid38cd312013-12-02 19:54:44 -080045 ope = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
46 //ope = GraphDBManager.getDBOperation(dbStore, conf);
yoshi0fee3de2013-11-23 09:13:37 -080047 } catch (Exception e) {
48 log.error(e.getMessage());
Teru87cd2da2013-06-15 20:33:08 -070049 }
yoshi0fee3de2013-11-23 09:13:37 -080050 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080051
yoshi0fee3de2013-11-23 09:13:37 -080052 /**
53 * *
54 * Finalize/close function. After you use this class, please call this
55 * method. It will close the DB connection.
56 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080057 @Override
58 public void close() {
59 ope.close();
60 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080061
yoshi0fee3de2013-11-23 09:13:37 -080062 /**
63 * *
64 * Finalize/close function. After you use this class, please call this
65 * method. It will close the DB connection. This is for Java garbage
66 * collection.
67 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080068 @Override
69 public void finalize() {
70 close();
71 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080072
yoshi0fee3de2013-11-23 09:13:37 -080073 /**
74 * *
75 * This function is for adding the device into the DB.
76 *
77 * @param device The device you want to add into the DB.
78 * @return IDeviceObject which was added in the DB.
79 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080080 @Override
81 public IDeviceObject addDevice(IDevice device) {
82 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080083 for (int i = 0; i < 6; i++) {
84 try {
85 if (i > 0) {
86 log.debug("Retrying add device: i is {}", i);
87 }
88 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
89 log.debug("Adding device {}: found existing device", device.getMACAddressString());
90 } else {
91 obj = ope.newDevice();
92 log.debug("Adding device {}: creating new device", device.getMACAddressString());
93 }
94
Naoki Shiotae2f4da72013-12-09 16:34:17 -080095 if (obj == null) {
96 return null;
97 }
98
Jonathan Hartba9ced92013-11-24 16:52:13 -080099 changeDeviceAttachments(device, obj);
100
101 changeDeviceIpv4Addresses(device, obj);
102
103 obj.setMACAddress(device.getMACAddressString());
104 obj.setType("device");
105 obj.setState("ACTIVE");
106 ope.commit();
107
108 break;
109 //log.debug("Adding device {}",device.getMACAddressString());
110 } catch (TitanException e) {
111 ope.rollback();
112 log.error("Adding device {} failed", device.getMACAddressString(), e);
113 obj = null;
yoshi0fee3de2013-11-23 09:13:37 -0800114 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700115 }
116
117 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800118 }
yoshi0fee3de2013-11-23 09:13:37 -0800119 /**
120 * *
121 * This function is for updating the Device properties.
122 *
123 * @param device The device you want to add into the DB.
124 * @return IDeviceObject which was added in the DB.
125 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800126 @Override
127 public IDeviceObject updateDevice(IDevice device) {
128 return addDevice(device);
129 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800130
Teru87cd2da2013-06-15 20:33:08 -0700131 /***
132 * This function is for removing the Device from the DB.
133 * @param device The device you want to delete from the DB.
134 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800135 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800136 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800137 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800138
139 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
140 removeDevice(dev);
141 }
142 }
143
144 public void removeDevice(IDeviceObject deviceObject) {
145 String deviceMac = deviceObject.getMACAddress();
yoshi2fd4c7e2013-11-22 15:47:55 -0800146
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800147 removeDeviceImpl(deviceObject);
yoshi2fd4c7e2013-11-22 15:47:55 -0800148
Pankaj Berdeda809572013-02-22 15:31:20 -0800149 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800150 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800151 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700152 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700153 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800154 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800155 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800156 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800157
158 public void removeDeviceImpl(IDeviceObject deviceObject) {
159 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
160 ope.removeIpv4Address(ipv4AddressVertex);
161 }
162
163 ope.removeDevice(deviceObject);
164 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800165 /**
166 * *
167 * This function is for getting the Device from the DB by Mac address of the
168 * device.
169 *
170 * @param mac The device mac address you want to get from the DB.
171 * @return IDeviceObject you want to get.
172 */
173 @Override
174 public IDeviceObject getDeviceByMac(String mac) {
175 return ope.searchDevice(mac);
176 }
177
Teru87cd2da2013-06-15 20:33:08 -0700178 /***
179 * This function is for getting the Device from the DB by IP address of the device.
180 * @param ip The device ip address you want to get from the DB.
181 * @return IDeviceObject you want to get.
182 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800183 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700184 public IDeviceObject getDeviceByIP(int ipv4Address) {
185 try {
186 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
187 if (ipv4AddressVertex != null) {
188 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700189 }
190 return null;
191 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700192 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700193 log.error("DeviceStorage:getDeviceByIP:{} failed");
194 return null;
195 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800196 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800197
yoshi0fee3de2013-11-23 09:13:37 -0800198 /**
199 * *
200 * This function is for changing the Device attachment point.
201 *
202 * @param device The device you want change the attachment point
203 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 @Override
205 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800206 IDeviceObject obj = null;
yoshi0fee3de2013-11-23 09:13:37 -0800207 try {
208 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
209 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
210 changeDeviceAttachments(device, obj);
211 ope.commit();
212 } else {
213 log.debug("failed to search device...now adding {}", device.getMACAddressString());
214 addDevice(device);
215 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700216 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700217 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800218 log.error(":addDevice mac:{} failed", device.getMACAddressString());
yoshi0fee3de2013-11-23 09:13:37 -0800219 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800220 }
221
Teru87cd2da2013-06-15 20:33:08 -0700222 /***
223 * This function is for changing the Device attachment point.
224 * @param device The new device you want change the attachment point
225 * @param obj The old device IDeviceObject that is going to change the attachment point.
226 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800227 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
228 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
229 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Pankaj Berdeda809572013-02-22 15:31:20 -0800230
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700231 for (SwitchPort ap : attachmentPoints) {
232 //Check if there is the port
233 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
234 (short) ap.getPort());
235 log.debug("New Switch Port is {},{}",
236 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
Teru87cd2da2013-06-15 20:33:08 -0700237
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700238 if (port != null){
239 if (attachedPorts.contains(port)) {
240 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
241 //This port will be remained, so remove from the removed port lists.
242 attachedPorts.remove(port);
243 } else {
244 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
245 port.setDevice(obj);
246 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800247
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700248 log.debug("port number is {}", port.getNumber().toString());
249 log.debug("port desc is {}", port.getDesc());
250 }
251 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800252
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700253 for (IPortObject port: attachedPorts) {
254 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
255 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800256
257 if (!obj.getAttachedPorts().iterator().hasNext()) {
258 // XXX If there are no more ports attached to the device,
259 // delete it. Otherwise we have a situation where the
260 // device remains forever with an IP address attached.
261 // When we implement device probing we should get rid of this.
262 removeDevice(obj);
263 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700264 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800265 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800266
Pankaj Berdeb6031342013-02-19 18:51:51 -0800267 /***
268 * This function is for changing the Device IPv4 address.
269 * @param device The new device you want change the ipaddress
270 */
271 @Override
272 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700273 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
274 device.getIPv4Addresses());
Pankaj Berdeb6031342013-02-19 18:51:51 -0800275 IDeviceObject obj;
yoshi0fee3de2013-11-23 09:13:37 -0800276 try {
277 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
278 changeDeviceIpv4Addresses(device, obj);
279 ope.commit();
280 } else {
281 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
282 }
283 } catch (TitanException e) {
284 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700285 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800286 }
287 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800288
289 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800290 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800291 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800292 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
293 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800294 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800295 }
296
297 List<String> memIpv4Addresses = new ArrayList<String>();
298 for (int addr : device.getIPv4Addresses()) {
299 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
300 }
301
302 log.debug("Device IP addresses {}, database IP addresses {}",
303 memIpv4Addresses, dbIpv4Addresses);
304
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800305 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800306 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
307 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800308 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800309
Jonathan Hartba9ced92013-11-24 16:52:13 -0800310 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800311 IDeviceObject oldDevice = dbIpv4Address.getDevice();
312 if (oldDevice != null) {
313 oldDevice.removeIpv4Address(dbIpv4Address);
314 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800315 */
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800316
317 log.debug("Adding IP address {}",
318 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800319 deviceObject.addIpv4Address(dbIpv4Address);
320 }
321 }
322
323 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
324 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
325 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800326 log.debug("Removing IP address {}",
327 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
328 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800329 deviceObject.removeIpv4Address(dbIpv4Address);
330 }
331 }
332 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800333
334 /**
335 * Takes an {@link OnosDevice} and adds it into the database. There is no
336 * checking of the database data and removing old data - an
337 * {@link OnosDevice} basically corresponds to a packet we've just seen,
338 * and we need to add that information into the database.
339 */
340 @Override
341 public void addOnosDevice(OnosDevice onosDevice) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800342 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
343
pingping-lin00926032013-12-18 12:13:08 +0800344 //if the switch port we try to attach a new device already has a link, then stop adding device
345 IPortObject portObject1 = ope.searchPort(HexString.toHexString(
346 onosDevice.getSwitchDPID()), onosDevice.getSwitchPort());
347
348 if (portObject1.getLinkedPorts().iterator().hasNext()) {
349 log.debug("stop adding OnosDevice: {} due to there is a link to: {}",
350 onosDevice, portObject1.getLinkedPorts().iterator().next().getPortId());
351 return;
352 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800353
pingping-lin00926032013-12-18 12:13:08 +0800354 log.debug("addOnosDevice: {}", onosDevice);
355
Jonathan Hartd857ad62013-12-14 18:08:17 -0800356 try {
357 IDeviceObject device = ope.searchDevice(macAddress);
358
359 if (device == null) {
360 device = ope.newDevice();
361 device.setType("device");
362 device.setState("ACTIVE");
363 device.setMACAddress(macAddress);
364 }
365
366 // Check if the device has the IP address, add it if it doesn't
Jonathan Hartd857ad62013-12-14 18:08:17 -0800367 if (onosDevice.getIpv4Address() != null) {
368 boolean hasIpAddress = false;
369 for (IIpv4Address ipv4Address : device.getIpv4Addresses()) {
370 if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) {
371 hasIpAddress = true;
372 break;
373 }
374 }
375
376 if (!hasIpAddress) {
377 IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue());
378 IDeviceObject oldDevice = ipv4Address.getDevice();
379 if (oldDevice != null && oldDevice.getMACAddress() != macAddress) {
380 oldDevice.removeIpv4Address(ipv4Address);
381 }
382 device.addIpv4Address(ipv4Address);
383 }
384 }
385
386 // Check if the device has the attachment point, add it if not
Jonathan Hart18ad9502013-12-15 18:28:00 -0800387 // TODO single attachment point for now, extend to multiple later
Jonathan Hartd857ad62013-12-14 18:08:17 -0800388 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
389 boolean hasAttachmentPoint = false;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800390 Iterator<IPortObject> it = device.getAttachedPorts().iterator();
391 if (it.hasNext()) {
392 IPortObject existingPort = it.next();
393 if (existingPort != null) {
394 ISwitchObject existingSwitch = existingPort.getSwitch();
395 if (!existingSwitch.getDPID().equals(switchDpid) ||
396 existingPort.getNumber() != onosDevice.getSwitchPort()) {
397 existingPort.removeDevice(device);
398 }
399 else {
400 hasAttachmentPoint = true;
401 }
402 }
403 }
404
405 /*
Jonathan Hartd857ad62013-12-14 18:08:17 -0800406 for (IPortObject portObject : device.getAttachedPorts()) {
407 ISwitchObject switchObject = portObject.getSwitch();
408 if (switchObject.getDPID().equals(switchDpid)
409 && portObject.getNumber() == onosDevice.getSwitchPort()) {
410 hasAttachmentPoint = true;
411 break;
412 }
413 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800414 */
Jonathan Hartd857ad62013-12-14 18:08:17 -0800415
416 if (!hasAttachmentPoint) {
417 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800418 if (portObject != null) {
419 portObject.setDevice(device);
420 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800421 }
422
423 ope.commit();
424 }
425 catch (TitanException e) {
426 log.error("addOnosDevice {} failed:", macAddress, e);
427 ope.rollback();
428 }
429 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800430
431}