blob: 73b0bdb9ef09a05b6248a6cc23e9a94fc68ca90e [file] [log] [blame]
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berdeb6031342013-02-19 18:51:51 -08002
Teru87cd2da2013-06-15 20:33:08 -07003import java.util.ArrayList;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07004import java.util.Arrays;
Jonathan Hart18ad9502013-12-15 18:28:00 -08005import java.util.Iterator;
Pankaj Berdeb6031342013-02-19 18:51:51 -08006import java.util.List;
Pankaj Berdeb6031342013-02-19 18:51:51 -08007
Pankaj Berdeb6031342013-02-19 18:51:51 -08008import net.floodlightcontroller.devicemanager.IDevice;
Pankaj Berdeb6031342013-02-19 18:51:51 -08009import net.floodlightcontroller.devicemanager.SwitchPort;
yoshi2fd4c7e2013-11-22 15:47:55 -080010import net.onrc.onos.graph.DBOperation;
11import net.onrc.onos.graph.GraphDBManager;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070012import net.onrc.onos.ofcontroller.core.IDeviceStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070013import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070014import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
17import net.onrc.onos.ofcontroller.devicemanager.OnosDevice;
Pankaj Berdeb6031342013-02-19 18:51:51 -080018
Pankaj Berdeb6031342013-02-19 18:51:51 -080019import org.openflow.util.HexString;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import com.google.common.collect.Lists;
Jonathan Hart1a6f1d62013-11-14 11:33:46 -080024import com.google.common.net.InetAddresses;
Pankaj Berdeb6031342013-02-19 18:51:51 -080025import com.thinkaurelius.titan.core.TitanException;
Teru87cd2da2013-06-15 20:33:08 -070026/**
27 * This is the class for storing the information of devices into CassandraDB
yoshi2fd4c7e2013-11-22 15:47:55 -080028 *
Teru87cd2da2013-06-15 20:33:08 -070029 * @author Pankaj
30 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080031public class DeviceStorageImpl implements IDeviceStorage {
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080032 protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080033
yoshi0fee3de2013-11-23 09:13:37 -080034 private DBOperation ope;
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080035 /***
36 * Initialize function. Before you use this class, please call this method
37 * @param conf configuration file for Cassandra DB
38 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080039 @Override
yoshi0fee3de2013-11-23 09:13:37 -080040 public void init(final String dbStore, final String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070041 try {
yoshib6a66eb2014-01-21 10:11:49 -080042 ope = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
yoshid38cd312013-12-02 19:54:44 -080043 //ope = GraphDBManager.getDBOperation(dbStore, conf);
yoshi0fee3de2013-11-23 09:13:37 -080044 } catch (Exception e) {
45 log.error(e.getMessage());
Teru87cd2da2013-06-15 20:33:08 -070046 }
yoshi0fee3de2013-11-23 09:13:37 -080047 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080048
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080049 /***
50 * Finalize/close function. After you use this class, please call this method.
51 * It will close the DB connection.
52 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080053 @Override
54 public void close() {
55 ope.close();
56 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080057
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080058 /***
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 Berdeb6031342013-02-19 18:51:51 -080062 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080063 protected void finalize() {
Pankaj Berdeb6031342013-02-19 18:51:51 -080064 close();
65 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080066
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080067 /***
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 Berdeb6031342013-02-19 18:51:51 -080072 @Override
73 public IDeviceObject addDevice(IDevice device) {
74 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080075 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 HIGUCHIe85e1152014-01-06 16:12:53 -080086
Naoki Shiotae2f4da72013-12-09 16:34:17 -080087 if (obj == null) {
88 return null;
89 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080090
Jonathan Hartba9ced92013-11-24 16:52:13 -080091 changeDeviceAttachments(device, obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080092
Jonathan Hartba9ced92013-11-24 16:52:13 -080093 changeDeviceIpv4Addresses(device, obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080094
Jonathan Hartba9ced92013-11-24 16:52:13 -080095 obj.setMACAddress(device.getMACAddressString());
96 obj.setType("device");
97 obj.setState("ACTIVE");
98 ope.commit();
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080099
Jonathan Hartba9ced92013-11-24 16:52:13 -0800100 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;
yoshi0fee3de2013-11-23 09:13:37 -0800106 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700107 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800108
109 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800110 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800111 /***
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 Berdeb6031342013-02-19 18:51:51 -0800116 @Override
117 public IDeviceObject updateDevice(IDevice device) {
118 return addDevice(device);
119 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800120
Teru87cd2da2013-06-15 20:33:08 -0700121 /***
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 Berdeb6031342013-02-19 18:51:51 -0800125 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800127 IDeviceObject dev;
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800128
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800129 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
130 removeDevice(dev);
131 }
132 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800133
134 @Override
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800135 public void removeDevice(IDeviceObject deviceObject) {
136 String deviceMac = deviceObject.getMACAddress();
yoshi2fd4c7e2013-11-22 15:47:55 -0800137
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800138 removeDeviceImpl(deviceObject);
yoshi2fd4c7e2013-11-22 15:47:55 -0800139
Pankaj Berdeda809572013-02-22 15:31:20 -0800140 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800141 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800142 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700143 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700144 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800145 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800146 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800147 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800148
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800149 public void removeDeviceImpl(IDeviceObject deviceObject) {
150 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
151 ope.removeIpv4Address(ipv4AddressVertex);
152 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800153
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800154 ope.removeDevice(deviceObject);
155 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800156 /***
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 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800165
Teru87cd2da2013-06-15 20:33:08 -0700166 /***
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 Berdeb6031342013-02-19 18:51:51 -0800171 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700172 public IDeviceObject getDeviceByIP(int ipv4Address) {
173 try {
174 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
175 if (ipv4AddressVertex != null) {
176 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700177 }
178 return null;
179 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700180 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700181 log.error("DeviceStorage:getDeviceByIP:{} failed");
182 return null;
183 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800184 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800185
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800186 /***
187 * This function is for changing the Device attachment point.
188 * @param device The device you want change the attachment point
189 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800190 @Override
191 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800192 IDeviceObject obj = null;
yoshi0fee3de2013-11-23 09:13:37 -0800193 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 Hartd6ed62b2013-11-01 13:18:25 -0700202 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700203 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 log.error(":addDevice mac:{} failed", device.getMACAddressString());
yoshi0fee3de2013-11-23 09:13:37 -0800205 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800206 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800207
Teru87cd2da2013-06-15 20:33:08 -0700208 /***
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 Berdeda809572013-02-22 15:31:20 -0800213 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
214 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
215 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Pankaj Berdeda809572013-02-22 15:31:20 -0800216
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700217 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800221 log.debug("New Switch Port is {},{}",
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700222 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
Teru87cd2da2013-06-15 20:33:08 -0700223
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700224 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800231 port.setDevice(obj);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700232 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800233
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800234 log.debug("port number is {}", port.getNumber());
235 log.debug("port desc is {}", port.getDesc());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700236 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800237 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800238
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700239 for (IPortObject port: attachedPorts) {
240 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
241 port.removeDevice(obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800242
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800243 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 Hartd6ed62b2013-11-01 13:18:25 -0700250 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800251 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800252
Pankaj Berdeb6031342013-02-19 18:51:51 -0800253 /***
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 Hartd6ed62b2013-11-01 13:18:25 -0700259 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
260 device.getIPv4Addresses());
Pankaj Berdeb6031342013-02-19 18:51:51 -0800261 IDeviceObject obj;
yoshi0fee3de2013-11-23 09:13:37 -0800262 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 Hartd6ed62b2013-11-01 13:18:25 -0700271 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800272 }
273 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800274
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800275 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800276 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800277 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800278 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
279 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800280 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800281 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800282
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800283 List<String> memIpv4Addresses = new ArrayList<String>();
284 for (int addr : device.getIPv4Addresses()) {
285 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
286 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800287
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800288 log.debug("Device IP addresses {}, database IP addresses {}",
289 memIpv4Addresses, dbIpv4Addresses);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800290
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800291 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800292 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
293 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800294 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800295
Jonathan Hartba9ced92013-11-24 16:52:13 -0800296 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800297 IDeviceObject oldDevice = dbIpv4Address.getDevice();
298 if (oldDevice != null) {
299 oldDevice.removeIpv4Address(dbIpv4Address);
300 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800301 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800302
303 log.debug("Adding IP address {}",
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800304 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800305 deviceObject.addIpv4Address(dbIpv4Address);
306 }
307 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800308
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800309 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
310 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
311 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800312 log.debug("Removing IP address {}",
313 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
314 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800315 deviceObject.removeIpv4Address(dbIpv4Address);
316 }
317 }
318 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800319
Jonathan Hartd857ad62013-12-14 18:08:17 -0800320 /**
321 * Takes an {@link OnosDevice} and adds it into the database. There is no
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800322 * checking of the database data and removing old data - an
Jonathan Hartd857ad62013-12-14 18:08:17 -0800323 * {@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 Hartd857ad62013-12-14 18:08:17 -0800328 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800329
pingping-lin00926032013-12-18 12:13:08 +0800330 //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 Radoslavov78073282013-12-19 17:01:23 -0800334 if ((portObject1 != null) && portObject1.getLinkedPorts().iterator().hasNext()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800335 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-lin00926032013-12-18 12:13:08 +0800339 return;
340 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800341
pingping-lin00926032013-12-18 12:13:08 +0800342 log.debug("addOnosDevice: {}", onosDevice);
343
Jonathan Hartd857ad62013-12-14 18:08:17 -0800344 try {
345 IDeviceObject device = ope.searchDevice(macAddress);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800346
Jonathan Hartd857ad62013-12-14 18:08:17 -0800347 if (device == null) {
348 device = ope.newDevice();
349 device.setType("device");
350 device.setState("ACTIVE");
351 device.setMACAddress(macAddress);
352 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800353
Jonathan Hartd857ad62013-12-14 18:08:17 -0800354 // Check if the device has the IP address, add it if it doesn't
Jonathan Hartd857ad62013-12-14 18:08:17 -0800355 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800363
Jonathan Hartd857ad62013-12-14 18:08:17 -0800364 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800373
Jonathan Hartd857ad62013-12-14 18:08:17 -0800374 // Check if the device has the attachment point, add it if not
Jonathan Hart18ad9502013-12-15 18:28:00 -0800375 // TODO single attachment point for now, extend to multiple later
Jonathan Hartd857ad62013-12-14 18:08:17 -0800376 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
377 boolean hasAttachmentPoint = false;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800378 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800392
Jonathan Hart18ad9502013-12-15 18:28:00 -0800393 /*
Jonathan Hartd857ad62013-12-14 18:08:17 -0800394 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 Hart18ad9502013-12-15 18:28:00 -0800402 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800403
Jonathan Hartd857ad62013-12-14 18:08:17 -0800404 if (!hasAttachmentPoint) {
405 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800406 if (portObject != null) {
407 portObject.setDevice(device);
408 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800409 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800410
Jonathan Hartd857ad62013-12-14 18:08:17 -0800411 ope.commit();
412 }
413 catch (TitanException e) {
414 log.error("addOnosDevice {} failed:", macAddress, e);
415 ope.rollback();
416 }
417 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800418
419}