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