blob: 98605b27df3e3ad3b50c0c7e3e6af2e5256a1081 [file] [log] [blame]
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08001package net.floodlightcontroller.linkdiscovery.internal;
2
Pankaj Berde6debb042013-01-16 18:04:32 -08003import java.util.ArrayList;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08004import java.util.List;
5import java.util.Set;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08006
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08007import net.floodlightcontroller.linkdiscovery.ILinkStorage;
8import net.floodlightcontroller.linkdiscovery.LinkInfo;
9import net.floodlightcontroller.routing.Link;
10
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080011import org.openflow.util.HexString;
12import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080015import com.thinkaurelius.titan.core.TitanException;
16import com.thinkaurelius.titan.core.TitanFactory;
17import com.thinkaurelius.titan.core.TitanGraph;
18import com.tinkerpop.blueprints.Direction;
19import com.tinkerpop.blueprints.TransactionalGraph.Conclusion;
20import com.tinkerpop.blueprints.Vertex;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080021import com.tinkerpop.blueprints.Edge;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080022import com.tinkerpop.gremlin.java.GremlinPipeline;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080023
24public class LinkStorageImpl implements ILinkStorage {
25 public TitanGraph graph;
26 protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080027
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080028 @Override
29 public void update(Link link, DM_OPERATION op) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080030 update(link, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080031 }
32
33 @Override
34 public void update(List<Link> links, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080035 for (Link lt: links) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080036 update(lt, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080037 }
38 }
39
40 @Override
41 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080042 switch (op) {
43 case UPDATE:
44 case CREATE:
45 case INSERT:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080046 addOrUpdateLink(link, linkinfo, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080047 break;
48 case DELETE:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080049 deleteLink(link);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080050 break;
51 }
52 }
53
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080054 private Vertex getPortVertex(String dpid, short port) {
55 Vertex vsw, vport = null;
56 if ((vsw = graph.getVertices("dpid", dpid).iterator().next()) != null) {
57 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
58 pipe.start(vsw).out("on").has("number", port);
59 if (pipe.hasNext()) {
60 vport = pipe.next();
61 }
62 }
63 return vport;
64 }
65
66 public void addOrUpdateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080067 Vertex vportSrc = null, vportDst = null;
68
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080069 log.debug("addOrUpdateLink(): op {} {} {}", new Object[]{op, lt, linkinfo});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080070
71 try {
72 // get source port vertex
73 String dpid = HexString.toHexString(lt.getSrc());
74 short port = lt.getSrcPort();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080075 vportSrc = getPortVertex(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080076
77 // get dest port vertex
78 dpid = HexString.toHexString(lt.getDst());
79 port = lt.getDstPort();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080080 vportDst = getPortVertex(dpid, port);
81
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080082 if (vportSrc != null && vportDst != null) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080083
84 // check if the link exists
Pankaj Berde6debb042013-01-16 18:04:32 -080085 List<Vertex> currLinks = new ArrayList<Vertex>();
86 for (Vertex V : vportSrc.query().direction(Direction.OUT).labels("link").vertices()) {
87 currLinks.add(V);
88 }
89
90 if (currLinks.contains(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080091 // TODO: update linkinfo
92 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -080093 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080094 new Object[]{op, lt, vportSrc, vportDst});
95 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -080096 } else {
97 graph.addEdge(null, vportSrc, vportDst, "link");
98 graph.stopTransaction(Conclusion.SUCCESS);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080099 log.debug("addOrUpdateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800100 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800101 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800102 log.error("addOrUpdateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800103 graph.stopTransaction(Conclusion.FAILURE);
104 }
105 } catch (TitanException e) {
106 /*
107 * retry till we succeed?
108 */
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800109 log.error("addOrUpdateLink(): titan exception {} {}", new Object[]{op, lt});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800110 }
111 }
112
113 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800114 public void deleteLinks(List<Link> links) {
115
116 for (Link lt : links) {
117 deleteLink(lt);
118 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800119 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800120
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800121
122 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800123 public void deleteLink(Link lt) {
124 Vertex vportSrc = null, vportDst = null;
125 int count = 0;
126
127 log.debug("deleteLink(): {}", lt);
128
129 try {
130 // get source port vertex
131 String dpid = HexString.toHexString(lt.getSrc());
132 short port = lt.getSrcPort();
133 vportSrc = getPortVertex(dpid, port);
134
135 // get dst port vertex
136 dpid = HexString.toHexString(lt.getDst());
137 port = lt.getDstPort();
138 vportDst = getPortVertex(dpid, port);
139
140 if (vportSrc != null && vportDst != null) {
141 for (Edge e : vportSrc.getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800142 log.debug("deleteLink(): {} in {} out {}",
143 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
144 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800145 graph.removeEdge(e);
146 count++;
147 }
148 }
149 graph.stopTransaction(Conclusion.SUCCESS);
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800150 log.debug("deleteLink(): deleted {} edges {} src {} dst {}", new Object[]{
151 count, lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800152
153 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800154 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800155 graph.stopTransaction(Conclusion.FAILURE);
156 }
157
158 } catch (TitanException e) {
159 /*
160 * retry till we succeed?
161 */
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800162 log.error("deleteLink(): titan exception {}", lt);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800163 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800164 }
165
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800166 // TODO: Fix me
167 @Override
168 public List<Link> getLinks(Long dpid, short port) {
169 Vertex vportSrc, vportDst;
170 List<Link> links = null;
171 Link lt;
172
173 vportSrc = getPortVertex(HexString.toHexString(dpid), port);
174 if (vportSrc != null) {
175 for (Edge e : vportSrc.getEdges(Direction.OUT)) {
176 if (e.getLabel().equals("link")) {
177 break;
178 }
179 }
180 }
181 return null;
182 }
183
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800184 @Override
185 public void init(String conf) {
186 //TODO extract the DB location from conf
Pankaj Berde67f88fb2013-01-15 17:16:01 -0800187
188 graph = TitanFactory.open(conf);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800189
Pankaj Berde67f88fb2013-01-15 17:16:01 -0800190 // FIXME: These keys are not needed for Links but we better create it before using it as per titan
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800191 Set<String> s = graph.getIndexedKeys(Vertex.class);
192 if (!s.contains("dpid")) {
193 graph.createKeyIndex("dpid", Vertex.class);
194 graph.stopTransaction(Conclusion.SUCCESS);
195 }
196 if (!s.contains("type")) {
197 graph.createKeyIndex("type", Vertex.class);
198 graph.stopTransaction(Conclusion.SUCCESS);
199 }
200 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800201
202 @Override
203 public void deleteLinksOnPort(Long dpid, short port) {
204 // TODO Auto-generated method stub
205
206 }
207
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800208}