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; |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 12 | import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 13 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 14 | import org.openflow.util.HexString; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
Naoki Shiota | 1c393ce | 2013-06-28 22:55:14 -0700 | [diff] [blame] | 18 | /** |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 19 | * This is the class for storing the information of links into GraphDB |
Naoki Shiota | 1c393ce | 2013-06-28 22:55:14 -0700 | [diff] [blame] | 20 | */ |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 21 | public class LinkStorageImpl implements ILinkStorage { |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 22 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 23 | protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 24 | protected GraphDBOperation op; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 25 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Initialize the object. Open LinkStorage using given configuration file. |
| 29 | * @param conf Path (absolute path for now) to configuration file. |
| 30 | */ |
| 31 | @Override |
| 32 | public void init(String conf) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 33 | this.op = new GraphDBOperation(conf); |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 36 | // Method designing policy: |
| 37 | // op.commit() and op.rollback() MUST called in public (first-class) methods. |
| 38 | // A first-class method MUST NOT call other first-class method. |
| 39 | // Routine process should be implemented in private method. |
| 40 | // A private method MUST NOT call commit or rollback. |
| 41 | |
| 42 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 43 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 44 | * Update a record in the LinkStorage in a way provided by dmop. |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 45 | * @param link Record of a link to be updated. |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 46 | * @param linkinfo Meta-information of a link to be updated. |
| 47 | * @param dmop Operation to be done. |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 48 | */ |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 49 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 50 | public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) { |
| 51 | boolean success = false; |
| 52 | |
| 53 | switch (dmop) { |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 54 | case CREATE: |
| 55 | case INSERT: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 56 | if (link != null) { |
| 57 | try { |
| 58 | if (addLinkImpl(link)) { |
| 59 | op.commit(); |
| 60 | success = true; |
| 61 | } |
| 62 | } catch (Exception e) { |
| 63 | op.rollback(); |
| 64 | e.printStackTrace(); |
| 65 | log.error("LinkStorageImpl:update {} link:{} failed", dmop, link); |
| 66 | } |
| 67 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 68 | break; |
| 69 | case UPDATE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 70 | if (link != null && linkinfo != null) { |
| 71 | try { |
| 72 | if (setLinkInfoImpl(link, linkinfo)) { |
| 73 | op.commit(); |
| 74 | success = true; |
| 75 | } |
| 76 | } catch (Exception e) { |
| 77 | op.rollback(); |
| 78 | e.printStackTrace(); |
| 79 | log.error("LinkStorageImpl:update {} link:{} failed", dmop, link); |
| 80 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | case DELETE: |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 84 | if (link != null) { |
| 85 | try { |
| 86 | if (deleteLinkImpl(link)) { |
| 87 | op.commit(); |
| 88 | success = true; |
| 89 | log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link); |
| 90 | } else { |
| 91 | op.rollback(); |
| 92 | log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link); |
| 93 | } |
| 94 | } catch (Exception e) { |
| 95 | op.rollback(); |
| 96 | e.printStackTrace(); |
| 97 | log.error("LinkStorageImpl:update {} link:{} failed", dmop, link); |
| 98 | } |
| 99 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 100 | break; |
| 101 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 102 | |
| 103 | return success; |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 106 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 107 | public boolean addLink(Link link) { |
| 108 | return addLink(link, null); |
| 109 | } |
| 110 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 111 | private void deleteDeviceOnPort(Long dpid, Short number) |
| 112 | { |
| 113 | IPortObject srcPortObject = op.searchPort(HexString.toHexString(dpid), number); |
Pavlin Radoslavov | 7807328 | 2013-12-19 17:01:23 -0800 | [diff] [blame^] | 114 | if (srcPortObject == null) |
| 115 | return; |
| 116 | Iterable<IDeviceObject> devices = srcPortObject.getDevices(); |
| 117 | if (devices == null) |
| 118 | return; |
| 119 | if (devices.iterator().hasNext()) { |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 120 | for (IDeviceObject deviceObject: srcPortObject.getDevices()) { |
| 121 | srcPortObject.removeDevice(deviceObject); |
| 122 | log.debug("delete Device "+ deviceObject.getMACAddress() + |
| 123 | " from sw: {} port: {} due to a new link added", |
| 124 | dpid, number); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public boolean addLink(Link link, LinkInfo linfo) { |
| 131 | boolean success = false; |
| 132 | |
| 133 | try { |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 134 | //delete the Device attachment points for the related switch and port |
| 135 | deleteDeviceOnPort(link.getSrc(),link.getSrcPort()); |
| 136 | deleteDeviceOnPort(link.getDst(),link.getDstPort()); |
| 137 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 138 | if (addLinkImpl(link)) { |
| 139 | // Set LinkInfo only if linfo is non-null. |
| 140 | if (linfo != null && (! setLinkInfoImpl(link, linfo))) { |
Jonathan Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 141 | log.debug("Adding linkinfo failed: {}", link); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 142 | op.rollback(); |
| 143 | } |
| 144 | op.commit(); |
| 145 | success = true; |
| 146 | } else { |
Jonathan Hart | 13ccdca | 2013-10-30 15:23:28 -0700 | [diff] [blame] | 147 | // If we fail here that's because the ports aren't added |
| 148 | // before we try to add the link |
| 149 | log.debug("Adding link failed: {}", link); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 150 | op.rollback(); |
| 151 | } |
| 152 | } catch (Exception e) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 153 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 154 | e.printStackTrace(); |
| 155 | log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo); |
| 156 | } |
| 157 | |
| 158 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Update multiple records in the LinkStorage in a way provided by op. |
| 163 | * @param links List of records to be updated. |
| 164 | * @param op Operation to be done. |
| 165 | */ |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 166 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 167 | public boolean addLinks(List<Link> links) { |
| 168 | boolean success = false; |
| 169 | |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 170 | for (Link lt: links) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 171 | if (! addLinkImpl(lt)) { |
| 172 | return false; |
| 173 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 174 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 175 | |
| 176 | try { |
| 177 | op.commit(); |
| 178 | success = true; |
| 179 | } catch (Exception e) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 180 | op.rollback(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 181 | e.printStackTrace(); |
| 182 | log.error("LinkStorageImpl:addLinks link:s{} failed", links); |
| 183 | } |
| 184 | |
| 185 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 189 | * Delete a record in the LinkStorage. |
| 190 | * @param lt Record to be deleted. |
| 191 | */ |
| 192 | @Override |
| 193 | public boolean deleteLink(Link lt) { |
| 194 | boolean success = false; |
| 195 | |
| 196 | log.debug("LinkStorageImpl:deleteLink(): {}", lt); |
| 197 | |
| 198 | try { |
| 199 | if (deleteLinkImpl(lt)) { |
| 200 | op.commit(); |
| 201 | success = true; |
| 202 | log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt); |
| 203 | } else { |
| 204 | op.rollback(); |
| 205 | log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt); |
| 206 | } |
| 207 | } catch (Exception e) { |
| 208 | op.rollback(); |
| 209 | log.error("LinkStorageImpl:deleteLink(): failed {} {}", |
| 210 | new Object[]{lt, e.toString()}); |
| 211 | e.printStackTrace(); |
| 212 | } |
| 213 | |
| 214 | return success; |
| 215 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Delete multiple records in LinkStorage. |
| 219 | * @param links List of records to be deleted. |
| 220 | */ |
| 221 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 222 | public boolean deleteLinks(List<Link> links) { |
| 223 | boolean success = false; |
| 224 | |
| 225 | try { |
| 226 | for (Link lt : links) { |
| 227 | if (! deleteLinkImpl(lt)) { |
| 228 | op.rollback(); |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | op.commit(); |
| 233 | success = true; |
| 234 | } catch (Exception e) { |
| 235 | op.rollback(); |
| 236 | e.printStackTrace(); |
| 237 | log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links); |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 238 | } |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 239 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 240 | return success; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 243 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 244 | * Get list of all links connected to the port specified by given DPID and port number. |
| 245 | * @param dpid DPID of desired port. |
| 246 | * @param port Port number of desired port. |
| 247 | * @return List of links. Empty list if no port was found. |
| 248 | */ |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 249 | @Override |
| 250 | public List<Link> getLinks(Long dpid, short port) { |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 251 | List<Link> links = new ArrayList<Link>(); |
| 252 | |
| 253 | IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port); |
| 254 | if (srcPort == null) |
| 255 | return links; |
| 256 | ISwitchObject srcSw = srcPort.getSwitch(); |
| 257 | if (srcSw == null) |
| 258 | return links; |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 259 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 260 | for(IPortObject dstPort : srcPort.getLinkedPorts()) { |
| 261 | ISwitchObject dstSw = dstPort.getSwitch(); |
| 262 | if (dstSw != null) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 263 | Link link = new Link(dpid, port, |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 264 | HexString.toLong(dstSw.getDPID()), |
| 265 | dstPort.getNumber()); |
| 266 | links.add(link); |
| 267 | } |
| 268 | } |
| 269 | return links; |
Pavlin Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get list of all reverse links connected to the port specified by given DPID and port number. |
| 274 | * @param dpid DPID of desired port. |
| 275 | * @param port Port number of desired port. |
| 276 | * @return List of reverse links. Empty list if no port was found. |
| 277 | */ |
| 278 | @Override |
| 279 | public List<Link> getReverseLinks(Long dpid, short port) { |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 280 | List<Link> links = new ArrayList<Link>(); |
Pavlin Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 281 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 282 | IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port); |
| 283 | if (srcPort == null) |
| 284 | return links; |
| 285 | ISwitchObject srcSw = srcPort.getSwitch(); |
| 286 | if (srcSw == null) |
| 287 | return links; |
Pavlin Radoslavov | 43781cd | 2013-11-04 18:19:10 -0800 | [diff] [blame] | 288 | |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 289 | for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) { |
| 290 | ISwitchObject dstSw = dstPort.getSwitch(); |
| 291 | if (dstSw != null) { |
| 292 | Link link = new Link(HexString.toLong(dstSw.getDPID()), |
| 293 | dstPort.getNumber(), |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 294 | dpid, port); |
Pavlin Radoslavov | d822679 | 2013-11-04 20:28:07 -0800 | [diff] [blame] | 295 | links.add(link); |
| 296 | } |
| 297 | } |
| 298 | return links; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 301 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 302 | * Delete records of the links connected to the port specified by given DPID and port number. |
| 303 | * @param dpid DPID of desired port. |
| 304 | * @param port Port number of desired port. |
| 305 | */ |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 306 | @Override |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 307 | public boolean deleteLinksOnPort(Long dpid, short port) { |
| 308 | boolean success = false; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 309 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 310 | List<Link> linksToDelete = getLinks(dpid, port); |
| 311 | try { |
| 312 | for(Link l : linksToDelete) { |
| 313 | if (! deleteLinkImpl(l)) { |
| 314 | op.rollback(); |
| 315 | log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port); |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | op.commit(); |
| 320 | success = true; |
| 321 | } catch (Exception e) { |
| 322 | op.rollback(); |
| 323 | e.printStackTrace(); |
| 324 | log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 325 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 326 | |
| 327 | return success; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Get list of all links connected to the switch specified by given DPID. |
| 332 | * @param dpid DPID of desired switch. |
| 333 | * @return List of links. Empty list if no port was found. |
| 334 | */ |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 335 | @Override |
| 336 | public List<Link> getLinks(String dpid) { |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 337 | List<Link> links = new ArrayList<Link>(); |
| 338 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 339 | ISwitchObject srcSw = op.searchSwitch(dpid); |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 340 | |
| 341 | if(srcSw != null) { |
| 342 | for(IPortObject srcPort : srcSw.getPorts()) { |
| 343 | for(IPortObject dstPort : srcPort.getLinkedPorts()) { |
| 344 | ISwitchObject dstSw = dstPort.getSwitch(); |
| 345 | if(dstSw != null) { |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 346 | Link link = new Link(HexString.toLong(dpid), |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 347 | srcPort.getNumber(), |
| 348 | HexString.toLong(dstSw.getDPID()), |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 349 | dstPort.getNumber()); |
Naoki Shiota | 93ec171 | 2013-06-13 15:49:36 -0700 | [diff] [blame] | 350 | links.add(link); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 356 | return links; |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 359 | /** |
Pavlin Radoslavov | c934b4a | 2013-11-02 14:53:52 -0700 | [diff] [blame] | 360 | * Get list of all reverse links connected to the switch specified by |
| 361 | * given DPID. |
| 362 | * @param dpid DPID of desired switch. |
| 363 | * @return List of reverse links. Empty list if no port was found. |
| 364 | */ |
| 365 | @Override |
| 366 | public List<Link> getReverseLinks(String dpid) { |
| 367 | List<Link> links = new ArrayList<Link>(); |
| 368 | |
| 369 | ISwitchObject srcSw = op.searchSwitch(dpid); |
| 370 | |
| 371 | if(srcSw != null) { |
| 372 | for(IPortObject srcPort : srcSw.getPorts()) { |
| 373 | for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) { |
| 374 | ISwitchObject dstSw = dstPort.getSwitch(); |
| 375 | if(dstSw != null) { |
| 376 | Link link = new Link( |
| 377 | HexString.toLong(dstSw.getDPID()), |
| 378 | dstPort.getNumber(), |
| 379 | |
Pavlin Radoslavov | 0aad60c | 2013-11-05 08:33:57 -0800 | [diff] [blame] | 380 | HexString.toLong(dpid), |
Pavlin Radoslavov | c934b4a | 2013-11-02 14:53:52 -0700 | [diff] [blame] | 381 | srcPort.getNumber()); |
| 382 | links.add(link); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return links; |
| 389 | } |
| 390 | |
| 391 | /** |
Naoki Shiota | 1151671 | 2013-06-05 22:36:01 -0700 | [diff] [blame] | 392 | * Get list of all links whose state is ACTIVE. |
| 393 | * @return List of active links. Empty list if no port was found. |
| 394 | */ |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 395 | public List<Link> getActiveLinks() { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 396 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 397 | |
| 398 | List<Link> links = new ArrayList<Link>(); |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 399 | |
| 400 | for (ISwitchObject srcSw : switches) { |
| 401 | for(IPortObject srcPort : srcSw.getPorts()) { |
| 402 | for(IPortObject dstPort : srcPort.getLinkedPorts()) { |
| 403 | ISwitchObject dstSw = dstPort.getSwitch(); |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 404 | |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 405 | if(dstSw != null && dstSw.getState().equals("ACTIVE")) { |
| 406 | links.add(new Link(HexString.toLong(srcSw.getDPID()), |
| 407 | srcPort.getNumber(), |
| 408 | HexString.toLong(dstSw.getDPID()), |
| 409 | dstPort.getNumber())); |
| 410 | } |
| 411 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 412 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 413 | } |
Naoki Shiota | 826e84a | 2013-06-18 13:13:25 -0700 | [diff] [blame] | 414 | |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 415 | return links; |
Pankaj Berde | ff42180 | 2013-01-29 20:28:52 -0800 | [diff] [blame] | 416 | } |
Pankaj Berde | 5024ec1 | 2013-01-31 17:07:29 -0800 | [diff] [blame] | 417 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 418 | @Override |
| 419 | public LinkInfo getLinkInfo(Link link) { |
| 420 | // TODO implement this |
| 421 | return null; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Finalize the object. |
| 426 | */ |
| 427 | public void finalize() { |
| 428 | close(); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Close LinkStorage. |
| 433 | */ |
| 434 | @Override |
| 435 | public void close() { |
| 436 | // TODO Auto-generated method stub |
| 437 | // graph.shutdown(); |
| 438 | } |
| 439 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 440 | /** |
| 441 | * Update a record of link with meta-information in the LinkStorage. |
| 442 | * @param link Record of a link to update. |
| 443 | * @param linkinfo Meta-information of a link to be updated. |
| 444 | */ |
| 445 | private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) { |
| 446 | // TODO implement this |
| 447 | |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | private boolean addLinkImpl(Link lt) { |
| 452 | boolean success = false; |
| 453 | |
| 454 | IPortObject vportSrc = null, vportDst = null; |
| 455 | |
| 456 | // get source port vertex |
| 457 | String dpid = HexString.toHexString(lt.getSrc()); |
| 458 | short port = lt.getSrcPort(); |
| 459 | vportSrc = op.searchPort(dpid, port); |
| 460 | |
| 461 | // get dest port vertex |
| 462 | dpid = HexString.toHexString(lt.getDst()); |
| 463 | port = lt.getDstPort(); |
| 464 | vportDst = op.searchPort(dpid, port); |
| 465 | |
| 466 | if (vportSrc != null && vportDst != null) { |
| 467 | IPortObject portExist = null; |
| 468 | // check if the link exists |
| 469 | for (IPortObject V : vportSrc.getLinkedPorts()) { |
| 470 | if (V.equals(vportDst)) { |
| 471 | portExist = V; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (portExist == null) { |
| 477 | vportSrc.setLinkPort(vportDst); |
| 478 | success = true; |
| 479 | } else { |
| 480 | log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}", |
| 481 | new Object[]{op, lt, vportSrc, vportDst}); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return success; |
| 486 | } |
| 487 | |
| 488 | private boolean deleteLinkImpl(Link lt) { |
| 489 | boolean success = false; |
| 490 | IPortObject vportSrc = null, vportDst = null; |
| 491 | |
| 492 | // get source port vertex |
| 493 | String dpid = HexString.toHexString(lt.getSrc()); |
| 494 | short port = lt.getSrcPort(); |
| 495 | vportSrc = op.searchPort(dpid, port); |
| 496 | |
| 497 | // get dst port vertex |
| 498 | dpid = HexString.toHexString(lt.getDst()); |
| 499 | port = lt.getDstPort(); |
| 500 | vportDst = op.searchPort(dpid, port); |
| 501 | |
| 502 | // FIXME: This needs to remove all edges |
| 503 | if (vportSrc != null && vportDst != null) { |
| 504 | vportSrc.removeLink(vportDst); |
| 505 | log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{ |
| 506 | lt, vportSrc, vportDst}); |
| 507 | success = true; |
| 508 | } |
| 509 | |
| 510 | return success; |
| 511 | } |
Umesh Krishnaswamy | b676ca2 | 2013-01-11 12:39:25 -0800 | [diff] [blame] | 512 | } |