Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.linkdiscovery.internal; |
| 2 | |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 3 | import java.util.List; |
| 4 | import java.util.Set; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 5 | |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.linkdiscovery.ILinkStorage; |
| 7 | import net.floodlightcontroller.linkdiscovery.LinkInfo; |
| 8 | import net.floodlightcontroller.routing.Link; |
| 9 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 10 | import org.openflow.util.HexString; |
| 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 14 | import com.thinkaurelius.titan.core.TitanException; |
| 15 | import com.thinkaurelius.titan.core.TitanFactory; |
| 16 | import com.thinkaurelius.titan.core.TitanGraph; |
| 17 | import com.tinkerpop.blueprints.Direction; |
| 18 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 19 | import com.tinkerpop.blueprints.Vertex; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 20 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 21 | |
| 22 | public class LinkStorageImpl implements ILinkStorage { |
| 23 | public TitanGraph graph; |
| 24 | protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class); |
| 25 | |
| 26 | @Override |
| 27 | public void update(Link link, DM_OPERATION op) { |
| 28 | update (link, (LinkInfo)null, op); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public void update(List<Link> links, DM_OPERATION op) { |
| 33 | log.debug("LinkStorage:update(): {} {}", op, links); |
| 34 | |
| 35 | for (Link lt: links) { |
| 36 | update(lt, op); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) { |
| 42 | log.debug("LinkStorage:update(): {} {}", op, link); |
| 43 | |
| 44 | switch (op) { |
| 45 | case UPDATE: |
| 46 | case CREATE: |
| 47 | case INSERT: |
| 48 | addLink(link, linkinfo); |
| 49 | break; |
| 50 | case DELETE: |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | protected void addLink(Link lt, LinkInfo linkinfo) { |
| 56 | Vertex vswSrc, vswDst; |
| 57 | Vertex vportSrc = null, vportDst = null; |
| 58 | |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 59 | log.info("addLink(): {} {} getSrc {}", new Object[]{lt, linkinfo, lt.getSrc()}); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 60 | |
| 61 | try { |
| 62 | // get source port vertex |
| 63 | String dpid = HexString.toHexString(lt.getSrc()); |
| 64 | short port = lt.getSrcPort(); |
| 65 | if ((vswSrc = graph.getVertices("dpid", dpid).iterator().next()) != null) { |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 66 | log.debug("addLink(): sw exists {} {}", dpid, vswSrc); |
| 67 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 68 | |
| 69 | //if (vswSrc.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().hasNext()) { |
| 70 | pipe.start(vswSrc).out("on").has("number", (int)port); |
| 71 | //pipe.start(vswSrc).out("on"); |
| 72 | //log.debug("pipe count {}", pipe.count()); |
| 73 | if (pipe.hasNext()) { |
| 74 | //vportSrc = vswSrc.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
| 75 | vportSrc = pipe.next(); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 76 | log.debug("addLink(): port found {} {}", port, vportSrc); |
| 77 | } else { |
| 78 | log.error("addLink(): sw {} port {} not found", dpid, port); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // get dest port vertex |
| 83 | dpid = HexString.toHexString(lt.getDst()); |
| 84 | port = lt.getDstPort(); |
| 85 | if ((vswDst = graph.getVertices("dpid",dpid).iterator().next()) != null) { |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 86 | log.debug("addLink(): sw exists {} {}", dpid, vswDst); |
| 87 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 88 | pipe.start(vswDst).out("on").has("number", (int)port); |
| 89 | //if (vswDst.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().hasNext()) { |
| 90 | if (pipe.hasNext()){ |
| 91 | //vportDst = vswDst.query().direction(Direction.OUT).labels("on").has("number",port).vertices().iterator().next(); |
| 92 | vportDst = pipe.next(); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 93 | log.debug("addLink(): port found {} {}", port, vportDst); |
| 94 | } else { |
| 95 | log.error ("addLink(): sw {} port {} not found", dpid, port); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (vportSrc != null && vportDst != null) { |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 100 | //TODO: If Edge already exists should we remove and add again? |
Pankaj Berde | 0a31dab | 2013-01-15 16:06:36 -0800 | [diff] [blame] | 101 | if (vportSrc.query().direction(Direction.OUT).labels("link").vertices().iterator().hasNext() && |
| 102 | vportSrc.query().direction(Direction.OUT).labels("link").vertices().iterator().next().equals(vportDst)) { |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 103 | //FIXME: Succeed silently for now |
| 104 | } else { |
| 105 | graph.addEdge(null, vportSrc, vportDst, "link"); |
| 106 | graph.stopTransaction(Conclusion.SUCCESS); |
| 107 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 108 | log.debug("addLink(): link added {} src {} dst {}", new Object[]{lt, vportSrc, vportDst}); |
| 109 | } else { |
| 110 | log.error("addLink(): failed {} src {} dst {}", new Object[]{lt, vportSrc, vportDst}); |
| 111 | graph.stopTransaction(Conclusion.FAILURE); |
| 112 | } |
| 113 | } catch (TitanException e) { |
| 114 | /* |
| 115 | * retry till we succeed? |
| 116 | */ |
| 117 | log.error("addLink(): {} failed", lt); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public List<Link> getLinks(Long dpid, int port) { |
| 123 | // TODO Auto-generated method stub |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void deleteLinks(Long dpid, int port) { |
| 129 | // TODO Auto-generated method stub |
| 130 | |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void init(String conf) { |
| 135 | //TODO extract the DB location from conf |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 136 | |
| 137 | graph = TitanFactory.open(conf); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 138 | |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 139 | // FIXME: These keys are not needed for Links but we better create it before using it as per titan |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 140 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 141 | if (!s.contains("dpid")) { |
| 142 | graph.createKeyIndex("dpid", Vertex.class); |
| 143 | graph.stopTransaction(Conclusion.SUCCESS); |
| 144 | } |
| 145 | if (!s.contains("type")) { |
| 146 | graph.createKeyIndex("type", Vertex.class); |
| 147 | graph.stopTransaction(Conclusion.SUCCESS); |
| 148 | } |
| 149 | } |
| 150 | } |