/**
   * build an intersection of the locally configured capabilities and the capabilites passed in from
   * the peer.
   *
   * @return
   */
  public Set<Capability> intersectLocalAndRemoteCapabilities() {
    Set<Capability> caps = new TreeSet<Capability>();

    for (Capability cap : peerConfiguration.getCapabilities().getRequiredCapabilities())
      if (remoteCapabilities.contains(cap)) caps.add(cap);
    for (Capability cap : peerConfiguration.getCapabilities().getOptionalCapabilities())
      if (remoteCapabilities.contains(cap)) caps.add(cap);

    return caps;
  }
  /**
   * List all locally configured capabilities of a given type
   *
   * @param capClass
   * @return
   */
  public <T extends Capability> Set<T> listLocalCapabilities(Class<T> capClass) {
    Set<T> caps = new HashSet<T>();

    caps.addAll(
        listCapabilities(peerConfiguration.getCapabilities().getRequiredCapabilities(), capClass));
    caps.addAll(
        listCapabilities(peerConfiguration.getCapabilities().getOptionalCapabilities(), capClass));

    return caps;
  }
  /**
   * build a set of capabilites which are marked as required in the local configuration and which
   * are not supported by the peer. The local speaker may build an Unsupported Capabilities
   * notification message based on that info
   *
   * @return
   */
  public Set<Capability> missingRequiredCapabilities() {
    Set<Capability> caps = new TreeSet<Capability>();

    for (Capability cap : peerConfiguration.getCapabilities().getRequiredCapabilities())
      if (!remoteCapabilities.contains(cap)) caps.add(cap);

    return caps;
  }
  /**
   * Insert the currently negotiated capabilities into the OPEN packet.
   *
   * <p>If the capabilities contain an AS number larger than 65536 then the AS number in the OPEN
   * packet is changed to AS_TRANS (23456)
   *
   * @param packet
   * @see BGPv4Constants#BGP_AS_TRANS
   */
  void insertLocalCapabilities(OpenPacket packet) {
    packet.getCapabilities().clear();

    insertLocalCapabilities(packet, peerConfiguration.getCapabilities().getRequiredCapabilities());
    insertLocalCapabilities(packet, peerConfiguration.getCapabilities().getOptionalCapabilities());
  }