HIGUCHI Yuta | f05c480 | 2013-06-17 11:15:50 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core.internal; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 2 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 4 | import java.util.List; |
| 5 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.routing.Link; |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 7 | import net.onrc.onos.graph.DBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 8 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 10 | |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 11 | import org.openflow.util.HexString; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | import com.tinkerpop.blueprints.Vertex; |
| 16 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 17 | import net.onrc.onos.graph.GraphDBManager; |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 18 | import com.tinkerpop.pipes.PipeFunction; |
| 19 | import com.tinkerpop.pipes.transform.PathPipe; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 20 | |
| 21 | public class TopoLinkServiceImpl implements ITopoLinkService { |
| 22 | |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 23 | protected DBOperation dbop; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 24 | protected final static Logger log = LoggerFactory.getLogger(TopoLinkServiceImpl.class); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 25 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 26 | public void finalize() { |
| 27 | close(); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public void close() { |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 32 | dbop.close(); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 33 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 34 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 35 | @Override |
Pankaj Berde | 1cde50b | 2013-02-19 20:16:06 -0800 | [diff] [blame] | 36 | public List<Link> getActiveLinks() { |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 37 | // TODO Auto-generated method stub |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 38 | dbop = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
| 39 | //dbop = GraphDBManager.getDBOperation("", ""); |
yoshi | 9535fc8 | 2013-12-05 11:34:52 -0800 | [diff] [blame] | 40 | //dbop.commit(); //Commit to ensure we see latest data |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 41 | Iterable<ISwitchObject> switches = dbop.getActiveSwitches(); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 42 | List<Link> links = new ArrayList<Link>(); |
| 43 | for (ISwitchObject sw : switches) { |
| 44 | GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>(); |
| 45 | ExtractLink extractor = new ExtractLink(); |
| 46 | |
| 47 | pipe.start(sw.asVertex()); |
| 48 | pipe.enablePath(true); |
| 49 | pipe.out("on").out("link").in("on").path().step(extractor); |
| 50 | |
| 51 | while (pipe.hasNext() ) { |
| 52 | Link l = pipe.next(); |
| 53 | links.add(l); |
| 54 | } |
| 55 | |
| 56 | } |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 57 | dbop.commit(); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 58 | return links; |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Pankaj Berde | 1cde50b | 2013-02-19 20:16:06 -0800 | [diff] [blame] | 62 | public List<Link> getLinksOnSwitch(String dpid) { |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 63 | List<Link> links = new ArrayList<Link>(); |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 64 | ISwitchObject sw = dbop.searchSwitch(dpid); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 65 | GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>(); |
| 66 | ExtractLink extractor = new ExtractLink(); |
| 67 | |
| 68 | pipe.start(sw.asVertex()); |
| 69 | pipe.enablePath(true); |
| 70 | pipe.out("on").out("link").in("on").path().step(extractor); |
| 71 | |
| 72 | while (pipe.hasNext() ) { |
| 73 | Link l = pipe.next(); |
| 74 | links.add(l); |
| 75 | } |
| 76 | return links; |
| 77 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 78 | } |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 79 | |
| 80 | private class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> { |
| 81 | @Override |
| 82 | public Link compute(PathPipe<Vertex> pipe) { |
| 83 | long s_dpid = 0; |
| 84 | long d_dpid = 0; |
| 85 | short s_port = 0; |
| 86 | short d_port = 0; |
| 87 | |
Naoki Shiota | de5d842 | 2013-12-11 13:56:57 -0800 | [diff] [blame] | 88 | List<?> V = pipe.next(); |
| 89 | Vertex src_sw = (Vertex)V.get(0); |
| 90 | Vertex dest_sw = (Vertex)V.get(3); |
| 91 | Vertex src_port = (Vertex)V.get(1); |
| 92 | Vertex dest_port = (Vertex)V.get(2); |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 93 | s_dpid = HexString.toLong((String) src_sw.getProperty("dpid")); |
| 94 | d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid")); |
| 95 | s_port = (Short) src_port.getProperty("number"); |
| 96 | d_port = (Short) dest_port.getProperty("number"); |
| 97 | |
| 98 | Link l = new Link(s_dpid,s_port,d_dpid,d_port); |
| 99 | |
| 100 | return l; |
| 101 | } |
| 102 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 103 | } |