blob: 718816d8535570ae0347ad7a13a611add31662f4 [file] [log] [blame]
HIGUCHI Yutaf05c4802013-06-17 11:15:50 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berde5024ec12013-01-31 17:07:29 -08002
Pankaj Berde15193092013-03-21 17:30:14 -07003import java.util.ArrayList;
Pankaj Berde5024ec12013-01-31 17:07:29 -08004import java.util.List;
5
Pankaj Berde5024ec12013-01-31 17:07:29 -08006import net.floodlightcontroller.routing.Link;
yoshi2db7ff42013-11-25 19:30:25 -08007import net.onrc.onos.graph.DBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07008import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
Pankaj Berde15193092013-03-21 17:30:14 -070010
Naoki Shiota991093a2013-12-10 14:47:18 -080011import org.openflow.util.HexString;
Pankaj Berde15193092013-03-21 17:30:14 -070012import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
15import com.tinkerpop.blueprints.Vertex;
16import com.tinkerpop.gremlin.java.GremlinPipeline;
yoshi2db7ff42013-11-25 19:30:25 -080017import net.onrc.onos.graph.GraphDBManager;
Naoki Shiota991093a2013-12-10 14:47:18 -080018import com.tinkerpop.pipes.PipeFunction;
19import com.tinkerpop.pipes.transform.PathPipe;
Pankaj Berde5024ec12013-01-31 17:07:29 -080020
21public class TopoLinkServiceImpl implements ITopoLinkService {
22
yoshi2db7ff42013-11-25 19:30:25 -080023 protected DBOperation dbop;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070024 protected final static Logger log = LoggerFactory.getLogger(TopoLinkServiceImpl.class);
Pankaj Berde15193092013-03-21 17:30:14 -070025
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080026 @Override
27 protected void finalize() {
Pankaj Berde15193092013-03-21 17:30:14 -070028 close();
29 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080030
Pankaj Berde15193092013-03-21 17:30:14 -070031 @Override
32 public void close() {
yoshi2db7ff42013-11-25 19:30:25 -080033 dbop.close();
Pankaj Berde15193092013-03-21 17:30:14 -070034 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080035
Pankaj Berde5024ec12013-01-31 17:07:29 -080036 @Override
Pankaj Berde1cde50b2013-02-19 20:16:06 -080037 public List<Link> getActiveLinks() {
Yoshi Muroi5804ce92014-02-08 03:58:04 -080038 dbop = GraphDBManager.getDBOperation();
yoshi2db7ff42013-11-25 19:30:25 -080039 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde15193092013-03-21 17:30:14 -070040 List<Link> links = new ArrayList<Link>();
41 for (ISwitchObject sw : switches) {
42 GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
43 ExtractLink extractor = new ExtractLink();
44
45 pipe.start(sw.asVertex());
46 pipe.enablePath(true);
47 pipe.out("on").out("link").in("on").path().step(extractor);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080048
Pankaj Berde15193092013-03-21 17:30:14 -070049 while (pipe.hasNext() ) {
50 Link l = pipe.next();
51 links.add(l);
52 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080053
Pankaj Berde15193092013-03-21 17:30:14 -070054 }
yoshi2db7ff42013-11-25 19:30:25 -080055 dbop.commit();
Pankaj Berde15193092013-03-21 17:30:14 -070056 return links;
Pankaj Berde5024ec12013-01-31 17:07:29 -080057 }
58
59 @Override
Pankaj Berde1cde50b2013-02-19 20:16:06 -080060 public List<Link> getLinksOnSwitch(String dpid) {
Pankaj Berde15193092013-03-21 17:30:14 -070061 List<Link> links = new ArrayList<Link>();
yoshi2db7ff42013-11-25 19:30:25 -080062 ISwitchObject sw = dbop.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -070063 GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
64 ExtractLink extractor = new ExtractLink();
65
66 pipe.start(sw.asVertex());
67 pipe.enablePath(true);
68 pipe.out("on").out("link").in("on").path().step(extractor);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080069
Pankaj Berde15193092013-03-21 17:30:14 -070070 while (pipe.hasNext() ) {
71 Link l = pipe.next();
72 links.add(l);
73 }
74 return links;
75
Pankaj Berde5024ec12013-01-31 17:07:29 -080076 }
Naoki Shiota991093a2013-12-10 14:47:18 -080077
Naoki Shiotadf051d42014-01-20 16:12:41 -080078 private static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Naoki Shiota991093a2013-12-10 14:47:18 -080079 @Override
80 public Link compute(PathPipe<Vertex> pipe) {
81 long s_dpid = 0;
82 long d_dpid = 0;
83 short s_port = 0;
84 short d_port = 0;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080085
Naoki Shiotade5d8422013-12-11 13:56:57 -080086 List<?> V = pipe.next();
87 Vertex src_sw = (Vertex)V.get(0);
88 Vertex dest_sw = (Vertex)V.get(3);
89 Vertex src_port = (Vertex)V.get(1);
90 Vertex dest_port = (Vertex)V.get(2);
Naoki Shiota991093a2013-12-10 14:47:18 -080091 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
92 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
93 s_port = (Short) src_port.getProperty("number");
94 d_port = (Short) dest_port.getProperty("number");
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080095
Naoki Shiota991093a2013-12-10 14:47:18 -080096 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080097
Naoki Shiota991093a2013-12-10 14:47:18 -080098 return l;
99 }
100 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800101}