blob: ac76defcae12d3d820cebe00cf0fd16a5140b002 [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;
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 Yuta20514902013-06-12 11:24:16 -07008import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
HIGUCHI Yuta80c3ab82013-06-12 13:17:05 -070010import net.onrc.onos.ofcontroller.linkdiscovery.ILinkStorage;
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
Naoki Shiota11516712013-06-05 22:36:01 -070031 /**
32 * Update a record in the LinkStorage in a way provided by op.
33 * @param link Record of a link to be updated.
34 * @param op Operation to be done.
35 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080036 @Override
37 public void update(Link link, DM_OPERATION op) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080038 update(link, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080039 }
40
Naoki Shiota11516712013-06-05 22:36:01 -070041 /**
42 * Update multiple records in the LinkStorage in a way provided by op.
43 * @param links List of records to be updated.
44 * @param op Operation to be done.
45 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080046 @Override
47 public void update(List<Link> links, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080048 for (Link lt: links) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080049 update(lt, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080050 }
51 }
52
Naoki Shiota11516712013-06-05 22:36:01 -070053 /**
54 * Update a record of link with meta-information in the LinkStorage in a way provided by op.
55 * @param link Record of a link to update.
56 * @param linkinfo Meta-information of a link to be updated.
57 * @param op Operation to be done.
58 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080059 @Override
60 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080061 switch (op) {
62 case UPDATE:
63 case CREATE:
64 case INSERT:
Pankaj Berde254abb42013-06-10 21:21:49 -070065 updateLink(link, linkinfo, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080066 break;
67 case DELETE:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080068 deleteLink(link);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080069 break;
70 }
71 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080072
Naoki Shiota11516712013-06-05 22:36:01 -070073 /**
74 * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
75 * @param lt Record of a link to be updated.
76 * @param linkinfo Meta-information of a link to be updated.
77 * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
78 */
Pankaj Berde254abb42013-06-10 21:21:49 -070079 public void updateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
Pankaj Berde5fb27632013-04-05 08:56:12 -070080 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080081
Pankaj Berde254abb42013-06-10 21:21:49 -070082 log.trace("updateLink(): op {} {} {}", new Object[]{op, lt, linkinfo});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080083
84 try {
85 // get source port vertex
86 String dpid = HexString.toHexString(lt.getSrc());
87 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070088 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080089
90 // get dest port vertex
91 dpid = HexString.toHexString(lt.getDst());
92 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070093 vportDst = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080094
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080095 if (vportSrc != null && vportDst != null) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080096 // check if the link exists
Pankaj Berde77b58512013-04-05 12:06:37 -070097
Pankaj Berde62016142013-04-09 15:35:50 -070098 Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
99 List<IPortObject> currLinks = new ArrayList<IPortObject>();
Pankaj Berde77b58512013-04-05 12:06:37 -0700100 for (IPortObject V : currPorts) {
Pankaj Berde6debb042013-01-16 18:04:32 -0800101 currLinks.add(V);
102 }
Naoki Shiotacae568a2013-06-05 17:53:41 -0700103
Pankaj Berde6debb042013-01-16 18:04:32 -0800104 if (currLinks.contains(vportDst)) {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700105 // TODO: update linkinfo
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800106 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800107 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800108 new Object[]{op, lt, vportSrc, vportDst});
109 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800110 } else {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700111 vportSrc.setLinkPort(vportDst);
Pankaj Berde62016142013-04-09 15:35:50 -0700112
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700113 dbop.commit();
Pankaj Berde254abb42013-06-10 21:21:49 -0700114 log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800115 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800116 } else {
Pankaj Berde254abb42013-06-10 21:21:49 -0700117 log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700118 dbop.rollback();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800119 }
120 } catch (TitanException e) {
121 /*
122 * retry till we succeed?
123 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800124 e.printStackTrace();
Pankaj Berde254abb42013-06-10 21:21:49 -0700125 log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800126 }
127 }
128
Naoki Shiota11516712013-06-05 22:36:01 -0700129 /**
130 * Delete multiple records in the LinkStorage.
131 * @param links List of records to be deleted.
132 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800133 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800134 public void deleteLinks(List<Link> links) {
135
136 for (Link lt : links) {
137 deleteLink(lt);
138 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800139 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800140
Naoki Shiota11516712013-06-05 22:36:01 -0700141 /**
142 * Delete a record in the LinkStorage.
143 * @param link Record to be deleted.
144 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800145 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800146 public void deleteLink(Link lt) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700147 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800148 int count = 0;
149
150 log.debug("deleteLink(): {}", lt);
151
152 try {
153 // get source port vertex
154 String dpid = HexString.toHexString(lt.getSrc());
155 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700156 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800157
158 // get dst port vertex
159 dpid = HexString.toHexString(lt.getDst());
160 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700161 vportDst = dbop.searchPort(dpid, port);
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700162 // FIXME: This needs to remove all edges
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800163
164 if (vportSrc != null && vportDst != null) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700165
166 /* for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800167 log.debug("deleteLink(): {} in {} out {}",
168 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
169 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800170 graph.removeEdge(e);
171 count++;
172 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700173 }*/
174 vportSrc.removeLink(vportDst);
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700175 dbop.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700176 log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
177 lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800178
179 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800180 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700181 dbop.rollback();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800182 }
183
184 } catch (TitanException e) {
185 /*
186 * retry till we succeed?
187 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800188 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700189 dbop.rollback();
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800190 e.printStackTrace();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800191 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800192 }
193
Naoki Shiota11516712013-06-05 22:36:01 -0700194 /**
195 * Get list of all links connected to the port specified by given DPID and port number.
196 * @param dpid DPID of desired port.
197 * @param port Port number of desired port.
198 * @return List of links. Empty list if no port was found.
199 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700200 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800201 @Override
202 public List<Link> getLinks(Long dpid, short port) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800203 List<Link> links = null;
mininet403d5892013-06-05 03:48:17 -0700204 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800205 }
206
Naoki Shiota11516712013-06-05 22:36:01 -0700207 /**
208 * Initialize the object. Open LinkStorage using given configuration file.
209 * @param conf Path (absolute path for now) to configuration file.
210 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800211 @Override
212 public void init(String conf) {
Pankaj Berde1f10be02013-02-01 14:06:02 -0800213 //TODO extract the DB location from properties
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700214 this.dbop = new GraphDBOperation(GraphDBConnection.getInstance(conf));
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800215 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800216
Naoki Shiota11516712013-06-05 22:36:01 -0700217 /**
218 * Delete records of the links connected to the port specified by given DPID and port number.
219 * @param dpid DPID of desired port.
220 * @param port Port number of desired port.
221 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700222 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800223 @Override
224 public void deleteLinksOnPort(Long dpid, short port) {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700225 // BEGIN: Trial code
226 // author: Naoki Shiota
mininet403d5892013-06-05 03:48:17 -0700227 List<Link> linksToDelete = getLinks(dpid,port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800228
mininet403d5892013-06-05 03:48:17 -0700229 for(Link l : linksToDelete) {
230 deleteLink(l);
231 }
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700232 // END: Trial code
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800233 }
234
Naoki Shiota11516712013-06-05 22:36:01 -0700235 /**
236 * Get list of all links connected to the switch specified by given DPID.
237 * @param dpid DPID of desired switch.
238 * @return List of links. Empty list if no port was found.
239 */
Naoki Shiota1b972862013-06-05 19:49:09 -0700240 // TODO: Fix me
Pankaj Berdeff421802013-01-29 20:28:52 -0800241 @Override
242 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700243 List<Link> links = new ArrayList<Link>();
244
mininet403d5892013-06-05 03:48:17 -0700245 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800246 }
247
Naoki Shiota11516712013-06-05 22:36:01 -0700248 /**
249 * Get list of all links whose state is ACTIVE.
250 * @return List of active links. Empty list if no port was found.
251 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800252 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700253 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800254
255 List<Link> links = new ArrayList<Link>();
256 for (ISwitchObject sw : switches) {
257 GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
258 ExtractLink extractor = new ExtractLink();
259
260 pipe.start(sw.asVertex());
261 pipe.enablePath(true);
Pankaj Berde1f10be02013-02-01 14:06:02 -0800262 pipe.out("on").out("link").in("on").path().step(extractor);
Pankaj Berde5024ec12013-01-31 17:07:29 -0800263
Pankaj Berde1f10be02013-02-01 14:06:02 -0800264 while (pipe.hasNext() ) {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800265 Link l = pipe.next();
266 links.add(l);
267 }
268
269 }
270 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800271 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800272
HIGUCHI Yuta909de462013-06-12 14:32:26 -0700273 // FIXME Scope changed to public to allow access from TopoLinkServiceImpl. Move class definition to appropriate place.
274 static public class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800275
Pankaj Berde5024ec12013-01-31 17:07:29 -0800276 @Override
277 public Link compute(PathPipe<Vertex> pipe ) {
Pankaj Berded18c7622013-02-04 10:28:35 -0800278 // TODO Auto-generated method stub
Pankaj Berde5024ec12013-01-31 17:07:29 -0800279 long s_dpid = 0;
280 long d_dpid = 0;
281 short s_port = 0;
282 short d_port = 0;
283 List<Vertex> V = new ArrayList<Vertex>();
284 V = pipe.next();
285 Vertex src_sw = V.get(0);
286 Vertex dest_sw = V.get(3);
287 Vertex src_port = V.get(1);
288 Vertex dest_port = V.get(2);
289 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
290 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
291 s_port = (Short) src_port.getProperty("number");
292 d_port = (Short) dest_port.getProperty("number");
293
294 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
295
296 return l;
297 }
298 }
Pankaj Berded18c7622013-02-04 10:28:35 -0800299
Naoki Shiota11516712013-06-05 22:36:01 -0700300 /**
301 * Finalize the object.
302 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800303 public void finalize() {
304 close();
305 }
306
Naoki Shiota11516712013-06-05 22:36:01 -0700307 /**
308 * Close LinkStorage.
309 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800310 @Override
311 public void close() {
312 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700313// graph.shutdown();
Pankaj Berded18c7622013-02-04 10:28:35 -0800314 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800315
Pankaj Berdeff421802013-01-29 20:28:52 -0800316
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800317}