blob: 6e42771c8fbe68e0aa515c5a88f7323c75568a05 [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.routing.Link;
Pankaj Berde38646d62013-06-21 11:34:04 -07007import net.onrc.onos.graph.GraphDBOperation;
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;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070011import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080012
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080013import org.openflow.util.HexString;
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080017import com.thinkaurelius.titan.core.TitanException;
Naoki Shiotac57efb82013-06-18 13:40:28 -070018import com.tinkerpop.blueprints.Direction;
19import com.tinkerpop.blueprints.Edge;
20import com.tinkerpop.blueprints.Vertex;
21import com.tinkerpop.gremlin.java.GremlinPipeline;
22import com.tinkerpop.pipes.PipeFunction;
23import com.tinkerpop.pipes.transform.PathPipe;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080024
Naoki Shiota1c393ce2013-06-28 22:55:14 -070025/**
26 * This is the class for storing the information of links into CassandraDB
27 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080028public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070029
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080030 protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Toshio Koide7bdea5b2013-06-13 13:57:18 -070031 protected GraphDBOperation dbop;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080032
Naoki Shiota11516712013-06-05 22:36:01 -070033 /**
34 * Update a record in the LinkStorage in a way provided by op.
35 * @param link Record of a link to be updated.
36 * @param op Operation to be done.
37 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080038 @Override
39 public void update(Link link, DM_OPERATION op) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080040 update(link, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080041 }
42
Naoki Shiota11516712013-06-05 22:36:01 -070043 /**
44 * Update multiple records in the LinkStorage in a way provided by op.
45 * @param links List of records to be updated.
46 * @param op Operation to be done.
47 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080048 @Override
49 public void update(List<Link> links, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080050 for (Link lt: links) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080051 update(lt, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080052 }
53 }
54
Naoki Shiota11516712013-06-05 22:36:01 -070055 /**
56 * Update a record of link with meta-information in the LinkStorage in a way provided by op.
57 * @param link Record of a link to update.
58 * @param linkinfo Meta-information of a link to be updated.
59 * @param op Operation to be done.
60 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080061 @Override
62 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080063 switch (op) {
64 case UPDATE:
65 case CREATE:
66 case INSERT:
Pankaj Berde254abb42013-06-10 21:21:49 -070067 updateLink(link, linkinfo, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080068 break;
69 case DELETE:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080070 deleteLink(link);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080071 break;
72 }
73 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080074
Naoki Shiota11516712013-06-05 22:36:01 -070075 /**
76 * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
77 * @param lt Record of a link to be updated.
78 * @param linkinfo Meta-information of a link to be updated.
79 * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
80 */
Pankaj Berde254abb42013-06-10 21:21:49 -070081 public void updateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
Pankaj Berde5fb27632013-04-05 08:56:12 -070082 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080083
Pankaj Berde254abb42013-06-10 21:21:49 -070084 log.trace("updateLink(): op {} {} {}", new Object[]{op, lt, linkinfo});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080085
86 try {
87 // get source port vertex
88 String dpid = HexString.toHexString(lt.getSrc());
89 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070090 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080091
92 // get dest port vertex
93 dpid = HexString.toHexString(lt.getDst());
94 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070095 vportDst = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080096
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080097 if (vportSrc != null && vportDst != null) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080098 // check if the link exists
Pankaj Berde77b58512013-04-05 12:06:37 -070099
Pankaj Berde62016142013-04-09 15:35:50 -0700100 Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
101 List<IPortObject> currLinks = new ArrayList<IPortObject>();
Pankaj Berde77b58512013-04-05 12:06:37 -0700102 for (IPortObject V : currPorts) {
Pankaj Berde6debb042013-01-16 18:04:32 -0800103 currLinks.add(V);
104 }
Naoki Shiotacae568a2013-06-05 17:53:41 -0700105
Pankaj Berde6debb042013-01-16 18:04:32 -0800106 if (currLinks.contains(vportDst)) {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700107 // TODO: update linkinfo
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800108 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800109 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800110 new Object[]{op, lt, vportSrc, vportDst});
111 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800112 } else {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700113 vportSrc.setLinkPort(vportDst);
Pankaj Berde62016142013-04-09 15:35:50 -0700114
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700115 dbop.commit();
Pankaj Berde254abb42013-06-10 21:21:49 -0700116 log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800117 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800118 } else {
Pankaj Berde254abb42013-06-10 21:21:49 -0700119 log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700120 dbop.rollback();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800121 }
122 } catch (TitanException e) {
123 /*
124 * retry till we succeed?
125 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800126 e.printStackTrace();
Pankaj Berde254abb42013-06-10 21:21:49 -0700127 log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800128 }
129 }
130
Naoki Shiota11516712013-06-05 22:36:01 -0700131 /**
Naoki Shiota1c393ce2013-06-28 22:55:14 -0700132 * Delete multiple records in LinkStorage.
Naoki Shiota11516712013-06-05 22:36:01 -0700133 * @param links List of records to be deleted.
134 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800135 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800136 public void deleteLinks(List<Link> links) {
137
138 for (Link lt : links) {
139 deleteLink(lt);
140 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800141 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800142
Naoki Shiota11516712013-06-05 22:36:01 -0700143 /**
144 * Delete a record in the LinkStorage.
145 * @param link Record to be deleted.
146 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800147 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800148 public void deleteLink(Link lt) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700149 IPortObject vportSrc = null, vportDst = null;
Naoki Shiotac57efb82013-06-18 13:40:28 -0700150 int count = 0;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800151
152 log.debug("deleteLink(): {}", lt);
153
154 try {
155 // get source port vertex
156 String dpid = HexString.toHexString(lt.getSrc());
157 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700158 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800159
160 // get dst port vertex
161 dpid = HexString.toHexString(lt.getDst());
162 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700163 vportDst = dbop.searchPort(dpid, port);
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700164 // FIXME: This needs to remove all edges
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800165
166 if (vportSrc != null && vportDst != null) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700167
168 /* for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800169 log.debug("deleteLink(): {} in {} out {}",
170 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
171 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800172 graph.removeEdge(e);
173 count++;
174 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700175 }*/
176 vportSrc.removeLink(vportDst);
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700177 dbop.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700178 log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
179 lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800180
181 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800182 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700183 dbop.rollback();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800184 }
185
186 } catch (TitanException e) {
187 /*
188 * retry till we succeed?
189 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800190 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700191 dbop.rollback();
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800192 e.printStackTrace();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800193 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800194 }
195
Naoki Shiota11516712013-06-05 22:36:01 -0700196 /**
197 * Get list of all links connected to the port specified by given DPID and port number.
198 * @param dpid DPID of desired port.
199 * @param port Port number of desired port.
200 * @return List of links. Empty list if no port was found.
201 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700202 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800203 @Override
204 public List<Link> getLinks(Long dpid, short port) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700205 List<Link> links = new ArrayList<Link>();
206
207 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
208 ISwitchObject srcSw = srcPort.getSwitch();
209
210 if(srcSw != null) {
211 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
212 ISwitchObject dstSw = dstPort.getSwitch();
213 Link link = new Link(HexString.toLong(srcSw.getDPID()),
214 srcPort.getNumber(),
215 HexString.toLong(dstSw.getDPID()),
216 dstPort.getNumber());
217
218 links.add(link);
219 }
220 }
221
mininet403d5892013-06-05 03:48:17 -0700222 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800223 }
224
Naoki Shiota11516712013-06-05 22:36:01 -0700225 /**
226 * Initialize the object. Open LinkStorage using given configuration file.
227 * @param conf Path (absolute path for now) to configuration file.
228 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800229 @Override
230 public void init(String conf) {
Pankaj Berde1f10be02013-02-01 14:06:02 -0800231 //TODO extract the DB location from properties
Toshio Koidebfe9b922013-06-18 10:56:05 -0700232 this.dbop = new GraphDBOperation(conf);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800233 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800234
Naoki Shiota11516712013-06-05 22:36:01 -0700235 /**
236 * Delete records of the links connected to the port specified by given DPID and port number.
237 * @param dpid DPID of desired port.
238 * @param port Port number of desired port.
239 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700240 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800241 @Override
242 public void deleteLinksOnPort(Long dpid, short port) {
mininet403d5892013-06-05 03:48:17 -0700243 List<Link> linksToDelete = getLinks(dpid,port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800244
mininet403d5892013-06-05 03:48:17 -0700245 for(Link l : linksToDelete) {
246 deleteLink(l);
247 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800248 }
249
Naoki Shiota11516712013-06-05 22:36:01 -0700250 /**
251 * Get list of all links connected to the switch specified by given DPID.
252 * @param dpid DPID of desired switch.
253 * @return List of links. Empty list if no port was found.
254 */
Naoki Shiota1b972862013-06-05 19:49:09 -0700255 // TODO: Fix me
Pankaj Berdeff421802013-01-29 20:28:52 -0800256 @Override
257 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700258 List<Link> links = new ArrayList<Link>();
259
Naoki Shiota93ec1712013-06-13 15:49:36 -0700260 ISwitchObject srcSw = dbop.searchSwitch(dpid);
261
262 if(srcSw != null) {
263 for(IPortObject srcPort : srcSw.getPorts()) {
264 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
265 ISwitchObject dstSw = dstPort.getSwitch();
266 if(dstSw != null) {
267 Link link = new Link(HexString.toLong(srcSw.getDPID()),
268 srcPort.getNumber(),
269 HexString.toLong(dstSw.getDPID()),
270 dstPort.getNumber());
271 links.add(link);
272 }
273 }
274 }
275 }
276
mininet403d5892013-06-05 03:48:17 -0700277 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800278 }
279
Naoki Shiota11516712013-06-05 22:36:01 -0700280 /**
281 * Get list of all links whose state is ACTIVE.
282 * @return List of active links. Empty list if no port was found.
283 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800284 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700285 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800286
287 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700288
289 for (ISwitchObject srcSw : switches) {
290 for(IPortObject srcPort : srcSw.getPorts()) {
291 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
292 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800293
Naoki Shiota826e84a2013-06-18 13:13:25 -0700294 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
295 links.add(new Link(HexString.toLong(srcSw.getDPID()),
296 srcPort.getNumber(),
297 HexString.toLong(dstSw.getDPID()),
298 dstPort.getNumber()));
299 }
300 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800301 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800302 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700303
Pankaj Berde5024ec12013-01-31 17:07:29 -0800304 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800305 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800306
HIGUCHI Yutaf05c4802013-06-17 11:15:50 -0700307 static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700308
309 @Override
310 public Link compute(PathPipe<Vertex> pipe ) {
311 // TODO Auto-generated method stub
312 long s_dpid = 0;
313 long d_dpid = 0;
314 short s_port = 0;
315 short d_port = 0;
316 List<Vertex> V = new ArrayList<Vertex>();
317 V = pipe.next();
318 Vertex src_sw = V.get(0);
319 Vertex dest_sw = V.get(3);
320 Vertex src_port = V.get(1);
321 Vertex dest_port = V.get(2);
322 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
323 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
324 s_port = (Short) src_port.getProperty("number");
325 d_port = (Short) dest_port.getProperty("number");
326
327 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
328
329 return l;
330 }
331 }
332
Naoki Shiota11516712013-06-05 22:36:01 -0700333 /**
334 * Finalize the object.
335 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800336 public void finalize() {
337 close();
338 }
339
Naoki Shiota11516712013-06-05 22:36:01 -0700340 /**
341 * Close LinkStorage.
342 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800343 @Override
344 public void close() {
345 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700346// graph.shutdown();
Pankaj Berded18c7622013-02-04 10:28:35 -0800347 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800348
Pankaj Berdeff421802013-01-29 20:28:52 -0800349
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800350}