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