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)) { |
Umesh Krishnaswamy | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 93 | log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}", |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 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 | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 102 | log.error("addOrUpdateLink(): failed invalid vertices {} {} 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 | c56a8cc | 2013-01-24 21:47:51 -0800 | [diff] [blame] | 109 | log.error("addOrUpdateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()}); |
| 110 | e.printStackTrace(); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 115 | public void deleteLinks(List<Link> links) { |
| 116 | |
| 117 | for (Link lt : links) { |
| 118 | deleteLink(lt); |
| 119 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 120 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 121 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 122 | |
| 123 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 124 | public void deleteLink(Link lt) { |
| 125 | Vertex vportSrc = null, vportDst = null; |
| 126 | int count = 0; |
| 127 | |
| 128 | log.debug("deleteLink(): {}", lt); |
| 129 | |
| 130 | try { |
| 131 | // get source port vertex |
| 132 | String dpid = HexString.toHexString(lt.getSrc()); |
| 133 | short port = lt.getSrcPort(); |
| 134 | vportSrc = getPortVertex(dpid, port); |
| 135 | |
| 136 | // get dst port vertex |
| 137 | dpid = HexString.toHexString(lt.getDst()); |
| 138 | port = lt.getDstPort(); |
| 139 | vportDst = getPortVertex(dpid, port); |
| 140 | |
| 141 | if (vportSrc != null && vportDst != null) { |
| 142 | for (Edge e : vportSrc.getEdges(Direction.OUT)) { |
Umesh Krishnaswamy | 11060ed | 2013-01-23 19:24:36 -0800 | [diff] [blame] | 143 | log.debug("deleteLink(): {} in {} out {}", |
| 144 | new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)}); |
| 145 | if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 146 | graph.removeEdge(e); |
| 147 | count++; |
| 148 | } |
| 149 | } |
| 150 | graph.stopTransaction(Conclusion.SUCCESS); |
Umesh Krishnaswamy | 11060ed | 2013-01-23 19:24:36 -0800 | [diff] [blame] | 151 | log.debug("deleteLink(): deleted {} edges {} src {} dst {}", new Object[]{ |
| 152 | count, lt, vportSrc, vportDst}); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 153 | |
| 154 | } else { |
Umesh Krishnaswamy | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 155 | log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst}); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 156 | graph.stopTransaction(Conclusion.FAILURE); |
| 157 | } |
| 158 | |
| 159 | } catch (TitanException e) { |
| 160 | /* |
| 161 | * retry till we succeed? |
| 162 | */ |
Umesh Krishnaswamy | c56a8cc | 2013-01-24 21:47:51 -0800 | [diff] [blame] | 163 | log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()}); |
| 164 | e.printStackTrace(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 165 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 168 | // TODO: Fix me |
| 169 | @Override |
| 170 | public List<Link> getLinks(Long dpid, short port) { |
| 171 | Vertex vportSrc, vportDst; |
| 172 | List<Link> links = null; |
| 173 | Link lt; |
| 174 | |
| 175 | vportSrc = getPortVertex(HexString.toHexString(dpid), port); |
| 176 | if (vportSrc != null) { |
| 177 | for (Edge e : vportSrc.getEdges(Direction.OUT)) { |
| 178 | if (e.getLabel().equals("link")) { |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return null; |
| 184 | } |
| 185 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 186 | @Override |
| 187 | public void init(String conf) { |
| 188 | //TODO extract the DB location from conf |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 189 | |
| 190 | graph = TitanFactory.open(conf); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 191 | |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 192 | // 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] | 193 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 194 | if (!s.contains("dpid")) { |
| 195 | graph.createKeyIndex("dpid", Vertex.class); |
| 196 | graph.stopTransaction(Conclusion.SUCCESS); |
| 197 | } |
| 198 | if (!s.contains("type")) { |
| 199 | graph.createKeyIndex("type", Vertex.class); |
| 200 | graph.stopTransaction(Conclusion.SUCCESS); |
| 201 | } |
| 202 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 203 | |
| 204 | @Override |
| 205 | public void deleteLinksOnPort(Long dpid, short port) { |
| 206 | // TODO Auto-generated method stub |
| 207 | |
| 208 | } |
| 209 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 210 | } |