blob: 4bbc054cfb0f495e4fd6b6e3b2c8528255bb1d0f [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;
14
Pankaj Berdeb6031342013-02-19 18:51:51 -080015import org.openflow.util.HexString;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import com.google.common.collect.Lists;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080020import com.google.common.net.InetAddresses;
Pankaj Berdeb6031342013-02-19 18:51:51 -080021import com.thinkaurelius.titan.core.TitanException;
Pankaj Berdeb6031342013-02-19 18:51:51 -080022
Teru87cd2da2013-06-15 20:33:08 -070023/**
24 * This is the class for storing the information of devices into CassandraDB
25 * @author Pankaj
26 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080027public class DeviceStorageImpl implements IDeviceStorage {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080028 protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080029
Teru87cd2da2013-06-15 20:33:08 -070030 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080031
Teru87cd2da2013-06-15 20:33:08 -070032 /***
33 * Initialize function. Before you use this class, please call this method
34 * @param conf configuration file for Cassandra DB
35 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080036 @Override
37 public void init(String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070038 try {
Toshio Koidebfe9b922013-06-18 10:56:05 -070039 ope = new GraphDBOperation(conf);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070040 } catch (TitanException e) {
41 log.error("Couldn't open graph operation", e);
Teru87cd2da2013-06-15 20:33:08 -070042 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080043 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080044
Teru87cd2da2013-06-15 20:33:08 -070045 /***
46 * Finalize/close function. After you use this class, please call this method.
47 * It will close the DB connection.
48 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080049 @Override
50 public void close() {
Toshio Koidebfe9b922013-06-18 10:56:05 -070051 ope.close();
Teru87cd2da2013-06-15 20:33:08 -070052 }
53
54 /***
55 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070056 * It will close the DB connection. This is for Java garbage collection.
Teru87cd2da2013-06-15 20:33:08 -070057 */
58 @Override
59 public void finalize() {
60 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080061 }
62
Teru87cd2da2013-06-15 20:33:08 -070063 /***
64 * This function is for adding the device into the DB.
65 * @param device The device you want to add into the DB.
66 * @return IDeviceObject which was added in the DB.
67 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080068 @Override
69 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080070 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080071 for (int i = 0; i < 6; i++) {
72 try {
73 if (i > 0) {
74 log.debug("Retrying add device: i is {}", i);
75 }
76 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
77 log.debug("Adding device {}: found existing device", device.getMACAddressString());
78 } else {
79 obj = ope.newDevice();
80 log.debug("Adding device {}: creating new device", device.getMACAddressString());
81 }
82
Naoki Shiotae2f4da72013-12-09 16:34:17 -080083 if (obj == null) {
84 return null;
85 }
86
Jonathan Hartba9ced92013-11-24 16:52:13 -080087 changeDeviceAttachments(device, obj);
88
89 changeDeviceIpv4Addresses(device, obj);
90
91 obj.setMACAddress(device.getMACAddressString());
92 obj.setType("device");
93 obj.setState("ACTIVE");
94 ope.commit();
95
96 break;
97 //log.debug("Adding device {}",device.getMACAddressString());
98 } catch (TitanException e) {
99 ope.rollback();
100 log.error("Adding device {} failed", device.getMACAddressString(), e);
101 obj = null;
102 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700103 }
104
105 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800106 }
107
Teru87cd2da2013-06-15 20:33:08 -0700108 /***
109 * This function is for updating the Device properties.
110 * @param device The device you want to add into the DB.
111 * @return IDeviceObject which was added in the DB.
112 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800113 @Override
114 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800115 return addDevice(device);
116 }
117
Teru87cd2da2013-06-15 20:33:08 -0700118 /***
119 * This function is for removing the Device from the DB.
120 * @param device The device you want to delete from the DB.
121 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800122 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800123 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800124 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800125
126 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
127 removeDevice(dev);
128 }
129 }
130
131 public void removeDevice(IDeviceObject deviceObject) {
132 String deviceMac = deviceObject.getMACAddress();
133
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800134 removeDeviceImpl(deviceObject);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800135
Pankaj Berdeda809572013-02-22 15:31:20 -0800136 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800137 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800138 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700139 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700140 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800141 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800142 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800143 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800144
145 public void removeDeviceImpl(IDeviceObject deviceObject) {
146 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
147 ope.removeIpv4Address(ipv4AddressVertex);
148 }
149
150 ope.removeDevice(deviceObject);
151 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800152
Teru87cd2da2013-06-15 20:33:08 -0700153 /***
154 * This function is for getting the Device from the DB by Mac address of the device.
155 * @param mac The device mac address you want to get from the DB.
156 * @return IDeviceObject you want to get.
157 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800158 @Override
159 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700160 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800161 }
162
Teru87cd2da2013-06-15 20:33:08 -0700163 /***
164 * This function is for getting the Device from the DB by IP address of the device.
165 * @param ip The device ip address you want to get from the DB.
166 * @return IDeviceObject you want to get.
167 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800168 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700169 public IDeviceObject getDeviceByIP(int ipv4Address) {
170 try {
171 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
172 if (ipv4AddressVertex != null) {
173 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700174 }
175 return null;
176 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700177 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700178 log.error("DeviceStorage:getDeviceByIP:{} failed");
179 return null;
180 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800181 }
182
Teru87cd2da2013-06-15 20:33:08 -0700183 /***
184 * This function is for changing the Device attachment point.
185 * @param device The device you want change the attachment point
186 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800187 @Override
188 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800189 IDeviceObject obj = null;
190 try {
Teru87cd2da2013-06-15 20:33:08 -0700191 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700192 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800193 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700194 ope.commit();
195 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700196 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800197 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700198 }
199 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700200 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800201 log.error(":addDevice mac:{} failed", device.getMACAddressString());
202 }
203 }
204
Teru87cd2da2013-06-15 20:33:08 -0700205 /***
206 * This function is for changing the Device attachment point.
207 * @param device The new device you want change the attachment point
208 * @param obj The old device IDeviceObject that is going to change the attachment point.
209 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800210 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
211 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
212 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700213
214 for (SwitchPort ap : attachmentPoints) {
215 //Check if there is the port
216 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
217 (short) ap.getPort());
218 log.debug("New Switch Port is {},{}",
219 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
220
221 if (port != null){
222 if (attachedPorts.contains(port)) {
223 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
224 //This port will be remained, so remove from the removed port lists.
225 attachedPorts.remove(port);
226 } else {
227 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
228 port.setDevice(obj);
229 }
230
231 log.debug("port number is {}", port.getNumber().toString());
232 log.debug("port desc is {}", port.getDesc());
233 }
234 }
235
236 for (IPortObject port: attachedPorts) {
237 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
238 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800239
240 if (!obj.getAttachedPorts().iterator().hasNext()) {
241 // XXX If there are no more ports attached to the device,
242 // delete it. Otherwise we have a situation where the
243 // device remains forever with an IP address attached.
244 // When we implement device probing we should get rid of this.
245 removeDevice(obj);
246 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700247 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800248 }
249
Teru87cd2da2013-06-15 20:33:08 -0700250 /***
251 * This function is for changing the Device IPv4 address.
252 * @param device The new device you want change the ipaddress
253 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800254 @Override
255 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700256 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
257 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800258 IDeviceObject obj;
259 try {
Teru87cd2da2013-06-15 20:33:08 -0700260 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800261 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700262
263 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800264 } else {
265 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700266 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800267 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700268 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700269 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800270 }
271 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800272
273 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800274 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800275 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800276 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
277 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800278 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800279 }
280
281 List<String> memIpv4Addresses = new ArrayList<String>();
282 for (int addr : device.getIPv4Addresses()) {
283 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
284 }
285
286 log.debug("Device IP addresses {}, database IP addresses {}",
287 memIpv4Addresses, dbIpv4Addresses);
288
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800289 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800290 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
291 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800292 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800293
Jonathan Hartba9ced92013-11-24 16:52:13 -0800294 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800295 IDeviceObject oldDevice = dbIpv4Address.getDevice();
296 if (oldDevice != null) {
297 oldDevice.removeIpv4Address(dbIpv4Address);
298 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800299 */
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800300
301 log.debug("Adding IP address {}",
302 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800303 deviceObject.addIpv4Address(dbIpv4Address);
304 }
305 }
306
307 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
308 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
309 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800310 log.debug("Removing IP address {}",
311 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
312 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800313 deviceObject.removeIpv4Address(dbIpv4Address);
314 }
315 }
316 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800317
Pankaj Berdeb6031342013-02-19 18:51:51 -0800318}