Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 3 | import java.net.InetAddress; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.HashSet; |
| 6 | import java.util.List; |
| 7 | import java.util.Set; |
| 8 | |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 9 | import net.onrc.onos.datastore.topology.RCLink; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 10 | import net.onrc.onos.datastore.topology.RCPort; |
| 11 | import net.onrc.onos.datastore.topology.RCSwitch; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 12 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort; |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 13 | import net.onrc.onos.ofcontroller.util.Dpid; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 14 | |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 19 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 20 | /** |
| 21 | * The "NB" read-only Network Map. |
| 22 | * |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 23 | * - Maintain Invariant/Relationships between Topology Objects. |
| 24 | * |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 25 | * TODO To be synchronized based on TopologyEvent Notification. |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 26 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 27 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 28 | * Object must exist before adding sub component(Add Switch -> Port). |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 29 | * |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 30 | * TODO TBD: This class may delay the requested change to handle event |
| 31 | * 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] | 32 | * |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 33 | */ |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 34 | public class NetworkGraphImpl extends AbstractNetworkGraph implements |
| 35 | NetworkGraphDiscoveryInterface, NetworkGraphReplicationInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 36 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 37 | private static final Logger log = LoggerFactory |
| 38 | .getLogger(NetworkGraphImpl.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 40 | private final NetworkGraphDatastore datastore; |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 42 | public NetworkGraphImpl() { |
| 43 | super(); |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 44 | datastore = new NetworkGraphDatastore(this); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 45 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 46 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 47 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 48 | * put Switch |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 49 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 50 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 51 | * Will not fire Notification. |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 52 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 53 | * @param swEvt |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 54 | */ |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 55 | void putSwitch(SwitchEvent swEvt) { |
| 56 | if (swEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 57 | throw new IllegalArgumentException("Switch cannot be null"); |
| 58 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 59 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 60 | Switch sw = switches.get(swEvt.getDpid()); |
| 61 | |
| 62 | if (sw == null) { |
| 63 | sw = new SwitchImpl(this, swEvt.getDpid()); |
| 64 | Switch existing = switches.putIfAbsent(swEvt.getDpid(), sw); |
| 65 | if (existing != null) { |
| 66 | log.warn( |
| 67 | "Concurrent putSwitch not expected. Continuing updating {}", |
| 68 | existing); |
| 69 | sw = existing; |
| 70 | } |
| 71 | } |
| 72 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 73 | // Update when more attributes are added to Event object |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 74 | // no attribute to update for now |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 75 | |
| 76 | // TODO handle child Port event properly for performance |
| 77 | for (PortEvent portEvt : swEvt.getPorts() ) { |
| 78 | putPort(portEvt); |
| 79 | } |
| 80 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * remove Switch. |
| 85 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 86 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 87 | * Will not fire Notification. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 88 | * |
| 89 | * @param swEvt |
| 90 | */ |
| 91 | void removeSwitch(SwitchEvent swEvt) { |
| 92 | if (swEvt == null) { |
| 93 | throw new IllegalArgumentException("Switch cannot be null"); |
| 94 | } |
| 95 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 96 | // TODO handle child Port event properly for performance |
| 97 | for (PortEvent portEvt : swEvt.getPorts() ) { |
| 98 | removePort(portEvt); |
| 99 | } |
| 100 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 101 | Switch sw = switches.get(swEvt.getDpid()); |
| 102 | |
| 103 | if (sw == null) { |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 104 | log.warn("Switch {} already removed, ignoring", swEvt); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Sanity check |
| 109 | if (!sw.getPorts().isEmpty()) { |
| 110 | log.warn( |
| 111 | "Ports on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 112 | swEvt); |
| 113 | // XXX Should we remove Port? |
| 114 | } |
| 115 | if (!sw.getDevices().isEmpty()) { |
| 116 | log.warn( |
| 117 | "Devices on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 118 | swEvt); |
| 119 | // XXX Should we remove Device to Switch relation? |
| 120 | } |
| 121 | if (!sw.getIncomingLinks().iterator().hasNext()) { |
| 122 | log.warn( |
| 123 | "IncomingLinks on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 124 | swEvt); |
| 125 | // XXX Should we remove Link? |
| 126 | } |
| 127 | if (!sw.getOutgoingLinks().iterator().hasNext()) { |
| 128 | log.warn( |
| 129 | "OutgoingLinks on Switch {} should be removed prior to removing Switch. Removing Switch anyways", |
| 130 | swEvt); |
| 131 | // XXX Should we remove Link? |
| 132 | } |
| 133 | |
| 134 | boolean removed = switches.remove(swEvt.getDpid(), sw); |
| 135 | if (removed) { |
| 136 | log.warn( |
| 137 | "Switch instance was replaced concurrently while removing {}. Something is not right.", |
| 138 | sw); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 139 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 140 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 141 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 142 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 143 | * put Port |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 144 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 145 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 146 | * Will not fire Notification. |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 147 | * |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 148 | * @param portEvt |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 149 | */ |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 150 | void putPort(PortEvent portEvt) { |
| 151 | if (portEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 152 | throw new IllegalArgumentException("Port cannot be null"); |
| 153 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 154 | Switch sw = switches.get(portEvt.getDpid()); |
| 155 | if (sw == null) { |
| 156 | throw new BrokenInvariantException(String.format( |
| 157 | "Switch with dpid %s did not exist.", |
| 158 | new Dpid(portEvt.getDpid()))); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 159 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 160 | Port p = sw.getPort(portEvt.getNumber()); |
| 161 | PortImpl port = null; |
| 162 | if (p != null) { |
| 163 | port = getPortImpl(p); |
| 164 | } |
| 165 | |
| 166 | if (port == null) { |
| 167 | port = new PortImpl(this, sw, portEvt.getNumber()); |
| 168 | } |
| 169 | |
| 170 | // TODO update attributes |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 171 | |
| 172 | SwitchImpl s = getSwitchImpl(sw); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 173 | s.addPort(port); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 176 | /** |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 177 | * remove Port |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 178 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 179 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 180 | * Will not fire Notification. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 181 | * |
| 182 | * @param portEvt |
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 | void removePort(PortEvent portEvt) { |
| 185 | if (portEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 186 | throw new IllegalArgumentException("Port cannot be null"); |
| 187 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 188 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 189 | Switch sw = switches.get(portEvt.getDpid()); |
| 190 | if (sw == null) { |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 191 | log.warn("Parent Switch for Port {} already removed, ignoring", portEvt); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 192 | return; |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 195 | Port p = sw.getPort(portEvt.getNumber()); |
| 196 | if (p == null) { |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 197 | log.warn("Port {} already removed, ignoring", portEvt); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 200 | |
| 201 | // check if there is something referring to this Port |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 202 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 203 | if (!p.getDevices().iterator().hasNext()) { |
| 204 | log.warn( |
| 205 | "Devices on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 206 | portEvt); |
| 207 | // XXX Should we remove Device to Port relation? |
| 208 | } |
| 209 | if (p.getIncomingLink() != null) { |
| 210 | log.warn( |
| 211 | "IncomingLinks on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 212 | portEvt); |
| 213 | // XXX Should we remove Link? |
| 214 | } |
| 215 | if (p.getOutgoingLink() != null) { |
| 216 | log.warn( |
| 217 | "OutgoingLinks on Port {} should be removed prior to removing Port. Removing Port anyways", |
| 218 | portEvt); |
| 219 | // XXX Should we remove Link? |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // remove Port from Switch |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 223 | SwitchImpl s = getSwitchImpl(sw); |
| 224 | s.removePort(p); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 227 | /** |
| 228 | * put Link |
| 229 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 230 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 231 | * Will not fire Notification. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 232 | * |
| 233 | * @param linkEvt |
| 234 | */ |
| 235 | void putLink(LinkEvent linkEvt) { |
| 236 | if (linkEvt == null) { |
| 237 | throw new IllegalArgumentException("Link cannot be null"); |
| 238 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 239 | |
| 240 | Switch srcSw = switches.get(linkEvt.getSrc().dpid); |
| 241 | if (srcSw == null) { |
| 242 | throw new BrokenInvariantException( |
| 243 | String.format( |
| 244 | "Switch with dpid %s did not exist.", |
| 245 | new Dpid(linkEvt.getSrc().dpid))); |
| 246 | } |
| 247 | |
| 248 | Switch dstSw = switches.get(linkEvt.getDst().dpid); |
| 249 | if (dstSw == null) { |
| 250 | throw new BrokenInvariantException( |
| 251 | String.format( |
| 252 | "Switch with dpid %s did not exist.", |
| 253 | new Dpid(linkEvt.getDst().dpid))); |
| 254 | } |
| 255 | |
| 256 | Port srcPort = srcSw.getPort(linkEvt.getSrc().number); |
| 257 | if (srcPort == null) { |
| 258 | throw new BrokenInvariantException( |
| 259 | String.format( |
| 260 | "Src Port %s of a Link did not exist.", |
| 261 | linkEvt.getSrc() )); |
| 262 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 263 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 264 | Port dstPort = dstSw.getPort(linkEvt.getDst().number); |
| 265 | if (dstPort == null) { |
| 266 | throw new BrokenInvariantException( |
| 267 | String.format( |
| 268 | "Dst Port %s of a Link did not exist.", |
| 269 | linkEvt.getDst() )); |
| 270 | } |
| 271 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 272 | // getting Link instance from destination port incoming Link |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 273 | Link l = dstPort.getIncomingLink(); |
| 274 | LinkImpl link = null; |
| 275 | assert( l == srcPort.getOutgoingLink() ); |
| 276 | if (l != null) { |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 277 | link = getLinkImpl(l); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 280 | if (link == null) { |
| 281 | link = new LinkImpl(this, srcPort, dstPort); |
| 282 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 283 | |
| 284 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 285 | PortImpl dstPortMem = getPortImpl(dstPort); |
| 286 | PortImpl srcPortMem = getPortImpl(srcPort); |
| 287 | |
| 288 | // Add Link first to avoid further Device addition |
| 289 | |
| 290 | // add Link to Port |
| 291 | dstPortMem.setIncomingLink(link); |
| 292 | srcPortMem.setOutgoingLink(link); |
| 293 | |
| 294 | // remove Device Pointing to Port if any |
| 295 | for(Device d : dstPortMem.getDevices() ) { |
| 296 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, dstPort, linkEvt); |
| 297 | DeviceImpl dev = getDeviceImpl(d); |
| 298 | dev.removeAttachmentPoint(dstPort); |
| 299 | // XXX This implies that change is made to Device Object, |
| 300 | // which need to be written to DB, how should that be done? |
| 301 | // should we write here or ignore and leave DB in inconsistent state? |
| 302 | } |
| 303 | dstPortMem.removeAllDevice(); |
| 304 | for(Device d : srcPortMem.getDevices() ) { |
| 305 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", d, srcPort, linkEvt); |
| 306 | DeviceImpl dev = getDeviceImpl(d); |
| 307 | dev.removeAttachmentPoint(srcPort); |
| 308 | // XXX This implies that change is made to Device Object, |
| 309 | // which need to be written to DB, how should that be done? |
| 310 | // should we write here or ignore and leave DB in inconsistent state? |
| 311 | } |
| 312 | srcPortMem.removeAllDevice(); |
| 313 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * removeLink |
| 318 | * |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 319 | * XXX Internal In-memory object mutation method. Will not write to DB. |
| 320 | * Will not fire Notification. |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 321 | * |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 322 | * @param linkEvt |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 323 | */ |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 324 | void removeLink(LinkEvent linkEvt) { |
| 325 | if (linkEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 326 | throw new IllegalArgumentException("Link cannot be null"); |
| 327 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 328 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 329 | Switch srcSw = switches.get(linkEvt.getSrc().dpid); |
| 330 | if (srcSw == null) { |
| 331 | log.warn("Src Switch for Link {} already removed, ignoring", linkEvt); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | Switch dstSw = switches.get(linkEvt.getDst().dpid); |
| 336 | if (dstSw == null) { |
| 337 | log.warn("Dst Switch for Link {} already removed, ignoring", linkEvt); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | Port srcPort = srcSw.getPort(linkEvt.getSrc().number); |
| 342 | if (srcPort == null) { |
| 343 | log.warn("Src Port for Link {} already removed, ignoring", linkEvt); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | Port dstPort = dstSw.getPort(linkEvt.getDst().number); |
| 348 | if (dstPort == null) { |
| 349 | log.warn("Dst Port for Link {} already removed, ignoring", linkEvt); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | Link l = dstPort.getIncomingLink(); |
| 354 | if ( l == null ) { |
| 355 | log.warn("Link {} already removed on destination Port", linkEvt); |
| 356 | } |
| 357 | l = srcPort.getOutgoingLink(); |
| 358 | if ( l == null ) { |
| 359 | log.warn("Link {} already removed on src Port", linkEvt); |
| 360 | } |
| 361 | |
| 362 | getPortImpl(dstPort).setIncomingLink(null); |
| 363 | getPortImpl(srcPort).setOutgoingLink(null); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 366 | // XXX Need to rework Device related |
| 367 | /** |
| 368 | * Add new device to DB |
| 369 | * |
| 370 | * @param device |
| 371 | */ |
Yuta HIGUCHI | 0be4a66 | 2014-02-11 18:01:28 -0800 | [diff] [blame] | 372 | void putDevice(DeviceEvent deviceEvt) { |
| 373 | if (deviceEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 374 | throw new IllegalArgumentException("Device cannot be null"); |
| 375 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 376 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 377 | Device device = getDeviceByMac(deviceEvt.getMac()); |
| 378 | if ( device == null ) { |
| 379 | device = new DeviceImpl(this, deviceEvt.getMac()); |
| 380 | Device existing = mac2Device.putIfAbsent(deviceEvt.getMac(), device); |
| 381 | if (existing != null) { |
| 382 | log.warn( |
| 383 | "Concurrent putDevice seems to be in action. Continuing updating {}", |
| 384 | existing); |
| 385 | device = existing; |
| 386 | } |
| 387 | } |
| 388 | DeviceImpl memDevice = getDeviceImpl(device); |
| 389 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 390 | // for each attachment point |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 391 | for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) { |
| 392 | // Attached Ports' Parent Switch must exist |
| 393 | Switch sw = getSwitch(swp.dpid); |
| 394 | if ( sw == null ) { |
| 395 | log.warn("Switch {} for the attachment point did not exist. skipping mutation", sw); |
| 396 | continue; |
| 397 | } |
| 398 | // Attached Ports must exist |
| 399 | Port port = sw.getPort(swp.number); |
| 400 | if ( port == null ) { |
| 401 | log.warn("Port {} for the attachment point did not exist. skipping mutation", port); |
| 402 | continue; |
| 403 | } |
| 404 | // Attached Ports must not have Link |
| 405 | if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) { |
| 406 | log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", port.getOutgoingLink(), port.getIncomingLink()); |
| 407 | continue; |
| 408 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 409 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 410 | // finally add Device <-> Port on In-memory structure |
| 411 | PortImpl memPort = getPortImpl(port); |
| 412 | memPort.addDevice(device); |
| 413 | memDevice.addAttachmentPoint(port); |
| 414 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 415 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 416 | // for each IP address |
| 417 | for( InetAddress ipAddr : deviceEvt.getIpAddresses() ) { |
| 418 | // Add Device -> IP |
| 419 | memDevice.addIpAddress(ipAddr); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 420 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 421 | // Add IP -> Set<Device> |
| 422 | boolean updated = false; |
| 423 | do { |
| 424 | Set<Device> devices = this.addr2Device.get(ipAddr); |
| 425 | if ( devices == null ) { |
| 426 | devices = new HashSet<>(); |
| 427 | Set<Device> existing = this.addr2Device.putIfAbsent(ipAddr, devices); |
| 428 | if ( existing == null ) { |
| 429 | // success |
| 430 | updated = true; |
| 431 | } |
| 432 | } else { |
| 433 | Set<Device> updateDevices = new HashSet<>(devices); |
| 434 | updateDevices.add(device); |
| 435 | updated = this.addr2Device.replace(ipAddr, devices, updateDevices); |
| 436 | } |
| 437 | if (!updated) { |
| 438 | log.debug("Collision detected, updating IP to Device mapping retrying."); |
| 439 | } |
| 440 | } while( !updated ); |
| 441 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 444 | void removeDevice(DeviceEvent deviceEvt) { |
| 445 | if (deviceEvt == null) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 446 | throw new IllegalArgumentException("Device cannot be null"); |
| 447 | } |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame^] | 448 | |
| 449 | Device device = getDeviceByMac(deviceEvt.getMac()); |
| 450 | if ( device == null ) { |
| 451 | log.warn("Device {} already removed, ignoring", deviceEvt); |
| 452 | return; |
| 453 | } |
| 454 | DeviceImpl memDevice = getDeviceImpl(device); |
| 455 | |
| 456 | // for each attachment point |
| 457 | for (SwitchPort swp : deviceEvt.getAttachmentPoints() ) { |
| 458 | // Attached Ports' Parent Switch must exist |
| 459 | Switch sw = getSwitch(swp.dpid); |
| 460 | if ( sw == null ) { |
| 461 | log.warn("Switch {} for the attachment point did not exist. skipping mutation", sw); |
| 462 | continue; |
| 463 | } |
| 464 | // Attached Ports must exist |
| 465 | Port port = sw.getPort(swp.number); |
| 466 | if ( port == null ) { |
| 467 | log.warn("Port {} for the attachment point did not exist. skipping mutation", port); |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | // finally remove Device <-> Port on In-memory structure |
| 472 | PortImpl memPort = getPortImpl(port); |
| 473 | memPort.removeDevice(device); |
| 474 | memDevice.removeAttachmentPoint(port); |
| 475 | } |
| 476 | |
| 477 | // for each IP address |
| 478 | for( InetAddress ipAddr : deviceEvt.getIpAddresses() ) { |
| 479 | // Remove Device -> IP |
| 480 | memDevice.removeIpAddress(ipAddr); |
| 481 | |
| 482 | // Remove IP -> Set<Device> |
| 483 | boolean updated = false; |
| 484 | do { |
| 485 | Set<Device> devices = this.addr2Device.get(ipAddr); |
| 486 | if ( devices == null ) { |
| 487 | // already empty set, nothing to do |
| 488 | updated = true; |
| 489 | } else { |
| 490 | Set<Device> updateDevices = new HashSet<>(devices); |
| 491 | updateDevices.remove(device); |
| 492 | updated = this.addr2Device.replace(ipAddr, devices, updateDevices); |
| 493 | } |
| 494 | if (!updated) { |
| 495 | log.debug("Collision detected, updating IP to Device mapping retrying."); |
| 496 | } |
| 497 | } while( !updated ); |
| 498 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | private SwitchImpl getSwitchImpl(Switch sw) { |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 502 | if (sw instanceof SwitchImpl) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 503 | return (SwitchImpl) sw; |
| 504 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 505 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
| 506 | } |
| 507 | |
| 508 | private PortImpl getPortImpl(Port p) { |
| 509 | if (p instanceof PortImpl) { |
| 510 | return (PortImpl) p; |
| 511 | } |
| 512 | throw new ClassCastException("PortImpl expected, but found: " + p); |
| 513 | } |
| 514 | |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 515 | private LinkImpl getLinkImpl(Link l) { |
| 516 | if (l instanceof LinkImpl) { |
| 517 | return (LinkImpl) l; |
| 518 | } |
| 519 | throw new ClassCastException("LinkImpl expected, but found: " + l); |
| 520 | } |
| 521 | |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 522 | private DeviceImpl getDeviceImpl(Device d) { |
| 523 | if (d instanceof DeviceImpl) { |
| 524 | return (DeviceImpl) d; |
| 525 | } |
| 526 | throw new ClassCastException("DeviceImpl expected, but found: " + d); |
| 527 | } |
| 528 | |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 529 | public void loadWholeTopologyFromDB() { |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 530 | // 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] | 531 | // XXX clear everything first? |
| 532 | |
| 533 | for (RCSwitch sw : RCSwitch.getAllSwitches()) { |
| 534 | try { |
| 535 | sw.read(); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 536 | // TODO if there is going to be inactive Switch in DB, skip |
| 537 | // TODO update other attributes if there exist any |
| 538 | putSwitch(new SwitchEvent(sw.getDpid())); |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 539 | } catch (ObjectDoesntExistException e) { |
| 540 | log.error("Read Switch Failed, skipping", e); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | for (RCPort p : RCPort.getAllPorts()) { |
| 545 | try { |
| 546 | p.read(); |
| 547 | |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 548 | Switch sw = this.getSwitch(p.getDpid()); |
| 549 | if (sw == null) { |
| 550 | log.error("Switch {} missing when adding Port {}", |
| 551 | new Dpid(p.getDpid()), p); |
| 552 | continue; |
| 553 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 554 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 555 | // TODO update other attributes if there exist any |
| 556 | putPort(portEvent); |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 557 | } catch (ObjectDoesntExistException e) { |
| 558 | log.error("Read Port Failed, skipping", e); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // TODO Is Device going to be in DB? If so, read from DB. |
| 563 | // for (RCDevice d : RCDevice.getAllDevices()) { |
| 564 | // try { |
| 565 | // d.read(); |
| 566 | // |
| 567 | // } catch (ObjectDoesntExistException e) { |
| 568 | // log.debug("Read Device Failed, skipping", e); |
| 569 | // } |
| 570 | // } |
| 571 | |
| 572 | for (RCLink l : RCLink.getAllLinks()) { |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 573 | try { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 574 | l.read(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 575 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 576 | Switch srcSw = this.getSwitch(l.getSrc().dpid); |
| 577 | if (srcSw == null) { |
| 578 | log.error("Switch {} missing when adding Link {}", |
| 579 | new Dpid(l.getSrc().dpid), l); |
| 580 | continue; |
| 581 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 582 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 583 | Switch dstSw = this.getSwitch(l.getDst().dpid); |
| 584 | if (dstSw == null) { |
| 585 | log.error("Switch {} missing when adding Link {}", |
| 586 | new Dpid(l.getDst().dpid), l); |
| 587 | continue; |
| 588 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 589 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 590 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 591 | l.getSrc().number, l.getDst().dpid, l.getDst().number); |
| 592 | // TODO update other attributes if there exist any |
| 593 | putLink(linkEvent); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 594 | } catch (ObjectDoesntExistException e) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 595 | log.debug("Delete Link Failed", e); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 596 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 597 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 598 | } |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * Exception to be thrown when Modification to the Network Graph cannot be continued due to broken invariant. |
| 602 | * |
| 603 | * XXX Should this be checked exception or RuntimeException |
| 604 | */ |
| 605 | public static class BrokenInvariantException extends RuntimeException { |
| 606 | private static final long serialVersionUID = 1L; |
| 607 | |
| 608 | public BrokenInvariantException() { |
| 609 | super(); |
| 610 | } |
| 611 | |
| 612 | public BrokenInvariantException(String message) { |
| 613 | super(message); |
| 614 | } |
| 615 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 616 | |
| 617 | /* ****************************** |
| 618 | * NetworkGraphDiscoveryInterface methods |
| 619 | * ******************************/ |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 620 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 621 | @Override |
| 622 | public void putSwitchEvent(SwitchEvent switchEvent) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 623 | if (prepareForAddSwitchEvent(switchEvent)) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 624 | datastore.addSwitch(switchEvent); |
| 625 | putSwitch(switchEvent); |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 626 | // TODO send out notification |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 627 | } |
| 628 | // TODO handle invariant violation |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public void removeSwitchEvent(SwitchEvent switchEvent) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 633 | if (prepareForRemoveSwitchEvent(switchEvent)) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 634 | datastore.deactivateSwitch(switchEvent); |
| 635 | removeSwitch(switchEvent); |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 636 | // TODO send out notification |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 637 | } |
| 638 | // TODO handle invariant violation |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void putPortEvent(PortEvent portEvent) { |
| 643 | // TODO Auto-generated method stub |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 644 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public void removePortEvent(PortEvent portEvent) { |
| 649 | // TODO Auto-generated method stub |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 650 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public void putLinkEvent(LinkEvent linkEvent) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 655 | if (prepareForAddLinkEvent(linkEvent)) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 656 | datastore.addLink(linkEvent); |
| 657 | putLink(linkEvent); |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 658 | // TODO send out notification |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 659 | } |
| 660 | // TODO handle invariant violation |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void removeLinkEvent(LinkEvent linkEvent) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 665 | if (prepareForRemoveLinkEvent(linkEvent)) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 666 | datastore.removeLink(linkEvent); |
| 667 | removeLink(linkEvent); |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 668 | // TODO send out notification |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 669 | } |
| 670 | // TODO handle invariant violation |
| 671 | } |
| 672 | |
| 673 | @Override |
Yuta HIGUCHI | 0be4a66 | 2014-02-11 18:01:28 -0800 | [diff] [blame] | 674 | public void putDeviceEvent(DeviceEvent device) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 675 | // XXX if prepareFor~ method returned false, event should be dropped |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 676 | // TODO Auto-generated method stub |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 677 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public void removeDeviceEvent(DeviceEvent deviceEvent) { |
| 682 | // TODO Auto-generated method stub |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 683 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 684 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 685 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 686 | /* ***************** |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 687 | * Internal methods to maintain invariants of the network graph |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 688 | * *****************/ |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 689 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 690 | /** |
| 691 | * |
| 692 | * @param swEvt |
| 693 | * @return true if ready to accept event. |
| 694 | */ |
| 695 | private boolean prepareForAddSwitchEvent(SwitchEvent swEvt) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 696 | // No show stopping precondition? |
| 697 | // Prep: remove(deactivate) Ports on Switch, which is not on event |
| 698 | removePortsNotOnEvent(swEvt); |
| 699 | return true; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 700 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 701 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 702 | private boolean prepareForRemoveSwitchEvent(SwitchEvent swEvt) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 703 | // No show stopping precondition? |
| 704 | // Prep: remove(deactivate) Ports on Switch, which is not on event |
| 705 | // XXX may be remove switch should imply wipe all ports |
| 706 | removePortsNotOnEvent(swEvt); |
| 707 | return true; |
| 708 | } |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 709 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 710 | private void removePortsNotOnEvent(SwitchEvent swEvt) { |
| 711 | Switch sw = switches.get( swEvt.getDpid() ); |
| 712 | if ( sw != null ) { |
| 713 | Set<Long> port_noOnEvent = new HashSet<>(); |
| 714 | for( PortEvent portEvent : swEvt.getPorts()) { |
| 715 | port_noOnEvent.add(portEvent.getNumber()); |
| 716 | } |
| 717 | // Existing ports not on event should be removed. |
| 718 | // TODO Should batch eventually for performance? |
| 719 | for( Port p : sw.getPorts() ) { |
| 720 | if ( !port_noOnEvent.contains(p.getNumber()) ) { |
| 721 | PortEvent rmEvent = new PortEvent(p.getSwitch().getDpid(), p.getNumber()); |
| 722 | // calling Discovery removePort() API to wipe from DB, etc. |
| 723 | removePortEvent(rmEvent); |
| 724 | } |
| 725 | } |
| 726 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 727 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 728 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 729 | private boolean prepareForAddPortEvent(PortEvent portEvt) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 730 | // Parent Switch must exist |
| 731 | if ( getSwitch(portEvt.getDpid()) == null) { |
| 732 | return false; |
| 733 | } |
| 734 | // Prep: None |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 735 | return true; |
| 736 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 737 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 738 | private boolean prepareForRemovePortEvent(PortEvent portEvt) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 739 | // Parent Switch must exist |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 740 | Switch sw = getSwitch(portEvt.getDpid()); |
| 741 | if ( sw == null ) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 742 | return false; |
| 743 | } |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 744 | Port port = sw.getPort(portEvt.getNumber()); |
| 745 | if ( port == null ) { |
| 746 | log.debug("Port already removed? {}", portEvt); |
| 747 | // let it pass |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | // Prep: Remove Link and Device Attachment |
| 752 | for (Device device : port.getDevices()) { |
| 753 | DeviceEvent devEvt = new DeviceEvent(device.getMacAddress()); |
| 754 | devEvt.addAttachmentPoint(new SwitchPort(port.getSwitch().getDpid(), port.getNumber())); |
| 755 | // calling Discovery API to wipe from DB, etc. |
| 756 | removeDeviceEvent(devEvt); |
| 757 | } |
| 758 | Set<Link> links = new HashSet<>(); |
| 759 | links.add(port.getOutgoingLink()); |
| 760 | links.add(port.getIncomingLink()); |
| 761 | for ( Link link : links) { |
| 762 | if (link == null ) { |
| 763 | continue; |
| 764 | } |
| 765 | LinkEvent linkEvent = new LinkEvent(link.getSourceSwitchDpid(), link.getSourcePortNumber(), link.getDestinationSwitchDpid(), link.getDestinationPortNumber()); |
| 766 | // calling Discovery API to wipe from DB, etc. |
| 767 | removeLinkEvent(linkEvent); |
| 768 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 769 | return true; |
| 770 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 771 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 772 | private boolean prepareForAddLinkEvent(LinkEvent linkEvt) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 773 | // Src/Dst Switch must exist |
| 774 | Switch srcSw = getSwitch(linkEvt.getSrc().dpid); |
| 775 | Switch dstSw = getSwitch(linkEvt.getDst().dpid); |
| 776 | if ( srcSw == null || dstSw == null ) { |
| 777 | return false; |
| 778 | } |
| 779 | // Src/Dst Port must exist |
| 780 | Port srcPort = srcSw.getPort(linkEvt.getSrc().number); |
| 781 | Port dstPort = srcSw.getPort(linkEvt.getDst().number); |
| 782 | if ( srcPort == null || dstPort == null ) { |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | // Prep: remove Device attachment on both Ports |
| 787 | for (Device device : srcPort.getDevices()) { |
| 788 | DeviceEvent devEvt = new DeviceEvent(device.getMacAddress()); |
| 789 | devEvt.addAttachmentPoint(new SwitchPort(srcPort.getSwitch().getDpid(), srcPort.getNumber())); |
| 790 | // calling Discovery API to wipe from DB, etc. |
| 791 | removeDeviceEvent(devEvt); |
| 792 | } |
| 793 | for (Device device : dstPort.getDevices()) { |
| 794 | DeviceEvent devEvt = new DeviceEvent(device.getMacAddress()); |
| 795 | devEvt.addAttachmentPoint(new SwitchPort(dstPort.getSwitch().getDpid(), dstPort.getNumber())); |
| 796 | // calling Discovery API to wipe from DB, etc. |
| 797 | removeDeviceEvent(devEvt); |
| 798 | } |
| 799 | |
| 800 | return true; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 801 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 802 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 803 | private boolean prepareForRemoveLinkEvent(LinkEvent linkEvt) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 804 | // Src/Dst Switch must exist |
| 805 | Switch srcSw = getSwitch(linkEvt.getSrc().dpid); |
| 806 | Switch dstSw = getSwitch(linkEvt.getDst().dpid); |
| 807 | if ( srcSw == null || dstSw == null ) { |
| 808 | return false; |
| 809 | } |
| 810 | // Src/Dst Port must exist |
| 811 | Port srcPort = srcSw.getPort(linkEvt.getSrc().number); |
| 812 | Port dstPort = srcSw.getPort(linkEvt.getDst().number); |
| 813 | if ( srcPort == null || dstPort == null ) { |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | // Prep: None |
| 818 | return true; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 819 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 820 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 821 | /** |
| 822 | * |
| 823 | * @param deviceEvt Event will be modified to remove inapplicable attachemntPoints/ipAddress |
| 824 | * @return false if this event should be dropped. |
| 825 | */ |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 826 | private boolean prepareForAddDeviceEvent(DeviceEvent deviceEvt) { |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 827 | boolean preconditionBroken = false; |
| 828 | ArrayList<PortEvent.SwitchPort> failedSwitchPort = new ArrayList<>(); |
| 829 | for ( PortEvent.SwitchPort swp : deviceEvt.getAttachmentPoints() ) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 830 | // Attached Ports' Parent Switch must exist |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 831 | Switch sw = getSwitch(swp.dpid); |
| 832 | if ( sw == null ) { |
| 833 | preconditionBroken = true; |
| 834 | failedSwitchPort.add(swp); |
| 835 | continue; |
| 836 | } |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 837 | // Attached Ports must exist |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 838 | Port port = sw.getPort(swp.number); |
| 839 | if ( port == null ) { |
| 840 | preconditionBroken = true; |
| 841 | failedSwitchPort.add(swp); |
| 842 | continue; |
| 843 | } |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 844 | // Attached Ports must not have Link |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 845 | if ( port.getOutgoingLink() != null || port.getIncomingLink() != null ) { |
| 846 | preconditionBroken = true; |
| 847 | failedSwitchPort.add(swp); |
| 848 | continue; |
| 849 | } |
| 850 | } |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 851 | |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 852 | // Rewriting event to exclude failed attachmentPoint |
| 853 | // XXX Assumption behind this is that inapplicable device event should |
| 854 | // be dropped, not deferred. If we decide to defer Device event, |
| 855 | // rewriting can become a problem |
| 856 | List<SwitchPort> attachmentPoints = deviceEvt.getAttachmentPoints(); |
| 857 | attachmentPoints.removeAll(failedSwitchPort); |
| 858 | deviceEvt.setAttachmentPoints(attachmentPoints); |
| 859 | |
| 860 | if ( deviceEvt.getAttachmentPoints().isEmpty() && deviceEvt.getIpAddresses().isEmpty() ) { |
| 861 | // XXX return false to represent: Nothing left to do for this event. Caller should drop event |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | // Should we return false to tell caller that the event was trimmed? |
| 866 | // if ( preconditionBroken ) { |
| 867 | // return false; |
| 868 | // } |
| 869 | |
| 870 | return true; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 871 | } |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 872 | |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 873 | private boolean prepareForRemoveDeviceEvent(DeviceEvent deviceEvt) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 874 | // No show stopping precondition? |
| 875 | // Prep: none |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 876 | return true; |
| 877 | } |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 878 | |
| 879 | @Override |
| 880 | public void putSwitchReplicationEvent(SwitchEvent switchEvent) { |
Yuta HIGUCHI | d02e928 | 2014-02-12 09:24:41 -0800 | [diff] [blame] | 881 | if (prepareForAddSwitchEvent(switchEvent)) { |
Yuta HIGUCHI | 928fa68 | 2014-02-11 19:07:57 -0800 | [diff] [blame] | 882 | putSwitch(switchEvent); |
| 883 | } |
| 884 | // TODO handle invariant violation |
| 885 | } |
| 886 | |
| 887 | @Override |
| 888 | public void removeSwitchReplicationEvent(SwitchEvent switchEvent) { |
| 889 | // TODO Auto-generated method stub |
| 890 | |
| 891 | } |
| 892 | |
| 893 | @Override |
| 894 | public void putPortReplicationEvent(PortEvent portEvent) { |
| 895 | // TODO Auto-generated method stub |
| 896 | |
| 897 | } |
| 898 | |
| 899 | @Override |
| 900 | public void removePortReplicationEvent(PortEvent portEvent) { |
| 901 | // TODO Auto-generated method stub |
| 902 | |
| 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public void putLinkReplicationEvent(LinkEvent linkEvent) { |
| 907 | // TODO Auto-generated method stub |
| 908 | |
| 909 | } |
| 910 | |
| 911 | @Override |
| 912 | public void removeLinkReplicationEvent(LinkEvent linkEvent) { |
| 913 | // TODO Auto-generated method stub |
| 914 | |
| 915 | } |
| 916 | |
| 917 | @Override |
| 918 | public void putDeviceReplicationEvent(DeviceEvent deviceEvent) { |
| 919 | // TODO Auto-generated method stub |
| 920 | |
| 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public void removeDeviceReplicationEvent(DeviceEvent deviceEvent) { |
| 925 | // TODO Auto-generated method stub |
| 926 | |
| 927 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 928 | } |