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