Class: Rbeapi::Api::Bgp

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/bgp.rb

Overview

The Bgp class implements global BGP router configuration.

Instance Attribute Summary collapse

Attributes inherited from Entity

#config, #error, #node

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, instance

Constructor Details

#initialize(node) ⇒ Bgp

Returns a new instance of Bgp.



45
46
47
48
# File 'lib/rbeapi/api/bgp.rb', line 45

def initialize(node)
  super(node)
  @neighbors = BgpNeighbors.new(node)
end

Instance Attribute Details

#neighborsObject (readonly)

Returns the value of attribute neighbors.



43
44
45
# File 'lib/rbeapi/api/bgp.rb', line 43

def neighbors
  @neighbors
end

Class Method Details

.parse_bgp_as(config) ⇒ Hash<Symbol, Object>

parse_bgp_as scans the BGP routing configuration for the AS number. Defined as a class method. Used by the BgpNeighbors class below.

Parameters:

  • config (String)

    The switch config.

Returns:

  • (Hash<Symbol, Object>)

    Returns the resource hash attribute.



121
122
123
124
# File 'lib/rbeapi/api/bgp.rb', line 121

def self.parse_bgp_as(config)
  value = config.scan(/^router bgp (\d+)/).first
  { bgp_as: value[0] }
end

Instance Method Details

#add_network(prefix, masklen, route_map = nil) ⇒ Boolean

add_network creates a new instance of a BGP network on the node.

===Commands router bgp <bgp_as> network / route-map <route_map>

Parameters:

  • prefix (String)

    The IPv4 prefix to configure as part of the network statement. The value must be a valid IPv4 prefix.

  • masklen (String)

    The IPv4 subnet mask length in bits. The masklen must be in the valid range of 1 to 32.

  • route_map (String) (defaults to: nil)

    The route-map name to apply to the network statement when configured.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



409
410
411
412
413
# File 'lib/rbeapi/api/bgp.rb', line 409

def add_network(prefix, masklen, route_map = nil)
  cmd = "network #{prefix}/#{masklen}"
  cmd << " route-map #{route_map}" if route_map
  configure_bgp(cmd)
end

#create(bgp_as, opts = {}) ⇒ Boolean

create will create a new instance of BGP routing on the node. Optional parameters can be passed in to initialize BGP specific settings.

===Commands router bgp <bgp_as>

paths.

Parameters:

  • bgp_as (String)

    The BGP autonomous system number to be configured for the local BGP routing instance.

  • opts (hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • router_id (String)

    The BGP routing process router-id value. When no ID has been specified (i.e. value not set), the local router ID is set to the following:

    • The loopback IP address when a single loopback interface is configured.
    • The loopback with the highest IP address when multiple loopback interfaces are configured.
    • The highest IP address on a physical interface when no loopback interfaces are configure
  • maximum_paths (Integer)

    Maximum number of equal cost

  • maximum_ecmp_paths (Integer)

    Maximum number of installed ECMP routes. The maximum_paths option must be set if maximum_ecmp_paths is set.

  • enable (Boolean)

    If true then the BGP router is enabled. If false then the BGP router is disabled.

Returns:

  • (Boolean)

    returns true if the command completed successfully.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rbeapi/api/bgp.rb', line 227

def create(bgp_as, opts = {})
  if opts[:maximum_ecmp_paths] && !opts[:maximum_paths]
    message = 'maximum_paths must be set if maximum_ecmp_paths is set'
    raise ArgumentError, message
  end
  cmds = ["router bgp #{bgp_as}"]
  if opts.key?(:enable)
    cmds << (opts[:enable] == true ? 'no shutdown' : 'shutdown')
  end
  cmds << "router-id #{opts[:router_id]}" if opts.key?(:router_id)
  if opts.key?(:maximum_paths)
    cmd = "maximum-paths #{opts[:maximum_paths]}"
    if opts.key?(:maximum_ecmp_paths)
      cmd << " ecmp #{opts[:maximum_ecmp_paths]}"
    end
    cmds << cmd
  end
  configure(cmds)
end

#defaultBoolean

default will configure the BGP routing using the default keyword. This command has the same effect as deleting the BGP routine instance from the nodes running configuration.

===Commands default router bgp <bgp_as>

Returns:

  • (Boolean)

    returns true if the command complete successfully



269
270
271
272
273
# File 'lib/rbeapi/api/bgp.rb', line 269

def default
  config = get
  return true unless config
  configure("default router bgp #{config[:bgp_as]}")
end

#deleteBoolean

delete will delete the BGP routing instance from the node.

===Commands no router bgp <bgp_as>

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



254
255
256
257
258
# File 'lib/rbeapi/api/bgp.rb', line 254

def delete
  config = get
  return true unless config
  configure("no router bgp #{config[:bgp_as]}")
end

#getnil, Hash<Symbol, Object>

get returns the BGP routing configuration from the nodes current configuration.

Examples:

{
  bgp_as: <string>,
  router_id: <string>,
  shutdown: <string>,
  maximum_paths: <integer>,
  maximum_ecmp_paths: <integer>
  networks: [
    {
      prefix: <string>,
      masklen: <integer>,
      route_map: <string>
    },
    {
      prefix: <string>,
      masklen: <integer>,
      route_map: <string>
    }
  ],
  neighbors: {
    name: {
      peer_group: <string>,
      remote_as: <string>,
      send_community: <boolean>,
      shutdown: <boolean>,
      description: <string>,
      next_hop_selp: <boolean>,
      route_map_in: <string>,
      route_map_out: <string>
    },
    name: {
      peer_group: <string>,
      remote_as: <string>,
      send_community: <boolean>,
      shutdown: <boolean>,
      description: <string>,
      next_hop_selp: <boolean>,
      route_map_in: <string>,
      route_map_out: <string>
    },
    ...
  }
}

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the BGP resource as a Hash.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbeapi/api/bgp.rb', line 100

def get
  config = get_block('^router bgp .*')
  return nil unless config

  response = Bgp.parse_bgp_as(config)
  response.merge!(parse_router_id(config))
  response.merge!(parse_shutdown(config))
  response.merge!(parse_maximum_paths(config))
  response.merge!(parse_networks(config))
  response[:neighbors] = @neighbors.getall
  response
end

#remove_network(prefix, masklen, route_map = nil) ⇒ Boolean

remove_network removes the instance of a BGP network on the node.

===Commands router bgp <bgp_as> no shutdown

Parameters:

  • prefix (String)

    The IPv4 prefix to configure as part of the network statement. The value must be a valid IPv4 prefix.

  • masklen (String)

    The IPv4 subnet mask length in bits. The masklen must be in the valid range of 1 to 32.

  • route_map (String) (defaults to: nil)

    The route-map name to apply to the network statement when configured.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



432
433
434
435
436
# File 'lib/rbeapi/api/bgp.rb', line 432

def remove_network(prefix, masklen, route_map = nil)
  cmd = "no network #{prefix}/#{masklen}"
  cmd << " route-map #{route_map}" if route_map
  configure_bgp(cmd)
end

#set_maximum_paths(maximum_paths, maximum_ecmp_paths, opts = {}) ⇒ Boolean

set_maximum_paths sets the maximum number of equal cost paths and the maximum number of installed ECMP routes.

===Commands router bgp <bgp_as> | default maximum-paths <maximum_paths> [ecmp <maximum_ecmp_paths>]

Parameters:

  • maximum_paths (Integer)

    Maximum number of equal cost paths.

  • maximum_ecmp_paths (Integer)

    Maximum number of installed ECMP routes.

  • opts (hash) (defaults to: {})

    Optional keyword arguments

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the maximum paths using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/rbeapi/api/bgp.rb', line 374

def set_maximum_paths(maximum_paths, maximum_ecmp_paths, opts = {})
  enable = opts.fetch(:enable, true)
  default = opts[:default] || false

  case default
  when true
    cmd = 'default maximum-paths'
  when false
    if enable
      cmd = "maximum-paths #{maximum_paths} ecmp #{maximum_ecmp_paths}"
    else
      cmd = 'no maximum-paths'
    end
  end
  configure_bgp(cmd)
end

#set_router_id(opts = {}) ⇒ Boolean

set_router_id sets the router_id for the BGP routing instance.

===Commands router bgp <bgp_as> | default router-id <router_id>

Parameters:

  • opts (hash) (defaults to: {})

    Optional keyword arguments

Options Hash (opts):

  • value (String)

    The BGP routing process router-id value. When no ID has been specified (i.e. value not set), the local router ID is set to the following:

    • The loopback IP address when a single loopback interface is configured.
    • The loopback with the highest IP address when multiple loopback interfaces are configured.
    • The highest IP address on a physical interface when no loopback interfaces are configure
  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the router-id using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



320
321
322
# File 'lib/rbeapi/api/bgp.rb', line 320

def set_router_id(opts = {})
  configure_bgp(command_builder('router-id', opts))
end

#set_shutdown(opts = {}) ⇒ Boolean

set_shutdown configures the administrative state for the global BGP routing process. The value option is not used by this method.

===Commands router bgp <bgp_as> | default shutdown

Parameters:

  • opts (hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • enable (Boolean)

    If enable is true then the BGP routing process is administratively enabled and if enable is False then the BGP routing process is administratively disabled.

  • default (Boolean)

    Configure the router-id using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



343
344
345
346
347
348
349
# File 'lib/rbeapi/api/bgp.rb', line 343

def set_shutdown(opts = {})
  raise 'set_shutdown has the value option set' if opts[:value]
  # Shutdown semantics are opposite of enable semantics so invert enable
  value = !opts[:enable]
  opts[:enable] = value
  configure_bgp(command_builder('shutdown', opts))
end