submodule ietf-bgp-rib {
  yang-version 1.1;
  belongs-to ietf-bgp {
    prefix bgp;
  }

  /*
   * Import and Include
   */

  import iana-bgp-afi-safi-types {
    prefix bast;
    reference
      "RFC XXXX: YANG Model for Border Gateway Protocol (BGP-4).";
  }
  import ietf-bgp-types {
    prefix ibt;
    reference
      "RFC XXXX: YANG Model for Border Gateway Protocol (BGP-4).";
  }
  import ietf-inet-types {
    prefix inet;
    reference
      "RFC 9911: Common YANG Types.";
  }
  import ietf-yang-types {
    prefix yang;
    reference
      "RFC 9911: Common YANG Types.";
  }
  import ietf-yang-semver {
    prefix ysv;
    reference
      "I-D.ietf-netmod-yang-semver: YANG Semantic Versioning";
  }
  include ietf-bgp-rib-tables;

  // groupings of attributes in three categories:
  //  - shared across multiple routes
  //  - common to Loc-RIB and Adj-RIB, but not shared across routes
  //  - specific to Loc-RIB or Adj-RIB
  // groupings of annotations for each route or table
  include ietf-bgp-rib-attributes;

  organization
    "IETF IDR Working Group";
  contact
    "WG Web:   <http://datatracker.ietf.org/wg/idr>
     WG List:  <idr@ietf.org>

     Authors: Mahesh Jethanandani (mjethanandani at gmail.com),
              Keyur Patel (keyur at arrcus.com),
              Susan Hares (shares at ndzh.com),
              Jeffrey Haas (jeffrey.haas at hpe.com).";

  description
    "Defines a submodule for representing BGP Routing Information
     Base (RIB) contents.  The submodule supports 5 logical RIBs per
     address family:

     loc-rib: This is the main BGP routing table for the local
     routing instance, containing best-path selections for each
     prefix.  The key for the loc-rib will correspond either to a
     route in the adj-rib-in-post table, described below, or a route
     redistributed from another protocol.

     adj-rib-in-pre: This is a per-neighbor table containing the BGP
     routes received from the neighbor before any local input policy
     rules has been applied.  This can be considered the 'raw' routes
     from a given neighbor.

     adj-rib-in-post: This is a per-neighbor table containing
     the routes received from the neighbor that are eligible for
     best-path selection after local input policy rules have been
     applied. The 'best-path' leaf is attached to the route selected
     by the BGP Decision Process (RFC 4271, Section 9.1).  Such
     routes may be present in the loc-rib table, described above,
     when local configuration determines that the BGP best-path will
     be used for that destination.

     adj-rib-out-pre: This is a per-neighbor table containing routes
     eligible to be sent (advertised) to the neighbor before output
     policy rules have been applied.

     adj-rib-out-post: This is a per-neighbor table containing routes
     eligible to be sent (advertised) to the neighbor after output
     policy rules have been applied.

     Copyright (c) 2026 IETF Trust and the persons identified as
     authors of the code. All rights reserved.

     Redistribution and use in source and binary forms, with or
     without modification, is permitted pursuant to, and subject to
     the license terms contained in, the Revised BSD License set
     forth in Section 4.c of the IETF Trust's Legal Provisions
     Relating to IETF Documents
     (https://trustee.ietf.org/license-info).

     This version of this YANG module is part of RFC XXXX
     (https://www.rfc-editor.org/info/rfcXXXX); see the RFC itself
     for full legal notices.

     The key words 'MUST', 'MUST NOT', 'REQUIRED', 'SHALL', 'SHALL
     NOT', 'SHOULD', 'SHOULD NOT', 'RECOMMENDED', 'NOT RECOMMENDED',
     'MAY', and 'OPTIONAL' in this document are to be interpreted as
     described in BCP 14 (RFC 2119) (RFC 8174) when, and only when,
     they appear in all capitals, as shown here.";

  revision 2026-06-24 {
    description
      "Initial Version";
    ysv:version 0.0.9;
    reference
      "RFC XXXX: YANG Model for Border Gateway Protocol (BGP-4).";
  }

  grouping attr-set-attributes {
    description
      "A grouping for all BGP Path Attribute set parameters.";

    container attributes {
      description
        "A container for attribute set parameters.";

      leaf origin {
        type ibt:bgp-origin-attr-type;
        description
          "BGP attribute defining the origin of the path
           information.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.1.";
      }
      container as-path {
        description
          "Enclosing container for the list of AS path segments.

           In the Adj-RIB-In or Adj-RIB-Out, this list should show
           the received or sent AS_PATH, respectively.  For
           example, if the local router is not 4-byte capable, this
           value should consist of 2-octet ASNs or the AS_TRANS
           (AS 23456) values received or sent in BGP updates.

           In the Loc-RIB, this list should reflect the effective
           AS path for the route, e.g., a 4-octet value if the
           local router is 4-octet capable.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4),
           RFC 5065: Autonomous System Confederations for BGP,
           RFC 6793: BGP Support for Four-octet AS Number Space.";
        list segment {
          config false;
          uses bgp-as-path-segment;
          description
            "List of AS PATH segments";
        }
      }
      container as4-path {
        description
          "This is the path encoded with 4-octet
           AS numbers in the optional transitive AS4_PATH attribute.
           This value is populated with the received or sent
           attribute in Adj-RIB-In or Adj-RIB-Out, respectively.
           It should not be populated in Loc-RIB since the Loc-RIB
           is expected to store the effective AS-Path in the
           as-path leaf regardless of being 4-octet or 2-octet.";
        reference
          "RFC 6793: BGP Support for Four-octet AS Number Space";
        list segment {
          config false;
          uses bgp-as-path-segment;
          description
            "List of AS PATH segments";
        }
      }
      leaf next-hop {
        type inet:ip-address;
        description
          "BGP next hop attribute defining the IP address of the
           router that should be used as the next hop to the
           destination.  Used when the BGP routes' nexthop for that
           AFI/SAFI can be represented as an IPv4/IPv6 address.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.3.";
      }
      leaf link-local-next-hop {
        type inet:ipv6-address-link-local;
        description
          "When both a global and a link-local next-hop are sent
           when following RFC 2545 procedures, this leaf contains
           the link-local next-hop.";
        reference
          "RFC 2545: Use of BGP-4 Multiprotocol Extensions for IPv6
           Inter-Domain Routing.";
      }
      leaf med {
        type uint32;
        description
          "BGP multi-exit discriminator attribute used in the BGP
           route selection process.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.4.";
      }
      leaf local-pref {
        type uint32;
        description
          "BGP local preference attribute sent to internal peers to
           indicate the degree of preference for externally learned
           routes.  The route with the highest local preference
           value is preferred.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.5.";
      }
      container aggregator {
        config false;
        description
          "BGP attribute indicating the prefix has been
           aggregated by the specified AS and router.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.7,
           RFC 6793: BGP Support for Four-octet AS Number Space.";
        leaf as {
          type inet:as-number;
          description
            "AS number of the autonomous system that performed the
             aggregation.";
          reference
            "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
             4.3, Path Attributes (g).";
        }
        leaf identifier {
          type yang:dotted-quad;
          description
            "BGP Identifier of the router that performed the
             aggregation.";
          reference
            "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
             4.3, Path Attributes (g).";
        }
      }
      container aggregator4 {
        config false;
        description
          "BGP attribute indicating the prefix has been
           aggregated by the specified AS and router.
           This value is populated with the received or sent
           attribute in Adj-RIB-In or Adj-RIB-Out, respectively.
           It should not be populated in Loc-RIB since the Loc-RIB
           is expected to store the effective AGGREGATOR in the
           aggregator/as leaf regardless of being 4-octet or
           2-octet.";
        reference
          "RFC 6793: BGP Support for Four-octet AS Number Space.";
        leaf as4 {
          type inet:as-number;
          description
            "AS number of the autonomous system that performed the
             aggregation (4-octet representation).  This value is
             populated if an upstream router is not 4-octet capable.
             Its semantics are similar to the AS4_PATH optional
             transitive attribute";
          reference
            "RFC 6793: BGP Support for Four-octet AS Number Space,
             Section 3,
             RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
             4.3, Path Attributes (g).";
        }
        leaf identifier {
          type yang:dotted-quad;
          description
            "BGP Identifier of the router that performed the
             aggregation.";
          reference
            "RFC 6793: BGP Support for Four-octet AS Number Space,
             Section 3,
             RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
             4.3, Path Attributes (g).";
        }
      }
      leaf atomic-aggregate {
        type boolean;
        description
          "BGP attribute indicating that the prefix is an atomic
           aggregate; i.e., the peer selected is a less specific
           route without selecting a more specific route that is
           subsumed by it.";
        reference
          "RFC 4271: A Border Gateway Protocol 4 (BGP-4), Section
           5.1.6.";
      }
      leaf originator-id {
        type yang:dotted-quad;
        description
          "BGP attribute that provides the id as an IPv4 address
           of the originator of the announcement.";
        reference
          "RFC 4456: BGP Route Reflection: An Alternative to Full
           Mesh Internal BGP (IBGP).";
      }
      leaf-list cluster-list {
        type yang:dotted-quad;
        description
          "Represents the reflection path that the route has
           passed.";
        reference
          "RFC 4456: BGP Route Reflection: An Alternative to Full
           Mesh Internal BGP (IBGP).";
      }
      leaf aigp-metric {
        type uint64;
        description
          "BGP path attribute representing the accumulated IGP
           metric for the path";
        reference
          "RFC 7311: The Accumulated IGP Metric Attribute for BGP.";
      }
    }
  }

  grouping attr-sets {
    description
      "A grouping for all sets of path attributes.";

    container attr-sets {
      description
        "Enclosing container for the list of path attribute sets";

      list attr-set {
        key "index";
        description
          "List of path attributes that may be in use by multiple
           routes in the table";
        leaf index {
          type uint64;
          description
            "System generated index for each attribute set.  The
             index is used to reference an attribute set from a
             specific path.  Multiple paths may reference the same
             attribute set.";
        }
        uses attr-set-attributes;
      }
    }
  }

  grouping rib {
    description
      "Grouping for rib.";

    container rib {
      config false;
      uses attr-sets;
      container communities {
        description
          "Enclosing container for the list of community attribute
           sets.";
        list community {
          key "index";
          config false;
          description
            "List of path attributes that may be in use by multiple
             routes in the table.";
          leaf index {
            type uint64;
            description
              "System generated index for each attribute set.  The
               index is used to reference an attribute set from a
               specific path.  Multiple paths may reference the same
               attribute set.";
          }
          uses bgp-community-attr-state;
        }
      }
      container ext-communities {
        description
          "Enclosing container for the list of extended community
           attribute sets.";
        list ext-community {
          key "index";
          config false;
          description
            "List of path attributes that may be in use by multiple
             routes in the table.";
          leaf index {
            type uint64;
            description
              "System generated index for each attribute set.  The
               index is used to reference an attribute set from a
               specific path.  Multiple paths may reference the same
               attribute set.";
          }
          uses ext-community-attributes;
        }
      }
      container ipv6-ext-communities {
        description
          "Enclosing container for the list of IPv6 extended
           community attribute sets.";
        list ipv6-ext-community {
          key "index";
          config false;
          description
            "List of path attributes that may be in use by multiple
             routes in the table.";
          leaf index {
            type uint64;
            description
              "System generated index for each attribute set.  The
               index is used to reference an attribute set from a
               specific path.  Multiple paths may reference the same
               attribute set.";
          }
          uses ipv6-ext-community-attributes;
        }
      }
      container large-communities {
        description
          "Enclosing container for the list of large community
           attribute sets.";
        list large-community {
          key "index";
          config false;
          description
            "List of path attributes that may be in use by multiple
             routes in the table.";
          leaf index {
            type uint64;
            description
              "System generated index for each attribute set. The
               index is used to reference an attribute set from a
               specific path.  Multiple paths may reference the same
               attribute set.";
          }
          uses large-community-attributes;
        }
      }

      container afi-safis {
        config false;
        description
          "Enclosing container for address family list.";
        list afi-safi {
          key "name";
          description
            "List of afi-safi types.";
          leaf name {
            type identityref {
              base bast:afi-safi-type;
            }
            description
              "AFI,SAFI name.";
          }
          container ipv4-unicast {
            when "derived-from-or-self(../name, 'bast:ipv4-unicast')" {
              description
                "Include this container for IPv4 unicast RIB.";
            }
            description
              "Routing tables for IPv4 unicast -- active when the
               afi-safi name is ipv4-unicast.";
            container loc-rib {
              config false;
              description
                "Container for the IPv4 Unicast BGP Loc-RIB data.";
              container routes {
                description
                  "Enclosing container for list of routes in the
                   routing table.";
                list route {
                  key "prefix origin path-id";
                  description
                    "List of routes in the table, keyed by the route
                     prefix, the route origin, and path-id. The route
                     origin can be either the neighbor address from
                     which the route was learned, or the source
                     protocol that injected the route. The path-id
                     distinguishes routes for the same prefix
                     received from a neighbor (e.g., if add-paths is
                     enabled).";
                  leaf prefix {
                    type inet:ipv4-prefix;
                    description
                      "The IPv4 prefix corresponding to the route.";
                  }
                  uses bgp-loc-rib-common-keys;
                  uses bgp-loc-rib-common-attr-refs;
                  uses bgp-common-route-annotations-state;
                  uses bgp-unknown-attr-top;
                  uses rib-ext-route-annotations;
                }
              }
            }

            container neighbors {
              config false;
              description
                "Enclosing container for neighbor list.";
              list neighbor {
                key "neighbor-address";
                description
                  "List of neighbors (peers) of the local BGP
                   speaker.";
                leaf neighbor-address {
                  type inet:ip-address;
                  description
                    "IP address of the BGP neighbor or peer.";
                }
                container adj-rib-in-pre {
                  description
                    "Per-neighbor table containing the BGP routes
                     received from the neighbor before any local
                     input policy rules or filters have been applied.
                     This can be considered the 'raw' routes from
                     the neighbor.";
                  uses ipv4-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-in state for the containing
                       neighbor. Subsequently, implementations might
                       issue a 'route refresh' if 'route refresh' has
                       been negotiated, or reset the session. ";
                  }
                }
                container adj-rib-in-post {
                  description
                    "Per-neighbor table containing the paths received
                     from the neighbor that are eligible for
                     best-path selection after local input policy
                     rules have been applied.";
                  uses ipv4-adj-rib-in-post;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-in state for the containing
                       neighbor. Subsequently, implementations might
                       issue a 'route refresh' if 'route refresh' has
                       been negotiated, or reset the session. ";
                  }
                }
                container adj-rib-out-pre {
                  description
                    "Per-neighbor table containing paths eligible for
                     sending (advertising) to the neighbor before
                     output policy rules have been applied.";
                  uses ipv4-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-out state for the
                       containing neighbor. Subsequently, neighbors
                       will announce BGP updates to resynchronize
                       these routes.";
                  }
                }
                container adj-rib-out-post {
                  description
                    "Per-neighbor table containing paths eligible for
                     sending (advertising) to the neighbor after
                     output policy rules have been applied.";
                  uses ipv4-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-out state for the
                       containing neighbor. Subsequently, neighbors
                       will announce BGP updates to resynchronize
                       these routes.";
                  }
                }
              }
            }
          }

          container ipv6-unicast {
            when "derived-from-or-self(../name, 'bast:ipv6-unicast')" {
              description
                "Include this container for IPv6 unicast RIB.";
            }
            description
              "Routing tables for IPv6 unicast -- active when the
               afi-safi name is ipv6-unicast.";

            container loc-rib {
              config false;
              description
                "Container for the IPv6 BGP Loc-RIB data.";
              container routes {
                description
                  "Enclosing container for list of routes in the
                   routing table.";
                list route {
                  key "prefix origin path-id";
                  description
                    "List of routes in the table, keyed by the route
                     prefix, the route origin, and path-id. The route
                     origin can be either the neighbor address from
                     which the route was learned, or the source
                     protocol that injected the route. The path-id
                     distinguishes routes for the same prefix
                     received from a neighbor (e.g., if add-paths is
                     enabled).";
                  leaf prefix {
                    type inet:ipv6-prefix;
                    description
                      "The IPv6 prefix corresponding to the route.";
                  }
                  uses bgp-loc-rib-common-keys;
                  uses bgp-loc-rib-common-attr-refs;
                  uses bgp-common-route-annotations-state;
                  uses bgp-unknown-attr-top;
                  uses rib-ext-route-annotations;
                }
              }
            }

            container neighbors {
              config false;
              description
                "Enclosing container for neighbor list.";
              list neighbor {
                key "neighbor-address";
                description
                  "List of neighbors (peers) of the local BGP
                   speaker.";
                leaf neighbor-address {
                  type inet:ip-address;
                  description
                    "IP address of the BGP neighbor or peer.";
                }
                container adj-rib-in-pre {
                  description
                    "Per-neighbor table containing the BGP routes
                     received from the neighbor before any local
                     input policy rules or filters have been applied.
                     This can be considered the 'raw' routes from
                     the neighbor.";
                  uses ipv6-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-in state for the containing
                       neighbor. Subsequently, implementations might
                       issue a 'route refresh' if 'route refresh' has
                       been negotiated, or reset the session. ";
                  }
                }
                container adj-rib-in-post {
                  description
                    "Per-neighbor table containing the paths received
                     from the neighbor that are eligible for
                     best-path selection after local input policy
                     rules have been applied.";
                  uses ipv6-adj-rib-in-post;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-in state for the containing
                       neighbor. Subsequently, implementations might
                       issue a 'route refresh' if 'route refresh' has
                       been negotiated, or reset the session. ";
                  }
                }
                container adj-rib-out-pre {
                  description
                    "Per-neighbor table containing paths eligible for
                     sending (advertising) to the neighbor before
                     output policy rules have been applied.";
                  uses ipv6-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-out state for the
                       containing neighbor. Subsequently, neighbors
                       will announce BGP updates to resynchronize
                       these routes.";
                  }
                }
                container adj-rib-out-post {
                  description
                    "Per-neighbor table containing paths eligible for
                     sending (advertising) to the neighbor after
                     output policy rules have been applied.";
                  uses ipv6-adj-rib-common;
                  uses clear-routes {
                    description
                      "Clears the adj-rib-out state for the
                       containing neighbor. Subsequently, neighbors
                       will announce BGP updates to resynchronize
                       these routes.";
                  }
                }
              }
            }
          }
        }
      }
      description
        "Top level container for BGP RIB.";
    }
  }
}
