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