blob: f5f8b00f8a053d7c4e5e995bbd8354541743f847 [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
83 changeDeviceAttachments(device, obj);
84
85 changeDeviceIpv4Addresses(device, obj);
86
87 obj.setMACAddress(device.getMACAddressString());
88 obj.setType("device");
89 obj.setState("ACTIVE");
90 ope.commit();
91
92 break;
93 //log.debug("Adding device {}",device.getMACAddressString());
94 } catch (TitanException e) {
95 ope.rollback();
96 log.error("Adding device {} failed", device.getMACAddressString(), e);
97 obj = null;
98 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070099 }
100
101 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800102 }
103
Teru87cd2da2013-06-15 20:33:08 -0700104 /***
105 * This function is for updating the Device properties.
106 * @param device The device you want to add into the DB.
107 * @return IDeviceObject which was added in the DB.
108 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800109 @Override
110 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800111 return addDevice(device);
112 }
113
Teru87cd2da2013-06-15 20:33:08 -0700114 /***
115 * This function is for removing the Device from the DB.
116 * @param device The device you want to delete from the DB.
117 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800118 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800119 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800120 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800121
122 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
123 removeDevice(dev);
124 }
125 }
126
127 public void removeDevice(IDeviceObject deviceObject) {
128 String deviceMac = deviceObject.getMACAddress();
129
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800130 removeDeviceImpl(deviceObject);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800131
Pankaj Berdeda809572013-02-22 15:31:20 -0800132 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800133 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800134 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700135 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700136 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800137 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800138 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800139 }
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800140
141 public void removeDeviceImpl(IDeviceObject deviceObject) {
142 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
143 ope.removeIpv4Address(ipv4AddressVertex);
144 }
145
146 ope.removeDevice(deviceObject);
147 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800148
Teru87cd2da2013-06-15 20:33:08 -0700149 /***
150 * This function is for getting the Device from the DB by Mac address of the device.
151 * @param mac The device mac address you want to get from the DB.
152 * @return IDeviceObject you want to get.
153 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800154 @Override
155 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700156 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800157 }
158
Teru87cd2da2013-06-15 20:33:08 -0700159 /***
160 * This function is for getting the Device from the DB by IP address of the device.
161 * @param ip The device ip address you want to get from the DB.
162 * @return IDeviceObject you want to get.
163 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800164 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700165 public IDeviceObject getDeviceByIP(int ipv4Address) {
166 try {
167 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
168 if (ipv4AddressVertex != null) {
169 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700170 }
171 return null;
172 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700173 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700174 log.error("DeviceStorage:getDeviceByIP:{} failed");
175 return null;
176 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800177 }
178
Teru87cd2da2013-06-15 20:33:08 -0700179 /***
180 * This function is for changing the Device attachment point.
181 * @param device The device you want change the attachment point
182 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800183 @Override
184 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800185 IDeviceObject obj = null;
186 try {
Teru87cd2da2013-06-15 20:33:08 -0700187 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700188 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800189 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700190 ope.commit();
191 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700192 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800193 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700194 }
195 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700196 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800197 log.error(":addDevice mac:{} failed", device.getMACAddressString());
198 }
199 }
200
Teru87cd2da2013-06-15 20:33:08 -0700201 /***
202 * This function is for changing the Device attachment point.
203 * @param device The new device you want change the attachment point
204 * @param obj The old device IDeviceObject that is going to change the attachment point.
205 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800206 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
207 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
208 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700209
210 for (SwitchPort ap : attachmentPoints) {
211 //Check if there is the port
212 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
213 (short) ap.getPort());
214 log.debug("New Switch Port is {},{}",
215 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
216
217 if (port != null){
218 if (attachedPorts.contains(port)) {
219 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
220 //This port will be remained, so remove from the removed port lists.
221 attachedPorts.remove(port);
222 } else {
223 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
224 port.setDevice(obj);
225 }
226
227 log.debug("port number is {}", port.getNumber().toString());
228 log.debug("port desc is {}", port.getDesc());
229 }
230 }
231
232 for (IPortObject port: attachedPorts) {
233 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
234 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800235
236 if (!obj.getAttachedPorts().iterator().hasNext()) {
237 // XXX If there are no more ports attached to the device,
238 // delete it. Otherwise we have a situation where the
239 // device remains forever with an IP address attached.
240 // When we implement device probing we should get rid of this.
241 removeDevice(obj);
242 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700243 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800244 }
245
Teru87cd2da2013-06-15 20:33:08 -0700246 /***
247 * This function is for changing the Device IPv4 address.
248 * @param device The new device you want change the ipaddress
249 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800250 @Override
251 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700252 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
253 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800254 IDeviceObject obj;
255 try {
Teru87cd2da2013-06-15 20:33:08 -0700256 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800257 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700258
259 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800260 } else {
261 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700262 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800263 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700264 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700265 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800266 }
267 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800268
269 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800270 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800271 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800272 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
273 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800274 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800275 }
276
277 List<String> memIpv4Addresses = new ArrayList<String>();
278 for (int addr : device.getIPv4Addresses()) {
279 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
280 }
281
282 log.debug("Device IP addresses {}, database IP addresses {}",
283 memIpv4Addresses, dbIpv4Addresses);
284
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800285 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800286 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
287 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800288 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800289
Jonathan Hartba9ced92013-11-24 16:52:13 -0800290 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800291 IDeviceObject oldDevice = dbIpv4Address.getDevice();
292 if (oldDevice != null) {
293 oldDevice.removeIpv4Address(dbIpv4Address);
294 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800295 */
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800296
297 log.debug("Adding IP address {}",
298 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800299 deviceObject.addIpv4Address(dbIpv4Address);
300 }
301 }
302
303 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
304 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
305 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800306 log.debug("Removing IP address {}",
307 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
308 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800309 deviceObject.removeIpv4Address(dbIpv4Address);
310 }
311 }
312 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800313
Pankaj Berdeb6031342013-02-19 18:51:51 -0800314}