blob: f794a2b9dff89652351d1b99337f03543a5bcc78 [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) {
TeruU80ce5062014-03-03 17:16:13 -0800125 //Not used
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 IDeviceObject dev;
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800127
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800128 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
129 removeDevice(dev);
130 }
131 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800132
133 @Override
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800134 public void removeDevice(IDeviceObject deviceObject) {
135 String deviceMac = deviceObject.getMACAddress();
yoshi2fd4c7e2013-11-22 15:47:55 -0800136
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800137 removeDeviceImpl(deviceObject);
yoshi2fd4c7e2013-11-22 15:47:55 -0800138
Pankaj Berdeda809572013-02-22 15:31:20 -0800139 try {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800140 ope.commit();
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800141 log.debug("DeviceStorage:removeDevice mac:{} done", deviceMac);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700142 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700143 ope.rollback();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800144 log.error("DeviceStorage:removeDevice mac:{} failed", deviceMac);
Pankaj Berdeda809572013-02-22 15:31:20 -0800145 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800146 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800147
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800148 public void removeDeviceImpl(IDeviceObject deviceObject) {
149 for (IIpv4Address ipv4AddressVertex : deviceObject.getIpv4Addresses()) {
150 ope.removeIpv4Address(ipv4AddressVertex);
151 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800152
Jonathan Hart4cfd1932013-11-19 16:42:25 -0800153 ope.removeDevice(deviceObject);
154 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800155 /***
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 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800164
TeruU6464af02014-02-06 21:38:45 -0800165
Teru87cd2da2013-06-15 20:33:08 -0700166 /***
TeruUd1ba0e22014-02-10 11:44:15 -0800167 * 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 /***
Teru87cd2da2013-06-15 20:33:08 -0700181 * 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 Berdeb6031342013-02-19 18:51:51 -0800185 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700186 public IDeviceObject getDeviceByIP(int ipv4Address) {
187 try {
188 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
189 if (ipv4AddressVertex != null) {
190 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700191 }
192 return null;
193 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700194 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700195 log.error("DeviceStorage:getDeviceByIP:{} failed");
196 return null;
197 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800198 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800199
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800200 /***
201 * This function is for changing the Device attachment point.
202 * @param device The device you want change the attachment point
203 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 @Override
205 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800206 IDeviceObject obj = null;
yoshi0fee3de2013-11-23 09:13:37 -0800207 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 Hartd6ed62b2013-11-01 13:18:25 -0700216 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700217 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800218 log.error(":addDevice mac:{} failed", device.getMACAddressString());
yoshi0fee3de2013-11-23 09:13:37 -0800219 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800220 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800221
Teru87cd2da2013-06-15 20:33:08 -0700222 /***
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 Berdeda809572013-02-22 15:31:20 -0800227 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
228 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
229 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Pankaj Berdeda809572013-02-22 15:31:20 -0800230
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700231 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800235 log.debug("New Switch Port is {},{}",
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700236 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
Teru87cd2da2013-06-15 20:33:08 -0700237
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700238 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800245 port.setDevice(obj);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700246 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800247
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800248 log.debug("port number is {}", port.getNumber());
249 log.debug("port desc is {}", port.getDesc());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700250 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800251 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800252
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700253 for (IPortObject port: attachedPorts) {
254 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
255 port.removeDevice(obj);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800256
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800257 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 Hartd6ed62b2013-11-01 13:18:25 -0700264 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800265 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800266
Pankaj Berdeb6031342013-02-19 18:51:51 -0800267 /***
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 Hartd6ed62b2013-11-01 13:18:25 -0700273 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
274 device.getIPv4Addresses());
Pankaj Berdeb6031342013-02-19 18:51:51 -0800275 IDeviceObject obj;
yoshi0fee3de2013-11-23 09:13:37 -0800276 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 Hartd6ed62b2013-11-01 13:18:25 -0700285 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800286 }
287 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800288
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800289 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800290 List<String> dbIpv4Addresses = new ArrayList<String>();
Jonathan Hartba9ced92013-11-24 16:52:13 -0800291 List<Integer> intDbIpv4Addresses = new ArrayList<Integer>();
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800292 for (IIpv4Address ipv4Vertex : deviceObject.getIpv4Addresses()) {
293 dbIpv4Addresses.add(InetAddresses.fromInteger(ipv4Vertex.getIpv4Address()).getHostAddress());
Jonathan Hartba9ced92013-11-24 16:52:13 -0800294 intDbIpv4Addresses.add(ipv4Vertex.getIpv4Address());
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800295 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800296
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800297 List<String> memIpv4Addresses = new ArrayList<String>();
298 for (int addr : device.getIPv4Addresses()) {
299 memIpv4Addresses.add(InetAddresses.fromInteger(addr).getHostAddress());
300 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800301
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800302 log.debug("Device IP addresses {}, database IP addresses {}",
303 memIpv4Addresses, dbIpv4Addresses);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800304
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800305 for (int ipv4Address : device.getIPv4Addresses()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800306 //if (deviceObject.getIpv4Address(ipv4Address) == null) {
307 if (!intDbIpv4Addresses.contains(ipv4Address)) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800308 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800309
Jonathan Hartba9ced92013-11-24 16:52:13 -0800310 /*
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800311 IDeviceObject oldDevice = dbIpv4Address.getDevice();
312 if (oldDevice != null) {
313 oldDevice.removeIpv4Address(dbIpv4Address);
314 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800315 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800316
317 log.debug("Adding IP address {}",
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800318 InetAddresses.fromInteger(ipv4Address).getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800319 deviceObject.addIpv4Address(dbIpv4Address);
320 }
321 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800322
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800323 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
324 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
325 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
Jonathan Hart1a6f1d62013-11-14 11:33:46 -0800326 log.debug("Removing IP address {}",
327 InetAddresses.fromInteger(dbIpv4Address.getIpv4Address())
328 .getHostAddress());
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800329 deviceObject.removeIpv4Address(dbIpv4Address);
330 }
331 }
332 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800333
Jonathan Hartd857ad62013-12-14 18:08:17 -0800334 /**
335 * Takes an {@link OnosDevice} and adds it into the database. There is no
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800336 * checking of the database data and removing old data - an
Jonathan Hartd857ad62013-12-14 18:08:17 -0800337 * {@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 Hartd857ad62013-12-14 18:08:17 -0800342 String macAddress = HexString.toHexString(onosDevice.getMacAddress().toBytes());
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800343
pingping-lin00926032013-12-18 12:13:08 +0800344 //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 Radoslavov78073282013-12-19 17:01:23 -0800348 if ((portObject1 != null) && portObject1.getLinkedPorts().iterator().hasNext()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800349 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-lin00926032013-12-18 12:13:08 +0800353 return;
354 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800355
pingping-lin00926032013-12-18 12:13:08 +0800356 log.debug("addOnosDevice: {}", onosDevice);
357
Jonathan Hartd857ad62013-12-14 18:08:17 -0800358 try {
359 IDeviceObject device = ope.searchDevice(macAddress);
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800360
Jonathan Hartd857ad62013-12-14 18:08:17 -0800361 if (device == null) {
362 device = ope.newDevice();
363 device.setType("device");
364 device.setState("ACTIVE");
365 device.setMACAddress(macAddress);
366 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800367
Jonathan Hartd857ad62013-12-14 18:08:17 -0800368 // Check if the device has the IP address, add it if it doesn't
Jonathan Hartd857ad62013-12-14 18:08:17 -0800369 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800377
Jonathan Hartd857ad62013-12-14 18:08:17 -0800378 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800387
Jonathan Hartd857ad62013-12-14 18:08:17 -0800388 // Check if the device has the attachment point, add it if not
Jonathan Hart18ad9502013-12-15 18:28:00 -0800389 // TODO single attachment point for now, extend to multiple later
Jonathan Hartd857ad62013-12-14 18:08:17 -0800390 String switchDpid = HexString.toHexString(onosDevice.getSwitchDPID());
391 boolean hasAttachmentPoint = false;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800392 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 HIGUCHIe85e1152014-01-06 16:12:53 -0800406
Jonathan Hart18ad9502013-12-15 18:28:00 -0800407 /*
Jonathan Hartd857ad62013-12-14 18:08:17 -0800408 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 Hart18ad9502013-12-15 18:28:00 -0800416 */
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800417
Jonathan Hartd857ad62013-12-14 18:08:17 -0800418 if (!hasAttachmentPoint) {
419 IPortObject portObject = ope.searchPort(switchDpid, onosDevice.getSwitchPort());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800420 if (portObject != null) {
421 portObject.setDevice(device);
422 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800423 }
Yuta HIGUCHIe85e1152014-01-06 16:12:53 -0800424
Jonathan Hartd857ad62013-12-14 18:08:17 -0800425 ope.commit();
426 }
427 catch (TitanException e) {
428 log.error("addOnosDevice {} failed:", macAddress, e);
429 ope.rollback();
430 }
431 }
TeruU80ce5062014-03-03 17:16:13 -0800432
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 Berdeb6031342013-02-19 18:51:51 -0800448
449}