blob: e9e2bd139b9cd75a7e1f2acb5a8059dfc654254c [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;
Pankaj Berdeb6031342013-02-19 18:51:51 -080071 try {
Teru87cd2da2013-06-15 20:33:08 -070072 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070073 log.debug("Adding device {}: found existing device", device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080074 } else {
Teru87cd2da2013-06-15 20:33:08 -070075 obj = ope.newDevice();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070076 log.debug("Adding device {}: creating new device", device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080077 }
Pankaj Berdeda809572013-02-22 15:31:20 -080078
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070079 changeDeviceAttachments(device, obj);
Jonathan Hartc8ae36c2013-11-03 19:06:40 -080080
81 changeDeviceIpv4Addresses(device, obj);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070082
83 obj.setMACAddress(device.getMACAddressString());
84 obj.setType("device");
85 obj.setState("ACTIVE");
86 ope.commit();
87
88 //log.debug("Adding device {}",device.getMACAddressString());
89 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -070090 ope.rollback();
Jonathan Hart9ec4c6a2013-11-04 22:01:50 -080091 log.error("Adding device {} failed", device.getMACAddressString(), e);
Teru87cd2da2013-06-15 20:33:08 -070092 obj = null;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070093 }
94
95 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -080096 }
97
Teru87cd2da2013-06-15 20:33:08 -070098 /***
99 * This function is for updating the Device properties.
100 * @param device The device you want to add into the DB.
101 * @return IDeviceObject which was added in the DB.
102 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800103 @Override
104 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800105 return addDevice(device);
106 }
107
Teru87cd2da2013-06-15 20:33:08 -0700108 /***
109 * This function is for removing the Device from the DB.
110 * @param device The device you want to delete from the DB.
111 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800112 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800113 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800114 IDeviceObject dev;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800115
116 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
117 removeDevice(dev);
118 }
119 }
120
121 public void removeDevice(IDeviceObject deviceObject) {
122 String deviceMac = deviceObject.getMACAddress();
123
124 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
125 ope.removeIpv4Address(ipv4AddressVertex);
126 }
127
Pankaj Berdeda809572013-02-22 15:31:20 -0800128 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800129 ope.removeDevice(deviceObject);
130 ope.commit();
131 log.error("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700132 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700133 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800134 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800135 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800136 }
137
Teru87cd2da2013-06-15 20:33:08 -0700138 /***
139 * This function is for getting the Device from the DB by Mac address of the device.
140 * @param mac The device mac address you want to get from the DB.
141 * @return IDeviceObject you want to get.
142 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800143 @Override
144 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700145 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800146 }
147
Teru87cd2da2013-06-15 20:33:08 -0700148 /***
149 * This function is for getting the Device from the DB by IP address of the device.
150 * @param ip The device ip address you want to get from the DB.
151 * @return IDeviceObject you want to get.
152 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800153 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700154 public IDeviceObject getDeviceByIP(int ipv4Address) {
155 try {
156 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
157 if (ipv4AddressVertex != null) {
158 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700159 }
160 return null;
161 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700162 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700163 log.error("DeviceStorage:getDeviceByIP:{} failed");
164 return null;
165 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800166 }
167
Teru87cd2da2013-06-15 20:33:08 -0700168 /***
169 * This function is for changing the Device attachment point.
170 * @param device The device you want change the attachment point
171 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800172 @Override
173 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800174 IDeviceObject obj = null;
175 try {
Teru87cd2da2013-06-15 20:33:08 -0700176 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700177 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800178 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700179 ope.commit();
180 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700181 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800182 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700183 }
184 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700185 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800186 log.error(":addDevice mac:{} failed", device.getMACAddressString());
187 }
188 }
189
Teru87cd2da2013-06-15 20:33:08 -0700190 /***
191 * This function is for changing the Device attachment point.
192 * @param device The new device you want change the attachment point
193 * @param obj The old device IDeviceObject that is going to change the attachment point.
194 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800195 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
196 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
197 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700198
199 for (SwitchPort ap : attachmentPoints) {
200 //Check if there is the port
201 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
202 (short) ap.getPort());
203 log.debug("New Switch Port is {},{}",
204 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
205
206 if (port != null){
207 if (attachedPorts.contains(port)) {
208 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
209 //This port will be remained, so remove from the removed port lists.
210 attachedPorts.remove(port);
211 } else {
212 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
213 port.setDevice(obj);
214 }
215
216 log.debug("port number is {}", port.getNumber().toString());
217 log.debug("port desc is {}", port.getDesc());
218 }
219 }
220
221 for (IPortObject port: attachedPorts) {
222 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
223 port.removeDevice(obj);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800224
225 if (!obj.getAttachedPorts().iterator().hasNext()) {
226 // XXX If there are no more ports attached to the device,
227 // delete it. Otherwise we have a situation where the
228 // device remains forever with an IP address attached.
229 // When we implement device probing we should get rid of this.
230 removeDevice(obj);
231 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700232 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800233 }
234
Teru87cd2da2013-06-15 20:33:08 -0700235 /***
236 * This function is for changing the Device IPv4 address.
237 * @param device The new device you want change the ipaddress
238 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800239 @Override
240 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700241 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
242 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800243 IDeviceObject obj;
244 try {
Teru87cd2da2013-06-15 20:33:08 -0700245 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800246 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700247
248 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800249 } else {
250 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700251 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800252 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700253 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700254 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800255 }
256 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800257
258 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800259 List<String> dbIpv4Addresses = new ArrayList<String>();
260 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
261 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
262 }
263
264 List<String> memIpv4Addresses = new ArrayList<String>();
265 for (int addr : device.getIPv4Addresses()) {
266 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
267 }
268
269 log.debug("Device IP addresses {}, database IP addresses {}",
270 memIpv4Addresses, dbIpv4Addresses);
271
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800272 for (int ipv4Address : device.getIPv4Addresses()) {
273 if (deviceObject.getIpv4Address(ipv4Address) == null) {
274 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800275
276 IDeviceObject oldDevice = dbIpv4Address.getDevice();
277 if (oldDevice != null) {
278 oldDevice.removeIpv4Address(dbIpv4Address);
279 }
280
281 log.debug("Adding IP address {}",
282 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800283 deviceObject.addIpv4Address(dbIpv4Address);
284 }
285 }
286
287 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
288 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
289 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800290 log.debug("Removing IP address {}",
291 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
292 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800293 deviceObject.removeIpv4Address(dbIpv4Address);
294 }
295 }
296 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800297
Pankaj Berdeb6031342013-02-19 18:51:51 -0800298}