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; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 5 | import java.util.List; |
| 6 | import java.util.Set; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 7 | |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 8 | import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject; |
Pankaj Berde | 1e2f731 | 2013-02-15 08:25:31 -0800 | [diff] [blame] | 9 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 10 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoSwitchService; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.internal.TopoSwitchServiceImpl; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.linkdiscovery.ILinkStorage; |
| 13 | import net.floodlightcontroller.linkdiscovery.LinkInfo; |
| 14 | import net.floodlightcontroller.routing.Link; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 15 | import net.onrc.onos.util.GraphDBConnection; |
| 16 | import net.onrc.onos.util.GraphDBConnection.GenerateEvent; |
| 17 | import net.onrc.onos.util.GraphDBConnection.Transaction; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 18 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 19 | import org.openflow.util.HexString; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 23 | import com.thinkaurelius.titan.core.TitanException; |
| 24 | import com.thinkaurelius.titan.core.TitanFactory; |
| 25 | import com.thinkaurelius.titan.core.TitanGraph; |
| 26 | import com.tinkerpop.blueprints.Direction; |
| 27 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 28 | import com.tinkerpop.blueprints.Vertex; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 29 | import com.tinkerpop.blueprints.Edge; |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 30 | import com.tinkerpop.blueprints.util.wrappers.event.EventGraph; |
| 31 | import com.tinkerpop.blueprints.util.wrappers.event.EventTransactionalGraph; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 32 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 33 | import com.tinkerpop.pipes.PipeFunction; |
| 34 | import com.tinkerpop.pipes.transform.PathPipe; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 35 | |
| 36 | public class LinkStorageImpl implements ILinkStorage { |
| 37 | public TitanGraph graph; |
| 38 | protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 39 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 40 | @Override |
| 41 | public void update(Link link, DM_OPERATION op) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 42 | update(link, (LinkInfo)null, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void update(List<Link> links, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 47 | for (Link lt: links) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 48 | update(lt, (LinkInfo)null, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 54 | switch (op) { |
| 55 | case UPDATE: |
| 56 | case CREATE: |
| 57 | case INSERT: |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 58 | addOrUpdateLink(link, linkinfo, op); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 59 | break; |
| 60 | case DELETE: |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 61 | deleteLink(link); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 66 | private Vertex getPortVertex(String dpid, short port) { |
| 67 | Vertex vsw, vport = null; |
| 68 | if ((vsw = graph.getVertices("dpid", dpid).iterator().next()) != null) { |
| 69 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 70 | pipe.start(vsw).out("on").has("number", port); |
| 71 | if (pipe.hasNext()) { |
| 72 | vport = pipe.next(); |
| 73 | } |
| 74 | } |
| 75 | return vport; |
| 76 | } |
| 77 | |
| 78 | public void addOrUpdateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) { |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 79 | Vertex vportSrc = null, vportDst = null; |
| 80 | |
Umesh Krishnaswamy | 0ef75ee | 2013-03-25 17:50:27 -0700 | [diff] [blame] | 81 | log.trace("addOrUpdateLink(): op {} {} {}", new Object[]{op, lt, linkinfo}); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 82 | |
| 83 | try { |
| 84 | // get source port vertex |
| 85 | String dpid = HexString.toHexString(lt.getSrc()); |
| 86 | short port = lt.getSrcPort(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 87 | vportSrc = getPortVertex(dpid, port); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 88 | |
| 89 | // get dest port vertex |
| 90 | dpid = HexString.toHexString(lt.getDst()); |
| 91 | port = lt.getDstPort(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 92 | vportDst = getPortVertex(dpid, port); |
| 93 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 94 | if (vportSrc != null && vportDst != null) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 95 | |
| 96 | // check if the link exists |
Pankaj Berde | 6debb04 | 2013-01-16 18:04:32 -0800 | [diff] [blame] | 97 | List<Vertex> currLinks = new ArrayList<Vertex>(); |
| 98 | for (Vertex V : vportSrc.query().direction(Direction.OUT).labels("link").vertices()) { |
| 99 | currLinks.add(V); |
| 100 | } |
| 101 | |
| 102 | if (currLinks.contains(vportDst)) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 103 | // TODO: update linkinfo |
| 104 | if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) { |
Umesh Krishnaswamy | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 105 | log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}", |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 106 | new Object[]{op, lt, vportSrc, vportDst}); |
| 107 | } |
Pankaj Berde | 0fc4e43 | 2013-01-12 09:47:22 -0800 | [diff] [blame] | 108 | } else { |
| 109 | graph.addEdge(null, vportSrc, vportDst, "link"); |
| 110 | graph.stopTransaction(Conclusion.SUCCESS); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 111 | 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] | 112 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 113 | } else { |
Umesh Krishnaswamy | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 114 | 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] | 115 | graph.stopTransaction(Conclusion.FAILURE); |
| 116 | } |
| 117 | } catch (TitanException e) { |
| 118 | /* |
| 119 | * retry till we succeed? |
| 120 | */ |
Umesh Krishnaswamy | c56a8cc | 2013-01-24 21:47:51 -0800 | [diff] [blame] | 121 | log.error("addOrUpdateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()}); |
| 122 | e.printStackTrace(); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 127 | public void deleteLinks(List<Link> links) { |
| 128 | |
| 129 | for (Link lt : links) { |
| 130 | deleteLink(lt); |
| 131 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 132 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 133 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 134 | |
| 135 | @Override |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 136 | public void deleteLink(Link lt) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 137 | GraphDBConnection conn = GraphDBConnection.getInstance(""); |
| 138 | IPortObject vportSrc = null, vportDst = null; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 139 | int count = 0; |
| 140 | |
| 141 | log.debug("deleteLink(): {}", lt); |
| 142 | |
| 143 | try { |
| 144 | // get source port vertex |
| 145 | String dpid = HexString.toHexString(lt.getSrc()); |
| 146 | short port = lt.getSrcPort(); |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 147 | vportSrc = conn.utils().searchPort(conn, dpid, port); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 148 | |
| 149 | // get dst port vertex |
| 150 | dpid = HexString.toHexString(lt.getDst()); |
| 151 | port = lt.getDstPort(); |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 152 | vportDst = conn.utils().searchPort(conn, dpid, port); |
| 153 | // FIXME: This needs to remove all edges |
| 154 | // FIXME: Events will only be generated on singleton graph object (GraphDBConnection) |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 155 | |
| 156 | if (vportSrc != null && vportDst != null) { |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 157 | |
| 158 | /* for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) { |
Umesh Krishnaswamy | 11060ed | 2013-01-23 19:24:36 -0800 | [diff] [blame] | 159 | log.debug("deleteLink(): {} in {} out {}", |
| 160 | new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)}); |
| 161 | if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 162 | graph.removeEdge(e); |
| 163 | count++; |
| 164 | } |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 165 | }*/ |
| 166 | vportSrc.removeLink(vportDst); |
| 167 | conn.endTx(Transaction.COMMIT, GenerateEvent.TRUE); |
| 168 | log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{ |
| 169 | lt, vportSrc, vportDst}); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 170 | |
| 171 | } else { |
Umesh Krishnaswamy | 9306f95 | 2013-01-24 20:42:59 -0800 | [diff] [blame] | 172 | log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst}); |
Pankaj Berde | 2239f0d | 2013-04-04 09:42:43 -0700 | [diff] [blame] | 173 | conn.endTx(Transaction.ROLLBACK); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } catch (TitanException e) { |
| 177 | /* |
| 178 | * retry till we succeed? |
| 179 | */ |
Umesh Krishnaswamy | c56a8cc | 2013-01-24 21:47:51 -0800 | [diff] [blame] | 180 | log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()}); |
| 181 | e.printStackTrace(); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 182 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 185 | // TODO: Fix me |
| 186 | @Override |
| 187 | public List<Link> getLinks(Long dpid, short port) { |
| 188 | Vertex vportSrc, vportDst; |
| 189 | List<Link> links = null; |
| 190 | Link lt; |
| 191 | |
| 192 | vportSrc = getPortVertex(HexString.toHexString(dpid), port); |
| 193 | if (vportSrc != null) { |
| 194 | for (Edge e : vportSrc.getEdges(Direction.OUT)) { |
| 195 | if (e.getLabel().equals("link")) { |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | return null; |
| 201 | } |
| 202 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public void init(String conf) { |
Pankaj Berde | 1f10be0 | 2013-02-01 14:06:02 -0800 | [diff] [blame] | 205 | //TODO extract the DB location from properties |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 206 | |
| 207 | graph = TitanFactory.open(conf); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 208 | |
Pankaj Berde | 67f88fb | 2013-01-15 17:16:01 -0800 | [diff] [blame] | 209 | // 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] | 210 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 211 | if (!s.contains("dpid")) { |
| 212 | graph.createKeyIndex("dpid", Vertex.class); |
| 213 | graph.stopTransaction(Conclusion.SUCCESS); |
| 214 | } |
| 215 | if (!s.contains("type")) { |
| 216 | graph.createKeyIndex("type", Vertex.class); |
| 217 | graph.stopTransaction(Conclusion.SUCCESS); |
| 218 | } |
| 219 | } |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 220 | |
| 221 | @Override |
| 222 | public void deleteLinksOnPort(Long dpid, short port) { |
| 223 | // TODO Auto-generated method stub |
| 224 | |
| 225 | } |
| 226 | |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 227 | @Override |
| 228 | public List<Link> getLinks(String dpid) { |
| 229 | // TODO Auto-generated method stub |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | public List<Link> getActiveLinks() { |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 234 | |
| 235 | ITopoSwitchService swService = new TopoSwitchServiceImpl(); |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 236 | |
Pankaj Berde | 1cde50b | 2013-02-19 20:16:06 -0800 | [diff] [blame] | 237 | Iterable<ISwitchObject> switches = swService.getActiveSwitches(); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 238 | |
| 239 | List<Link> links = new ArrayList<Link>(); |
| 240 | for (ISwitchObject sw : switches) { |
| 241 | GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>(); |
| 242 | ExtractLink extractor = new ExtractLink(); |
| 243 | |
| 244 | pipe.start(sw.asVertex()); |
| 245 | pipe.enablePath(true); |
Pankaj Berde | 1f10be0 | 2013-02-01 14:06:02 -0800 | [diff] [blame] | 246 | pipe.out("on").out("link").in("on").path().step(extractor); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 247 | |
Pankaj Berde | 1f10be0 | 2013-02-01 14:06:02 -0800 | [diff] [blame] | 248 | while (pipe.hasNext() ) { |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 249 | Link l = pipe.next(); |
| 250 | links.add(l); |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | return links; |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 255 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 256 | |
| 257 | static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> { |
| 258 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 259 | @Override |
| 260 | public Link compute(PathPipe<Vertex> pipe ) { |
Pankaj Berde | d18c762 | 2013-02-04 10:28:35 -0800 | [diff] [blame] | 261 | // TODO Auto-generated method stub |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 262 | long s_dpid = 0; |
| 263 | long d_dpid = 0; |
| 264 | short s_port = 0; |
| 265 | short d_port = 0; |
| 266 | List<Vertex> V = new ArrayList<Vertex>(); |
| 267 | V = pipe.next(); |
| 268 | Vertex src_sw = V.get(0); |
| 269 | Vertex dest_sw = V.get(3); |
| 270 | Vertex src_port = V.get(1); |
| 271 | Vertex dest_port = V.get(2); |
| 272 | s_dpid = HexString.toLong((String) src_sw.getProperty("dpid")); |
| 273 | d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid")); |
| 274 | s_port = (Short) src_port.getProperty("number"); |
| 275 | d_port = (Short) dest_port.getProperty("number"); |
| 276 | |
| 277 | Link l = new Link(s_dpid,s_port,d_dpid,d_port); |
| 278 | |
| 279 | return l; |
| 280 | } |
| 281 | } |
Pankaj Berde | d18c762 | 2013-02-04 10:28:35 -0800 | [diff] [blame] | 282 | |
| 283 | public void finalize() { |
| 284 | close(); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void close() { |
| 289 | // TODO Auto-generated method stub |
| 290 | graph.shutdown(); |
| 291 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 292 | |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 293 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 294 | } |