blob: f3de81de6dbbadfeefe7945a34d6db40cc2acab5 [file] [log] [blame]
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07001package net.onrc.onos.ofcontroller.core.internal;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08002
Pankaj Berde6debb042013-01-16 18:04:32 -08003import java.util.ArrayList;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08004import java.util.List;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08005
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08006import net.floodlightcontroller.linkdiscovery.LinkInfo;
7import net.floodlightcontroller.routing.Link;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07008import net.onrc.onos.ofcontroller.core.ILinkStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07009import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde2239f0d2013-04-04 09:42:43 -070011import net.onrc.onos.util.GraphDBConnection;
Toshio Koide7bdea5b2013-06-13 13:57:18 -070012import net.onrc.onos.util.GraphDBOperation;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080013
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080014import org.openflow.util.HexString;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080018import com.thinkaurelius.titan.core.TitanException;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080019import com.tinkerpop.blueprints.Direction;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080020import com.tinkerpop.blueprints.Edge;
Pankaj Berde62016142013-04-09 15:35:50 -070021import com.tinkerpop.blueprints.Vertex;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080022import com.tinkerpop.gremlin.java.GremlinPipeline;
Pankaj Berde5024ec12013-01-31 17:07:29 -080023import com.tinkerpop.pipes.PipeFunction;
24import com.tinkerpop.pipes.transform.PathPipe;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080025
26public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070027
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080028 protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Toshio Koide7bdea5b2013-06-13 13:57:18 -070029 protected GraphDBOperation dbop;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080030
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080031 @Override
32 public void update(Link link, DM_OPERATION op) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080033 update(link, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080034 }
35
36 @Override
37 public void update(List<Link> links, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080038 for (Link lt: links) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080039 update(lt, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080040 }
41 }
42
43 @Override
44 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080045 switch (op) {
46 case UPDATE:
47 case CREATE:
48 case INSERT:
Pankaj Berde254abb42013-06-10 21:21:49 -070049 updateLink(link, linkinfo, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080050 break;
51 case DELETE:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080052 deleteLink(link);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080053 break;
54 }
55 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080056
Pankaj Berde254abb42013-06-10 21:21:49 -070057 public void updateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
Pankaj Berde5fb27632013-04-05 08:56:12 -070058 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080059
Pankaj Berde254abb42013-06-10 21:21:49 -070060 log.trace("updateLink(): op {} {} {}", new Object[]{op, lt, linkinfo});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080061
62 try {
63 // get source port vertex
64 String dpid = HexString.toHexString(lt.getSrc());
65 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070066 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080067
68 // get dest port vertex
69 dpid = HexString.toHexString(lt.getDst());
70 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070071 vportDst = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080072
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080073 if (vportSrc != null && vportDst != null) {
Pankaj Berde86a0d412013-04-05 15:06:26 -070074
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080075 // check if the link exists
Pankaj Berde77b58512013-04-05 12:06:37 -070076
Pankaj Berde62016142013-04-09 15:35:50 -070077 Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
78 List<IPortObject> currLinks = new ArrayList<IPortObject>();
Pankaj Berde77b58512013-04-05 12:06:37 -070079 for (IPortObject V : currPorts) {
Pankaj Berde6debb042013-01-16 18:04:32 -080080 currLinks.add(V);
81 }
82
83 if (currLinks.contains(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080084 // TODO: update linkinfo
85 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -080086 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080087 new Object[]{op, lt, vportSrc, vportDst});
88 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -080089 } else {
Pankaj Berde62016142013-04-09 15:35:50 -070090 vportSrc.setLinkPort(vportDst);
91
Toshio Koide7bdea5b2013-06-13 13:57:18 -070092 dbop.commit();
Pankaj Berde254abb42013-06-10 21:21:49 -070093 log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Pankaj Berde0fc4e432013-01-12 09:47:22 -080094 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080095 } else {
Pankaj Berde254abb42013-06-10 21:21:49 -070096 log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -070097 dbop.rollback();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080098 }
99 } catch (TitanException e) {
100 /*
101 * retry till we succeed?
102 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800103 e.printStackTrace();
Pankaj Berde254abb42013-06-10 21:21:49 -0700104 log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800105 }
106 }
107
108 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800109 public void deleteLinks(List<Link> links) {
110
111 for (Link lt : links) {
112 deleteLink(lt);
113 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800114 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800115
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800116
117 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800118 public void deleteLink(Link lt) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700119 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800120 int count = 0;
121
122 log.debug("deleteLink(): {}", lt);
123
124 try {
125 // get source port vertex
126 String dpid = HexString.toHexString(lt.getSrc());
127 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700128 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800129
130 // get dst port vertex
131 dpid = HexString.toHexString(lt.getDst());
132 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700133 vportDst = dbop.searchPort(dpid, port);
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700134 // FIXME: This needs to remove all edges
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800135
136 if (vportSrc != null && vportDst != null) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700137
138 /* for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800139 log.debug("deleteLink(): {} in {} out {}",
140 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
141 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800142 graph.removeEdge(e);
143 count++;
144 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700145 }*/
146 vportSrc.removeLink(vportDst);
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700147 dbop.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700148 log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
149 lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800150
151 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800152 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700153 dbop.rollback();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800154 }
155
156 } catch (TitanException e) {
157 /*
158 * retry till we succeed?
159 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800160 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700161 dbop.rollback();
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800162 e.printStackTrace();
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) {
Pankaj Berde62016142013-04-09 15:35:50 -0700169 IPortObject vportSrc, vportDst;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800170 List<Link> links = null;
171 Link lt;
172
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700173 vportSrc = dbop.searchPort(HexString.toHexString(dpid), port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800174 if (vportSrc != null) {
Pankaj Berde62016142013-04-09 15:35:50 -0700175
176 for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800177 if (e.getLabel().equals("link")) {
178 break;
179 }
180 }
181 }
182 return null;
183 }
184
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800185 @Override
186 public void init(String conf) {
Pankaj Berde1f10be02013-02-01 14:06:02 -0800187 //TODO extract the DB location from properties
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700188 this.dbop = new GraphDBOperation(GraphDBConnection.getInstance(conf));
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800189 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800190
191 @Override
192 public void deleteLinksOnPort(Long dpid, short port) {
193 // TODO Auto-generated method stub
194
195 }
196
Pankaj Berdeff421802013-01-29 20:28:52 -0800197 @Override
198 public List<Link> getLinks(String dpid) {
199 // TODO Auto-generated method stub
200 return null;
201 }
202
203 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700204 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800205
206 List<Link> links = new ArrayList<Link>();
207 for (ISwitchObject sw : switches) {
208 GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
209 ExtractLink extractor = new ExtractLink();
210
211 pipe.start(sw.asVertex());
212 pipe.enablePath(true);
Pankaj Berde1f10be02013-02-01 14:06:02 -0800213 pipe.out("on").out("link").in("on").path().step(extractor);
Pankaj Berde5024ec12013-01-31 17:07:29 -0800214
Pankaj Berde1f10be02013-02-01 14:06:02 -0800215 while (pipe.hasNext() ) {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800216 Link l = pipe.next();
217 links.add(l);
218 }
219
220 }
221 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800222 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800223
HIGUCHI Yuta909de462013-06-12 14:32:26 -0700224 // FIXME Scope changed to public to allow access from TopoLinkServiceImpl. Move class definition to appropriate place.
225 static public class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800226
Pankaj Berde5024ec12013-01-31 17:07:29 -0800227 @Override
228 public Link compute(PathPipe<Vertex> pipe ) {
Pankaj Berded18c7622013-02-04 10:28:35 -0800229 // TODO Auto-generated method stub
Pankaj Berde5024ec12013-01-31 17:07:29 -0800230 long s_dpid = 0;
231 long d_dpid = 0;
232 short s_port = 0;
233 short d_port = 0;
234 List<Vertex> V = new ArrayList<Vertex>();
235 V = pipe.next();
236 Vertex src_sw = V.get(0);
237 Vertex dest_sw = V.get(3);
238 Vertex src_port = V.get(1);
239 Vertex dest_port = V.get(2);
240 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
241 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
242 s_port = (Short) src_port.getProperty("number");
243 d_port = (Short) dest_port.getProperty("number");
244
245 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
246
247 return l;
248 }
249 }
Pankaj Berded18c7622013-02-04 10:28:35 -0800250
251 public void finalize() {
252 close();
253 }
254
255 @Override
256 public void close() {
257 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700258// graph.shutdown();
Pankaj Berded18c7622013-02-04 10:28:35 -0800259 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800260
Pankaj Berdeff421802013-01-29 20:28:52 -0800261
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800262}