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