github.com/openconfig/goyang@v1.4.5/pkg/yang/bgp_test.go (about)

     1  // Copyright 2015 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yang
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  // TestBGP simply makes sure we are able to parse a version of Anees's
    23  // BGP model.  We don't actually attempt to validate we got the right
    24  // AST.  ast_test.go will test smaller peices to make sure the basics
    25  // of BuildAST produce expected results.
    26  func TestBGP(t *testing.T) {
    27  	ss, err := Parse(bgp, "bgp.yang")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if len(ss) != 1 {
    32  		t.Fatalf("got %d results, want 1", len(ss))
    33  	}
    34  	typeDict := newTypeDictionary()
    35  	initTypes(reflect.TypeOf(&meta{}))
    36  	if _, err := buildASTWithTypeDict(ss[0], typeDict); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  }
    40  
    41  var bgp = `
    42  module google-bgp {
    43  
    44    yang-version "1";
    45  
    46    // namespace
    47    namespace "http://google.com/yang/google-bgp-protocol-cfg";
    48  
    49    prefix "gbgp";
    50  
    51    // import some basic types -- no other dependency on
    52    // in-progress models in draft status
    53    import ietf-inet-types { prefix inet; }
    54  
    55  
    56    // meta
    57    organization "Google, Inc.";
    58  
    59    contact
    60      "Google, Inc.
    61      1600 Amphitheatre Way
    62      Mountain View, CA  94043";
    63  
    64    description
    65      "This module describes a YANG model for BGP protocol
    66      configuration.It is a limited subset of all of the configuration
    67      parameters available in the variety of vendor implementations,
    68      hence it is expected that it would be augmented with vendor-
    69      specific configuration data as needed.Additional modules or
    70      submodules to handle other aspects of BGP configuration,
    71      including policy, VRFs, and additional address families are also 
    72      expected.";
    73  
    74    revision "2014-07-07" {
    75      description
    76        "Initial revision";
    77      reference "TBD";
    78    }
    79  
    80  
    81    identity afi-type {
    82      description
    83        "base identity type for BGP address family identifiers (AFI)";
    84      reference "IETF RFC 4760";
    85    }
    86  
    87    identity safi-type {
    88      description
    89        "base identity type for BGP subsequent address family
    90        identifiers (SAFI)";
    91      reference "IETF RFC 4760";
    92    }
    93  
    94    identity ipv4-afi {
    95      base afi-type;
    96      description
    97        "IPv4 AF identifier";
    98    }
    99  
   100    identity ipv6-afi {
   101      base afi-type;
   102      description
   103        "IPv6 AF identifier";
   104    }
   105  
   106    identity unicast-safi {
   107      base safi-type;
   108      description
   109        "unicast SAFI identifier";
   110    }
   111  
   112    identity labeled-unicast-safi {
   113      base safi-type;
   114      description
   115        "labeled unicast SAFI identifier";
   116      reference "RFC 3107 - Carrying Label Information in BGP-4";
   117    }
   118  
   119  
   120    typedef peer-group-type {
   121      type enumeration {
   122        enum INTERNAL {
   123          description "internal (iBGP) peer";
   124        }
   125        enum EXTERNAL {
   126          description "external (eBGP) peer";
   127        }
   128      }
   129      description
   130        "labels a peer as explicitly internal or external";
   131    }
   132  
   133  
   134    typedef remove-private-as-option {
   135      type enumeration {
   136        enum ALL {
   137          description "remove all private ASes in the path";
   138        }
   139        enum REPLACE {
   140          description "replace private ASes with local AS";
   141        }
   142      }
   143      description
   144        "set of options for configuring how private AS path numbers
   145        are removed from advertisements";
   146    }
   147  
   148    typedef percentage {
   149      type uint8 {
   150        range "0..100";
   151      }
   152      description
   153        "Integer indicating a percentage value";
   154    }
   155  
   156    typedef rr-cluster-id-type {
   157      type union {
   158        type uint32;
   159        type inet:ipv4-address;
   160      }
   161      description
   162        "union type for route reflector cluster ids:
   163        option 1: 4-byte number
   164        option 2: IP address";
   165    }
   166  
   167    grouping bgp-common-configuration {
   168      description "Common configuration across neighbors, groups,
   169      etc.";
   170  
   171      leaf description {
   172        type string;
   173        description
   174          "A textual description of the peer or group";
   175      }
   176      container use-multiple-paths {
   177        description
   178          "Configuration of BGP multipath to enable load sharing across
   179          multiple paths to peers.";
   180        leaf allow-multiple-as {
   181          type boolean;
   182          default "false";
   183          description
   184            "Allow multipath to use paths from different neighboring
   185            ASes.  The default is to only consider multiple paths from
   186            the same neighboring AS.";
   187        }
   188        leaf maximum-paths {
   189          type uint32;
   190          default 1;
   191          description
   192            "Maximum number of parallel paths to consider when using
   193            BGP multipath.  The default is to use a single path.";
   194          reference "draft-ietf-idr-add-paths-09.txt";
   195        }
   196      }
   197  
   198    }
   199  
   200    grouping bgp-group-common-configuration {
   201      description "Configuration items that are applied at the peer
   202      group level";
   203    }
   204  
   205    grouping bgp-group-neighbor-common-configuration {
   206      description "Configuration options for peer and group context";
   207  
   208      leaf auth-password {
   209        type string;
   210        description
   211          "Configures an authentication password for use with
   212          neighboring devices.";
   213      }
   214  
   215      container timers {
   216        description "Configuration of various BGP timers";
   217  
   218        leaf hold-time {
   219          type decimal64 {
   220            fraction-digits 2;
   221          }
   222          default 90;
   223          // hold-time should typically be set to 3x the
   224          // keepalive-interval -- create a constraint for this?
   225          description
   226            "Time interval in seconds that a BGP session will be
   227            considered active in the absence of keepalive or other
   228            messages from the peer";
   229          reference
   230            "RFC 1771 - A Border Gateway Protocol 4";
   231        }
   232  
   233        leaf keepalive-interval {
   234          type decimal64 {
   235            fraction-digits 2;
   236          }
   237          default 30;
   238          description
   239            "Time interval in seconds between transmission of keepalive
   240            messages to the neighbor.  Typically set to 1/3 the 
   241            hold-time.";
   242        }
   243  
   244        leaf advertisement-interval {
   245          type decimal64 {
   246            fraction-digits 2;
   247          }
   248          default 30;
   249          description
   250            "Mininum time interval in seconds between transmission 
   251            of BGP updates to neighbors";
   252          reference
   253            "RFC 1771 - A Border Gateway Protocol 4";
   254        }
   255  
   256        leaf connect-retry {
   257          type decimal64 {
   258            fraction-digits 2;
   259          }
   260          default 30;
   261          description
   262            "Time interval in seconds between attempts to establish a
   263            session with the peer.";
   264        }
   265      }
   266  
   267      container ebgp-multihop {
   268        description
   269          "Configure multihop BGP for peers that are not directly
   270          connected";
   271  
   272        leaf multihop-ttl {
   273          type uint8;
   274          default 1;
   275          description
   276            "Time-to-live for multihop BGP sessions.  The default
   277            value of 1 is for directly connected peers (i.e.,
   278            multihop disabled";
   279  
   280        }
   281  
   282      }
   283  
   284      container route-reflector {
   285        description
   286          "Configure the local router as a route-reflector
   287          server";
   288        leaf route-reflector-clusterid {
   289          type rr-cluster-id-type;
   290          description
   291            "route-reflector cluster id to use when local router is
   292            configured as a route reflector.  Commonly set at the group
   293            level, but allows a different cluster
   294            id to be set for each neighbor.";
   295        }
   296  
   297        leaf route-reflector-client {
   298          type boolean;
   299          default "false";
   300          description
   301            "configure the neighbor as a route reflector client";
   302        }
   303      }
   304  
   305      leaf remove-private-as {
   306        // could also make this a container with a flag to enable
   307        // remove-private and separate option.  here, option implies 
   308        // remove-private is enabled.
   309        type remove-private-as-option;
   310        description
   311          "Remove private AS numbers from updates sent to peers";
   312      }
   313  
   314  
   315      container bgp-logging-options {
   316        description
   317          "Configure various tracing/logging options for BGP peers
   318          or groups.  Expected that additional vendor-specific log
   319          options would augment this container";
   320  
   321        leaf log-neighbor-state-changes {
   322          type boolean;
   323          default "true";
   324          description
   325            "Configure logging of peer state changes.  Default is
   326            to enable logging of peer state changes.";
   327        }
   328      }
   329  
   330      container transport-options {
   331        description
   332          "Transport protocol options for BGP sessions";
   333  
   334          leaf tcp-mss {
   335            type uint16;
   336            description
   337              "Sets the max segment size for BGP TCP sessions";
   338          }
   339  
   340          leaf passive-mode {
   341            type boolean;
   342            description
   343              "Wait for peers to issue requests to open a BGP session,
   344              rather than initiating sessions from the local router";
   345          }
   346      }
   347  
   348      leaf local-address {
   349        type inet:ip-address;
   350        description
   351          "Set the local IP (either IPv4 or IPv6) address to use for
   352          the session when sending BGP update messages";
   353      }
   354  
   355      leaf route-flap-damping {
   356        type boolean;
   357        description
   358          "Enable route flap damping";
   359      }
   360    }
   361  
   362    grouping bgp-address-family-common-configuration {
   363      description "Configuration options per address family context";
   364  
   365      list address-family {
   366  
   367        key "afi-name";
   368        description
   369          "Per address-family configuration, uniquely identified by AF
   370          name"; 
   371        leaf afi-name {
   372          type identityref {
   373            base "afi-type";
   374          }
   375          description
   376            "Address family names are drawn from the afi-type base
   377            identity, which has specific address family types as
   378            derived identities";
   379        }
   380  
   381        list subsequent-address-family {
   382  
   383          key "safi-name";
   384          description
   385            "Per subsequent address family configuration, under a
   386            specific address family";
   387  
   388          leaf safi-name {
   389            // do we need to specify which SAFIs are possible within
   390            // each AF? with the current set  of AF/SAFI, all are
   391            /// applicable
   392            type identityref {
   393              base "safi-type";
   394            }
   395            description
   396              "Within each address family, subsequent address family
   397              names are drawn from the subsequent-address-family base
   398              identity";
   399          } 
   400     
   401  
   402          container prefix-limit {
   403            description
   404            "Configure the maximum number of prefixes that will be
   405            accepted from a peer";
   406  
   407            leaf max-prefixes {
   408              type uint32;
   409              description
   410                "Maximum number of prefixes that will be accepted from
   411                the neighbor";
   412            }
   413  
   414            leaf shutdown-threshold-pct {
   415              type percentage;
   416              description
   417                "Threshold on number of prefixes that can be received
   418                from a neighbor before generation of warning messages
   419                or log entries.  Expressed as a percentage of
   420                max-prefixes.";
   421            }
   422  
   423            leaf restart-timer {
   424              type decimal64 {
   425                fraction-digits 2;
   426              }
   427              units "seconds";
   428              description
   429                "Time interval in seconds after which the BGP session
   430                is reestablished after being torn down due to exceeding
   431                the max-prefixes limit.";
   432            }
   433          }
   434         }
   435      }
   436    }
   437  
   438  
   439  
   440    container bgp {
   441      description "Top-level configuration data for the BGP router";
   442  
   443      container global {
   444        description
   445          "Top-level bgp protocol options applied across peer-groups,
   446          neighbors, and address families";
   447  
   448        leaf as {
   449          type inet:as-number;
   450          mandatory "true";
   451          description
   452            "Local autonomous system number of the router.  Uses 
   453            the as-number type defined in RFC 6991";
   454        }
   455        leaf router-id {
   456          type inet:ipv4-address;
   457          description
   458            "Router id of the router, expressed as an
   459            IPv4 address";
   460            // there is a typedef for this in draft module ietf-routing
   461            // but it does not use an appropriate type
   462        }
   463        container route-selection-options {
   464          description
   465            "Set of configuration options that govern best
   466            path selection";
   467          leaf always-compare-med {
   468            type boolean;
   469            default "false";
   470            description
   471              "Compare multi-exit discriminator (MED) value from 
   472              different ASes when selecting the best route.  The
   473              default behavior is to only compare MEDs for paths
   474              received from the same AS.";
   475          }
   476          leaf ignore-as-path {
   477            type boolean;
   478            default "false";
   479            description
   480              "Ignore the AS path length when selecting the best path.
   481              The default is to use the AS path length and prefer paths
   482              with shorter length.";
   483          }
   484          leaf external-compare-router-id {
   485            type boolean;
   486            default "true";
   487            description
   488              "When comparing similar routes received from external
   489              BGP peers, use the router-id as a criterion to select 
   490              the active path.  The default is to use the router-id to
   491              select among similar routes.";
   492          }
   493          leaf advertise-inactive-routes {
   494            type boolean;
   495            default "false";
   496            description
   497              "Advertise inactive routes to external peers.  The
   498              default is to only advertise active routes.";
   499          }
   500        }
   501        container default-route-distance {
   502          description
   503            "Administrative distance (or preference) assigned to
   504            routes received from different sources
   505            (external, internal, and local.)";
   506          leaf external-route-distance {
   507            type uint8 {
   508              range "1..255";
   509            }
   510            description
   511              "Administrative distance for routes learned from external
   512              BGP (eBGP)";
   513          }
   514          leaf internal-route-distance {
   515            type uint8 {
   516              range "1..255";
   517            }
   518            description
   519              "Administrative distance for routes learned from internal
   520              BGP (iBGP)";
   521  
   522          }
   523        }
   524      }
   525  
   526      uses bgp-address-family-common-configuration;
   527  
   528      list peer-group {
   529        key "group-name";
   530        description
   531          "List of peer-groups, uniquely identified by the peer group
   532          names";
   533        leaf group-name {
   534          type string;
   535          description "Name of the peer group";
   536        }
   537        leaf group-type {
   538          type peer-group-type;
   539          description
   540            "Explicitly designate the peer group as internal (iBGP)
   541            or external (eBGP)";
   542        }
   543        uses bgp-common-configuration;
   544        uses bgp-address-family-common-configuration;
   545        uses bgp-group-neighbor-common-configuration;
   546      }
   547  
   548      list neighbor {
   549        key "neighbor-address";
   550        description
   551          "List of BGP peers, uniquely identified by neighbor address";
   552        leaf neighbor-address {
   553          type inet:ip-address;
   554          description
   555            "Address of the BGP peer, either IPv4 or IPv6";
   556        }
   557  
   558        leaf peer-as {
   559          type inet:as-number;
   560          mandatory "true";
   561          description
   562            "AS number of the peer";
   563  
   564        }
   565        uses bgp-common-configuration;
   566        uses bgp-address-family-common-configuration;
   567        uses bgp-group-neighbor-common-configuration;
   568      }
   569  
   570    }
   571  }`