blob: 635e24ece25c3f25487fc72b6e59539110eb4213 [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
Naoki Shiota1c393ce2013-06-28 22:55:14 -070017/**
Naoki Shiotab2d17e82013-10-18 18:08:16 -070018 * This is the class for storing the information of links into GraphDB
Naoki Shiota1c393ce2013-06-28 22:55:14 -070019 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080020public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070021
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070022 protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Naoki Shiota987a5722013-10-23 11:59:36 -070023 protected GraphDBOperation op;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080024
Naoki Shiotab2d17e82013-10-18 18:08:16 -070025
26 /**
27 * Initialize the object. Open LinkStorage using given configuration file.
28 * @param conf Path (absolute path for now) to configuration file.
29 */
30 @Override
31 public void init(String conf) {
Naoki Shiota987a5722013-10-23 11:59:36 -070032 this.op = new GraphDBOperation(conf);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070033 }
34
Naoki Shiota987a5722013-10-23 11:59:36 -070035 // Method designing policy:
36 // op.commit() and op.rollback() MUST called in public (first-class) methods.
37 // A first-class method MUST NOT call other first-class method.
38 // Routine process should be implemented in private method.
39 // A private method MUST NOT call commit or rollback.
40
41
Naoki Shiota11516712013-06-05 22:36:01 -070042 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070043 * Update a record in the LinkStorage in a way provided by dmop.
Naoki Shiota11516712013-06-05 22:36:01 -070044 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070045 * @param linkinfo Meta-information of a link to be updated.
46 * @param dmop Operation to be done.
Naoki Shiota11516712013-06-05 22:36:01 -070047 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080048 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070049 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
50 boolean success = false;
51
52 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070053 case CREATE:
54 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070055 if (link != null) {
56 try {
57 if (addLinkImpl(link)) {
58 op.commit();
59 success = true;
60 }
61 } catch (Exception e) {
62 op.rollback();
63 e.printStackTrace();
64 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
65 }
66 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070067 break;
68 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070069 if (link != null && linkinfo != null) {
70 try {
71 if (setLinkInfoImpl(link, linkinfo)) {
72 op.commit();
73 success = true;
74 }
75 } catch (Exception e) {
76 op.rollback();
77 e.printStackTrace();
78 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
79 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070080 }
81 break;
82 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070083 if (link != null) {
84 try {
85 if (deleteLinkImpl(link)) {
86 op.commit();
87 success = true;
88 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
89 } else {
90 op.rollback();
91 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
92 }
93 } catch (Exception e) {
94 op.rollback();
95 e.printStackTrace();
96 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
97 }
98 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070099 break;
100 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700101
102 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800103 }
104
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700105 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700106 public boolean addLink(Link link) {
107 return addLink(link, null);
108 }
109
110 @Override
111 public boolean addLink(Link link, LinkInfo linfo) {
112 boolean success = false;
113
114 try {
115 if (addLinkImpl(link)) {
116 // Set LinkInfo only if linfo is non-null.
117 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700118 log.debug("Adding linkinfo failed: {}", link);
Naoki Shiota987a5722013-10-23 11:59:36 -0700119 op.rollback();
120 }
121 op.commit();
122 success = true;
123 } else {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700124 // If we fail here that's because the ports aren't added
125 // before we try to add the link
126 log.debug("Adding link failed: {}", link);
Naoki Shiota987a5722013-10-23 11:59:36 -0700127 op.rollback();
128 }
129 } catch (Exception e) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800130 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700131 e.printStackTrace();
132 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
133 }
134
135 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700136 }
137
Naoki Shiota11516712013-06-05 22:36:01 -0700138 /**
139 * Update multiple records in the LinkStorage in a way provided by op.
140 * @param links List of records to be updated.
141 * @param op Operation to be done.
142 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800143 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700144 public boolean addLinks(List<Link> links) {
145 boolean success = false;
146
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800147 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700148 if (! addLinkImpl(lt)) {
149 return false;
150 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700151 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700152
153 try {
154 op.commit();
155 success = true;
156 } catch (Exception e) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800157 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700158 e.printStackTrace();
159 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
160 }
161
162 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700163 }
164
165 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700166 * Delete a record in the LinkStorage.
167 * @param lt Record to be deleted.
168 */
169 @Override
170 public boolean deleteLink(Link lt) {
171 boolean success = false;
172
173 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
174
175 try {
176 if (deleteLinkImpl(lt)) {
177 op.commit();
178 success = true;
179 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
180 } else {
181 op.rollback();
182 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
183 }
184 } catch (Exception e) {
185 op.rollback();
186 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
187 new Object[]{lt, e.toString()});
188 e.printStackTrace();
189 }
190
191 return success;
192 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700193
194 /**
195 * Delete multiple records in LinkStorage.
196 * @param links List of records to be deleted.
197 */
198 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700199 public boolean deleteLinks(List<Link> links) {
200 boolean success = false;
201
202 try {
203 for (Link lt : links) {
204 if (! deleteLinkImpl(lt)) {
205 op.rollback();
206 return false;
207 }
208 }
209 op.commit();
210 success = true;
211 } catch (Exception e) {
212 op.rollback();
213 e.printStackTrace();
214 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800215 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700216
Naoki Shiota987a5722013-10-23 11:59:36 -0700217 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700218 }
219
Naoki Shiota11516712013-06-05 22:36:01 -0700220 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700221 * Get list of all links connected to the port specified by given DPID and port number.
222 * @param dpid DPID of desired port.
223 * @param port Port number of desired port.
224 * @return List of links. Empty list if no port was found.
225 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800226 @Override
227 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800228 List<Link> links = new ArrayList<Link>();
229
230 IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
231 if (srcPort == null)
232 return links;
233 ISwitchObject srcSw = srcPort.getSwitch();
234 if (srcSw == null)
235 return links;
Naoki Shiota93ec1712013-06-13 15:49:36 -0700236
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800237 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
238 ISwitchObject dstSw = dstPort.getSwitch();
239 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800240 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800241 HexString.toLong(dstSw.getDPID()),
242 dstPort.getNumber());
243 links.add(link);
244 }
245 }
246 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800247 }
248
249 /**
250 * Get list of all reverse links connected to the port specified by given DPID and port number.
251 * @param dpid DPID of desired port.
252 * @param port Port number of desired port.
253 * @return List of reverse links. Empty list if no port was found.
254 */
255 @Override
256 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800257 List<Link> links = new ArrayList<Link>();
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800258
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800259 IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
260 if (srcPort == null)
261 return links;
262 ISwitchObject srcSw = srcPort.getSwitch();
263 if (srcSw == null)
264 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800265
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800266 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
267 ISwitchObject dstSw = dstPort.getSwitch();
268 if (dstSw != null) {
269 Link link = new Link(HexString.toLong(dstSw.getDPID()),
270 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800271 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800272 links.add(link);
273 }
274 }
275 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800276 }
277
Naoki Shiota11516712013-06-05 22:36:01 -0700278 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700279 * Delete records of the links connected to the port specified by given DPID and port number.
280 * @param dpid DPID of desired port.
281 * @param port Port number of desired port.
282 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800283 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700284 public boolean deleteLinksOnPort(Long dpid, short port) {
285 boolean success = false;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800286
Naoki Shiota987a5722013-10-23 11:59:36 -0700287 List<Link> linksToDelete = getLinks(dpid, port);
288 try {
289 for(Link l : linksToDelete) {
290 if (! deleteLinkImpl(l)) {
291 op.rollback();
292 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
293 return false;
294 }
295 }
296 op.commit();
297 success = true;
298 } catch (Exception e) {
299 op.rollback();
300 e.printStackTrace();
301 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700302 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700303
304 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800305 }
306
Naoki Shiota11516712013-06-05 22:36:01 -0700307 /**
308 * Get list of all links connected to the switch specified by given DPID.
309 * @param dpid DPID of desired switch.
310 * @return List of links. Empty list if no port was found.
311 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800312 @Override
313 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700314 List<Link> links = new ArrayList<Link>();
315
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 ISwitchObject srcSw = op.searchSwitch(dpid);
Naoki Shiota93ec1712013-06-13 15:49:36 -0700317
318 if(srcSw != null) {
319 for(IPortObject srcPort : srcSw.getPorts()) {
320 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
321 ISwitchObject dstSw = dstPort.getSwitch();
322 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800323 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700324 srcPort.getNumber(),
325 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800326 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700327 links.add(link);
328 }
329 }
330 }
331 }
332
mininet403d5892013-06-05 03:48:17 -0700333 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800334 }
335
Naoki Shiota11516712013-06-05 22:36:01 -0700336 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700337 * Get list of all reverse links connected to the switch specified by
338 * given DPID.
339 * @param dpid DPID of desired switch.
340 * @return List of reverse links. Empty list if no port was found.
341 */
342 @Override
343 public List<Link> getReverseLinks(String dpid) {
344 List<Link> links = new ArrayList<Link>();
345
346 ISwitchObject srcSw = op.searchSwitch(dpid);
347
348 if(srcSw != null) {
349 for(IPortObject srcPort : srcSw.getPorts()) {
350 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
351 ISwitchObject dstSw = dstPort.getSwitch();
352 if(dstSw != null) {
353 Link link = new Link(
354 HexString.toLong(dstSw.getDPID()),
355 dstPort.getNumber(),
356
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800357 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700358 srcPort.getNumber());
359 links.add(link);
360 }
361 }
362 }
363 }
364
365 return links;
366 }
367
368 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700369 * Get list of all links whose state is ACTIVE.
370 * @return List of active links. Empty list if no port was found.
371 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800372 public List<Link> getActiveLinks() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700373 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800374
375 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700376
377 for (ISwitchObject srcSw : switches) {
378 for(IPortObject srcPort : srcSw.getPorts()) {
379 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
380 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800381
Naoki Shiota826e84a2013-06-18 13:13:25 -0700382 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
383 links.add(new Link(HexString.toLong(srcSw.getDPID()),
384 srcPort.getNumber(),
385 HexString.toLong(dstSw.getDPID()),
386 dstPort.getNumber()));
387 }
388 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800389 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800390 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700391
Pankaj Berde5024ec12013-01-31 17:07:29 -0800392 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800393 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800394
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700395 @Override
396 public LinkInfo getLinkInfo(Link link) {
397 // TODO implement this
398 return null;
399 }
400
401 /**
402 * Finalize the object.
403 */
404 public void finalize() {
405 close();
406 }
407
408 /**
409 * Close LinkStorage.
410 */
411 @Override
412 public void close() {
413 // TODO Auto-generated method stub
414// graph.shutdown();
415 }
416
Naoki Shiota987a5722013-10-23 11:59:36 -0700417 /**
418 * Update a record of link with meta-information in the LinkStorage.
419 * @param link Record of a link to update.
420 * @param linkinfo Meta-information of a link to be updated.
421 */
422 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
423 // TODO implement this
424
425 return false;
426 }
427
428 private boolean addLinkImpl(Link lt) {
429 boolean success = false;
430
431 IPortObject vportSrc = null, vportDst = null;
432
433 // get source port vertex
434 String dpid = HexString.toHexString(lt.getSrc());
435 short port = lt.getSrcPort();
436 vportSrc = op.searchPort(dpid, port);
437
438 // get dest port vertex
439 dpid = HexString.toHexString(lt.getDst());
440 port = lt.getDstPort();
441 vportDst = op.searchPort(dpid, port);
442
443 if (vportSrc != null && vportDst != null) {
444 IPortObject portExist = null;
445 // check if the link exists
446 for (IPortObject V : vportSrc.getLinkedPorts()) {
447 if (V.equals(vportDst)) {
448 portExist = V;
449 break;
450 }
451 }
452
453 if (portExist == null) {
454 vportSrc.setLinkPort(vportDst);
455 success = true;
456 } else {
457 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
458 new Object[]{op, lt, vportSrc, vportDst});
459 }
460 }
461
462 return success;
463 }
464
465 private boolean deleteLinkImpl(Link lt) {
466 boolean success = false;
467 IPortObject vportSrc = null, vportDst = null;
468
469 // get source port vertex
470 String dpid = HexString.toHexString(lt.getSrc());
471 short port = lt.getSrcPort();
472 vportSrc = op.searchPort(dpid, port);
473
474 // get dst port vertex
475 dpid = HexString.toHexString(lt.getDst());
476 port = lt.getDstPort();
477 vportDst = op.searchPort(dpid, port);
478
479 // FIXME: This needs to remove all edges
480 if (vportSrc != null && vportDst != null) {
481 vportSrc.removeLink(vportDst);
482 log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{
483 lt, vportSrc, vportDst});
484 success = true;
485 }
486
487 return success;
488 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800489}