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