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 | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 4 | import java.util.Arrays; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 5 | import java.util.List; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 6 | |
| 7 | import net.floodlightcontroller.devicemanager.IDevice; |
| 8 | import net.floodlightcontroller.devicemanager.SwitchPort; |
| 9 | import net.onrc.onos.graph.GraphDBOperation; |
| 10 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address; |
| 13 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 14 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 15 | import net.onrc.onos.ofcontroller.devicemanager.OnosDevice; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 16 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 17 | import org.openflow.util.HexString; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import com.google.common.collect.Lists; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 22 | import com.google.common.net.InetAddresses; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 23 | import com.thinkaurelius.titan.core.TitanException; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 24 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 25 | /** |
| 26 | * This is the class for storing the information of devices into CassandraDB |
| 27 | * @author Pankaj |
| 28 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 29 | public class DeviceStorageImpl implements IDeviceStorage { |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 30 | protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.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 | private GraphDBOperation ope; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 33 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 34 | /*** |
| 35 | * Initialize function. Before you use this class, please call this method |
| 36 | * @param conf configuration file for Cassandra DB |
| 37 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 38 | @Override |
| 39 | public void init(String conf) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 40 | try { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 41 | ope = new GraphDBOperation(conf); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 42 | } catch (TitanException e) { |
| 43 | log.error("Couldn't open graph operation", e); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 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() { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 53 | ope.close(); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /*** |
| 57 | * Finalize/close function. After you use this class, please call this method. |
Pavlin Radoslavov | ef0cb00 | 2013-06-21 14:55:23 -0700 | [diff] [blame] | 58 | * It will close the DB connection. This is for Java garbage collection. |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 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; |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 73 | for (int i = 0; i < 6; i++) { |
| 74 | try { |
| 75 | if (i > 0) { |
| 76 | log.debug("Retrying add device: i is {}", i); |
| 77 | } |
| 78 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
| 79 | log.debug("Adding device {}: found existing device", device.getMACAddressString()); |
| 80 | } else { |
| 81 | obj = ope.newDevice(); |
| 82 | log.debug("Adding device {}: creating new device", device.getMACAddressString()); |
| 83 | } |
| 84 | |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 85 | if (obj == null) { |
| 86 | return null; |
| 87 | } |
| 88 | |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 89 | changeDeviceAttachments(device, obj); |
| 90 | |
| 91 | changeDeviceIpv4Addresses(device, obj); |
| 92 | |
| 93 | obj.setMACAddress(device.getMACAddressString()); |
| 94 | obj.setType("device"); |
| 95 | obj.setState("ACTIVE"); |
| 96 | ope.commit(); |
| 97 | |
| 98 | break; |
| 99 | //log.debug("Adding device {}",device.getMACAddressString()); |
| 100 | } catch (TitanException e) { |
| 101 | ope.rollback(); |
| 102 | log.error("Adding device {} failed", device.getMACAddressString(), e); |
| 103 | obj = null; |
| 104 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 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; |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 127 | |
| 128 | if ((dev = ope.searchDevice(device.getMACAddressString())) != null) { |
| 129 | removeDevice(dev); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public void removeDevice(IDeviceObject deviceObject) { |
| 134 | String deviceMac = deviceObject.getMACAddress(); |
| 135 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 136 | removeDeviceImpl(deviceObject); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 137 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 138 | try { |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 139 | ope.commit(); |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 140 | log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 141 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 142 | ope.rollback(); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 143 | log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 144 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 145 | } |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 146 | |
| 147 | public void removeDeviceImpl(IDeviceObject deviceObject) { |
| 148 | for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) { |
| 149 | ope.removeIpv4Address(ipv4AddressVertex); |
| 150 | } |
| 151 | |
| 152 | ope.removeDevice(deviceObject); |
| 153 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 154 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 155 | /*** |
| 156 | * This function is for getting the Device from the DB by Mac address of the device. |
| 157 | * @param mac The device mac address you want to get from the DB. |
| 158 | * @return IDeviceObject you want to get. |
| 159 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 160 | @Override |
| 161 | public IDeviceObject getDeviceByMac(String mac) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 162 | return ope.searchDevice(mac); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 165 | /*** |
| 166 | * This function is for getting the Device from the DB by IP address of the device. |
| 167 | * @param ip The device ip address you want to get from the DB. |
| 168 | * @return IDeviceObject you want to get. |
| 169 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 170 | @Override |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 171 | public IDeviceObject getDeviceByIP(int ipv4Address) { |
| 172 | try { |
| 173 | IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address); |
| 174 | if (ipv4AddressVertex != null) { |
| 175 | return ipv4AddressVertex.getDevice(); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 176 | } |
| 177 | return null; |
| 178 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 179 | catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 180 | log.error("DeviceStorage:getDeviceByIP:{} failed"); |
| 181 | return null; |
| 182 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 185 | /*** |
| 186 | * This function is for changing the Device attachment point. |
| 187 | * @param device The device you want change the attachment point |
| 188 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 189 | @Override |
| 190 | public void changeDeviceAttachments(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 191 | IDeviceObject obj = null; |
| 192 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 193 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 194 | log.debug("Changing device ports {}: found existing device", device.getMACAddressString()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 195 | changeDeviceAttachments(device, obj); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 196 | ope.commit(); |
| 197 | } else { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 198 | log.debug("failed to search device...now adding {}", device.getMACAddressString()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 199 | addDevice(device); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 200 | } |
| 201 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 202 | ope.rollback(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 203 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
| 204 | } |
| 205 | } |
| 206 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 207 | /*** |
| 208 | * This function is for changing the Device attachment point. |
| 209 | * @param device The new device you want change the attachment point |
| 210 | * @param obj The old device IDeviceObject that is going to change the attachment point. |
| 211 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 212 | public void changeDeviceAttachments(IDevice device, IDeviceObject obj) { |
| 213 | SwitchPort[] attachmentPoints = device.getAttachmentPoints(); |
| 214 | List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts()); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 215 | |
| 216 | for (SwitchPort ap : attachmentPoints) { |
| 217 | //Check if there is the port |
| 218 | IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()), |
| 219 | (short) ap.getPort()); |
| 220 | log.debug("New Switch Port is {},{}", |
| 221 | HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort()); |
| 222 | |
| 223 | if (port != null){ |
| 224 | if (attachedPorts.contains(port)) { |
| 225 | log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString()); |
| 226 | //This port will be remained, so remove from the removed port lists. |
| 227 | attachedPorts.remove(port); |
| 228 | } else { |
| 229 | log.debug("Adding device {}: attaching to port", device.getMACAddressString()); |
| 230 | port.setDevice(obj); |
| 231 | } |
| 232 | |
| 233 | log.debug("port number is {}", port.getNumber().toString()); |
| 234 | log.debug("port desc is {}", port.getDesc()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | for (IPortObject port: attachedPorts) { |
| 239 | log.debug("Detaching the device {}: detaching from port", device.getMACAddressString()); |
| 240 | port.removeDevice(obj); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 241 | |
| 242 | if (!obj.getAttachedPorts().iterator().hasNext()) { |
| 243 | // XXX If there are no more ports attached to the device, |
| 244 | // delete it. Otherwise we have a situation where the |
| 245 | // device remains forever with an IP address attached. |
| 246 | // When we implement device probing we should get rid of this. |
| 247 | removeDevice(obj); |
| 248 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 249 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 252 | /*** |
| 253 | * This function is for changing the Device IPv4 address. |
| 254 | * @param device The new device you want change the ipaddress |
| 255 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 256 | @Override |
| 257 | public void changeDeviceIPv4Address(IDevice device) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 258 | log.debug("Changing IP address for {} to {}", device.getMACAddressString(), |
| 259 | device.getIPv4Addresses()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 260 | IDeviceObject obj; |
| 261 | try { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 262 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 263 | changeDeviceIpv4Addresses(device, obj); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 264 | |
| 265 | ope.commit(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 266 | } else { |
| 267 | log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString()); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 268 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 269 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 270 | ope.rollback(); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 271 | log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 274 | |
| 275 | private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) { |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 276 | List<String> dbIpv4Addresses = new ArrayList<String>(); |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 277 | List<Integer> intDbIpv4Addresses = new ArrayList<Integer>(); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 278 | for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) { |
| 279 | dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress()); |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 280 | intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address()); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | List<String> memIpv4Addresses = new ArrayList<String>(); |
| 284 | for (int addr : device.getIPv4Addresses()) { |
| 285 | memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress()); |
| 286 | } |
| 287 | |
| 288 | log.debug("Device IP addresses {}, database IP addresses {}", |
| 289 | memIpv4Addresses, dbIpv4Addresses); |
| 290 | |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 291 | for (int ipv4Address : device.getIPv4Addresses()) { |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 292 | //if (deviceObject.getIpv4Address(ipv4Address) == null) { |
| 293 | if (!intDbIpv4Addresses.contains(ipv4Address)) { |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 294 | IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address); |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 295 | |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 296 | /* |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 297 | IDeviceObject oldDevice = dbIpv4Address.getDevice(); |
| 298 | if (oldDevice != null) { |
| 299 | oldDevice.removeIpv4Address(dbIpv4Address); |
| 300 | } |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 301 | */ |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 302 | |
| 303 | log.debug("Adding IP address {}", |
| 304 | InetAddresses.fromInteger(ipv4Address).getHostAddress()); |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 305 | deviceObject.addIpv4Address(dbIpv4Address); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses()); |
| 310 | for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) { |
| 311 | if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) { |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 312 | log.debug("Removing IP address {}", |
| 313 | InetAddresses.fromInteger(dbIpv4Address.getIpv4Address()) |
| 314 | .getHostAddress()); |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 315 | deviceObject.removeIpv4Address(dbIpv4Address); |
| 316 | } |
| 317 | } |
| 318 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * Takes an {@link OnosDevice} and adds it into the database. There is no |
| 322 | * checking of the database data and removing old data - an |
| 323 | * {@link OnosDevice} basically corresponds to a packet we've just seen, |
| 324 | * and we need to add that information into the database. |
| 325 | */ |
| 326 | @Override |
| 327 | public void addOnosDevice(OnosDevice onosDevice) { |
| 328 | |
| 329 | String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes()); |
| 330 | |
| 331 | try { |
| 332 | IDeviceObject device = ope.searchDevice(macAddress); |
| 333 | |
| 334 | if (device == null) { |
| 335 | device = ope.newDevice(); |
| 336 | device.setType("device"); |
| 337 | device.setState("ACTIVE"); |
| 338 | device.setMACAddress(macAddress); |
| 339 | } |
| 340 | |
| 341 | // Check if the device has the IP address, add it if it doesn't |
| 342 | // TODO multiple IP addresses |
| 343 | if (onosDevice.getIpv4Address() != null) { |
| 344 | boolean hasIpAddress = false; |
| 345 | for (IIpv4Address ipv4Address : device.getIpv4Addresses()) { |
| 346 | if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) { |
| 347 | hasIpAddress = true; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (!hasIpAddress) { |
| 353 | IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue()); |
| 354 | IDeviceObject oldDevice = ipv4Address.getDevice(); |
| 355 | if (oldDevice != null && oldDevice.getMACAddress() != macAddress) { |
| 356 | oldDevice.removeIpv4Address(ipv4Address); |
| 357 | } |
| 358 | device.addIpv4Address(ipv4Address); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Check if the device has the attachment point, add it if not |
| 363 | String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID()); |
| 364 | boolean hasAttachmentPoint = false; |
| 365 | for (IPortObject portObject : device.getAttachedPorts()) { |
| 366 | ISwitchObject switchObject = portObject.getSwitch(); |
| 367 | if (switchObject.getDPID().equals(switchDpid) |
| 368 | && portObject.getNumber() == onosDevice.getSwitchPort()) { |
| 369 | hasAttachmentPoint = true; |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (!hasAttachmentPoint) { |
| 375 | IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort()); |
| 376 | portObject.setDevice(device); |
| 377 | } |
| 378 | |
| 379 | ope.commit(); |
| 380 | } |
| 381 | catch (TitanException e) { |
| 382 | log.error("addOnosDevice {} failed:", macAddress, e); |
| 383 | ope.rollback(); |
| 384 | } |
| 385 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 386 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 387 | } |