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