blob: a71b7629540b8390f9f631b791149270602e337d [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;
Pankaj Berdeb6031342013-02-19 18:51:51 -08005import java.util.List;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07006
7import net.floodlightcontroller.devicemanager.IDevice;
8import net.floodlightcontroller.devicemanager.SwitchPort;
9import net.onrc.onos.graph.GraphDBOperation;
10import net.onrc.onos.ofcontroller.core.IDeviceStorage;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080014import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
15import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070016
Pankaj Berdeb6031342013-02-19 18:51:51 -080017import org.openflow.util.HexString;
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import com.google.common.collect.Lists;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080022import com.google.common.net.InetAddresses;
Pankaj Berdeb6031342013-02-19 18:51:51 -080023import com.thinkaurelius.titan.core.TitanException;
Pankaj Berdeb6031342013-02-19 18:51:51 -080024
Teru87cd2da2013-06-15 20:33:08 -070025/**
26 * This is the class for storing the information of devices into CassandraDB
27 * @author Pankaj
28 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080029public class DeviceStorageImpl implements IDeviceStorage {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080030 protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080031
Teru87cd2da2013-06-15 20:33:08 -070032 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080033
Teru87cd2da2013-06-15 20:33:08 -070034 /***
35 * Initialize function. Before you use this class, please call this method
36 * @param conf configuration file for Cassandra DB
37 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080038 @Override
39 public void init(String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070040 try {
Toshio Koidebfe9b922013-06-18 10:56:05 -070041 ope = new GraphDBOperation(conf);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070042 } catch (TitanException e) {
43 log.error("Couldn't open graph operation", e);
Teru87cd2da2013-06-15 20:33:08 -070044 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080045 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080046
Teru87cd2da2013-06-15 20:33:08 -070047 /***
48 * Finalize/close function. After you use this class, please call this method.
49 * It will close the DB connection.
50 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080051 @Override
52 public void close() {
Toshio Koidebfe9b922013-06-18 10:56:05 -070053 ope.close();
Teru87cd2da2013-06-15 20:33:08 -070054 }
55
56 /***
57 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070058 * It will close the DB connection. This is for Java garbage collection.
Teru87cd2da2013-06-15 20:33:08 -070059 */
60 @Override
61 public void finalize() {
62 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080063 }
64
Teru87cd2da2013-06-15 20:33:08 -070065 /***
66 * This function is for adding the device into the DB.
67 * @param device The device you want to add into the DB.
68 * @return IDeviceObject which was added in the DB.
69 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080070 @Override
71 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080072 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080073 for (int i = 0; i < 6; i++) {
74 try {
75 if (i > 0) {
76 log.debug("Retrying add device: i is {}", i);
77 }
78 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
79 log.debug("Adding device {}: found existing device", device.getMACAddressString());
80 } else {
81 obj = ope.newDevice();
82 log.debug("Adding device {}: creating new device", device.getMACAddressString());
83 }
84
Naoki Shiotae2f4da72013-12-09 16:34:17 -080085 if (obj == null) {
86 return null;
87 }
88
Jonathan Hartba9ced92013-11-24 16:52:13 -080089 changeDeviceAttachments(device, obj);
90
91 changeDeviceIpv4Addresses(device, obj);
92
93 obj.setMACAddress(device.getMACAddressString());
94 obj.setType("device");
95 obj.setState("ACTIVE");
96 ope.commit();
97
98 break;
99 //log.debug("Adding device {}",device.getMACAddressString());
100 } catch (TitanException e) {
101 ope.rollback();
102 log.error("Adding device {} failed", device.getMACAddressString(), e);
103 obj = null;
104 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700105 }
106
107 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800108 }
109
Teru87cd2da2013-06-15 20:33:08 -0700110 /***
111 * This function is for updating the Device properties.
112 * @param device The device you want to add into the DB.
113 * @return IDeviceObject which was added in the DB.
114 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800115 @Override
116 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800117 return addDevice(device);
118 }
119
Teru87cd2da2013-06-15 20:33:08 -0700120 /***
121 * This function is for removing the Device from the DB.
122 * @param device The device you want to delete from the DB.
123 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800124 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800125 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800127
128 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
129 removeDevice(dev);
130 }
131 }
132
133 public void removeDevice(IDeviceObject deviceObject) {
134 String deviceMac = deviceObject.getMACAddress();
135
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800136 removeDeviceImpl(deviceObject);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800137
Pankaj Berdeda809572013-02-22 15:31:20 -0800138 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800139 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800140 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700141 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700142 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800143 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800144 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800145 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800146
147 public void removeDeviceImpl(IDeviceObject deviceObject) {
148 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
149 ope.removeIpv4Address(ipv4AddressVertex);
150 }
151
152 ope.removeDevice(deviceObject);
153 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800154
Teru87cd2da2013-06-15 20:33:08 -0700155 /***
156 * This function is for getting the Device from the DB by Mac address of the device.
157 * @param mac The device mac address you want to get from the DB.
158 * @return IDeviceObject you want to get.
159 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800160 @Override
161 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700162 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800163 }
164
Teru87cd2da2013-06-15 20:33:08 -0700165 /***
166 * This function is for getting the Device from the DB by IP address of the device.
167 * @param ip The device ip address you want to get from the DB.
168 * @return IDeviceObject you want to get.
169 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800170 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700171 public IDeviceObject getDeviceByIP(int ipv4Address) {
172 try {
173 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
174 if (ipv4AddressVertex != null) {
175 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700176 }
177 return null;
178 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700179 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700180 log.error("DeviceStorage:getDeviceByIP:{} failed");
181 return null;
182 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800183 }
184
Teru87cd2da2013-06-15 20:33:08 -0700185 /***
186 * This function is for changing the Device attachment point.
187 * @param device The device you want change the attachment point
188 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800189 @Override
190 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800191 IDeviceObject obj = null;
192 try {
Teru87cd2da2013-06-15 20:33:08 -0700193 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700194 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800195 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700196 ope.commit();
197 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700198 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800199 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700200 }
201 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700202 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800203 log.error(":addDevice mac:{} failed", device.getMACAddressString());
204 }
205 }
206
Teru87cd2da2013-06-15 20:33:08 -0700207 /***
208 * This function is for changing the Device attachment point.
209 * @param device The new device you want change the attachment point
210 * @param obj The old device IDeviceObject that is going to change the attachment point.
211 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800212 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
213 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
214 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700215
216 for (SwitchPort ap : attachmentPoints) {
217 //Check if there is the port
218 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
219 (short) ap.getPort());
220 log.debug("New Switch Port is {},{}",
221 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
222
223 if (port != null){
224 if (attachedPorts.contains(port)) {
225 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
226 //This port will be remained, so remove from the removed port lists.
227 attachedPorts.remove(port);
228 } else {
229 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
230 port.setDevice(obj);
231 }
232
233 log.debug("port number is {}", port.getNumber().toString());
234 log.debug("port desc is {}", port.getDesc());
235 }
236 }
237
238 for (IPortObject port: attachedPorts) {
239 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
240 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800241
242 if (!obj.getAttachedPorts().iterator().hasNext()) {
243 // XXX If there are no more ports attached to the device,
244 // delete it. Otherwise we have a situation where the
245 // device remains forever with an IP address attached.
246 // When we implement device probing we should get rid of this.
247 removeDevice(obj);
248 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700249 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800250 }
251
Teru87cd2da2013-06-15 20:33:08 -0700252 /***
253 * This function is for changing the Device IPv4 address.
254 * @param device The new device you want change the ipaddress
255 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800256 @Override
257 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700258 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
259 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800260 IDeviceObject obj;
261 try {
Teru87cd2da2013-06-15 20:33:08 -0700262 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800263 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700264
265 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800266 } else {
267 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700268 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800269 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700270 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700271 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800272 }
273 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800274
275 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800276 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800277 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800278 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
279 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800280 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800281 }
282
283 List<String> memIpv4Addresses = new ArrayList<String>();
284 for (int addr : device.getIPv4Addresses()) {
285 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
286 }
287
288 log.debug("Device IP addresses {}, database IP addresses {}",
289 memIpv4Addresses, dbIpv4Addresses);
290
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800291 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800292 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
293 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800294 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800295
Jonathan Hartba9ced92013-11-24 16:52:13 -0800296 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800297 IDeviceObject oldDevice = dbIpv4Address.getDevice();
298 if (oldDevice != null) {
299 oldDevice.removeIpv4Address(dbIpv4Address);
300 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800301 */
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800302
303 log.debug("Adding IP address {}",
304 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800305 deviceObject.addIpv4Address(dbIpv4Address);
306 }
307 }
308
309 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
310 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
311 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800312 log.debug("Removing IP address {}",
313 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
314 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800315 deviceObject.removeIpv4Address(dbIpv4Address);
316 }
317 }
318 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800319
320 /**
321 * Takes an {@link OnosDevice} and adds it into the database. There is no
322 * checking of the database data and removing old data - an
323 * {@link OnosDevice} basically corresponds to a packet we've just seen,
324 * and we need to add that information into the database.
325 */
326 @Override
327 public void addOnosDevice(OnosDevice onosDevice) {
328
329 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
330
331 try {
332 IDeviceObject device = ope.searchDevice(macAddress);
333
334 if (device == null) {
335 device = ope.newDevice();
336 device.setType("device");
337 device.setState("ACTIVE");
338 device.setMACAddress(macAddress);
339 }
340
341 // Check if the device has the IP address, add it if it doesn't
342 // TODO multiple IP addresses
343 if (onosDevice.getIpv4Address() != null) {
344 boolean hasIpAddress = false;
345 for (IIpv4Address ipv4Address : device.getIpv4Addresses()) {
346 if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) {
347 hasIpAddress = true;
348 break;
349 }
350 }
351
352 if (!hasIpAddress) {
353 IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue());
354 IDeviceObject oldDevice = ipv4Address.getDevice();
355 if (oldDevice != null && oldDevice.getMACAddress() != macAddress) {
356 oldDevice.removeIpv4Address(ipv4Address);
357 }
358 device.addIpv4Address(ipv4Address);
359 }
360 }
361
362 // Check if the device has the attachment point, add it if not
363 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
364 boolean hasAttachmentPoint = false;
365 for (IPortObject portObject : device.getAttachedPorts()) {
366 ISwitchObject switchObject = portObject.getSwitch();
367 if (switchObject.getDPID().equals(switchDpid)
368 && portObject.getNumber() == onosDevice.getSwitchPort()) {
369 hasAttachmentPoint = true;
370 break;
371 }
372 }
373
374 if (!hasAttachmentPoint) {
375 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
376 portObject.setDevice(device);
377 }
378
379 ope.commit();
380 }
381 catch (TitanException e) {
382 log.error("addOnosDevice {} failed:", macAddress, e);
383 ope.rollback();
384 }
385 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800386
Pankaj Berdeb6031342013-02-19 18:51:51 -0800387}