blob: 51a88dbf5b6ffc3f317eb8bcb85403e56e1d8685 [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.Vertex;
Naoki Shiotac57efb82013-06-18 13:40:28 -070019import com.tinkerpop.pipes.PipeFunction;
20import com.tinkerpop.pipes.transform.PathPipe;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080021
Naoki Shiota1c393ce2013-06-28 22:55:14 -070022/**
23 * This is the class for storing the information of links into CassandraDB
24 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080025public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070026
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080027 protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Toshio Koide7bdea5b2013-06-13 13:57:18 -070028 protected GraphDBOperation dbop;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080029
Naoki Shiota11516712013-06-05 22:36:01 -070030 /**
31 * Update a record in the LinkStorage in a way provided by op.
32 * @param link Record of a link to be updated.
33 * @param op Operation to be done.
34 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080035 @Override
36 public void update(Link link, DM_OPERATION op) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080037 update(link, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080038 }
39
Naoki Shiota11516712013-06-05 22:36:01 -070040 /**
41 * Update multiple records in the LinkStorage in a way provided by op.
42 * @param links List of records to be updated.
43 * @param op Operation to be done.
44 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080045 @Override
46 public void update(List<Link> links, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080047 for (Link lt: links) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080048 update(lt, (LinkInfo)null, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080049 }
50 }
51
Naoki Shiota11516712013-06-05 22:36:01 -070052 /**
53 * Update a record of link with meta-information in the LinkStorage in a way provided by op.
54 * @param link Record of a link to update.
55 * @param linkinfo Meta-information of a link to be updated.
56 * @param op Operation to be done.
57 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080058 @Override
59 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080060 switch (op) {
61 case UPDATE:
62 case CREATE:
63 case INSERT:
Pankaj Berde254abb42013-06-10 21:21:49 -070064 updateLink(link, linkinfo, op);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080065 break;
66 case DELETE:
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080067 deleteLink(link);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080068 break;
69 }
70 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080071
Naoki Shiota11516712013-06-05 22:36:01 -070072 /**
73 * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
74 * @param lt Record of a link to be updated.
75 * @param linkinfo Meta-information of a link to be updated.
76 * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
77 */
Pankaj Berde254abb42013-06-10 21:21:49 -070078 public void updateLink(Link lt, LinkInfo linkinfo, DM_OPERATION op) {
Pankaj Berde5fb27632013-04-05 08:56:12 -070079 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080080
Pankaj Berde254abb42013-06-10 21:21:49 -070081 log.trace("updateLink(): op {} {} {}", new Object[]{op, lt, linkinfo});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080082
83 try {
84 // get source port vertex
85 String dpid = HexString.toHexString(lt.getSrc());
86 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070087 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080088
89 // get dest port vertex
90 dpid = HexString.toHexString(lt.getDst());
91 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -070092 vportDst = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080093
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080094 if (vportSrc != null && vportDst != null) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080095 // check if the link exists
Pankaj Berde77b58512013-04-05 12:06:37 -070096
Pankaj Berde62016142013-04-09 15:35:50 -070097 Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
98 List<IPortObject> currLinks = new ArrayList<IPortObject>();
Pankaj Berde77b58512013-04-05 12:06:37 -070099 for (IPortObject V : currPorts) {
Pankaj Berde6debb042013-01-16 18:04:32 -0800100 currLinks.add(V);
101 }
Naoki Shiotacae568a2013-06-05 17:53:41 -0700102
Pankaj Berde6debb042013-01-16 18:04:32 -0800103 if (currLinks.contains(vportDst)) {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700104 // TODO: update linkinfo
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800105 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800106 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800107 new Object[]{op, lt, vportSrc, vportDst});
108 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800109 } else {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700110 vportSrc.setLinkPort(vportDst);
Pankaj Berde62016142013-04-09 15:35:50 -0700111
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700112 dbop.commit();
Pankaj Berde254abb42013-06-10 21:21:49 -0700113 log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800114 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800115 } else {
Pankaj Berde254abb42013-06-10 21:21:49 -0700116 log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700117 dbop.rollback();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800118 }
119 } catch (TitanException e) {
120 /*
121 * retry till we succeed?
122 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800123 e.printStackTrace();
Pankaj Berde254abb42013-06-10 21:21:49 -0700124 log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800125 }
126 }
127
Naoki Shiota11516712013-06-05 22:36:01 -0700128 /**
Naoki Shiota1c393ce2013-06-28 22:55:14 -0700129 * Delete multiple records in LinkStorage.
Naoki Shiota11516712013-06-05 22:36:01 -0700130 * @param links List of records to be deleted.
131 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800132 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800133 public void deleteLinks(List<Link> links) {
134
135 for (Link lt : links) {
136 deleteLink(lt);
137 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800138 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800139
Naoki Shiota11516712013-06-05 22:36:01 -0700140 /**
141 * Delete a record in the LinkStorage.
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -0700142 * @param lt Record to be deleted.
Naoki Shiota11516712013-06-05 22:36:01 -0700143 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800144 @Override
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800145 public void deleteLink(Link lt) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700146 IPortObject vportSrc = null, vportDst = null;
Naoki Shiotac57efb82013-06-18 13:40:28 -0700147 int count = 0;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800148
149 log.debug("deleteLink(): {}", lt);
150
151 try {
152 // get source port vertex
153 String dpid = HexString.toHexString(lt.getSrc());
154 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700155 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800156
157 // get dst port vertex
158 dpid = HexString.toHexString(lt.getDst());
159 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700160 vportDst = dbop.searchPort(dpid, port);
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700161 // FIXME: This needs to remove all edges
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800162
163 if (vportSrc != null && vportDst != null) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700164
165 /* for (Edge e : vportSrc.asVertex().getEdges(Direction.OUT)) {
Umesh Krishnaswamy11060ed2013-01-23 19:24:36 -0800166 log.debug("deleteLink(): {} in {} out {}",
167 new Object[]{e.getLabel(), e.getVertex(Direction.IN), e.getVertex(Direction.OUT)});
168 if (e.getLabel().equals("link") && e.getVertex(Direction.IN).equals(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800169 graph.removeEdge(e);
170 count++;
171 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700172 }*/
173 vportSrc.removeLink(vportDst);
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700174 dbop.commit();
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700175 log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
176 lt, vportSrc, vportDst});
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800177
178 } else {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800179 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700180 dbop.rollback();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800181 }
182
183 } catch (TitanException e) {
184 /*
185 * retry till we succeed?
186 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800187 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700188 dbop.rollback();
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800189 e.printStackTrace();
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800190 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800191 }
192
Naoki Shiota11516712013-06-05 22:36:01 -0700193 /**
194 * Get list of all links connected to the port specified by given DPID and port number.
195 * @param dpid DPID of desired port.
196 * @param port Port number of desired port.
197 * @return List of links. Empty list if no port was found.
198 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700199 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800200 @Override
201 public List<Link> getLinks(Long dpid, short port) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700202 List<Link> links = new ArrayList<Link>();
203
204 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
205 ISwitchObject srcSw = srcPort.getSwitch();
206
Pankaj Berdee7c21522013-08-01 16:52:29 -0700207 if(srcSw != null && srcPort != null) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700208 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
209 ISwitchObject dstSw = dstPort.getSwitch();
210 Link link = new Link(HexString.toLong(srcSw.getDPID()),
211 srcPort.getNumber(),
212 HexString.toLong(dstSw.getDPID()),
213 dstPort.getNumber());
214
215 links.add(link);
216 }
217 }
218
mininet403d5892013-06-05 03:48:17 -0700219 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800220 }
221
Naoki Shiota11516712013-06-05 22:36:01 -0700222 /**
223 * Initialize the object. Open LinkStorage using given configuration file.
224 * @param conf Path (absolute path for now) to configuration file.
225 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800226 @Override
227 public void init(String conf) {
Pankaj Berde1f10be02013-02-01 14:06:02 -0800228 //TODO extract the DB location from properties
Toshio Koidebfe9b922013-06-18 10:56:05 -0700229 this.dbop = new GraphDBOperation(conf);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800230 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800231
Naoki Shiota11516712013-06-05 22:36:01 -0700232 /**
233 * Delete records of the links connected to the port specified by given DPID and port number.
234 * @param dpid DPID of desired port.
235 * @param port Port number of desired port.
236 */
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700237 // TODO: Fix me
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800238 @Override
239 public void deleteLinksOnPort(Long dpid, short port) {
mininet403d5892013-06-05 03:48:17 -0700240 List<Link> linksToDelete = getLinks(dpid,port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800241
mininet403d5892013-06-05 03:48:17 -0700242 for(Link l : linksToDelete) {
243 deleteLink(l);
244 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800245 }
246
Naoki Shiota11516712013-06-05 22:36:01 -0700247 /**
248 * Get list of all links connected to the switch specified by given DPID.
249 * @param dpid DPID of desired switch.
250 * @return List of links. Empty list if no port was found.
251 */
Naoki Shiota1b972862013-06-05 19:49:09 -0700252 // TODO: Fix me
Pankaj Berdeff421802013-01-29 20:28:52 -0800253 @Override
254 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700255 List<Link> links = new ArrayList<Link>();
256
Naoki Shiota93ec1712013-06-13 15:49:36 -0700257 ISwitchObject srcSw = dbop.searchSwitch(dpid);
258
259 if(srcSw != null) {
260 for(IPortObject srcPort : srcSw.getPorts()) {
261 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
262 ISwitchObject dstSw = dstPort.getSwitch();
263 if(dstSw != null) {
264 Link link = new Link(HexString.toLong(srcSw.getDPID()),
265 srcPort.getNumber(),
266 HexString.toLong(dstSw.getDPID()),
267 dstPort.getNumber());
268 links.add(link);
269 }
270 }
271 }
272 }
273
mininet403d5892013-06-05 03:48:17 -0700274 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800275 }
276
Naoki Shiota11516712013-06-05 22:36:01 -0700277 /**
278 * Get list of all links whose state is ACTIVE.
279 * @return List of active links. Empty list if no port was found.
280 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800281 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700282 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800283
284 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700285
286 for (ISwitchObject srcSw : switches) {
287 for(IPortObject srcPort : srcSw.getPorts()) {
288 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
289 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800290
Naoki Shiota826e84a2013-06-18 13:13:25 -0700291 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
292 links.add(new Link(HexString.toLong(srcSw.getDPID()),
293 srcPort.getNumber(),
294 HexString.toLong(dstSw.getDPID()),
295 dstPort.getNumber()));
296 }
297 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800298 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800299 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700300
Pankaj Berde5024ec12013-01-31 17:07:29 -0800301 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800302 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800303
HIGUCHI Yutaf05c4802013-06-17 11:15:50 -0700304 static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700305
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700306 @SuppressWarnings("unchecked")
Naoki Shiotac57efb82013-06-18 13:40:28 -0700307 @Override
308 public Link compute(PathPipe<Vertex> pipe ) {
309 // TODO Auto-generated method stub
310 long s_dpid = 0;
311 long d_dpid = 0;
312 short s_port = 0;
313 short d_port = 0;
314 List<Vertex> V = new ArrayList<Vertex>();
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700315 V = (List<Vertex>)pipe.next();
Naoki Shiotac57efb82013-06-18 13:40:28 -0700316 Vertex src_sw = V.get(0);
317 Vertex dest_sw = V.get(3);
318 Vertex src_port = V.get(1);
319 Vertex dest_port = V.get(2);
320 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
321 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
322 s_port = (Short) src_port.getProperty("number");
323 d_port = (Short) dest_port.getProperty("number");
324
325 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
326
327 return l;
328 }
329 }
330
Naoki Shiota11516712013-06-05 22:36:01 -0700331 /**
332 * Finalize the object.
333 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800334 public void finalize() {
335 close();
336 }
337
Naoki Shiota11516712013-06-05 22:36:01 -0700338 /**
339 * Close LinkStorage.
340 */
Pankaj Berded18c7622013-02-04 10:28:35 -0800341 @Override
342 public void close() {
343 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700344// graph.shutdown();
Pankaj Berded18c7622013-02-04 10:28:35 -0800345 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800346
Pankaj Berdeff421802013-01-29 20:28:52 -0800347
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800348}