HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core.internal; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 3 | import java.util.Arrays; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 4 | import java.util.List; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 5 | |
| 6 | import net.floodlightcontroller.devicemanager.IDevice; |
| 7 | import net.floodlightcontroller.devicemanager.SwitchPort; |
| 8 | import net.onrc.onos.graph.GraphDBOperation; |
| 9 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 13 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 14 | import org.openflow.util.HexString; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.thinkaurelius.titan.core.TitanException; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 20 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 21 | /** |
| 22 | * This is the class for storing the information of devices into CassandraDB |
| 23 | * @author Pankaj |
| 24 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 25 | public class DeviceStorageImpl implements IDeviceStorage { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 26 | protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 27 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 28 | private GraphDBOperation ope; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 29 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 30 | /*** |
| 31 | * Initialize function. Before you use this class, please call this method |
| 32 | * @param conf configuration file for Cassandra DB |
| 33 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 34 | @Override |
| 35 | public void init(String conf) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 36 | try { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 37 | ope = new GraphDBOperation(conf); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 38 | } catch (TitanException e) { |
| 39 | log.error("Couldn't open graph operation", e); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 40 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 41 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 42 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 43 | /*** |
| 44 | * Finalize/close function. After you use this class, please call this method. |
| 45 | * It will close the DB connection. |
| 46 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public void close() { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 49 | ope.close(); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /*** |
| 53 | * Finalize/close function. After you use this class, please call this method. |
Pavlin Radoslavov | ef0cb00 | 2013-06-21 14:55:23 -0700 | [diff] [blame] | 54 | * It will close the DB connection. This is for Java garbage collection. |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Override |
| 57 | public void finalize() { |
| 58 | close(); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 61 | /*** |
| 62 | * This function is for adding the device into the DB. |
| 63 | * @param device The device you want to add into the DB. |
| 64 | * @return IDeviceObject which was added in the DB. |
| 65 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 66 | @Override |
| 67 | public IDeviceObject addDevice(IDevice device) { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 68 | IDeviceObject obj = null; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 69 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 70 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 71 | log.debug("Adding device {}: found existing device", device.getMACAddressString()); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 72 | } else { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 73 | obj = ope.newDevice(); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 74 | log.debug("Adding device {}: creating new device", device.getMACAddressString()); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 75 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 76 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 77 | changeDeviceAttachments(device, obj); |
| 78 | |
| 79 | for (Integer intIpv4Address : device.getIPv4Addresses()) { |
| 80 | obj.addIpv4Address(ope.ensureIpv4Address(intIpv4Address)); |
| 81 | } |
| 82 | |
| 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) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 90 | ope.rollback(); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 91 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 92 | obj = null; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return obj; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 98 | /*** |
| 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 Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 103 | @Override |
| 104 | public IDeviceObject updateDevice(IDevice device) { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 105 | return addDevice(device); |
| 106 | } |
| 107 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 108 | /*** |
| 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 Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 112 | @Override |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 113 | public void removeDevice(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 114 | IDeviceObject dev; |
| 115 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 116 | if ((dev = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | 4fce4ed | 2013-11-01 21:29:21 -0700 | [diff] [blame^] | 117 | for (IIpv4Address ipv4AddressVertex : dev.getIpv4Addresses()) { |
| 118 | ope.removeIpv4Address(ipv4AddressVertex); |
| 119 | } |
| 120 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 121 | ope.removeDevice(dev); |
| 122 | ope.commit(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 123 | log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString()); |
| 124 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 125 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 126 | ope.rollback(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 127 | log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString()); |
| 128 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 131 | /*** |
| 132 | * This function is for getting the Device from the DB by Mac address of the device. |
| 133 | * @param mac The device mac address you want to get from the DB. |
| 134 | * @return IDeviceObject you want to get. |
| 135 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 136 | @Override |
| 137 | public IDeviceObject getDeviceByMac(String mac) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 138 | return ope.searchDevice(mac); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 141 | /*** |
| 142 | * This function is for getting the Device from the DB by IP address of the device. |
| 143 | * @param ip The device ip address you want to get from the DB. |
| 144 | * @return IDeviceObject you want to get. |
| 145 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 146 | @Override |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 147 | public IDeviceObject getDeviceByIP(int ipv4Address) { |
| 148 | try { |
| 149 | IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address); |
| 150 | if (ipv4AddressVertex != null) { |
| 151 | return ipv4AddressVertex.getDevice(); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 152 | } |
| 153 | return null; |
| 154 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 155 | catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 156 | log.error("DeviceStorage:getDeviceByIP:{} failed"); |
| 157 | return null; |
| 158 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 161 | /*** |
| 162 | * This function is for changing the Device attachment point. |
| 163 | * @param device The device you want change the attachment point |
| 164 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public void changeDeviceAttachments(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 167 | IDeviceObject obj = null; |
| 168 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 169 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 170 | log.debug("Changing device ports {}: found existing device", device.getMACAddressString()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 171 | changeDeviceAttachments(device, obj); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 172 | ope.commit(); |
| 173 | } else { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 174 | log.debug("failed to search device...now adding {}", device.getMACAddressString()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 175 | addDevice(device); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 176 | } |
| 177 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 178 | ope.rollback(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 179 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
| 180 | } |
| 181 | } |
| 182 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 183 | /*** |
| 184 | * This function is for changing the Device attachment point. |
| 185 | * @param device The new device you want change the attachment point |
| 186 | * @param obj The old device IDeviceObject that is going to change the attachment point. |
| 187 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 188 | public void changeDeviceAttachments(IDevice device, IDeviceObject obj) { |
| 189 | SwitchPort[] attachmentPoints = device.getAttachmentPoints(); |
| 190 | List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts()); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 191 | |
| 192 | for (SwitchPort ap : attachmentPoints) { |
| 193 | //Check if there is the port |
| 194 | IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()), |
| 195 | (short) ap.getPort()); |
| 196 | log.debug("New Switch Port is {},{}", |
| 197 | HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort()); |
| 198 | |
| 199 | if (port != null){ |
| 200 | if (attachedPorts.contains(port)) { |
| 201 | log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString()); |
| 202 | //This port will be remained, so remove from the removed port lists. |
| 203 | attachedPorts.remove(port); |
| 204 | } else { |
| 205 | log.debug("Adding device {}: attaching to port", device.getMACAddressString()); |
| 206 | port.setDevice(obj); |
| 207 | } |
| 208 | |
| 209 | log.debug("port number is {}", port.getNumber().toString()); |
| 210 | log.debug("port desc is {}", port.getDesc()); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | for (IPortObject port: attachedPorts) { |
| 215 | log.debug("Detaching the device {}: detaching from port", device.getMACAddressString()); |
| 216 | port.removeDevice(obj); |
| 217 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 220 | /*** |
| 221 | * This function is for changing the Device IPv4 address. |
| 222 | * @param device The new device you want change the ipaddress |
| 223 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 224 | @Override |
| 225 | public void changeDeviceIPv4Address(IDevice device) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 226 | log.debug("Changing IP address for {} to {}", device.getMACAddressString(), |
| 227 | device.getIPv4Addresses()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 228 | IDeviceObject obj; |
| 229 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 230 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 231 | for (int ipv4Address : device.getIPv4Addresses()) { |
| 232 | if (obj.getIpv4Address(ipv4Address) == null) { |
| 233 | IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address); |
| 234 | obj.addIpv4Address(dbIpv4Address); |
| 235 | } |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 238 | List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses()); |
| 239 | for (IIpv4Address dbIpv4Address : obj.getIpv4Addresses()) { |
| 240 | if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) { |
| 241 | obj.removeIpv4Address(dbIpv4Address); |
| 242 | } |
| 243 | } |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 244 | |
| 245 | ope.commit(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 246 | } else { |
| 247 | log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString()); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 248 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 249 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 250 | ope.rollback(); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 251 | log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 255 | } |