blob: ee08177af1de3b229531202418b05fef585dcdff [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
Jonathan Hart1a6f1d62013-11-14 11:33:46 -08003import 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;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07007
8import net.floodlightcontroller.devicemanager.IDevice;
9import net.floodlightcontroller.devicemanager.SwitchPort;
10import net.onrc.onos.graph.GraphDBOperation;
11import net.onrc.onos.ofcontroller.core.IDeviceStorage;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
16import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070017
Pankaj Berdeb6031342013-02-19 18:51:51 -080018import org.openflow.util.HexString;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import com.google.common.collect.Lists;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080023import com.google.common.net.InetAddresses;
Pankaj Berdeb6031342013-02-19 18:51:51 -080024import com.thinkaurelius.titan.core.TitanException;
Pankaj Berdeb6031342013-02-19 18:51:51 -080025
Teru87cd2da2013-06-15 20:33:08 -070026/**
27 * This is the class for storing the information of devices into CassandraDB
28 * @author Pankaj
29 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080030public class DeviceStorageImpl implements IDeviceStorage {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080031 protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080032
Teru87cd2da2013-06-15 20:33:08 -070033 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080034
Teru87cd2da2013-06-15 20:33:08 -070035 /***
36 * Initialize function. Before you use this class, please call this method
37 * @param conf configuration file for Cassandra DB
38 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080039 @Override
40 public void init(String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070041 try {
Toshio Koidebfe9b922013-06-18 10:56:05 -070042 ope = new GraphDBOperation(conf);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070043 } catch (TitanException e) {
44 log.error("Couldn't open graph operation", e);
Teru87cd2da2013-06-15 20:33:08 -070045 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080046 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080047
Teru87cd2da2013-06-15 20:33:08 -070048 /***
49 * Finalize/close function. After you use this class, please call this method.
50 * It will close the DB connection.
51 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080052 @Override
53 public void close() {
Toshio Koidebfe9b922013-06-18 10:56:05 -070054 ope.close();
Teru87cd2da2013-06-15 20:33:08 -070055 }
56
57 /***
58 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070059 * It will close the DB connection. This is for Java garbage collection.
Teru87cd2da2013-06-15 20:33:08 -070060 */
61 @Override
62 public void finalize() {
63 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080064 }
65
Teru87cd2da2013-06-15 20:33:08 -070066 /***
67 * This function is for adding the device into the DB.
68 * @param device The device you want to add into the DB.
69 * @return IDeviceObject which was added in the DB.
70 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080071 @Override
72 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080073 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080074 for (int i = 0; i < 6; i++) {
75 try {
76 if (i > 0) {
77 log.debug("Retrying add device: i is {}", i);
78 }
79 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
80 log.debug("Adding device {}: found existing device", device.getMACAddressString());
81 } else {
82 obj = ope.newDevice();
83 log.debug("Adding device {}: creating new device", device.getMACAddressString());
84 }
85
Naoki Shiotae2f4da72013-12-09 16:34:17 -080086 if (obj == null) {
87 return null;
88 }
89
Jonathan Hartba9ced92013-11-24 16:52:13 -080090 changeDeviceAttachments(device, obj);
91
92 changeDeviceIpv4Addresses(device, obj);
93
94 obj.setMACAddress(device.getMACAddressString());
95 obj.setType("device");
96 obj.setState("ACTIVE");
97 ope.commit();
98
99 break;
100 //log.debug("Adding device {}",device.getMACAddressString());
101 } catch (TitanException e) {
102 ope.rollback();
103 log.error("Adding device {} failed", device.getMACAddressString(), e);
104 obj = null;
105 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700106 }
107
108 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800109 }
110
Teru87cd2da2013-06-15 20:33:08 -0700111 /***
112 * This function is for updating the Device properties.
113 * @param device The device you want to add into the DB.
114 * @return IDeviceObject which was added in the DB.
115 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800116 @Override
117 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800118 return addDevice(device);
119 }
120
Teru87cd2da2013-06-15 20:33:08 -0700121 /***
122 * This function is for removing the Device from the DB.
123 * @param device The device you want to delete from the DB.
124 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800125 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800127 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800128
129 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
130 removeDevice(dev);
131 }
132 }
133
134 public void removeDevice(IDeviceObject deviceObject) {
135 String deviceMac = deviceObject.getMACAddress();
136
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800137 removeDeviceImpl(deviceObject);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800138
Pankaj Berdeda809572013-02-22 15:31:20 -0800139 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800140 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800141 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700142 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700143 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800144 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800145 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800146 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800147
148 public void removeDeviceImpl(IDeviceObject deviceObject) {
149 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
150 ope.removeIpv4Address(ipv4AddressVertex);
151 }
152
153 ope.removeDevice(deviceObject);
154 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800155
Teru87cd2da2013-06-15 20:33:08 -0700156 /***
157 * This function is for getting the Device from the DB by Mac address of the device.
158 * @param mac The device mac address you want to get from the DB.
159 * @return IDeviceObject you want to get.
160 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800161 @Override
162 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700163 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800164 }
165
Teru87cd2da2013-06-15 20:33:08 -0700166 /***
167 * This function is for getting the Device from the DB by IP address of the device.
168 * @param ip The device ip address you want to get from the DB.
169 * @return IDeviceObject you want to get.
170 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800171 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700172 public IDeviceObject getDeviceByIP(int ipv4Address) {
173 try {
174 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
175 if (ipv4AddressVertex != null) {
176 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700177 }
178 return null;
179 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700180 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700181 log.error("DeviceStorage:getDeviceByIP:{} failed");
182 return null;
183 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800184 }
185
Teru87cd2da2013-06-15 20:33:08 -0700186 /***
187 * This function is for changing the Device attachment point.
188 * @param device The device you want change the attachment point
189 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800190 @Override
191 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800192 IDeviceObject obj = null;
193 try {
Teru87cd2da2013-06-15 20:33:08 -0700194 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700195 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800196 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700197 ope.commit();
198 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700199 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800200 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700201 }
202 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700203 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 log.error(":addDevice mac:{} failed", device.getMACAddressString());
205 }
206 }
207
Teru87cd2da2013-06-15 20:33:08 -0700208 /***
209 * This function is for changing the Device attachment point.
210 * @param device The new device you want change the attachment point
211 * @param obj The old device IDeviceObject that is going to change the attachment point.
212 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800213 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
214 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
215 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700216
217 for (SwitchPort ap : attachmentPoints) {
218 //Check if there is the port
219 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
220 (short) ap.getPort());
221 log.debug("New Switch Port is {},{}",
222 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
223
224 if (port != null){
225 if (attachedPorts.contains(port)) {
226 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
227 //This port will be remained, so remove from the removed port lists.
228 attachedPorts.remove(port);
229 } else {
230 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
231 port.setDevice(obj);
232 }
233
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800234 log.debug("port number is {}", port.getNumber());
235 log.debug("port desc is {}", port.getDesc());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700236 }
237 }
238
239 for (IPortObject port: attachedPorts) {
240 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
241 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800242
243 if (!obj.getAttachedPorts().iterator().hasNext()) {
244 // XXX If there are no more ports attached to the device,
245 // delete it. Otherwise we have a situation where the
246 // device remains forever with an IP address attached.
247 // When we implement device probing we should get rid of this.
248 removeDevice(obj);
249 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700250 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800251 }
252
Teru87cd2da2013-06-15 20:33:08 -0700253 /***
254 * This function is for changing the Device IPv4 address.
255 * @param device The new device you want change the ipaddress
256 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800257 @Override
258 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700259 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
260 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800261 IDeviceObject obj;
262 try {
Teru87cd2da2013-06-15 20:33:08 -0700263 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800264 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700265
266 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800267 } else {
268 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700269 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800270 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700271 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700272 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800273 }
274 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800275
276 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800277 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800278 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800279 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
280 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800281 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800282 }
283
284 List<String> memIpv4Addresses = new ArrayList<String>();
285 for (int addr : device.getIPv4Addresses()) {
286 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
287 }
288
289 log.debug("Device IP addresses {}, database IP addresses {}",
290 memIpv4Addresses, dbIpv4Addresses);
291
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800292 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800293 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
294 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800295 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800296
Jonathan Hartba9ced92013-11-24 16:52:13 -0800297 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800298 IDeviceObject oldDevice = dbIpv4Address.getDevice();
299 if (oldDevice != null) {
300 oldDevice.removeIpv4Address(dbIpv4Address);
301 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800302 */
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800303
304 log.debug("Adding IP address {}",
305 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800306 deviceObject.addIpv4Address(dbIpv4Address);
307 }
308 }
309
310 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
311 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
312 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800313 log.debug("Removing IP address {}",
314 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
315 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800316 deviceObject.removeIpv4Address(dbIpv4Address);
317 }
318 }
319 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800320
321 /**
322 * Takes an {@link OnosDevice} and adds it into the database. There is no
323 * checking of the database data and removing old data - an
324 * {@link OnosDevice} basically corresponds to a packet we've just seen,
325 * and we need to add that information into the database.
326 */
327 @Override
328 public void addOnosDevice(OnosDevice onosDevice) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800329 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
330
pingping-lin00926032013-12-18 12:13:08 +0800331 //if the switch port we try to attach a new device already has a link, then stop adding device
332 IPortObject portObject1 = ope.searchPort(HexString.toHexString(
333 onosDevice.getSwitchDPID()), onosDevice.getSwitchPort());
334
Pavlin Radoslavov78073282013-12-19 17:01:23 -0800335 if ((portObject1 != null) && portObject1.getLinkedPorts().iterator().hasNext()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800336 if (log.isDebugEnabled()) {
337 log.debug("stop adding OnosDevice: {} due to there is a link to: {}",
338 onosDevice, portObject1.getLinkedPorts().iterator().next().getPortId());
339 }
pingping-lin00926032013-12-18 12:13:08 +0800340 return;
341 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800342
pingping-lin00926032013-12-18 12:13:08 +0800343 log.debug("addOnosDevice: {}", onosDevice);
344
Jonathan Hartd857ad62013-12-14 18:08:17 -0800345 try {
346 IDeviceObject device = ope.searchDevice(macAddress);
347
348 if (device == null) {
349 device = ope.newDevice();
350 device.setType("device");
351 device.setState("ACTIVE");
352 device.setMACAddress(macAddress);
353 }
354
355 // Check if the device has the IP address, add it if it doesn't
Jonathan Hartd857ad62013-12-14 18:08:17 -0800356 if (onosDevice.getIpv4Address() != null) {
357 boolean hasIpAddress = false;
358 for (IIpv4Address ipv4Address : device.getIpv4Addresses()) {
359 if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) {
360 hasIpAddress = true;
361 break;
362 }
363 }
364
365 if (!hasIpAddress) {
366 IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue());
367 IDeviceObject oldDevice = ipv4Address.getDevice();
368 if (oldDevice != null && oldDevice.getMACAddress() != macAddress) {
369 oldDevice.removeIpv4Address(ipv4Address);
370 }
371 device.addIpv4Address(ipv4Address);
372 }
373 }
374
375 // Check if the device has the attachment point, add it if not
Jonathan Hart18ad9502013-12-15 18:28:00 -0800376 // TODO single attachment point for now, extend to multiple later
Jonathan Hartd857ad62013-12-14 18:08:17 -0800377 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
378 boolean hasAttachmentPoint = false;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800379 Iterator<IPortObject> it = device.getAttachedPorts().iterator();
380 if (it.hasNext()) {
381 IPortObject existingPort = it.next();
382 if (existingPort != null) {
383 ISwitchObject existingSwitch = existingPort.getSwitch();
384 if (!existingSwitch.getDPID().equals(switchDpid) ||
385 existingPort.getNumber() != onosDevice.getSwitchPort()) {
386 existingPort.removeDevice(device);
387 }
388 else {
389 hasAttachmentPoint = true;
390 }
391 }
392 }
393
394 /*
Jonathan Hartd857ad62013-12-14 18:08:17 -0800395 for (IPortObject portObject : device.getAttachedPorts()) {
396 ISwitchObject switchObject = portObject.getSwitch();
397 if (switchObject.getDPID().equals(switchDpid)
398 && portObject.getNumber() == onosDevice.getSwitchPort()) {
399 hasAttachmentPoint = true;
400 break;
401 }
402 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800403 */
Jonathan Hartd857ad62013-12-14 18:08:17 -0800404
405 if (!hasAttachmentPoint) {
406 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800407 if (portObject != null) {
408 portObject.setDevice(device);
409 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800410 }
411
412 ope.commit();
413 }
414 catch (TitanException e) {
415 log.error("addOnosDevice {} failed:", macAddress, e);
416 ope.rollback();
417 }
418 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800419
Pankaj Berdeb6031342013-02-19 18:51:51 -0800420}