Pankaj Berde | b603134 | 2013-02-19 18:51:51 -0800 | [diff] [blame^] | 1 | package net.floodlightcontroller.devicemanager.internal; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | import java.util.Set; |
| 6 | |
| 7 | import org.openflow.util.HexString; |
| 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
| 10 | |
| 11 | import com.google.common.collect.Lists; |
| 12 | import com.thinkaurelius.titan.core.TitanException; |
| 13 | import com.thinkaurelius.titan.core.TitanFactory; |
| 14 | import com.thinkaurelius.titan.core.TitanGraph; |
| 15 | import com.tinkerpop.blueprints.Vertex; |
| 16 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 17 | import com.tinkerpop.frames.FramedGraph; |
| 18 | |
| 19 | import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 20 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
| 21 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoSwitchService; |
| 22 | import net.floodlightcontroller.core.internal.SwitchStorageImpl; |
| 23 | import net.floodlightcontroller.devicemanager.IDevice; |
| 24 | import net.floodlightcontroller.devicemanager.IDeviceStorage; |
| 25 | import net.floodlightcontroller.devicemanager.SwitchPort; |
| 26 | |
| 27 | public class DeviceStorageImpl implements IDeviceStorage { |
| 28 | |
| 29 | public TitanGraph graph; |
| 30 | protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class); |
| 31 | public ITopoSwitchService svc; |
| 32 | |
| 33 | @Override |
| 34 | public void init(String conf) { |
| 35 | graph = TitanFactory.open(conf); |
| 36 | |
| 37 | // FIXME: Creation on Indexes should be done only once |
| 38 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 39 | if (!s.contains("dpid")) { |
| 40 | graph.createKeyIndex("dpid", Vertex.class); |
| 41 | graph.stopTransaction(Conclusion.SUCCESS); |
| 42 | } |
| 43 | if (!s.contains("type")) { |
| 44 | graph.createKeyIndex("type", Vertex.class); |
| 45 | graph.stopTransaction(Conclusion.SUCCESS); |
| 46 | } |
| 47 | if (!s.contains("dl_address")) { |
| 48 | graph.createKeyIndex("dl_address", Vertex.class); |
| 49 | graph.stopTransaction(Conclusion.SUCCESS); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void finalize() { |
| 54 | close(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void close() { |
| 59 | graph.shutdown(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public IDeviceObject addDevice(IDevice device) { |
| 64 | // TODO Auto-generated method stub |
| 65 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph);; |
| 66 | IDeviceObject obj = null; |
| 67 | SwitchPort[] attachmentPoints = device.getAttachmentPoints(); |
| 68 | List<IPortObject> attachedPorts; |
| 69 | try { |
| 70 | if (fg.getVertices("dl_address",device.getMACAddressString()).iterator().hasNext()) { |
| 71 | obj = fg.getVertices("dl_address",device.getMACAddressString(), |
| 72 | IDeviceObject.class).iterator().next(); |
| 73 | attachedPorts = Lists.newArrayList(obj.getAttachedPorts()); |
| 74 | |
| 75 | } else { |
| 76 | obj = fg.addVertex(null,IDeviceObject.class); |
| 77 | attachedPorts = new ArrayList<IPortObject>(); |
| 78 | } |
| 79 | for (SwitchPort ap : attachmentPoints) { |
| 80 | IPortObject port = svc.getPortOnSwitch(HexString.toHexString(ap.getSwitchDPID()), |
| 81 | (short) ap.getPort()); |
| 82 | if (attachedPorts.contains(port)) { |
| 83 | attachedPorts.remove(port); |
| 84 | } else { |
| 85 | obj.setHostPort(port); |
| 86 | } |
| 87 | } |
| 88 | for (IPortObject port: attachedPorts) { |
| 89 | obj.removeHostPort(port); |
| 90 | } |
| 91 | obj.setIPAddress(device.getIPv4Addresses().toString()); |
| 92 | obj.setMACAddress(device.getMACAddressString()); |
| 93 | obj.setType("device"); |
| 94 | obj.setState("ACTIVE"); |
| 95 | graph.stopTransaction(Conclusion.SUCCESS); |
| 96 | } catch (TitanException e) { |
| 97 | // TODO: handle exceptions |
| 98 | log.error(":addDevice mac:{} failed", device.getMACAddressString()); |
| 99 | } |
| 100 | |
| 101 | return obj; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public IDeviceObject updateDevice(IDevice device) { |
| 106 | // TODO Auto-generated method stub |
| 107 | return addDevice(device); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public IDeviceObject removeDevice(IDevice device) { |
| 112 | // TODO Auto-generated method stub |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public IDeviceObject getDeviceByMac(String mac) { |
| 118 | // TODO Auto-generated method stub |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public IDeviceObject getDeviceByIP(String ip) { |
| 124 | // TODO Auto-generated method stub |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | } |