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