HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.core.internal; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 2 | |
Pankaj Berde | 6debb04 | 2013-01-16 18:04:32 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 4 | import java.util.List; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 5 | |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.routing.Link; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 7 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 8 | import net.onrc.onos.ofcontroller.core.ILinkStorage; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 11 | import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 12 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 13 | import org.openflow.util.HexString; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
Naoki Shiota | 1c393ce | 2013-06-28 22:55:14 -0700 | [diff] [blame] | 17 | /** |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 18 | * This is the class for storing the information of links into GraphDB |
Naoki Shiota | 1c393ce | 2013-06-28 22:55:14 -0700 | [diff] [blame] | 19 | */ |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 20 | public class LinkStorageImpl implements ILinkStorage { |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 21 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 22 | protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 23 | protected GraphDBOperation op; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 24 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 25 | |
| 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 32 | this.op = new GraphDBOperation(conf); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 35 | // 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 Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 42 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 43 | * Update a record in the LinkStorage in a way provided by dmop. |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 44 | * @param link Record of a link to be updated. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 45 | * @param linkinfo Meta-information of a link to be updated. |
| 46 | * @param dmop Operation to be done. |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 47 | */ |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 48 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 49 | public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) { |
| 50 | boolean success = false; |
| 51 | |
| 52 | switch (dmop) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 53 | case CREATE: |
| 54 | case INSERT: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 55 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 67 | break; |
| 68 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 69 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 80 | } |
| 81 | break; |
| 82 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 83 | 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 99 | break; |
| 100 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 101 | |
| 102 | return success; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 105 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 106 | 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 Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 118 | log.debug("Adding linkinfo failed: {}", link); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 119 | op.rollback(); |
| 120 | } |
| 121 | op.commit(); |
| 122 | success = true; |
| 123 | } else { |
Jonathan Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 124 | // 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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 127 | op.rollback(); |
| 128 | } |
| 129 | } catch (Exception e) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 130 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 131 | e.printStackTrace(); |
| 132 | log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo); |
| 133 | } |
| 134 | |
| 135 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 138 | /** |
| 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 Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 143 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 144 | public boolean addLinks(List<Link> links) { |
| 145 | boolean success = false; |
| 146 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 147 | for (Link lt: links) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 148 | if (! addLinkImpl(lt)) { |
| 149 | return false; |
| 150 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 151 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 152 | |
| 153 | try { |
| 154 | op.commit(); |
| 155 | success = true; |
| 156 | } catch (Exception e) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 157 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 158 | e.printStackTrace(); |
| 159 | log.error("LinkStorageImpl:addLinks link:s{} failed", links); |
| 160 | } |
| 161 | |
| 162 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 166 | * 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 Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Delete multiple records in LinkStorage. |
| 196 | * @param links List of records to be deleted. |
| 197 | */ |
| 198 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 199 | 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 Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 215 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 216 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 217 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 220 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 221 | * 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 Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 226 | @Override |
| 227 | public List<Link> getLinks(Long dpid, short port) { |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 228 | 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 Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 236 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 237 | for(IPortObject dstPort : srcPort.getLinkedPorts()) { |
| 238 | ISwitchObject dstSw = dstPort.getSwitch(); |
| 239 | if (dstSw != null) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 240 | Link link = new Link(dpid, port, |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 241 | HexString.toLong(dstSw.getDPID()), |
| 242 | dstPort.getNumber()); |
| 243 | links.add(link); |
| 244 | } |
| 245 | } |
| 246 | return links; |
Pavlin Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 247 | } |
| 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 Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 257 | List<Link> links = new ArrayList<Link>(); |
Pavlin Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 258 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 259 | 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 Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 265 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 266 | 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 Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 271 | dpid, port); |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 272 | links.add(link); |
| 273 | } |
| 274 | } |
| 275 | return links; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 278 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 279 | * 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 Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 283 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 284 | public boolean deleteLinksOnPort(Long dpid, short port) { |
| 285 | boolean success = false; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 286 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 287 | 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); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 302 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 303 | |
| 304 | return success; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 307 | /** |
| 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 Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 312 | @Override |
| 313 | public List<Link> getLinks(String dpid) { |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 314 | List<Link> links = new ArrayList<Link>(); |
| 315 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 316 | ISwitchObject srcSw = op.searchSwitch(dpid); |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 317 | |
| 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 Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 323 | Link link = new Link(HexString.toLong(dpid), |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 324 | srcPort.getNumber(), |
| 325 | HexString.toLong(dstSw.getDPID()), |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 326 | dstPort.getNumber()); |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 327 | links.add(link); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 333 | return links; |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 336 | /** |
Pavlin Radoslavov | c934b4a | 2013-11-02 14:53:52 -0700 | [diff] [blame] | 337 | * 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 Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 357 | HexString.toLong(dpid), |
Pavlin Radoslavov | c934b4a | 2013-11-02 14:53:52 -0700 | [diff] [blame] | 358 | srcPort.getNumber()); |
| 359 | links.add(link); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return links; |
| 366 | } |
| 367 | |
| 368 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 369 | * Get list of all links whose state is ACTIVE. |
| 370 | * @return List of active links. Empty list if no port was found. |
| 371 | */ |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 372 | public List<Link> getActiveLinks() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 373 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 374 | |
| 375 | List<Link> links = new ArrayList<Link>(); |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 376 | |
| 377 | for (ISwitchObject srcSw : switches) { |
| 378 | for(IPortObject srcPort : srcSw.getPorts()) { |
| 379 | for(IPortObject dstPort : srcPort.getLinkedPorts()) { |
| 380 | ISwitchObject dstSw = dstPort.getSwitch(); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 381 | |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 382 | 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 Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 389 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 390 | } |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 391 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 392 | return links; |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 393 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 394 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 395 | @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 Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 417 | /** |
| 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 Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 489 | } |