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; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 21 | import com.tinkerpop.blueprints.Edge; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 22 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 23 | |
| 24 | public class LinkStorageImpl implements ILinkStorage { |
| 25 | public TitanGraph graph; |
| 26 | protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 27 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 28 | @Override |
| 29 | public void update(Link link, DM_OPERATION op) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 30 | update(link, (LinkInfo)null, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public void update(List<Link> links, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 35 | for (Link lt: links) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 36 | update(lt, (LinkInfo)null, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 42 | switch (op) { |
| 43 | case UPDATE: |
| 44 | case CREATE: |
| 45 | case INSERT: |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 46 | addOrUpdateLink(link, linkinfo, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 47 | break; |
| 48 | case DELETE: |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 49 | deleteLink(link); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 54 | private Vertex getPortVertex(String dpid, short port) { |
| 55 | Vertex vsw, vport = null; |
| 56 | if ((vsw = graph.getVertices("dpid", dpid).iterator().next()) != null) { |
| 57 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 58 | pipe.start(vsw).out("on").has("number", port); |
| 59 | if (pipe.hasNext()) { |
| 60 | vport = pipe.next(); |
| 61 | } |
| 62 | } |
| 63 | return vport; |
| 64 | } |
| 65 | |
| 66 | public void addOrUpdateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 67 | Vertex vportSrc = null, vportDst = null; |
| 68 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 69 | log.debug("addOrUpdateLink(): op {} {} {}", new Object[]{op, lt, linkinfo}); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 70 | |
| 71 | try { |
| 72 | // get source port vertex |
| 73 | String dpid = HexString.toHexString(lt.getSrc()); |
| 74 | short port = lt.getSrcPort(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 75 | vportSrc = getPortVertex(dpid, port); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 76 | |
| 77 | // get dest port vertex |
| 78 | dpid = HexString.toHexString(lt.getDst()); |
| 79 | port = lt.getDstPort(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 80 | vportDst = getPortVertex(dpid, port); |
| 81 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 82 | if (vportSrc != null && vportDst != null) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 83 | |
| 84 | // check if the link exists |
Pankaj Berde | 6debb04 | 2013-01-16 18:04:32 -0800 | [diff] [blame] | 85 | List<Vertex> currLinks = new ArrayList<Vertex>(); |
| 86 | for (Vertex V : vportSrc.query().direction(Direction.OUT).labels("link").vertices()) { |
| 87 | currLinks.add(V); |
| 88 | } |
| 89 | |
| 90 | if (currLinks.contains(vportDst)) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 91 | // TODO: update linkinfo |
| 92 | if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) { |
| 93 | log.debug("addOrUpdateLink(): Failure: link exists {} {} src {} dst {}", |
| 94 | new Object[]{op, lt, vportSrc, vportDst}); |
| 95 | } |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 96 | } else { |
| 97 | graph.addEdge(null, vportSrc, vportDst, "link"); |
| 98 | graph.stopTransaction(Conclusion.SUCCESS); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 99 | log.debug("addOrUpdateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst}); |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 100 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 101 | } else { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 102 | log.error("addOrUpdateLink(): failed {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst}); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 103 | graph.stopTransaction(Conclusion.FAILURE); |
| 104 | } |
| 105 | } catch (TitanException e) { |
| 106 | /* |
| 107 | * retry till we succeed? |
| 108 | */ |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 109 | log.error("addOrUpdateLink(): failed {} {}", new Object[]{op, lt}); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 114 | public void deleteLinks(List<Link> links) { |
| 115 | |
| 116 | for (Link lt : links) { |
| 117 | deleteLink(lt); |
| 118 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 119 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 120 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 121 | |
| 122 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 123 | public void deleteLink(Link lt) { |
| 124 | Vertex vportSrc = null, vportDst = null; |
| 125 | int count = 0; |
| 126 | |
| 127 | log.debug("deleteLink(): {}", lt); |
| 128 | |
| 129 | try { |
| 130 | // get source port vertex |
| 131 | String dpid = HexString.toHexString(lt.getSrc()); |
| 132 | short port = lt.getSrcPort(); |
| 133 | vportSrc = getPortVertex(dpid, port); |
| 134 | |
| 135 | // get dst port vertex |
| 136 | dpid = HexString.toHexString(lt.getDst()); |
| 137 | port = lt.getDstPort(); |
| 138 | vportDst = getPortVertex(dpid, port); |
| 139 | |
| 140 | if (vportSrc != null && vportDst != null) { |
| 141 | for (Edge e : vportSrc.getEdges(Direction.OUT)) { |
| 142 | log.debug("deleteLink(): {} {}", e.getLabel(), e.getVertex(Direction.IN)); |
| 143 | // if (e.getLabel().equals("link") && e.getVertex(Direction.OUT).equals(vportDst)) { |
| 144 | if (e.getLabel().equals("link")) { |
| 145 | graph.removeEdge(e); |
| 146 | count++; |
| 147 | } |
| 148 | } |
| 149 | graph.stopTransaction(Conclusion.SUCCESS); |
| 150 | log.debug("deleteLink(): {} {} src {} dst {}", new Object[]{ |
| 151 | (count > 0) ? "deleted " + count : "failure", lt, vportSrc, vportDst}); |
| 152 | |
| 153 | } else { |
| 154 | log.error("deleteLink(): failed src port vertex not found {} src {} dst {}", new Object[]{lt, vportSrc, vportDst}); |
| 155 | graph.stopTransaction(Conclusion.FAILURE); |
| 156 | } |
| 157 | |
| 158 | } catch (TitanException e) { |
| 159 | /* |
| 160 | * retry till we succeed? |
| 161 | */ |
| 162 | log.error("deleteLink(): {} failed", lt); |
| 163 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 166 | // TODO: Fix me |
| 167 | @Override |
| 168 | public List<Link> getLinks(Long dpid, short port) { |
| 169 | Vertex vportSrc, vportDst; |
| 170 | List<Link> links = null; |
| 171 | Link lt; |
| 172 | |
| 173 | vportSrc = getPortVertex(HexString.toHexString(dpid), port); |
| 174 | if (vportSrc != null) { |
| 175 | for (Edge e : vportSrc.getEdges(Direction.OUT)) { |
| 176 | if (e.getLabel().equals("link")) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return null; |
| 182 | } |
| 183 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public void init(String conf) { |
| 186 | //TODO extract the DB location from conf |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 187 | |
| 188 | graph = TitanFactory.open(conf); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 189 | |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 190 | // 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] | 191 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 192 | if (!s.contains("dpid")) { |
| 193 | graph.createKeyIndex("dpid", Vertex.class); |
| 194 | graph.stopTransaction(Conclusion.SUCCESS); |
| 195 | } |
| 196 | if (!s.contains("type")) { |
| 197 | graph.createKeyIndex("type", Vertex.class); |
| 198 | graph.stopTransaction(Conclusion.SUCCESS); |
| 199 | } |
| 200 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public void deleteLinksOnPort(Long dpid, short port) { |
| 204 | // TODO Auto-generated method stub |
| 205 | |
| 206 | } |
| 207 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 208 | } |