Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.devicemanager.internal; |
| 2 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 3 | import java.util.List; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 4 | import org.openflow.util.HexString; |
| 5 | import org.slf4j.Logger; |
| 6 | import org.slf4j.LoggerFactory; |
| 7 | |
| 8 | import com.google.common.collect.Lists; |
| 9 | import com.thinkaurelius.titan.core.TitanException; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 10 | import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 11 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.internal.SwitchStorageImpl; |
| 13 | import net.floodlightcontroller.devicemanager.IDevice; |
| 14 | import net.floodlightcontroller.devicemanager.IDeviceStorage; |
| 15 | import net.floodlightcontroller.devicemanager.SwitchPort; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 16 | import net.onrc.onos.util.GraphDBConnection; |
| 17 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 18 | |
| 19 | public class DeviceStorageImpl implements IDeviceStorage { |
| 20 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 21 | public GraphDBConnection conn; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 22 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 23 | |
| 24 | @Override |
| 25 | public void init(String conf) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 26 | conn = GraphDBConnection.getInstance(conf); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | public void finalize() { |
| 30 | close(); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public void close() { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 35 | conn.close(); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public IDeviceObject addDevice(IDevice device) { |
| 40 | // TODO Auto-generated method stub |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 41 | IDeviceObject obj = null; |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 42 | try { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 43 | if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) { |
| 44 | log.debug("Adding device {}: found existing device",device.getMACAddressString()); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 45 | } else { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 46 | obj = conn.utils().newDevice(conn); |
| 47 | log.debug("Adding device {}: creating new device",device.getMACAddressString()); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 48 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 49 | changeDeviceAttachments(device, obj); |
| 50 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 51 | obj.setIPAddress(device.getIPv4Addresses().toString()); |
| 52 | obj.setMACAddress(device.getMACAddressString()); |
| 53 | obj.setType("device"); |
| 54 | obj.setState("ACTIVE"); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 55 | conn.endTx(Transaction.COMMIT); |
| 56 | |
| 57 | log.debug("Adding device {}",device.getMACAddressString()); |
| 58 | } catch (Exception e) { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 59 | // TODO: handle exceptions |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 60 | conn.endTx(Transaction.ROLLBACK); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 61 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
| 62 | } |
| 63 | |
| 64 | return obj; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public IDeviceObject updateDevice(IDevice device) { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 69 | return addDevice(device); |
| 70 | } |
| 71 | |
| 72 | @Override |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 73 | public void removeDevice(IDevice device) { |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 74 | // TODO Auto-generated method stub |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 75 | IDeviceObject dev; |
| 76 | try { |
| 77 | if ((dev = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) { |
| 78 | conn.utils().removeDevice(conn, dev); |
| 79 | conn.endTx(Transaction.COMMIT); |
| 80 | log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString()); |
| 81 | } |
| 82 | } catch (Exception e) { |
| 83 | // TODO: handle exceptions |
| 84 | conn.endTx(Transaction.ROLLBACK); |
| 85 | log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString()); |
| 86 | } |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public IDeviceObject getDeviceByMac(String mac) { |
Pankaj Berde | ac1a8c3 | 2013-02-26 17:45:57 -0800 | [diff] [blame^] | 91 | return conn.utils().searchDevice(conn, mac); |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public IDeviceObject getDeviceByIP(String ip) { |
| 96 | // TODO Auto-generated method stub |
| 97 | return null; |
| 98 | } |
| 99 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 100 | @Override |
| 101 | public void changeDeviceAttachments(IDevice device) { |
| 102 | // TODO Auto-generated method stub |
| 103 | IDeviceObject obj = null; |
| 104 | try { |
| 105 | if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) { |
| 106 | log.debug("Changing device ports {}: found existing device",device.getMACAddressString()); |
| 107 | changeDeviceAttachments(device, obj); |
| 108 | conn.endTx(Transaction.COMMIT); |
| 109 | } else { |
| 110 | log.debug("failed to search device...now adding {}",device.getMACAddressString()); |
| 111 | addDevice(device); |
| 112 | } |
| 113 | } catch (Exception e) { |
| 114 | // TODO: handle exceptions |
| 115 | conn.endTx(Transaction.ROLLBACK); |
| 116 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public void changeDeviceAttachments(IDevice device, IDeviceObject obj) { |
| 121 | SwitchPort[] attachmentPoints = device.getAttachmentPoints(); |
| 122 | List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts()); |
| 123 | |
| 124 | for (SwitchPort ap : attachmentPoints) { |
| 125 | IPortObject port = conn.utils().searchPort(conn, |
| 126 | HexString.toHexString(ap.getSwitchDPID()), |
| 127 | (short) ap.getPort()); |
| 128 | if (attachedPorts.contains(port)) { |
| 129 | attachedPorts.remove(port); |
| 130 | } else { |
| 131 | log.debug("Adding device {}: attaching to port",device.getMACAddressString()); |
| 132 | port.setDevice(obj); |
| 133 | //obj.setHostPort(port); |
| 134 | } |
| 135 | } |
| 136 | for (IPortObject port: attachedPorts) { |
| 137 | port.removeDevice(obj); |
| 138 | // obj.removeHostPort(port); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void changeDeviceIPv4Address(IDevice device) { |
| 144 | // TODO Auto-generated method stub |
| 145 | IDeviceObject obj; |
| 146 | try { |
| 147 | if ((obj = conn.utils().searchDevice(conn, device.getMACAddressString())) != null) { |
| 148 | obj.setIPAddress(device.getIPv4Addresses().toString()); |
| 149 | conn.endTx(Transaction.COMMIT); |
| 150 | } else { |
| 151 | log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString()); |
| 152 | } |
| 153 | } catch (TitanException e) { |
| 154 | // TODO: handle exceptions |
| 155 | conn.endTx(Transaction.ROLLBACK); |
| 156 | log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e); |
| 157 | } |
| 158 | } |
| 159 | |
Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame] | 160 | } |