blob: 9186e43b38aaca0c284a9621d50aaf1e0734ab67 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -08003import net.onrc.onos.datastore.topology.RCLink;
Jonathan Hart062a2e82014-02-03 09:41:57 -08004import net.onrc.onos.datastore.topology.RCPort;
5import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -08006import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart062a2e82014-02-03 09:41:57 -08007
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
12
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080013/**
14 * The "NB" read-only Network Map.
15 *
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080016 * - Maintain Invariant/Relationships between Topology Objects.
17 *
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080018 * TODO To be synchronized based on TopologyEvent Notification.
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080019 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080020 * 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 HIGUCHI4bfdd532014-02-07 13:47:36 -080024 * 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 HIGUCHIcb951982014-02-11 13:31:44 -080026 *
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080027 */
Jonathan Hart22eb9882014-02-11 15:52:59 -080028public class NetworkGraphImpl extends AbstractNetworkGraph
29 implements NetworkGraphDiscoveryInterface {
Jonathan Hart062a2e82014-02-03 09:41:57 -080030
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080031 private static final Logger log = LoggerFactory
32 .getLogger(NetworkGraphImpl.class);
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -080033
Jonathan Hart22eb9882014-02-11 15:52:59 -080034 private final NetworkGraphDatastore datastore;
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080035
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080036 public NetworkGraphImpl() {
37 super();
Jonathan Hart22eb9882014-02-11 15:52:59 -080038 datastore = new NetworkGraphDatastore(this);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080039 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080040
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080041 /**
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080042 * put Switch
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080043 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080044 * XXX Internal Invariant-maintenance method. Will not write to DB. Will not
45 * fire Notification.
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080046 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080047 * @param swEvt
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080048 */
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080049 void putSwitch(SwitchEvent swEvt) {
50 if (swEvt == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080051 throw new IllegalArgumentException("Switch cannot be null");
52 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -080053
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080054 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 HIGUCHIcd922f42014-02-11 18:59:11 -080069
70 // TODO handle child Port event properly for performance
71 for (PortEvent portEvt : swEvt.getPorts() ) {
72 putPort(portEvt);
73 }
74
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080075 }
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 HIGUCHIcd922f42014-02-11 18:59:11 -080090 // TODO handle child Port event properly for performance
91 for (PortEvent portEvt : swEvt.getPorts() ) {
92 removePort(portEvt);
93 }
94
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080095 Switch sw = switches.get(swEvt.getDpid());
96
97 if (sw == null) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -080098 log.warn("Switch {} already removed, ignoring", swEvt);
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080099 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 HIGUCHI4bfdd532014-02-07 13:47:36 -0800133 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800134 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800135
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800136 /**
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800137 * put Port
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800138 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800139 * XXX Internal Invariant-maintenance method. Will not write to DB. Will not
140 * fire Notification.
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800141 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800142 * @param portEvt
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800143 */
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800144 void putPort(PortEvent portEvt) {
145 if (portEvt == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800146 throw new IllegalArgumentException("Port cannot be null");
147 }
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800148 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 HIGUCHI4bfdd532014-02-07 13:47:36 -0800153 }
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800154 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 HIGUCHI4bfdd532014-02-07 13:47:36 -0800165
166 SwitchImpl s = getSwitchImpl(sw);
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800167 s.addPort(port);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800168 }
169
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800170 /**
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800171 * remove Port
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800172 *
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800173 * XXX Internal Invariant-maintenance method. Will not write to DB. Will not
174 * fire Notification.
175 *
176 * @param portEvt
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800177 */
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800178 void removePort(PortEvent portEvt) {
179 if (portEvt == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800180 throw new IllegalArgumentException("Port cannot be null");
181 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800182
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800183 Switch sw = switches.get(portEvt.getDpid());
184 if (sw == null) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800185 log.warn("Parent Switch for Port {} already removed, ignoring", portEvt);
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800186 return;
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800187 }
188
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800189 Port p = sw.getPort(portEvt.getNumber());
190 if (p == null) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800191 log.warn("Port {} already removed, ignoring", portEvt);
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800192 return;
193 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800194
195 // check if there is something referring to this Port
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800196
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800197 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 HIGUCHI4bfdd532014-02-07 13:47:36 -0800214 }
215
216 // remove Port from Switch
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800217 SwitchImpl s = getSwitchImpl(sw);
218 s.removePort(p);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800219 }
220
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800221 /**
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 HIGUCHIcb951982014-02-11 13:31:44 -0800233
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 HIGUCHIcd922f42014-02-11 18:59:11 -0800257
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800258 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 HIGUCHIcd922f42014-02-11 18:59:11 -0800266 // getting Link instance from destination port incoming Link
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800267 Link l = dstPort.getIncomingLink();
268 LinkImpl link = null;
269 assert( l == srcPort.getOutgoingLink() );
270 if (l != null) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800271 link = getLinkImpl(l);
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800272 }
273
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800274 if (link == null) {
275 link = new LinkImpl(this, srcPort, dstPort);
276 }
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800277
278
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800279 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 HIGUCHIcb951982014-02-11 13:31:44 -0800308 }
309
310 /**
311 * removeLink
312 *
313 * XXX Internal Invariant-maintenance method. Will not write to DB. Will not
314 * fire Notification.
315 *
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800316 * @param linkEvt
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800317 */
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800318 void removeLink(LinkEvent linkEvt) {
319 if (linkEvt == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800320 throw new IllegalArgumentException("Link cannot be null");
321 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800322
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800323 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 HIGUCHI80829d12014-02-05 20:16:56 -0800358 }
359
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800360 // XXX Need to rework Device related
361 /**
362 * Add new device to DB
363 *
364 * @param device
365 */
Yuta HIGUCHI0be4a662014-02-11 18:01:28 -0800366 void putDevice(DeviceEvent deviceEvt) {
367 if (deviceEvt == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800368 throw new IllegalArgumentException("Device cannot be null");
369 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800370
371 // for each attachment point
372 /// TODO check if Link exist on that Port
373
374
375
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800376 // TODO Auto-generated method stub
377
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800378 // 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 HIGUCHI80829d12014-02-05 20:16:56 -0800446 }
447
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800448 void removeDevice(DeviceEvent device) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800449 if (device == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800450 throw new IllegalArgumentException("Device cannot be null");
451 }
452 // TODO Auto-generated method stub
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800453 }
454
455 private SwitchImpl getSwitchImpl(Switch sw) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800456 if (sw instanceof SwitchImpl) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800457 return (SwitchImpl) sw;
458 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800459 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 HIGUCHIcd922f42014-02-11 18:59:11 -0800469 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 HIGUCHIc0366272014-02-10 21:04:57 -0800476 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 HIGUCHI765cd0d2014-02-06 12:46:41 -0800483 public void loadWholeTopologyFromDB() {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800484 // TODO this method needs to use East-bound API if we still need this
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800485 // XXX clear everything first?
486
487 for (RCSwitch sw : RCSwitch.getAllSwitches()) {
488 try {
489 sw.read();
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800490 // 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 HIGUCHI765cd0d2014-02-06 12:46:41 -0800493 } 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 HIGUCHI765cd0d2014-02-06 12:46:41 -0800502 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 HIGUCHIcb951982014-02-11 13:31:44 -0800508 PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber());
509 // TODO update other attributes if there exist any
510 putPort(portEvent);
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800511 } 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 HIGUCHI80829d12014-02-05 20:16:56 -0800527 try {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800528 l.read();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800529
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800530 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 HIGUCHI80829d12014-02-05 20:16:56 -0800536
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800537 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 HIGUCHI80829d12014-02-05 20:16:56 -0800543
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800544 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 HIGUCHI80829d12014-02-05 20:16:56 -0800548 } catch (ObjectDoesntExistException e) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800549 log.debug("Delete Link Failed", e);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800550 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800551 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800552 }
Yuta HIGUCHIcb951982014-02-11 13:31:44 -0800553
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 Hart22eb9882014-02-11 15:52:59 -0800570
571 /* ******************************
572 * NetworkGraphDiscoveryInterface methods
573 * ******************************/
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800574
Jonathan Hart22eb9882014-02-11 15:52:59 -0800575 @Override
576 public void putSwitchEvent(SwitchEvent switchEvent) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800577 if (checkAddSwitchInvariant(switchEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800578 datastore.addSwitch(switchEvent);
579 putSwitch(switchEvent);
580 }
581 // TODO handle invariant violation
582 }
583
584 @Override
585 public void removeSwitchEvent(SwitchEvent switchEvent) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800586 if (checkRemoveSwitchInvariant(switchEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800587 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 HIGUCHIcd922f42014-02-11 18:59:11 -0800596
Jonathan Hart22eb9882014-02-11 15:52:59 -0800597 }
598
599 @Override
600 public void removePortEvent(PortEvent portEvent) {
601 // TODO Auto-generated method stub
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800602
Jonathan Hart22eb9882014-02-11 15:52:59 -0800603 }
604
605 @Override
606 public void putLinkEvent(LinkEvent linkEvent) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800607 if (checkAddLinkInvariant(linkEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800608 datastore.addLink(linkEvent);
609 putLink(linkEvent);
610 }
611 // TODO handle invariant violation
612 }
613
614 @Override
615 public void removeLinkEvent(LinkEvent linkEvent) {
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800616 if (checkRemoveLinkInvariant(linkEvent)) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800617 datastore.removeLink(linkEvent);
618 removeLink(linkEvent);
619 }
620 // TODO handle invariant violation
621 }
622
623 @Override
Yuta HIGUCHI0be4a662014-02-11 18:01:28 -0800624 public void putDeviceEvent(DeviceEvent device) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800625 // TODO Auto-generated method stub
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800626
Jonathan Hart22eb9882014-02-11 15:52:59 -0800627 }
628
629 @Override
630 public void removeDeviceEvent(DeviceEvent deviceEvent) {
631 // TODO Auto-generated method stub
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800632
Jonathan Hart22eb9882014-02-11 15:52:59 -0800633 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800634
Jonathan Hart22eb9882014-02-11 15:52:59 -0800635 /* *****************
636 * Internal methods to check invariants of the network graph
637 * *****************/
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800638
639 private boolean checkAddSwitchInvariant(SwitchEvent swEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800640 // TODO implement
641 return true;
642 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800643
644 private boolean checkRemoveSwitchInvariant(SwitchEvent swEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800645 // TODO implement
646 return true;
647 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800648
649 private boolean checkAddPortInvariant(PortEvent portEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800650 // TODO implement
651 return true;
652 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800653
654 private boolean checkRemovePortInvariant(PortEvent portEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800655 // TODO implement
656 return true;
657 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800658
659 private boolean checkAddLinkInvariant(LinkEvent linkEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800660 // TODO implement
661 return true;
662 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800663
664 private boolean checkRemoveLinkInvariant(LinkEvent linkEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800665 // TODO implement
666 return true;
667 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800668
669 private boolean checkAddDeviceInvariant(DeviceEvent deviceEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800670 // TODO implement
671 return true;
672 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800673
674 private boolean checkRemoveDeviceInvariant(DeviceEvent deviceEvt) {
Jonathan Hart22eb9882014-02-11 15:52:59 -0800675 // TODO implement
676 return true;
677 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800678}