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 |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 28 | * |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 29 | * @author Pankaj |
| 30 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 31 | public class DeviceStorageImpl implements IDeviceStorage { |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 32 | protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 33 | |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 34 | private DBOperation ope; |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [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 |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 40 | public void init(final String dbStore, final String conf) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 41 | try { |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 42 | ope = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
| 43 | //ope = GraphDBManager.getDBOperation(dbStore, conf); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 44 | } catch (Exception e) { |
| 45 | log.error(e.getMessage()); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 46 | } |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 47 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 48 | |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 49 | /*** |
| 50 | * Finalize/close function. After you use this class, please call this method. |
| 51 | * It will close the DB connection. |
| 52 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public void close() { |
| 55 | ope.close(); |
| 56 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 57 | |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 58 | /*** |
| 59 | * Finalize/close function. After you use this class, please call this method. |
| 60 | * It will close the DB connection. This is for Java garbage collection. |
| 61 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 62 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 63 | protected void finalize() { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 64 | close(); |
| 65 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 66 | |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 67 | /*** |
| 68 | * This function is for adding the device into the DB. |
| 69 | * @param device The device you want to add into the DB. |
| 70 | * @return IDeviceObject which was added in the DB. |
| 71 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 72 | @Override |
| 73 | public IDeviceObject addDevice(IDevice device) { |
| 74 | IDeviceObject obj = null; |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 75 | for (int i = 0; i < 6; i++) { |
| 76 | try { |
| 77 | if (i > 0) { |
| 78 | log.debug("Retrying add device: i is {}", i); |
| 79 | } |
| 80 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
| 81 | log.debug("Adding device {}: found existing device", device.getMACAddressString()); |
| 82 | } else { |
| 83 | obj = ope.newDevice(); |
| 84 | log.debug("Adding device {}: creating new device", device.getMACAddressString()); |
| 85 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 86 | |
Naoki Shiota | e2f4da7 | 2013-12-09 16:34:17 -0800 | [diff] [blame] | 87 | if (obj == null) { |
| 88 | return null; |
| 89 | } |
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 | changeDeviceAttachments(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 | changeDeviceIpv4Addresses(device, obj); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 95 | obj.setMACAddress(device.getMACAddressString()); |
| 96 | obj.setType("device"); |
| 97 | obj.setState("ACTIVE"); |
| 98 | ope.commit(); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 100 | break; |
| 101 | //log.debug("Adding device {}",device.getMACAddressString()); |
| 102 | } catch (TitanException e) { |
| 103 | ope.rollback(); |
| 104 | log.error("Adding device {} failed", device.getMACAddressString(), e); |
| 105 | obj = null; |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 106 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 107 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 108 | |
| 109 | return obj; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 110 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 111 | /*** |
| 112 | * This function is for updating the Device properties. |
| 113 | * @param device The device you want to add into the DB. |
| 114 | * @return IDeviceObject which was added in the DB. |
| 115 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public IDeviceObject updateDevice(IDevice device) { |
| 118 | return addDevice(device); |
| 119 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 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 | e85e115 | 2014-01-06 16:12:53 -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 | e85e115 | 2014-01-06 16:12:53 -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(); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 137 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 138 | removeDeviceImpl(deviceObject); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -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 | e85e115 | 2014-01-06 16:12:53 -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 | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 153 | |
Jonathan Hart | 4cfd193 | 2013-11-19 16:42:25 -0800 | [diff] [blame] | 154 | ope.removeDevice(deviceObject); |
| 155 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 156 | /*** |
| 157 | * This function is for getting the Device from the DB by Mac address of the device. |
| 158 | * @param mac The device mac address you want to get from the DB. |
| 159 | * @return IDeviceObject you want to get. |
| 160 | */ |
| 161 | @Override |
| 162 | public IDeviceObject getDeviceByMac(String mac) { |
| 163 | return ope.searchDevice(mac); |
| 164 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 165 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 166 | /*** |
| 167 | * This function is for getting the Device from the DB by IP address of the device. |
| 168 | * @param ip The device ip address you want to get from the DB. |
| 169 | * @return IDeviceObject you want to get. |
| 170 | */ |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 171 | @Override |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 172 | public IDeviceObject getDeviceByIP(int ipv4Address) { |
| 173 | try { |
| 174 | IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address); |
| 175 | if (ipv4AddressVertex != null) { |
| 176 | return ipv4AddressVertex.getDevice(); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 177 | } |
| 178 | return null; |
| 179 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 180 | catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 181 | log.error("DeviceStorage:getDeviceByIP:{} failed"); |
| 182 | return null; |
| 183 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 184 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 185 | |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 186 | /*** |
| 187 | * This function is for changing the Device attachment point. |
| 188 | * @param device The device you want change the attachment point |
| 189 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 190 | @Override |
| 191 | public void changeDeviceAttachments(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 192 | IDeviceObject obj = null; |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 193 | try { |
| 194 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
| 195 | log.debug("Changing device ports {}: found existing device", device.getMACAddressString()); |
| 196 | changeDeviceAttachments(device, obj); |
| 197 | ope.commit(); |
| 198 | } else { |
| 199 | log.debug("failed to search device...now adding {}", device.getMACAddressString()); |
| 200 | addDevice(device); |
| 201 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 202 | } catch (TitanException e) { |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 203 | ope.rollback(); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 204 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 205 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 206 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 207 | |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 208 | /*** |
| 209 | * This function is for changing the Device attachment point. |
| 210 | * @param device The new device you want change the attachment point |
| 211 | * @param obj The old device IDeviceObject that is going to change the attachment point. |
| 212 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 213 | public void changeDeviceAttachments(IDevice device, IDeviceObject obj) { |
| 214 | SwitchPort[] attachmentPoints = device.getAttachmentPoints(); |
| 215 | List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts()); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 216 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 217 | for (SwitchPort ap : attachmentPoints) { |
| 218 | //Check if there is the port |
| 219 | IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()), |
| 220 | (short) ap.getPort()); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 221 | log.debug("New Switch Port is {},{}", |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 222 | HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort()); |
Teru | 87cd2da | 2013-06-15 20:33:08 -0700 | [diff] [blame] | 223 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 224 | if (port != null){ |
| 225 | if (attachedPorts.contains(port)) { |
| 226 | log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString()); |
| 227 | //This port will be remained, so remove from the removed port lists. |
| 228 | attachedPorts.remove(port); |
| 229 | } else { |
| 230 | log.debug("Adding device {}: attaching to port", device.getMACAddressString()); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 231 | port.setDevice(obj); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 232 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 233 | |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 234 | log.debug("port number is {}", port.getNumber()); |
| 235 | log.debug("port desc is {}", port.getDesc()); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 236 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 237 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 239 | for (IPortObject port: attachedPorts) { |
| 240 | log.debug("Detaching the device {}: detaching from port", device.getMACAddressString()); |
| 241 | port.removeDevice(obj); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 242 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 243 | if (!obj.getAttachedPorts().iterator().hasNext()) { |
| 244 | // XXX If there are no more ports attached to the device, |
| 245 | // delete it. Otherwise we have a situation where the |
| 246 | // device remains forever with an IP address attached. |
| 247 | // When we implement device probing we should get rid of this. |
| 248 | removeDevice(obj); |
| 249 | } |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 250 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 251 | } |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 252 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 253 | /*** |
| 254 | * This function is for changing the Device IPv4 address. |
| 255 | * @param device The new device you want change the ipaddress |
| 256 | */ |
| 257 | @Override |
| 258 | public void changeDeviceIPv4Address(IDevice device) { |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 259 | log.debug("Changing IP address for {} to {}", device.getMACAddressString(), |
| 260 | device.getIPv4Addresses()); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 261 | IDeviceObject obj; |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 262 | try { |
| 263 | if ((obj = ope.searchDevice(device.getMACAddressString())) != null) { |
| 264 | changeDeviceIpv4Addresses(device, obj); |
| 265 | ope.commit(); |
| 266 | } else { |
| 267 | log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString()); |
| 268 | } |
| 269 | } catch (TitanException e) { |
| 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 | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 274 | |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 282 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 283 | List<String> memIpv4Addresses = new ArrayList<String>(); |
| 284 | for (int addr : device.getIPv4Addresses()) { |
| 285 | memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress()); |
| 286 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 287 | |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 288 | log.debug("Device IP addresses {}, database IP addresses {}", |
| 289 | memIpv4Addresses, dbIpv4Addresses); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 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); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -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 | */ |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 302 | |
| 303 | log.debug("Adding IP address {}", |
Jonathan Hart | 1a6f1d6 | 2013-11-14 11:33:46 -0800 | [diff] [blame] | 304 | InetAddresses.fromInteger(ipv4Address).getHostAddress()); |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 305 | deviceObject.addIpv4Address(dbIpv4Address); |
| 306 | } |
| 307 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 308 | |
Jonathan Hart | c8ae36c | 2013-11-03 19:06:40 -0800 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 319 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 320 | /** |
| 321 | * 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] | 322 | * checking of the database data and removing old data - an |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 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) { |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 328 | String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes()); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 329 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 330 | //if the switch port we try to attach a new device already has a link, then stop adding device |
| 331 | IPortObject portObject1 = ope.searchPort(HexString.toHexString( |
| 332 | onosDevice.getSwitchDPID()), onosDevice.getSwitchPort()); |
| 333 | |
Pavlin Radoslavov | 7807328 | 2013-12-19 17:01:23 -0800 | [diff] [blame] | 334 | if ((portObject1 != null) && portObject1.getLinkedPorts().iterator().hasNext()) { |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 335 | if (log.isDebugEnabled()) { |
| 336 | log.debug("stop adding OnosDevice: {} due to there is a link to: {}", |
| 337 | onosDevice, portObject1.getLinkedPorts().iterator().next().getPortId()); |
| 338 | } |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 339 | return; |
| 340 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 341 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 342 | log.debug("addOnosDevice: {}", onosDevice); |
| 343 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 344 | try { |
| 345 | IDeviceObject device = ope.searchDevice(macAddress); |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 346 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 347 | if (device == null) { |
| 348 | device = ope.newDevice(); |
| 349 | device.setType("device"); |
| 350 | device.setState("ACTIVE"); |
| 351 | device.setMACAddress(macAddress); |
| 352 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 353 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 354 | // 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] | 355 | if (onosDevice.getIpv4Address() != null) { |
| 356 | boolean hasIpAddress = false; |
| 357 | for (IIpv4Address ipv4Address : device.getIpv4Addresses()) { |
| 358 | if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) { |
| 359 | hasIpAddress = true; |
| 360 | break; |
| 361 | } |
| 362 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 363 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 364 | if (!hasIpAddress) { |
| 365 | IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue()); |
| 366 | IDeviceObject oldDevice = ipv4Address.getDevice(); |
| 367 | if (oldDevice != null && oldDevice.getMACAddress() != macAddress) { |
| 368 | oldDevice.removeIpv4Address(ipv4Address); |
| 369 | } |
| 370 | device.addIpv4Address(ipv4Address); |
| 371 | } |
| 372 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 373 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 374 | // Check if the device has the attachment point, add it if not |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 375 | // TODO single attachment point for now, extend to multiple later |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 376 | String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID()); |
| 377 | boolean hasAttachmentPoint = false; |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 378 | Iterator<IPortObject> it = device.getAttachedPorts().iterator(); |
| 379 | if (it.hasNext()) { |
| 380 | IPortObject existingPort = it.next(); |
| 381 | if (existingPort != null) { |
| 382 | ISwitchObject existingSwitch = existingPort.getSwitch(); |
| 383 | if (!existingSwitch.getDPID().equals(switchDpid) || |
| 384 | existingPort.getNumber() != onosDevice.getSwitchPort()) { |
| 385 | existingPort.removeDevice(device); |
| 386 | } |
| 387 | else { |
| 388 | hasAttachmentPoint = true; |
| 389 | } |
| 390 | } |
| 391 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 392 | |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 393 | /* |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 394 | for (IPortObject portObject : device.getAttachedPorts()) { |
| 395 | ISwitchObject switchObject = portObject.getSwitch(); |
| 396 | if (switchObject.getDPID().equals(switchDpid) |
| 397 | && portObject.getNumber() == onosDevice.getSwitchPort()) { |
| 398 | hasAttachmentPoint = true; |
| 399 | break; |
| 400 | } |
| 401 | } |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 402 | */ |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 403 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 404 | if (!hasAttachmentPoint) { |
| 405 | IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort()); |
Jonathan Hart | 18ad950 | 2013-12-15 18:28:00 -0800 | [diff] [blame] | 406 | if (portObject != null) { |
| 407 | portObject.setDevice(device); |
| 408 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 409 | } |
Yuta HIGUCHI | e85e115 | 2014-01-06 16:12:53 -0800 | [diff] [blame] | 410 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 411 | ope.commit(); |
| 412 | } |
| 413 | catch (TitanException e) { |
| 414 | log.error("addOnosDevice {} failed:", macAddress, e); |
| 415 | ope.rollback(); |
| 416 | } |
| 417 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 418 | |
| 419 | } |