module frr-route-map {
  yang-version 1.1;
  namespace "http://frrouting.org/yang/route-map";
  prefix frr-route-map;

  import ietf-inet-types {
    prefix inet;
  }

  import frr-filter {
    prefix filter;
  }

  import frr-interface {
    prefix frr-interface;
  }

  organization
    "FRRouting";
  contact
    "FRR Users List:       <mailto:frog@lists.frrouting.org>
     FRR Development List: <mailto:dev@lists.frrouting.org>";
  description
    "This module defines route map settings

     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-07-01 {
    description
      "Initial revision";
  }

  /*
   * Types.
   */
  typedef route-map-sequence {
    type uint16 {
      range "1..65535";
    }
    description
      "Route map valid sequence numbers.";
  }

  typedef route-map-name {
    type string;
    description
      "Route map name format.";
  }

  typedef route-map-ref {
    type leafref {
      path "/frr-route-map:lib/frr-route-map:route-map/frr-route-map:name";
      require-instance false;
    }
    description
      "Reference to a route-map.";
  }

  /*
   * Operational data.
   */
  container lib {
    list route-map {
      key "name";
      description
        "Route map instance.";
      leaf name {
        type route-map-name;
        description
          "Route map instance name.";
      }

      list entry {
        key "sequence";
        description
          "Route map entry.";
        leaf sequence {
          description
            "Route map instance priority (low number means higher priority).";
          type route-map-sequence;
        }

        leaf description {
          description "Route map description.";
          type string;
        }

        leaf action {
          description
            "Route map actions: permit (executes action), deny (quits evaluation).";
          mandatory true;
          type enumeration {
            enum permit {
              description
                "Executes configured action and permits the prefix/route
                 if the conditions matched. An alternative exit action can
                 be configured to continue processing the route map list
                 or jump to process another route map.";
              value 0;
            }
            enum deny {
              description
                "If all conditions are met the prefix/route is denied and
                 route map processing stops.";
              value 1;
            }
          }
        }

        leaf call {
          description
            "Call another route map before calling `exit-policy`. If the
             called route map returns deny then this route map will also
             return deny.";
          type route-map-name;
        }

        leaf exit-policy {
          description "What do to after route map successful match, set and call.";
          type enumeration {
            enum permit-or-deny {
              description "End route map evaluation and return.";
              value 0;
            }
            enum next {
              description
                "Proceed evaluating next route map entry per sequence.";
              value 1;
            }
            enum goto {
              description
                "Go to route map entry with the provided sequence number.";
              value 2;
            }
          }
          default "permit-or-deny";
        }

        leaf goto-value {
          when "../exit-policy = 'goto'";
          description
            "Sequence number to jump (when using `goto` exit policy).";
          mandatory true;
          type route-map-sequence;
        }

        list match-condition {
          key "condition";
          description
            "Route map match conditions.";
          leaf condition {
            description "Match condition.";
            type enumeration {
              enum interface {
                description "Match interface.";
                value 0;
              }
              enum ipv4-address-list {
                description "Match an IPv4 access-list.";
                value 1;
              }
              enum ipv4-prefix-list {
                description "Match an IPv4 prefix-list.";
                value 2;
              }
              enum ipv4-next-hop-list {
                description "Match an IPv4 next-hop.";
                value 3;
              }
              enum ipv4-next-hop-prefix-list {
                description "Match an IPv4 next-hop prefix list.";
                value 4;
              }
              enum ipv4-next-hop-type {
                description "Match an IPv4 next-hop type.";
                value 5;
              }
              enum ipv6-address-list {
                description "Match an IPv6 access-list.";
                value 6;
              }
              enum ipv6-prefix-list {
                description "Match an IPv6 prefix-list.";
                value 7;
              }
              enum ipv6-next-hop-type {
                description "Match an IPv6 next-hop type.";
                value 8;
              }
              enum metric {
                description "Match a route metric.";
                value 9;
              }
              enum tag {
                description "Match a route tag.";
                value 10;
              }
              /* zebra specific conditions. */
              enum ipv4-prefix-length {
                description "Match IPv4 prefix length.";
                value 100;
              }
              enum ipv6-prefix-length {
                description "Match IPv6 prefix length.";
                value 101;
              }
              enum ipv4-next-hop-prefix-length {
                description "Match next-hop prefix length.";
                value 102;
              }
              enum source-protocol {
                description "Match source protocol.";
                value 103;
              }
              enum source-instance {
                description "Match source protocol instance.";
                value 104;
              }
            }
          }

          choice condition-value {
            description
              "Value to match (interpretation depends on condition type).";
            mandatory true;
            case interface {
              when "./condition = 'interface'";
              leaf interface {
                type frr-interface:interface-ref;
              }
            }

            case list-name {
              when "./condition = 'ipv4-address-list' or
                    ./condition = 'ipv4-prefix-list' or
                    ./condition = 'ipv4-next-hop-list' or
                    ./condition = 'ipv4-next-hop-prefix-list' or
                    ./condition = 'ipv6-address-list' or
                    ./condition = 'ipv6-prefix-list'";
              leaf list-name {
                type filter:access-list-name;
              }
            }

            case ipv4-next-hop-type {
              when "./condition = 'ipv4-next-hop-type'";
              leaf ipv4-next-hop-type {
                type enumeration {
                  enum blackhole {
                    value 0;
                  }
                }
              }
            }

            case ipv6-next-hop-type {
              when "./condition = 'ipv6-next-hop-type'";
              leaf ipv6-next-hop-type {
                type enumeration {
                  enum blackhole {
                    value 0;
                  }
                }
              }
            }

            case metric {
              when "./condition = 'metric'";
              leaf metric {
                type uint32 {
                  range "1..4294967295";
                }
              }
            }

            case tag {
              when "./condition = 'tag'";
              leaf tag {
                type uint32 {
                  range "1..4294967295";
                }
              }
            }
          }
        }

        list set-action {
          description "Route map set actions.";

          key "action";

          leaf action {
            description "Action to do when the route map matches.";
            type enumeration {
              enum ipv4-next-hop {
                description "Set IPv4 address of the next hop.";
                value 0;
              }
              enum ipv6-next-hop {
                description "Set IPv6 address of the next hop.";
                value 1;
              }
              enum metric {
                description "Set prefix/route metric.";
                value 2;
              }
              enum tag {
                description "Set tag.";
                value 3;
              }
              /* zebra specific conditions. */
              enum source {
                description "Set source address for route.";
                value 100;
              }
            }
          }

          choice action-value {
            description
              "Value to set (interpretation depends on action-type).";
            case ipv4-address {
              when "./action = 'ipv4-next-hop'";
              leaf ipv4-address {
                description "IPv4 address.";
                type inet:ipv4-address;
              }
            }

            case ipv6-address {
              when "./action = 'ipv6-next-hop'";
              leaf ipv6-address {
                description "IPv6 address.";
                type inet:ipv6-address;
              }
            }

            case metric {
              when "./action = 'metric'";
              choice metric-value {
                description "Metric to set or use.";
                case value {
                  leaf value {
                    description "Use the following metric value.";
                    type uint32 {
                      range "0..4294967295";
                    }
                  }
                }

                case add-metric {
                  leaf add-metric {
                    description "Add value to metric.";
                    type uint32 {
                      range "0..4294967295";
                    }
                  }
                }

                case subtract-metric {
                  leaf subtract-metric {
                    description "Subtract value from metric.";
                    type uint32 {
                      range "0..4294967295";
                    }
                  }
                }

                case use-round-trip-time {
                  leaf use-round-trip-time {
                    description "Use the round trip time as metric.";
                    type boolean;
                  }
                }

                case add-round-trip-time {
                  leaf add-round-trip-time {
                    description "Add round trip time to metric.";
                    type boolean;
                  }
                }

                case subtract-round-trip-time {
                  leaf subtract-round-trip-time {
                    description "Subtract round trip time to metric.";
                    type boolean;
                  }
                }
              }
            }

            case tag {
              when "./action = 'tag'";
              leaf tag {
                description "Tag value.";
                type uint32 {
                  range "0..4294967295";
                }
              }
            }
          }
        }
      }
    }
  }
}
