// SPDX-License-Identifier: BSD-2-Clause
module frr-nexthop {
  yang-version 1.1;
  namespace "http://frrouting.org/yang/nexthop";
  prefix frr-nexthop;

  import ietf-inet-types {
    prefix inet;
  }

  import ietf-routing-types {
    prefix rt-types;
  }

  import frr-interface {
    prefix frr-interface;
  }

  import frr-vrf {
    prefix frr-vrf;
  }

  organization
    "FRRouting";
  contact
    "FRR Users List: <mailto:frog@lists.frrouting.org>
     FRR Development List: <mailto:dev@lists.frrouting.org>";
  description
    "This module defines a model for managing FRR nexthop information.

     Copyright 2020 FRRouting

     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions
     are met:

     1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

     2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";

  revision 2019-08-15 {
    description
      "Initial revision.";
  }

  typedef optional-ip-address {
    type union {
      type inet:ip-address;
      type string {
        length "0";
      }
    }
  }

  /*
   * Nexthop types.
   */
  typedef nexthop-type {
    type enumeration {
      enum "ifindex" {
        value 1;
        description
          "Specific interface.";
      }
      enum "ip4" {
        value 2;
        description
          "IPv4 address.";
      }
      enum "ip4-ifindex" {
        value 3;
        description
          "IPv4 address and interface.";
      }
      enum "ip6" {
        value 4;
        description
          "IPv6 address.";
      }
      enum "ip6-ifindex" {
        value 5;
        description
          "IPv6 address and interface.";
      }
      enum "blackhole" {
        value 6;
        description
          "Unreachable or prohibited.";
      }
    }
    description
      "Nexthop types.";
  }

  /*
   * Blockhole nexthop type.
   */
  typedef blackhole-type {
    type enumeration {
      enum "unspec" {
        value 0;
        description
          "Generic unreachable.";
      }
      enum "null" {
        value 1;
        description
          "Null type.";
      }
      enum "reject" {
        value 2;
        description
          "ICMP unreachable.";
      }
      enum "prohibited" {
        value 3;
        description
          "ICMP admin-prohibited.";
      }
    }
    default "null";
    description
      "Nexthop blackhole types.";
  }

  typedef nexthop-group-ref {
    type leafref {
      path "/frr-nexthop:frr-nexthop-group/frr-nexthop:nexthop-groups/frr-nexthop:name";
      require-instance false;
    }
  }

  /*
   * Common nexthop attributes grouping.
   */
  grouping frr-nexthop-attributes {
    leaf nh-type {
      type nexthop-type;
      mandatory true;
      description
        "The nexthop type.";
    }

    leaf vrf {
      type frr-vrf:vrf-ref;
      description
        "The nexthop vrf name, if different from the route.";
    }

    leaf gateway {
      type frr-nexthop:optional-ip-address;
      description
        "The nexthop gateway address.";
    }

    leaf interface {
      type frr-interface:interface-ref;
      description
        "The nexthop egress interface.";
    }

    leaf bh-type {
      type blackhole-type;
      description
        "A blackhole sub-type, if the nexthop is a blackhole type.";
    }

    leaf onlink {
      type boolean;
      default "false";
      description
        "Nexthop is directly connected.";
    }

    leaf srte-color {
      when "../nh-type = 'ip4' or
            ../nh-type = 'ip6' or
            ../nh-type = 'ip4-ifindex' or
            ../nh-type = 'ip6-ifindex'";
      type uint32;
      description
        "The nexthop SR-TE color";
    }

    uses rt-types:mpls-label-stack {
      description
        "Nexthop's MPLS label stack.";
    }

    uses srv6-segs-stack {
      description
        "Nexthop's SRv6 segs SIDs stack.";
    }
  }

  /*
   * operational common attributes for nexthop
   */
  grouping frr-nexthop-operational {
    leaf duplicate {
      type empty;
      config false;
      description
        "Duplicate nexthop";
    }

    leaf recursive {
      type empty;
      config false;
      description
        "Nexthop resolved through another gateway.";
    }

    leaf active {
      type empty;
      config false;
      description
        "Nexthop is active.";
    }

    leaf fib {
      type empty;
      config false;
      description
        "Nexthop is installed in fib.";
    }

    leaf weight {
      type uint8;
      config false;
      description
        "Weight to be used by the nexthop for purposes of ECMP";
    }
  }

  grouping nexthop-grouping {
    list nexthop {
      key "nh-type vrf gateway interface";
      min-elements 1;
      description
        "A list of nexthop objects.";
      uses frr-nexthop-attributes;
    }
  }

  /*
   * Single nexthop grouping.
   */
  grouping frr-nexthop {
    container frr-nexthops {
      description
        "FRR nexthop object.";
      uses nexthop-grouping;
    }
  }


  /*
   * Container for FRR nexthop group.
   */
  grouping frr-nexthop-grouping {
    list nexthop-groups {
      key "name";
      description
        "List of nexthop groups, each contains group of nexthops";
      leaf name {
        type string;
        description
          "The nexthop-group name.";
      }

      uses frr-nexthop;
    }
  }

  /* Operational nexthop-group */
  grouping frr-nexthop-group-operational {
    container nexthop-group {
      description
        "A group of nexthops.";
      leaf id {
        type uint32;
        description
          "The nexthop-group id.";
      }

      uses nexthop-grouping;
    }
  }

  /* Contaner for SRv6 segs SIDs */
  grouping srv6-segs-stack {
    description
      "This grouping specifies an SRv6 segs SIDs stack. The segs
      SIDs stack is encoded as a list of SID entries. The
      list key is an identifier that indicates the relative
      ordering of each entry.";
      container srv6-segs-stack {
        description
          "Container for a list of SRv6 segs SIDs entries.";
          list entry {
            key "id";
            description
              "List of SRv6 segs SIDs entries.";
	    leaf id {
              type uint8;
              description
              "Identifies the entry in a sequence of SRv6 segs SIDs
              entries.";
            }
            leaf seg {
                type inet:ipv6-address;
            }
          }
      }
  }
  container frr-nexthop-group {
    description
      "A nexthop-group, represented as a list of nexthop objects.";
    uses frr-nexthop-grouping;
  }

  /*
   * Augment weight attributes to nexthop group.
   */
  augment "/frr-nexthop-group/nexthop-groups/frr-nexthops/nexthop" {
    leaf weight {
      type uint8;
      description
        "Weight to be used by the nexthop for purposes of ECMP";
    }
  }
}
