blob: 91a225dba9c04b490aca97561307c9fc5de9eb7a [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) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700203 List<Link> links = new ArrayList<Link>();
204
205 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
206 ISwitchObject srcSw = srcPort.getSwitch();
207
208 if(srcSw != null) {
209 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
210 ISwitchObject dstSw = dstPort.getSwitch();
211 Link link = new Link(HexString.toLong(srcSw.getDPID()),
212 srcPort.getNumber(),
213 HexString.toLong(dstSw.getDPID()),
214 dstPort.getNumber());
215
216 links.add(link);
217 }
218 }
219
mininet403d5892013-06-05 03:48:17 -0700220 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800221 }
222
Naoki Shiota11516712013-06-05 22:36:01 -0700223 /**
224 * Initialize the object. Open LinkStorage using given configuration file.
225 * @param conf Path (absolute path for now) to configuration file.
226 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800227 @Override
228 public void init(String conf) {
Pankaj Berde1f10be02013-02-01 14:06:02 -0800229 //TODO extract the DB location from properties
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700230 this.dbop = new GraphDBOperation(GraphDBConnection.getInstance(conf));
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800231 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800232
Naoki Shiota11516712013-06-05 22:36:01 -0700233 /**
234 * Delete records of the links connected to the port specified by given DPID and port number.
235 * @param dpid DPID of desired port.
236 * @param port Port number of desired port.
237 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700238 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800239 @Override
240 public void deleteLinksOnPort(Long dpid, short port) {
mininet403d5892013-06-05 03:48:17 -0700241 List<Link> linksToDelete = getLinks(dpid,port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800242
mininet403d5892013-06-05 03:48:17 -0700243 for(Link l : linksToDelete) {
244 deleteLink(l);
245 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800246 }
247
Naoki Shiota11516712013-06-05 22:36:01 -0700248 /**
249 * Get list of all links connected to the switch specified by given DPID.
250 * @param dpid DPID of desired switch.
251 * @return List of links. Empty list if no port was found.
252 */
Naoki Shiota1b972862013-06-05 19:49:09 -0700253 // TODO: Fix me
Pankaj Berdeff421802013-01-29 20:28:52 -0800254 @Override
255 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700256 List<Link> links = new ArrayList<Link>();
257
Naoki Shiota93ec1712013-06-13 15:49:36 -0700258 ISwitchObject srcSw = dbop.searchSwitch(dpid);
259
260 if(srcSw != null) {
261 for(IPortObject srcPort : srcSw.getPorts()) {
262 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
263 ISwitchObject dstSw = dstPort.getSwitch();
264 if(dstSw != null) {
265 Link link = new Link(HexString.toLong(srcSw.getDPID()),
266 srcPort.getNumber(),
267 HexString.toLong(dstSw.getDPID()),
268 dstPort.getNumber());
269 links.add(link);
270 }
271 }
272 }
273 }
274
mininet403d5892013-06-05 03:48:17 -0700275 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800276 }
277
Naoki Shiota11516712013-06-05 22:36:01 -0700278 /**
279 * Get list of all links whose state is ACTIVE.
280 * @return List of active links. Empty list if no port was found.
281 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800282 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700283 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800284
285 List<Link> links = new ArrayList<Link>();
286 for (ISwitchObject sw : switches) {
287 GremlinPipeline<Vertex, Link> pipe = new GremlinPipeline<Vertex, Link>();
288 ExtractLink extractor = new ExtractLink();
289
290 pipe.start(sw.asVertex());
291 pipe.enablePath(true);
Pankaj Berde1f10be02013-02-01 14:06:02 -0800292 pipe.out("on").out("link").in("on").path().step(extractor);
Pankaj Berde5024ec12013-01-31 17:07:29 -0800293
Pankaj Berde1f10be02013-02-01 14:06:02 -0800294 while (pipe.hasNext() ) {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800295 Link l = pipe.next();
296 links.add(l);
297 }
298
299 }
300 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800301 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800302
HIGUCHI Yuta909de462013-06-12 14:32:26 -0700303 // FIXME Scope changed to public to allow access from TopoLinkServiceImpl. Move class definition to appropriate place.
304 static public class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Pankaj Berde5024ec12013-01-31 17:07:29 -0800305
Pankaj Berde5024ec12013-01-31 17:07:29 -0800306 @Override
307 public Link compute(PathPipe<Vertex> pipe ) {
Pankaj Berded18c7622013-02-04 10:28:35 -0800308 // TODO Auto-generated method stub
Pankaj Berde5024ec12013-01-31 17:07:29 -0800309 long s_dpid = 0;
310 long d_dpid = 0;
311 short s_port = 0;
312 short d_port = 0;
313 List<Vertex> V = new ArrayList<Vertex>();
314 V = pipe.next();
315 Vertex src_sw = V.get(0);
316 Vertex dest_sw = V.get(3);
317 Vertex src_port = V.get(1);
318 Vertex dest_port = V.get(2);
319 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
320 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
321 s_port = (Short) src_port.getProperty("number");
322 d_port = (Short) dest_port.getProperty("number");
323
324 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
325
326 return l;
327 }
328 }
Pankaj Berded18c7622013-02-04 10:28:35 -0800329
Naoki Shiota11516712013-06-05 22:36:01 -0700330 /**
331 * Finalize the object.
332 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800333 public void finalize() {
334 close();
335 }
336
Naoki Shiota11516712013-06-05 22:36:01 -0700337 /**
338 * Close LinkStorage.
339 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800340 @Override
341 public void close() {
342 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700343// graph.shutdown();
Pankaj Berded18c7622013-02-04 10:28:35 -0800344 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800345
Pankaj Berdeff421802013-01-29 20:28:52 -0800346
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800347}