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