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