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