@Test public void testHARoleChange() throws Exception { LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); IOFSwitch sw1 = createMockSwitch(1L); IOFSwitch sw2 = createMockSwitch(2L); replay(sw1, sw2); Link lt = new Link(DatapathId.of(1L), OFPort.of(2), DatapathId.of(2L), OFPort.of(1), U64.ZERO); NodePortTuple srcNpt = new NodePortTuple(DatapathId.of(1L), OFPort.of(2)); NodePortTuple dstNpt = new NodePortTuple(DatapathId.of(2L), OFPort.of(1)); LinkInfo info = new LinkInfo(new Date(), new Date(), null); linkDiscovery.addOrUpdateLink(lt, info); assertNotNull(linkDiscovery.switchLinks.get(lt.getSrc())); assertTrue(linkDiscovery.switchLinks.get(lt.getSrc()).contains(lt)); assertNotNull(linkDiscovery.portLinks.get(srcNpt)); assertTrue(linkDiscovery.portLinks.get(srcNpt).contains(lt)); assertNotNull(linkDiscovery.portLinks.get(dstNpt)); assertTrue(linkDiscovery.portLinks.get(dstNpt).contains(lt)); assertTrue(linkDiscovery.links.containsKey(lt)); }
protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { boolean linkChanged = false; lock.writeLock().lock(); try { LinkInfo existingInfo = null; if (links.get(lt) == null) { links.put(lt, newInfo); } else { existingInfo = links.get(lt); } if (existingInfo != null && existingInfo.getFirstSeenTime().before(newInfo.getFirstSeenTime())) { existingInfo.setFirstSeenTime(newInfo.getFirstSeenTime()); } if (log.isTraceEnabled()) { log.trace("addOrUpdateLink: {} {}", lt, (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast"); } UpdateOperation updateOperation = null; linkChanged = false; if (existingInfo == null) { addLink(lt, newInfo); updateOperation = UpdateOperation.LINK_UPDATED; linkChanged = true; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link detected: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "direct-link-added::rcvd LLDP")); } notifier.postNotification("Link added: " + lt.toString()); } else { linkChanged = updateLink(lt, existingInfo, newInfo); if (linkChanged) { updateOperation = UpdateOperation.LINK_UPDATED; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link updated: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "link-port-state-updated::rcvd LLDP")); } notifier.postNotification("Link updated: " + lt.toString()); } } if (linkChanged) { updates.add(new LDUpdate(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), lt.getLatency(), getLinkType(lt, newInfo), updateOperation)); Iterator<Entry<Link, LinkInfo>> it = links.entrySet().iterator(); while (it.hasNext()) { Entry<Link, LinkInfo> entry = it.next(); if (entry.getKey().equals(lt)) { entry.getKey().setLatency(lt.getLatency()); break; } } } writeLinkToStorage(lt, newInfo); } finally { lock.writeLock().unlock(); } return linkChanged; }
LinkDiscoveryManager implements IOFMessageListener,
IOFSwitchListener, IStorageSourceListener, ILinkDiscoveryService,
IFloodlightModule, IInfoProvider { protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { boolean linkChanged = false; lock.writeLock().lock(); try { LinkInfo existingInfo = null; if (links.get(lt) == null) { links.put(lt, newInfo); } else { existingInfo = links.get(lt); } if (existingInfo != null && existingInfo.getFirstSeenTime().before(newInfo.getFirstSeenTime())) { existingInfo.setFirstSeenTime(newInfo.getFirstSeenTime()); } if (log.isTraceEnabled()) { log.trace("addOrUpdateLink: {} {}", lt, (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast"); } UpdateOperation updateOperation = null; linkChanged = false; if (existingInfo == null) { addLink(lt, newInfo); updateOperation = UpdateOperation.LINK_UPDATED; linkChanged = true; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link detected: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "direct-link-added::rcvd LLDP")); } notifier.postNotification("Link added: " + lt.toString()); } else { linkChanged = updateLink(lt, existingInfo, newInfo); if (linkChanged) { updateOperation = UpdateOperation.LINK_UPDATED; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link updated: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "link-port-state-updated::rcvd LLDP")); } notifier.postNotification("Link updated: " + lt.toString()); } } if (linkChanged) { updates.add(new LDUpdate(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), lt.getLatency(), getLinkType(lt, newInfo), updateOperation)); Iterator<Entry<Link, LinkInfo>> it = links.entrySet().iterator(); while (it.hasNext()) { Entry<Link, LinkInfo> entry = it.next(); if (entry.getKey().equals(lt)) { entry.getKey().setLatency(lt.getLatency()); break; } } } writeLinkToStorage(lt, newInfo); } finally { lock.writeLock().unlock(); } return linkChanged; } }
LinkDiscoveryManager implements IOFMessageListener,
IOFSwitchListener, IStorageSourceListener, ILinkDiscoveryService,
IFloodlightModule, IInfoProvider { protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { boolean linkChanged = false; lock.writeLock().lock(); try { LinkInfo existingInfo = null; if (links.get(lt) == null) { links.put(lt, newInfo); } else { existingInfo = links.get(lt); } if (existingInfo != null && existingInfo.getFirstSeenTime().before(newInfo.getFirstSeenTime())) { existingInfo.setFirstSeenTime(newInfo.getFirstSeenTime()); } if (log.isTraceEnabled()) { log.trace("addOrUpdateLink: {} {}", lt, (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast"); } UpdateOperation updateOperation = null; linkChanged = false; if (existingInfo == null) { addLink(lt, newInfo); updateOperation = UpdateOperation.LINK_UPDATED; linkChanged = true; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link detected: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "direct-link-added::rcvd LLDP")); } notifier.postNotification("Link added: " + lt.toString()); } else { linkChanged = updateLink(lt, existingInfo, newInfo); if (linkChanged) { updateOperation = UpdateOperation.LINK_UPDATED; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link updated: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "link-port-state-updated::rcvd LLDP")); } notifier.postNotification("Link updated: " + lt.toString()); } } if (linkChanged) { updates.add(new LDUpdate(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), lt.getLatency(), getLinkType(lt, newInfo), updateOperation)); Iterator<Entry<Link, LinkInfo>> it = links.entrySet().iterator(); while (it.hasNext()) { Entry<Link, LinkInfo> entry = it.next(); if (entry.getKey().equals(lt)) { entry.getKey().setLatency(lt.getLatency()); break; } } } writeLinkToStorage(lt, newInfo); } finally { lock.writeLock().unlock(); } return linkChanged; }  }
LinkDiscoveryManager implements IOFMessageListener,
IOFSwitchListener, IStorageSourceListener, ILinkDiscoveryService,
IFloodlightModule, IInfoProvider { protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { boolean linkChanged = false; lock.writeLock().lock(); try { LinkInfo existingInfo = null; if (links.get(lt) == null) { links.put(lt, newInfo); } else { existingInfo = links.get(lt); } if (existingInfo != null && existingInfo.getFirstSeenTime().before(newInfo.getFirstSeenTime())) { existingInfo.setFirstSeenTime(newInfo.getFirstSeenTime()); } if (log.isTraceEnabled()) { log.trace("addOrUpdateLink: {} {}", lt, (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast"); } UpdateOperation updateOperation = null; linkChanged = false; if (existingInfo == null) { addLink(lt, newInfo); updateOperation = UpdateOperation.LINK_UPDATED; linkChanged = true; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link detected: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "direct-link-added::rcvd LLDP")); } notifier.postNotification("Link added: " + lt.toString()); } else { linkChanged = updateLink(lt, existingInfo, newInfo); if (linkChanged) { updateOperation = UpdateOperation.LINK_UPDATED; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link updated: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "link-port-state-updated::rcvd LLDP")); } notifier.postNotification("Link updated: " + lt.toString()); } } if (linkChanged) { updates.add(new LDUpdate(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), lt.getLatency(), getLinkType(lt, newInfo), updateOperation)); Iterator<Entry<Link, LinkInfo>> it = links.entrySet().iterator(); while (it.hasNext()) { Entry<Link, LinkInfo> entry = it.next(); if (entry.getKey().equals(lt)) { entry.getKey().setLatency(lt.getLatency()); break; } } } writeLinkToStorage(lt, newInfo); } finally { lock.writeLock().unlock(); } return linkChanged; }  @Override OFPacketOut generateLLDPMessage(IOFSwitch iofSwitch, OFPort port,
			boolean isStandard, boolean isReverse); int getLldpFrequency(); int getLldpTimeout(); @Override Map<NodePortTuple, Set<Link>> getPortLinks(); @Override Set<NodePortTuple> getSuppressLLDPsInfo(); @Override void AddToSuppressLLDPs(DatapathId sw, OFPort port); @Override void RemoveFromSuppressLLDPs(DatapathId sw, OFPort port); boolean isShuttingDown(); @Override boolean isTunnelPort(DatapathId sw, OFPort port); @Override ILinkDiscovery.LinkType getLinkType(Link lt, LinkInfo info); @Override Set<OFPort> getQuarantinedPorts(DatapathId sw); @Override Map<DatapathId, Set<Link>> getSwitchLinks(); @Override void addMACToIgnoreList(MacAddress mac, int ignoreBits); @Override boolean isAutoPortFastFeature(); @Override void setAutoPortFastFeature(boolean autoPortFastFeature); @Override void addListener(ILinkDiscoveryListener listener); @Override Map<Link, LinkInfo> getLinks(); @Override LinkInfo getLinkInfo(Link link); @Override String getName(); @Override Command receive(IOFSwitch sw, OFMessage msg,
			FloodlightContext cntx); @Override boolean isCallbackOrderingPrereq(OFType type, String name); @Override boolean isCallbackOrderingPostreq(OFType type, String name); @Override void switchPortChanged(DatapathId switchId,
			OFPortDesc port,
			PortChangeType type); @Override void switchAdded(DatapathId switchId); @Override void switchRemoved(DatapathId sw); @Override void switchActivated(DatapathId switchId); @Override void switchChanged(DatapathId switchId); void setStorageSource(IStorageSourceService storageSourceService); IStorageSourceService getStorageSource(); @Override void rowsModified(String tableName, Set<Object> rowKeys); @Override void rowsDeleted(String tableName, Set<Object> rowKeys); Long readLinkValidTime(Link lt); @Override Collection<Class<? extends IFloodlightService>> getModuleServices(); @Override Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls(); @Override Collection<Class<? extends IFloodlightService>> getModuleDependencies(); @Override void init(FloodlightModuleContext context); @Override void startUp(FloodlightModuleContext context); @Override Map<String, Object> getInfo(String type); }
LinkDiscoveryManager implements IOFMessageListener,
IOFSwitchListener, IStorageSourceListener, ILinkDiscoveryService,
IFloodlightModule, IInfoProvider { protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { boolean linkChanged = false; lock.writeLock().lock(); try { LinkInfo existingInfo = null; if (links.get(lt) == null) { links.put(lt, newInfo); } else { existingInfo = links.get(lt); } if (existingInfo != null && existingInfo.getFirstSeenTime().before(newInfo.getFirstSeenTime())) { existingInfo.setFirstSeenTime(newInfo.getFirstSeenTime()); } if (log.isTraceEnabled()) { log.trace("addOrUpdateLink: {} {}", lt, (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast"); } UpdateOperation updateOperation = null; linkChanged = false; if (existingInfo == null) { addLink(lt, newInfo); updateOperation = UpdateOperation.LINK_UPDATED; linkChanged = true; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link detected: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "direct-link-added::rcvd LLDP")); } notifier.postNotification("Link added: " + lt.toString()); } else { linkChanged = updateLink(lt, existingInfo, newInfo); if (linkChanged) { updateOperation = UpdateOperation.LINK_UPDATED; LinkType linkType = getLinkType(lt, newInfo); if (linkType == ILinkDiscovery.LinkType.DIRECT_LINK) { log.debug("Inter-switch link updated: {}", lt); eventCategory.newEventNoFlush(new DirectLinkEvent(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), "link-port-state-updated::rcvd LLDP")); } notifier.postNotification("Link updated: " + lt.toString()); } } if (linkChanged) { updates.add(new LDUpdate(lt.getSrc(), lt.getSrcPort(), lt.getDst(), lt.getDstPort(), lt.getLatency(), getLinkType(lt, newInfo), updateOperation)); Iterator<Entry<Link, LinkInfo>> it = links.entrySet().iterator(); while (it.hasNext()) { Entry<Link, LinkInfo> entry = it.next(); if (entry.getKey().equals(lt)) { entry.getKey().setLatency(lt.getLatency()); break; } } } writeLinkToStorage(lt, newInfo); } finally { lock.writeLock().unlock(); } return linkChanged; }  @Override OFPacketOut generateLLDPMessage(IOFSwitch iofSwitch, OFPort port,
			boolean isStandard, boolean isReverse); int getLldpFrequency(); int getLldpTimeout(); @Override Map<NodePortTuple, Set<Link>> getPortLinks(); @Override Set<NodePortTuple> getSuppressLLDPsInfo(); @Override void AddToSuppressLLDPs(DatapathId sw, OFPort port); @Override void RemoveFromSuppressLLDPs(DatapathId sw, OFPort port); boolean isShuttingDown(); @Override boolean isTunnelPort(DatapathId sw, OFPort port); @Override ILinkDiscovery.LinkType getLinkType(Link lt, LinkInfo info); @Override Set<OFPort> getQuarantinedPorts(DatapathId sw); @Override Map<DatapathId, Set<Link>> getSwitchLinks(); @Override void addMACToIgnoreList(MacAddress mac, int ignoreBits); @Override boolean isAutoPortFastFeature(); @Override void setAutoPortFastFeature(boolean autoPortFastFeature); @Override void addListener(ILinkDiscoveryListener listener); @Override Map<Link, LinkInfo> getLinks(); @Override LinkInfo getLinkInfo(Link link); @Override String getName(); @Override Command receive(IOFSwitch sw, OFMessage msg,
			FloodlightContext cntx); @Override boolean isCallbackOrderingPrereq(OFType type, String name); @Override boolean isCallbackOrderingPostreq(OFType type, String name); @Override void switchPortChanged(DatapathId switchId,
			OFPortDesc port,
			PortChangeType type); @Override void switchAdded(DatapathId switchId); @Override void switchRemoved(DatapathId sw); @Override void switchActivated(DatapathId switchId); @Override void switchChanged(DatapathId switchId); void setStorageSource(IStorageSourceService storageSourceService); IStorageSourceService getStorageSource(); @Override void rowsModified(String tableName, Set<Object> rowKeys); @Override void rowsDeleted(String tableName, Set<Object> rowKeys); Long readLinkValidTime(Link lt); @Override Collection<Class<? extends IFloodlightService>> getModuleServices(); @Override Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls(); @Override Collection<Class<? extends IFloodlightService>> getModuleDependencies(); @Override void init(FloodlightModuleContext context); @Override void startUp(FloodlightModuleContext context); @Override Map<String, Object> getInfo(String type); static final String MODULE_NAME; }