public void removeLink(Link link) {
    boolean flag1 = false, flag2 = false;

    flag1 = removeLinkFromStructure(directLinks, link);
    flag2 = removeLinkFromStructure(tunnelLinks, link);

    linksUpdated = true;
    dtLinksUpdated = flag1 || flag2;

    removeLinkFromStructure(portBroadcastDomainLinks, link);
    removeLinkFromStructure(switchPortLinks, link);

    NodePortTuple srcNpt = new NodePortTuple(link.getSrc(), link.getSrcPort());
    NodePortTuple dstNpt = new NodePortTuple(link.getDst(), link.getDstPort());

    // Remove switch ports if there are no links through those switch ports
    if (switchPortLinks.get(srcNpt) == null) {
      if (switchPorts.get(srcNpt.getNodeId()) != null)
        switchPorts.get(srcNpt.getNodeId()).remove(srcNpt.getPortId());
    }
    if (switchPortLinks.get(dstNpt) == null) {
      if (switchPorts.get(dstNpt.getNodeId()) != null)
        switchPorts.get(dstNpt.getNodeId()).remove(dstNpt.getPortId());
    }

    // Remove the node if no ports are present
    if (switchPorts.get(srcNpt.getNodeId()) != null
        && switchPorts.get(srcNpt.getNodeId()).isEmpty()) {
      switchPorts.remove(srcNpt.getNodeId());
    }
    if (switchPorts.get(dstNpt.getNodeId()) != null
        && switchPorts.get(dstNpt.getNodeId()).isEmpty()) {
      switchPorts.remove(dstNpt.getNodeId());
    }
  }
  /**
   * Delete the given link from the data strucure. Returns true if the link was deleted.
   *
   * @param s
   * @param l
   * @return
   */
  private boolean removeLinkFromStructure(Map<NodePortTuple, Set<Link>> s, Link l) {

    boolean result1 = false, result2 = false;
    NodePortTuple n1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
    NodePortTuple n2 = new NodePortTuple(l.getDst(), l.getDstPort());

    if (s.get(n1) != null) {
      result1 = s.get(n1).remove(l);
      if (s.get(n1).isEmpty()) s.remove(n1);
    }
    if (s.get(n2) != null) {
      result2 = s.get(n2).remove(l);
      if (s.get(n2).isEmpty()) s.remove(n2);
    }
    return result1 || result2;
  }
  /**
   * Add the given link to the data structure. Returns true if a link was added.
   *
   * @param s
   * @param l
   * @return
   */
  private boolean addLinkToStructure(Map<NodePortTuple, Set<Link>> s, Link l) {
    boolean result1 = false, result2 = false;

    NodePortTuple n1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
    NodePortTuple n2 = new NodePortTuple(l.getDst(), l.getDstPort());

    if (s.get(n1) == null) {
      s.put(n1, new HashSet<Link>());
    }
    if (s.get(n2) == null) {
      s.put(n2, new HashSet<Link>());
    }
    result1 = s.get(n1).add(l);
    result2 = s.get(n2).add(l);

    return (result1 || result2);
  }
  /** Calculates the node ports that are used by the overall spanning tree. */
  protected void calculateBroadcastNodePorts() {
    calculateBroadcastTree();

    Set<NodePortTuple> nptSet = new HashSet<NodePortTuple>();
    if (broadcastTree == null) return;
    Map<Long, Link> links = broadcastTree.getLinks();
    if (links == null) return;
    for (long nodeId : links.keySet()) {
      Link l = links.get(nodeId);
      if (l == null) continue;
      NodePortTuple npt1 = new NodePortTuple(l.getSrc(), l.getSrcPort());
      NodePortTuple npt2 = new NodePortTuple(l.getDst(), l.getDstPort());
      nptSet.add(npt1);
      nptSet.add(npt2);
    }
    topologyBroadcastNodePorts.addAll(nptSet);
  }