Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 3 | import net.onrc.onos.datastore.topology.RCLink; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 4 | import net.onrc.onos.datastore.topology.RCPort; |
| 5 | import net.onrc.onos.datastore.topology.RCSwitch; |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 6 | import net.onrc.onos.ofcontroller.util.Dpid; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 7 | |
| 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
| 10 | |
| 11 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 12 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 13 | /** |
| 14 | * The "NB" read-only Network Map. |
| 15 | * |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 16 | * - Maintain Invariant/Relationships between Topology Objects. |
| 17 | * |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 18 | * TODO To be synchronized based on TopologyEvent Notification. |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 19 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 20 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
| 21 | * Object must exist before adding sub component(Add Switch -> Port). Child |
| 22 | * Object need to be removed before removing parent (Delete Port->Switch) |
| 23 | * |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 24 | * TODO TBD: This class may delay the requested change to handle event |
| 25 | * re-ordering. e.g.) Link Add came in, but Switch was not there. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 26 | * |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 27 | */ |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 28 | public class NetworkGraphImpl extends AbstractNetworkGraph |
| 29 | implements NetworkGraphDiscoveryInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 30 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 31 | private static final Logger log = LoggerFactory |
| 32 | .getLogger(NetworkGraphImpl.class); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 33 | |
| 34 | private final NetworkGraphDatastore datastore; |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 35 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 36 | public NetworkGraphImpl() { |
| 37 | super(); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 38 | datastore = new NetworkGraphDatastore(this); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 39 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 41 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 42 | * put Switch |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 43 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 44 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 45 | * fire Notification. |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 46 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 47 | * @param swEvt |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 48 | */ |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 49 | void putSwitch(SwitchEvent swEvt) { |
| 50 | if (swEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 51 | throw new IllegalArgumentException("Switch cannot be null"); |
| 52 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 53 | Switch sw = switches.get(swEvt.getDpid()); |
| 54 | |
| 55 | if (sw == null) { |
| 56 | sw = new SwitchImpl(this, swEvt.getDpid()); |
| 57 | Switch existing = switches.putIfAbsent(swEvt.getDpid(), sw); |
| 58 | if (existing != null) { |
| 59 | log.warn( |
| 60 | "Concurrent putSwitch not expected. Continuing updating {}", |
| 61 | existing); |
| 62 | sw = existing; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Add upate when more attributes are added to Event object |
| 67 | // no attribute to update for now |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * remove Switch. |
| 72 | * |
| 73 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 74 | * fire Notification. |
| 75 | * |
| 76 | * @param swEvt |
| 77 | */ |
| 78 | void removeSwitch(SwitchEvent swEvt) { |
| 79 | if (swEvt == null) { |
| 80 | throw new IllegalArgumentException("Switch cannot be null"); |
| 81 | } |
| 82 | |
| 83 | Switch sw = switches.get(swEvt.getDpid()); |
| 84 | |
| 85 | if (sw == null) { |
| 86 | log.warn("Switch {} already removed, ignoreing", swEvt); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Sanity check |
| 91 | if (!sw.getPorts().isEmpty()) { |
| 92 | log.warn( |
| 93 | "Ports on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 94 | swEvt); |
| 95 | // XXX Should we remove Port? |
| 96 | } |
| 97 | if (!sw.getDevices().isEmpty()) { |
| 98 | log.warn( |
| 99 | "Devices on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 100 | swEvt); |
| 101 | // XXX Should we remove Device to Switch relation? |
| 102 | } |
| 103 | if (!sw.getIncomingLinks().iterator().hasNext()) { |
| 104 | log.warn( |
| 105 | "IncomingLinks on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 106 | swEvt); |
| 107 | // XXX Should we remove Link? |
| 108 | } |
| 109 | if (!sw.getOutgoingLinks().iterator().hasNext()) { |
| 110 | log.warn( |
| 111 | "OutgoingLinks on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 112 | swEvt); |
| 113 | // XXX Should we remove Link? |
| 114 | } |
| 115 | |
| 116 | boolean removed = switches.remove(swEvt.getDpid(), sw); |
| 117 | if (removed) { |
| 118 | log.warn( |
| 119 | "Switch instance was replaced concurrently while removing {}. Something is not right.", |
| 120 | sw); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 121 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 122 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 123 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 124 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 125 | * put Port |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 126 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 127 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 128 | * fire Notification. |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 129 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 130 | * @param portEvt |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 131 | */ |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 132 | void putPort(PortEvent portEvt) { |
| 133 | if (portEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 134 | throw new IllegalArgumentException("Port cannot be null"); |
| 135 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 136 | Switch sw = switches.get(portEvt.getDpid()); |
| 137 | if (sw == null) { |
| 138 | throw new BrokenInvariantException(String.format( |
| 139 | "Switch with dpid %s did not exist.", |
| 140 | new Dpid(portEvt.getDpid()))); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 141 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 142 | Port p = sw.getPort(portEvt.getNumber()); |
| 143 | PortImpl port = null; |
| 144 | if (p != null) { |
| 145 | port = getPortImpl(p); |
| 146 | } |
| 147 | |
| 148 | if (port == null) { |
| 149 | port = new PortImpl(this, sw, portEvt.getNumber()); |
| 150 | } |
| 151 | |
| 152 | // TODO update attributes |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 153 | |
| 154 | SwitchImpl s = getSwitchImpl(sw); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 155 | s.addPort(port); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 158 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 159 | * remove Port |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 160 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 161 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 162 | * fire Notification. |
| 163 | * |
| 164 | * @param portEvt |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 165 | */ |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 166 | void removePort(PortEvent portEvt) { |
| 167 | if (portEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 168 | throw new IllegalArgumentException("Port cannot be null"); |
| 169 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 170 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 171 | Switch sw = switches.get(portEvt.getDpid()); |
| 172 | if (sw == null) { |
| 173 | log.warn("Parent Switch for Port {} already removed, ignoreing", portEvt); |
| 174 | return; |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 177 | Port p = sw.getPort(portEvt.getNumber()); |
| 178 | if (p == null) { |
| 179 | log.warn("Port {} already removed, ignoreing", portEvt); |
| 180 | return; |
| 181 | } |
| 182 | // null check |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 183 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 184 | if (!p.getDevices().iterator().hasNext()) { |
| 185 | log.warn( |
| 186 | "Devices on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 187 | portEvt); |
| 188 | // XXX Should we remove Device to Port relation? |
| 189 | } |
| 190 | if (p.getIncomingLink() != null) { |
| 191 | log.warn( |
| 192 | "IncomingLinks on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 193 | portEvt); |
| 194 | // XXX Should we remove Link? |
| 195 | } |
| 196 | if (p.getOutgoingLink() != null) { |
| 197 | log.warn( |
| 198 | "OutgoingLinks on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 199 | portEvt); |
| 200 | // XXX Should we remove Link? |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // remove Port from Switch |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 204 | SwitchImpl s = getSwitchImpl(sw); |
| 205 | s.removePort(p); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 208 | /** |
| 209 | * put Link |
| 210 | * |
| 211 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 212 | * fire Notification. |
| 213 | * |
| 214 | * @param linkEvt |
| 215 | */ |
| 216 | void putLink(LinkEvent linkEvt) { |
| 217 | if (linkEvt == null) { |
| 218 | throw new IllegalArgumentException("Link cannot be null"); |
| 219 | } |
| 220 | // TODO Auto-generated method stub |
| 221 | |
| 222 | Switch srcSw = switches.get(linkEvt.getSrc().dpid); |
| 223 | if (srcSw == null) { |
| 224 | throw new BrokenInvariantException( |
| 225 | String.format( |
| 226 | "Switch with dpid %s did not exist.", |
| 227 | new Dpid(linkEvt.getSrc().dpid))); |
| 228 | } |
| 229 | |
| 230 | Switch dstSw = switches.get(linkEvt.getDst().dpid); |
| 231 | if (dstSw == null) { |
| 232 | throw new BrokenInvariantException( |
| 233 | String.format( |
| 234 | "Switch with dpid %s did not exist.", |
| 235 | new Dpid(linkEvt.getDst().dpid))); |
| 236 | } |
| 237 | |
| 238 | Port srcPort = srcSw.getPort(linkEvt.getSrc().number); |
| 239 | if (srcPort == null) { |
| 240 | throw new BrokenInvariantException( |
| 241 | String.format( |
| 242 | "Src Port %s of a Link did not exist.", |
| 243 | linkEvt.getSrc() )); |
| 244 | } |
| 245 | Port dstPort = dstSw.getPort(linkEvt.getDst().number); |
| 246 | if (dstPort == null) { |
| 247 | throw new BrokenInvariantException( |
| 248 | String.format( |
| 249 | "Dst Port %s of a Link did not exist.", |
| 250 | linkEvt.getDst() )); |
| 251 | } |
| 252 | |
| 253 | Link l = dstPort.getIncomingLink(); |
| 254 | LinkImpl link = null; |
| 255 | assert( l == srcPort.getOutgoingLink() ); |
| 256 | if (l != null) { |
| 257 | // link = getLink |
| 258 | } |
| 259 | |
| 260 | // TODO update Switch |
| 261 | // TODO update Link |
| 262 | |
| 263 | |
| 264 | // XXX check Existing Link first? |
| 265 | // srcPort.setOutgoingLink(link); |
| 266 | // dstPort.setIncomingLink(link); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * removeLink |
| 271 | * |
| 272 | * XXX Internal Invariant-maintenance method. Will not write to DB. Will not |
| 273 | * fire Notification. |
| 274 | * |
| 275 | * @param link |
| 276 | */ |
| 277 | void removeLink(LinkEvent link) { |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 278 | if (link == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 279 | throw new IllegalArgumentException("Link cannot be null"); |
| 280 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 281 | // TODO Auto-generated method stub |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 282 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 283 | // Switch srcSw = link.getSourceSwitch(); |
| 284 | // if (!isSwitchInstanceInTopology(srcSw)) { |
| 285 | // // XXX Define or choose more appropriate Exception. |
| 286 | // throw new RuntimeException( |
| 287 | // String.format( |
| 288 | // "Switch with dpid %s did not exist or different instance registered.", |
| 289 | // new Dpid(srcSw.getDpid()))); |
| 290 | // } |
| 291 | // |
| 292 | // Switch dstSw = link.getDestinationSwitch(); |
| 293 | // if (!isSwitchInstanceInTopology(dstSw)) { |
| 294 | // // XXX Define or choose more appropriate Exception. |
| 295 | // throw new RuntimeException( |
| 296 | // String.format( |
| 297 | // "Switch with dpid %s did not exist or different instance registered.", |
| 298 | // new Dpid(dstSw.getDpid()))); |
| 299 | // } |
| 300 | // |
| 301 | // PortImpl srcPort = getPortImpl(link.getSourcePort()); |
| 302 | // PortImpl dstPort = getPortImpl(link.getDestinationPort()); |
| 303 | // |
| 304 | // // XXX check Existing Link first? |
| 305 | // if (srcPort.getOutgoingLink() != link |
| 306 | // || dstPort.getIncomingLink() != link) { |
| 307 | // // XXX Define or choose more appropriate Exception. |
| 308 | // throw new RuntimeException(String.format( |
| 309 | // "Link %s did not belong to Topology", link.toString())); |
| 310 | // } |
| 311 | // // remove Link |
| 312 | // srcPort.setOutgoingLink(null); |
| 313 | // dstPort.setIncomingLink(null); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 316 | // XXX Need to rework Device related |
| 317 | /** |
| 318 | * Add new device to DB |
| 319 | * |
| 320 | * @param device |
| 321 | */ |
Yuta HIGUCHI | 0be4a66 | 2014-02-11 18:01:28 -0800 | [diff] [blame] | 322 | void putDevice(DeviceEvent deviceEvt) { |
| 323 | if (deviceEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 324 | throw new IllegalArgumentException("Device cannot be null"); |
| 325 | } |
| 326 | // TODO Auto-generated method stub |
| 327 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 328 | // Device existingDevice = |
| 329 | // getDeviceByMac(deviceToUpdate.getMacAddress()); |
| 330 | // if (existingDevice != deviceToUpdate) { |
| 331 | // throw new IllegalArgumentException( |
| 332 | // "Must supply Device Object in this NetworkGraph"); |
| 333 | // } |
| 334 | // |
| 335 | // DeviceImpl device = getDeviceImpl(deviceToUpdate); |
| 336 | // |
| 337 | // // Update IP Addr |
| 338 | // // uniq |
| 339 | // Set<InetAddress> prevAddrs = new HashSet<>( |
| 340 | // deviceToUpdate.getIpAddress()); |
| 341 | // Set<InetAddress> newAddrs = updatedIpAddrs; |
| 342 | // |
| 343 | // // delta |
| 344 | // @SuppressWarnings("unchecked") |
| 345 | // Collection<InetAddress> delAddr = CollectionUtils.subtract(newAddrs, |
| 346 | // prevAddrs); |
| 347 | // @SuppressWarnings("unchecked") |
| 348 | // Collection<InetAddress> addAddr = CollectionUtils.subtract(prevAddrs, |
| 349 | // newAddrs); |
| 350 | // |
| 351 | // for (InetAddress addr : delAddr) { |
| 352 | // Set<Device> devices = addr2Device.get(addr); |
| 353 | // if (devices == null) { |
| 354 | // continue; |
| 355 | // } |
| 356 | // devices.remove(device); |
| 357 | // device.removeIpAddress(addr); |
| 358 | // } |
| 359 | // for (InetAddress addr : addAddr) { |
| 360 | // Set<Device> devices = addr2Device.get(addr); |
| 361 | // if (devices == null) { |
| 362 | // devices = new HashSet<>(); |
| 363 | // addr2Device.put(addr, devices); |
| 364 | // } |
| 365 | // devices.add(device); |
| 366 | // device.addIpAddress(addr); |
| 367 | // } |
| 368 | // |
| 369 | // // Update Attachment Point |
| 370 | // // uniq |
| 371 | // Set<Port> prevPorts = new HashSet<>(); |
| 372 | // CollectionUtils.addAll(prevAddrs, |
| 373 | // deviceToUpdate.getAttachmentPoints() |
| 374 | // .iterator()); |
| 375 | // Set<Port> newPorts = updatedAttachmentPoints; |
| 376 | // // delta |
| 377 | // @SuppressWarnings("unchecked") |
| 378 | // Collection<Port> delPorts = CollectionUtils.subtract(newPorts, |
| 379 | // prevPorts); |
| 380 | // @SuppressWarnings("unchecked") |
| 381 | // Collection<Port> addPorts = CollectionUtils.subtract(prevPorts, |
| 382 | // newPorts); |
| 383 | // |
| 384 | // for (Port p : delPorts) { |
| 385 | // device.removeAttachmentPoint(p); |
| 386 | // getPortImpl(p).removeDevice(device); |
| 387 | // } |
| 388 | // |
| 389 | // for (Port p : addPorts) { |
| 390 | // device.addAttachmentPoint(p); |
| 391 | // getPortImpl(p).addDevice(device); |
| 392 | // } |
| 393 | |
| 394 | // TODO Auto-generated method stub |
| 395 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 398 | void removeDevice(DeviceEvent device) { |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 399 | if (device == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 400 | throw new IllegalArgumentException("Device cannot be null"); |
| 401 | } |
| 402 | // TODO Auto-generated method stub |
| 403 | |
| 404 | } |
| 405 | |
| 406 | private SwitchImpl getSwitchImpl(Switch sw) { |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 407 | if (sw instanceof SwitchImpl) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 408 | return (SwitchImpl) sw; |
| 409 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 410 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
| 411 | } |
| 412 | |
| 413 | private PortImpl getPortImpl(Port p) { |
| 414 | if (p instanceof PortImpl) { |
| 415 | return (PortImpl) p; |
| 416 | } |
| 417 | throw new ClassCastException("PortImpl expected, but found: " + p); |
| 418 | } |
| 419 | |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 420 | private DeviceImpl getDeviceImpl(Device d) { |
| 421 | if (d instanceof DeviceImpl) { |
| 422 | return (DeviceImpl) d; |
| 423 | } |
| 424 | throw new ClassCastException("DeviceImpl expected, but found: " + d); |
| 425 | } |
| 426 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 427 | public boolean isSwitchInstanceInTopology(Switch sw) { |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 428 | // check if the sw instance is valid in Topology |
| 429 | if (sw != switches.get(sw.getDpid())) { |
| 430 | return false; |
| 431 | } |
| 432 | return true; |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | public void loadWholeTopologyFromDB() { |
| 436 | // XXX clear everything first? |
| 437 | |
| 438 | for (RCSwitch sw : RCSwitch.getAllSwitches()) { |
| 439 | try { |
| 440 | sw.read(); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 441 | // TODO if there is going to be inactive Switch in DB, skip |
| 442 | // TODO update other attributes if there exist any |
| 443 | putSwitch(new SwitchEvent(sw.getDpid())); |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 444 | } catch (ObjectDoesntExistException e) { |
| 445 | log.error("Read Switch Failed, skipping", e); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | for (RCPort p : RCPort.getAllPorts()) { |
| 450 | try { |
| 451 | p.read(); |
| 452 | |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 453 | Switch sw = this.getSwitch(p.getDpid()); |
| 454 | if (sw == null) { |
| 455 | log.error("Switch {} missing when adding Port {}", |
| 456 | new Dpid(p.getDpid()), p); |
| 457 | continue; |
| 458 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 459 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 460 | // TODO update other attributes if there exist any |
| 461 | putPort(portEvent); |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 462 | } catch (ObjectDoesntExistException e) { |
| 463 | log.error("Read Port Failed, skipping", e); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // TODO Is Device going to be in DB? If so, read from DB. |
| 468 | // for (RCDevice d : RCDevice.getAllDevices()) { |
| 469 | // try { |
| 470 | // d.read(); |
| 471 | // |
| 472 | // } catch (ObjectDoesntExistException e) { |
| 473 | // log.debug("Read Device Failed, skipping", e); |
| 474 | // } |
| 475 | // } |
| 476 | |
| 477 | for (RCLink l : RCLink.getAllLinks()) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 478 | try { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 479 | l.read(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 480 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 481 | Switch srcSw = this.getSwitch(l.getSrc().dpid); |
| 482 | if (srcSw == null) { |
| 483 | log.error("Switch {} missing when adding Link {}", |
| 484 | new Dpid(l.getSrc().dpid), l); |
| 485 | continue; |
| 486 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 487 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 488 | Switch dstSw = this.getSwitch(l.getDst().dpid); |
| 489 | if (dstSw == null) { |
| 490 | log.error("Switch {} missing when adding Link {}", |
| 491 | new Dpid(l.getDst().dpid), l); |
| 492 | continue; |
| 493 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 494 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 495 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 496 | l.getSrc().number, l.getDst().dpid, l.getDst().number); |
| 497 | // TODO update other attributes if there exist any |
| 498 | putLink(linkEvent); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 499 | } catch (ObjectDoesntExistException e) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 500 | log.debug("Delete Link Failed", e); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 501 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 502 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 503 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 504 | |
| 505 | /** |
| 506 | * Exception to be thrown when Modification to the Network Graph cannot be continued due to broken invariant. |
| 507 | * |
| 508 | * XXX Should this be checked exception or RuntimeException |
| 509 | */ |
| 510 | public static class BrokenInvariantException extends RuntimeException { |
| 511 | private static final long serialVersionUID = 1L; |
| 512 | |
| 513 | public BrokenInvariantException() { |
| 514 | super(); |
| 515 | } |
| 516 | |
| 517 | public BrokenInvariantException(String message) { |
| 518 | super(message); |
| 519 | } |
| 520 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 521 | |
| 522 | /* ****************************** |
| 523 | * NetworkGraphDiscoveryInterface methods |
| 524 | * ******************************/ |
| 525 | |
| 526 | @Override |
| 527 | public void putSwitchEvent(SwitchEvent switchEvent) { |
| 528 | if (checkAddSwitchInvariant()) { |
| 529 | datastore.addSwitch(switchEvent); |
| 530 | putSwitch(switchEvent); |
| 531 | } |
| 532 | // TODO handle invariant violation |
| 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void removeSwitchEvent(SwitchEvent switchEvent) { |
| 537 | if (checkRemoveSwitchInvariant()) { |
| 538 | datastore.deactivateSwitch(switchEvent); |
| 539 | removeSwitch(switchEvent); |
| 540 | } |
| 541 | // TODO handle invariant violation |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void putPortEvent(PortEvent portEvent) { |
| 546 | // TODO Auto-generated method stub |
| 547 | |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void removePortEvent(PortEvent portEvent) { |
| 552 | // TODO Auto-generated method stub |
| 553 | |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public void putLinkEvent(LinkEvent linkEvent) { |
| 558 | if (checkAddLinkInvariant()) { |
| 559 | datastore.addLink(linkEvent); |
| 560 | putLink(linkEvent); |
| 561 | } |
| 562 | // TODO handle invariant violation |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public void removeLinkEvent(LinkEvent linkEvent) { |
| 567 | if (checkRemoveLinkInvariant()) { |
| 568 | datastore.removeLink(linkEvent); |
| 569 | removeLink(linkEvent); |
| 570 | } |
| 571 | // TODO handle invariant violation |
| 572 | } |
| 573 | |
| 574 | @Override |
Yuta HIGUCHI | 0be4a66 | 2014-02-11 18:01:28 -0800 | [diff] [blame] | 575 | public void putDeviceEvent(DeviceEvent device) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 576 | // TODO Auto-generated method stub |
| 577 | |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void removeDeviceEvent(DeviceEvent deviceEvent) { |
| 582 | // TODO Auto-generated method stub |
| 583 | |
| 584 | } |
| 585 | |
| 586 | /* ***************** |
| 587 | * Internal methods to check invariants of the network graph |
| 588 | * *****************/ |
| 589 | |
| 590 | private boolean checkAddSwitchInvariant() { |
| 591 | // TODO implement |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | private boolean checkRemoveSwitchInvariant() { |
| 596 | // TODO implement |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | private boolean checkAddPortInvariant() { |
| 601 | // TODO implement |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | private boolean checkRemovePortInvariant() { |
| 606 | // TODO implement |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | private boolean checkAddLinkInvariant() { |
| 611 | // TODO implement |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | private boolean checkRemoveLinkInvariant() { |
| 616 | // TODO implement |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | private boolean checkAddDeviceInvariant() { |
| 621 | // TODO implement |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | private boolean checkRemoveDeviceInvariant() { |
| 626 | // TODO implement |
| 627 | return true; |
| 628 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 629 | } |