refactor DeviceStorageImpl class to use GraphDBOperation class.
diff --git a/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceStorageImpl.java b/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceStorageImpl.java
index e964b8e..748d4ab 100644
--- a/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceStorageImpl.java
+++ b/src/main/java/net/floodlightcontroller/devicemanager/internal/DeviceStorageImpl.java
@@ -14,16 +14,16 @@
import net.floodlightcontroller.devicemanager.IDeviceStorage;
import net.floodlightcontroller.devicemanager.SwitchPort;
import net.onrc.onos.util.GraphDBConnection;
-import net.onrc.onos.util.GraphDBConnection.Transaction;
+import net.onrc.onos.util.GraphDBOperation;
public class DeviceStorageImpl implements IDeviceStorage {
- public GraphDBConnection conn;
+ public GraphDBOperation op;
protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
@Override
public void init(String conf) {
- conn = GraphDBConnection.getInstance(conf);
+ op = new GraphDBOperation(GraphDBConnection.getInstance(conf));
}
public void finalize() {
@@ -32,7 +32,7 @@
@Override
public void close() {
- conn.close();
+ op.close();
}
@Override
@@ -40,24 +40,23 @@
// TODO Auto-generated method stub
IDeviceObject obj = null;
try {
- if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) {
+ if ((obj = op.searchDevice(device.getMACAddressString())) != null) {
log.debug("Adding device {}: found existing device",device.getMACAddressString());
} else {
- obj = conn.utils().newDevice(conn);
+ obj = op.newDevice();
log.debug("Adding device {}: creating new device",device.getMACAddressString());
}
changeDeviceAttachments(device, obj);
obj.setIPAddress(device.getIPv4Addresses().toString());
obj.setMACAddress(device.getMACAddressString());
- obj.setType("device");
obj.setState("ACTIVE");
- conn.endTx(Transaction.COMMIT);
+ op.commit();
log.debug("Adding device {}",device.getMACAddressString());
} catch (Exception e) {
// TODO: handle exceptions
- conn.endTx(Transaction.ROLLBACK);
+ op.rollback();
log.error(":addDevice mac:{} failed", device.getMACAddressString());
}
@@ -74,21 +73,21 @@
// TODO Auto-generated method stub
IDeviceObject dev;
try {
- if ((dev = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) {
- conn.utils().removeDevice(conn, dev);
- conn.endTx(Transaction.COMMIT);
+ if ((dev = op.searchDevice(device.getMACAddressString())) != null) {
+ op.removeDevice(dev);
+ op.commit();
log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
}
} catch (Exception e) {
// TODO: handle exceptions
- conn.endTx(Transaction.ROLLBACK);
+ op.rollback();
log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
}
}
@Override
public IDeviceObject getDeviceByMac(String mac) {
- return conn.utils().searchDevice(conn, mac);
+ return op.searchDevice(mac);
}
@Override
@@ -102,17 +101,17 @@
// TODO Auto-generated method stub
IDeviceObject obj = null;
try {
- if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) {
+ if ((obj = op.searchDevice(device.getMACAddressString())) != null) {
log.debug("Changing device ports {}: found existing device",device.getMACAddressString());
changeDeviceAttachments(device, obj);
- conn.endTx(Transaction.COMMIT);
+ op.commit();
} else {
log.debug("failed to search device...now adding {}",device.getMACAddressString());
addDevice(device);
}
} catch (Exception e) {
// TODO: handle exceptions
- conn.endTx(Transaction.ROLLBACK);
+ op.rollback();
log.error(":addDevice mac:{} failed", device.getMACAddressString());
}
}
@@ -122,9 +121,9 @@
List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
for (SwitchPort ap : attachmentPoints) {
- IPortObject port = conn.utils().searchPort(conn,
- HexString.toHexString(ap.getSwitchDPID()),
- (short) ap.getPort());
+ IPortObject port = op.searchPort(
+ HexString.toHexString(ap.getSwitchDPID()),
+ (short) ap.getPort());
if (attachedPorts.contains(port)) {
attachedPorts.remove(port);
} else {
@@ -144,15 +143,15 @@
// TODO Auto-generated method stub
IDeviceObject obj;
try {
- if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) {
+ if ((obj = op.searchDevice(device.getMACAddressString())) != null) {
obj.setIPAddress(device.getIPv4Addresses().toString());
- conn.endTx(Transaction.COMMIT);
+ op.commit();
} else {
log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
}
} catch (TitanException e) {
// TODO: handle exceptions
- conn.endTx(Transaction.ROLLBACK);
+ op.rollback();
log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e);
}
}