blob: 0699491f95da15ac5194b36c7b4e1d77e43905fc [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
28 * @author Pankaj
29 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080030public class DeviceStorageImpl implements IDeviceStorage {
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080031 protected final static Logger log = LoggerFactory.getLogger(DeviceStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080032
yoshi0fee3de2013-11-23 09:13:37 -080033 private DBOperation ope;
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080034 /***
35 * Initialize function. Before you use this class, please call this method
36 * @param conf configuration file for Cassandra DB
37 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080038 @Override
yoshi0fee3de2013-11-23 09:13:37 -080039 public void init(final String dbStore, final String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070040 try {
Yoshi Muroi5804ce92014-02-08 03:58:04 -080041 ope = GraphDBManager.getDBOperation();
yoshi0fee3de2013-11-23 09:13:37 -080042 } catch (Exception e) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070043 log.error("Couldn't open graph operation", e);
Teru87cd2da2013-06-15 20:33:08 -070044 }
yoshi0fee3de2013-11-23 09:13:37 -080045 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080046
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080047 /***
48 * Finalize/close function. After you use this class, please call this method.
49 * It will close the DB connection.
50 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080051 @Override
52 public void close() {
53 ope.close();
54 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080055
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080056 /***
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 Berdeb6031342013-02-19 18:51:51 -080060 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080061 protected void finalize() {
Pankaj Berdeb6031342013-02-19 18:51:51 -080062 close();
63 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080064
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080065 /***
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 Berdeb6031342013-02-19 18:51:51 -080070 @Override
71 public IDeviceObject addDevice(IDevice device) {
72 IDeviceObject obj = null;
Jonathan Hartba9ced92013-11-24 16:52:13 -080073 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 HIGUCHIe85e1152014-01-06 16:12:53 -080084
Naoki Shiotae2f4da72013-12-09 16:34:17 -080085 if (obj == null) {
86 return null;
87 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080088
Jonathan Hartba9ced92013-11-24 16:52:13 -080089 changeDeviceAttachments(device, obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080090
Jonathan Hartba9ced92013-11-24 16:52:13 -080091 changeDeviceIpv4Addresses(device, obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080092
Jonathan Hartba9ced92013-11-24 16:52:13 -080093 obj.setMACAddress(device.getMACAddressString());
94 obj.setType("device");
95 obj.setState("ACTIVE");
96 ope.commit();
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -080097
Jonathan Hartba9ced92013-11-24 16:52:13 -080098 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;
yoshi0fee3de2013-11-23 09:13:37 -0800104 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700105 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800106
107 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800108 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800109 /***
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 Berdeb6031342013-02-19 18:51:51 -0800114 @Override
115 public IDeviceObject updateDevice(IDevice device) {
116 return addDevice(device);
117 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800118
Teru87cd2da2013-06-15 20:33:08 -0700119 /***
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 Berdeb6031342013-02-19 18:51:51 -0800123 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800124 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800125 IDeviceObject dev;
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800126
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800127 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
128 removeDevice(dev);
129 }
130 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800131
132 @Override
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800133 public void removeDevice(IDeviceObject deviceObject) {
134 String deviceMac = deviceObject.getMACAddress();
yoshi2fd4c7e2013-11-22 15:47:55 -0800135
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800136 removeDeviceImpl(deviceObject);
yoshi2fd4c7e2013-11-22 15:47:55 -0800137
Pankaj Berdeda809572013-02-22 15:31:20 -0800138 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800139 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800140 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700141 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700142 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800143 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800144 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800145 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800146
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800147 public void removeDeviceImpl(IDeviceObject deviceObject) {
148 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
149 ope.removeIpv4Address(ipv4AddressVertex);
150 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800151
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800152 ope.removeDevice(deviceObject);
153 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800154 /***
155 * This function is for getting the Device from the DB by Mac address of the device.
156 * @param mac The device mac address you want to get from the DB.
157 * @return IDeviceObject you want to get.
158 */
159 @Override
160 public IDeviceObject getDeviceByMac(String mac) {
161 return ope.searchDevice(mac);
162 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800163
Teru87cd2da2013-06-15 20:33:08 -0700164 /***
165 * This function is for getting the Device from the DB by IP address of the device.
166 * @param ip The device ip address you want to get from the DB.
167 * @return IDeviceObject you want to get.
168 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800169 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700170 public IDeviceObject getDeviceByIP(int ipv4Address) {
171 try {
172 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
173 if (ipv4AddressVertex != null) {
174 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700175 }
176 return null;
177 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700178 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700179 log.error("DeviceStorage:getDeviceByIP:{} failed");
180 return null;
181 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800182 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800183
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800184 /***
185 * This function is for changing the Device attachment point.
186 * @param device The device you want change the attachment point
187 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800188 @Override
189 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800190 IDeviceObject obj = null;
yoshi0fee3de2013-11-23 09:13:37 -0800191 try {
192 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
193 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
194 changeDeviceAttachments(device, obj);
195 ope.commit();
196 } else {
197 log.debug("failed to search device...now adding {}", device.getMACAddressString());
198 addDevice(device);
199 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700200 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700201 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800202 log.error(":addDevice mac:{} failed", device.getMACAddressString());
yoshi0fee3de2013-11-23 09:13:37 -0800203 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800205
Teru87cd2da2013-06-15 20:33:08 -0700206 /***
207 * This function is for changing the Device attachment point.
208 * @param device The new device you want change the attachment point
209 * @param obj The old device IDeviceObject that is going to change the attachment point.
210 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800211 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
212 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
213 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Pankaj Berdeda809572013-02-22 15:31:20 -0800214
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700215 for (SwitchPort ap : attachmentPoints) {
216 //Check if there is the port
217 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
218 (short) ap.getPort());
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800219 log.debug("New Switch Port is {},{}",
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700220 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
Teru87cd2da2013-06-15 20:33:08 -0700221
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700222 if (port != null){
223 if (attachedPorts.contains(port)) {
224 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
225 //This port will be remained, so remove from the removed port lists.
226 attachedPorts.remove(port);
227 } else {
228 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800229 port.setDevice(obj);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700230 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800231
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800232 log.debug("port number is {}", port.getNumber());
233 log.debug("port desc is {}", port.getDesc());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700234 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800235 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800236
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700237 for (IPortObject port: attachedPorts) {
238 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
239 port.removeDevice(obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800240
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800241 if (!obj.getAttachedPorts().iterator().hasNext()) {
242 // XXX If there are no more ports attached to the device,
243 // delete it. Otherwise we have a situation where the
244 // device remains forever with an IP address attached.
245 // When we implement device probing we should get rid of this.
246 removeDevice(obj);
247 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700248 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800249 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800250
Pankaj Berdeb6031342013-02-19 18:51:51 -0800251 /***
252 * This function is for changing the Device IPv4 address.
253 * @param device The new device you want change the ipaddress
254 */
255 @Override
256 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700257 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
258 device.getIPv4Addresses());
Pankaj Berdeb6031342013-02-19 18:51:51 -0800259 IDeviceObject obj;
yoshi0fee3de2013-11-23 09:13:37 -0800260 try {
261 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
262 changeDeviceIpv4Addresses(device, obj);
263 ope.commit();
264 } else {
265 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
266 }
267 } catch (TitanException e) {
268 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700269 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800270 }
271 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800272
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800273 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800274 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800275 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800276 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
277 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800278 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800279 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800280
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800281 List<String> memIpv4Addresses = new ArrayList<String>();
282 for (int addr : device.getIPv4Addresses()) {
283 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
284 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800285
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800286 log.debug("Device IP addresses {}, database IP addresses {}",
287 memIpv4Addresses, dbIpv4Addresses);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800288
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800289 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800290 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
291 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800292 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800293
Jonathan Hartba9ced92013-11-24 16:52:13 -0800294 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800295 IDeviceObject oldDevice = dbIpv4Address.getDevice();
296 if (oldDevice != null) {
297 oldDevice.removeIpv4Address(dbIpv4Address);
298 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800299 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800300
301 log.debug("Adding IP address {}",
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800302 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800303 deviceObject.addIpv4Address(dbIpv4Address);
304 }
305 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800306
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800307 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
308 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
309 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800310 log.debug("Removing IP address {}",
311 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
312 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800313 deviceObject.removeIpv4Address(dbIpv4Address);
314 }
315 }
316 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800317
Jonathan Hartd857ad62013-12-14 18:08:17 -0800318 /**
319 * Takes an {@link OnosDevice} and adds it into the database. There is no
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800320 * checking of the database data and removing old data - an
Jonathan Hartd857ad62013-12-14 18:08:17 -0800321 * {@link OnosDevice} basically corresponds to a packet we've just seen,
322 * and we need to add that information into the database.
323 */
324 @Override
325 public void addOnosDevice(OnosDevice onosDevice) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800326 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800327
pingping-lin00926032013-12-18 12:13:08 +0800328 //if the switch port we try to attach a new device already has a link, then stop adding device
329 IPortObject portObject1 = ope.searchPort(HexString.toHexString(
330 onosDevice.getSwitchDPID()), onosDevice.getSwitchPort());
331
Pavlin Radoslavov78073282013-12-19 17:01:23 -0800332 if ((portObject1 != null) && portObject1.getLinkedPorts().iterator().hasNext()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800333 if (log.isDebugEnabled()) {
334 log.debug("stop adding OnosDevice: {} due to there is a link to: {}",
335 onosDevice, portObject1.getLinkedPorts().iterator().next().getPortId());
336 }
pingping-lin00926032013-12-18 12:13:08 +0800337 return;
338 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800339
pingping-lin00926032013-12-18 12:13:08 +0800340 log.debug("addOnosDevice: {}", onosDevice);
341
Jonathan Hartd857ad62013-12-14 18:08:17 -0800342 try {
343 IDeviceObject device = ope.searchDevice(macAddress);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800344
Jonathan Hartd857ad62013-12-14 18:08:17 -0800345 if (device == null) {
346 device = ope.newDevice();
347 device.setType("device");
348 device.setState("ACTIVE");
349 device.setMACAddress(macAddress);
350 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800351
Jonathan Hartd857ad62013-12-14 18:08:17 -0800352 // Check if the device has the IP address, add it if it doesn't
Jonathan Hartd857ad62013-12-14 18:08:17 -0800353 if (onosDevice.getIpv4Address() != null) {
354 boolean hasIpAddress = false;
355 for (IIpv4Address ipv4Address : device.getIpv4Addresses()) {
356 if (ipv4Address.getIpv4Address() == onosDevice.getIpv4Address().intValue()) {
357 hasIpAddress = true;
358 break;
359 }
360 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800361
Jonathan Hartd857ad62013-12-14 18:08:17 -0800362 if (!hasIpAddress) {
363 IIpv4Address ipv4Address = ope.ensureIpv4Address(onosDevice.getIpv4Address().intValue());
364 IDeviceObject oldDevice = ipv4Address.getDevice();
365 if (oldDevice != null && oldDevice.getMACAddress() != macAddress) {
366 oldDevice.removeIpv4Address(ipv4Address);
367 }
368 device.addIpv4Address(ipv4Address);
369 }
370 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800371
Jonathan Hartd857ad62013-12-14 18:08:17 -0800372 // Check if the device has the attachment point, add it if not
Jonathan Hart18ad9502013-12-15 18:28:00 -0800373 // TODO single attachment point for now, extend to multiple later
Jonathan Hartd857ad62013-12-14 18:08:17 -0800374 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
375 boolean hasAttachmentPoint = false;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800376 Iterator<IPortObject> it = device.getAttachedPorts().iterator();
377 if (it.hasNext()) {
378 IPortObject existingPort = it.next();
379 if (existingPort != null) {
380 ISwitchObject existingSwitch = existingPort.getSwitch();
381 if (!existingSwitch.getDPID().equals(switchDpid) ||
382 existingPort.getNumber() != onosDevice.getSwitchPort()) {
383 existingPort.removeDevice(device);
384 }
385 else {
386 hasAttachmentPoint = true;
387 }
388 }
389 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800390
Jonathan Hart18ad9502013-12-15 18:28:00 -0800391 /*
Jonathan Hartd857ad62013-12-14 18:08:17 -0800392 for (IPortObject portObject : device.getAttachedPorts()) {
393 ISwitchObject switchObject = portObject.getSwitch();
394 if (switchObject.getDPID().equals(switchDpid)
395 && portObject.getNumber() == onosDevice.getSwitchPort()) {
396 hasAttachmentPoint = true;
397 break;
398 }
399 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800400 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800401
Jonathan Hartd857ad62013-12-14 18:08:17 -0800402 if (!hasAttachmentPoint) {
403 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800404 if (portObject != null) {
405 portObject.setDevice(device);
406 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800407 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800408
Jonathan Hartd857ad62013-12-14 18:08:17 -0800409 ope.commit();
410 }
411 catch (TitanException e) {
412 log.error("addOnosDevice {} failed:", macAddress, e);
413 ope.rollback();
414 }
415 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800416
417}