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