blob: 70d93c9d2a66224ff6b755fe9e0dd49fcb292047 [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 Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800109 log.error("addOrUpdateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
110 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800111 }
112 }
113
114 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800115 public void deleteLinks(List<Link> links) {
116
117 for (Link lt : links) {
118 deleteLink(lt);
119 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800120 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800121
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800122
123 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800124 public void deleteLink(Link lt) {
125 Vertex vportSrc = null, vportDst = null;
126 int count = 0;
127
128 log.debug("deleteLink(): {}", lt);
129
130 try {
131 // get source port vertex
132 String dpid = HexString.toHexString(lt.getSrc());
133 short port = lt.getSrcPort();
134 vportSrc = getPortVertex(dpid, port);
135
136 // get dst port vertex
137 dpid = HexString.toHexString(lt.getDst());
138 port = lt.getDstPort();
139 vportDst = getPortVertex(dpid, port);
140
141 if (vportSrc != null && vportDst != null) {
142 for (Edge e : vportSrc.getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800143 log.debug("deleteLink(): {} in {} out {}",
144 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
145 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800146 graph.removeEdge(e);
147 count++;
148 }
149 }
150 graph.stopTransaction(Conclusion.SUCCESS);
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800151 log.debug("deleteLink(): deleted {} edges {} src {} dst {}", new Object[]{
152 count, lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800153
154 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800155 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800156 graph.stopTransaction(Conclusion.FAILURE);
157 }
158
159 } catch (TitanException e) {
160 /*
161 * retry till we succeed?
162 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800163 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
164 e.printStackTrace();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800165 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800166 }
167
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800168 // TODO: Fix me
169 @Override
170 public List<Link> getLinks(Long dpid, short port) {
171 Vertex vportSrc, vportDst;
172 List<Link> links = null;
173 Link lt;
174
175 vportSrc = getPortVertex(HexString.toHexString(dpid), port);
176 if (vportSrc != null) {
177 for (Edge e : vportSrc.getEdges(Direction.OUT)) {
178 if (e.getLabel().equals("link")) {
179 break;
180 }
181 }
182 }
183 return null;
184 }
185
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800186 @Override
187 public void init(String conf) {
188 //TODO extract the DB location from conf
Pankaj Berde67f88fb2013-01-15 17:16:01 -0800189
190 graph = TitanFactory.open(conf);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800191
Pankaj Berde67f88fb2013-01-15 17:16:01 -0800192 // 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 -0800193 Set<String> s = graph.getIndexedKeys(Vertex.class);
194 if (!s.contains("dpid")) {
195 graph.createKeyIndex("dpid", Vertex.class);
196 graph.stopTransaction(Conclusion.SUCCESS);
197 }
198 if (!s.contains("type")) {
199 graph.createKeyIndex("type", Vertex.class);
200 graph.stopTransaction(Conclusion.SUCCESS);
201 }
202 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800203
204 @Override
205 public void deleteLinksOnPort(Long dpid, short port) {
206 // TODO Auto-generated method stub
207
208 }
209
Pankaj Berdeff421802013-01-29 20:28:52 -0800210 @Override
211 public List<Link> getLinks(String dpid) {
212 // TODO Auto-generated method stub
213 return null;
214 }
215
216 public List<Link> getActiveLinks() {
217 // TODO Auto-generated method stub
218
219 return null;
220 }
221
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800222}