class Rbeapi::Api::Bgp

The Bgp class implements global BGP router configuration.

Attributes

neighbors[R]

Public Class Methods

new(node) click to toggle source
Calls superclass method Rbeapi::Api::Entity::new
# File lib/rbeapi/api/bgp.rb, line 45
def initialize(node)
  super(node)
  @neighbors = BgpNeighbors.new(node)
end
parse_bgp_as(config) click to toggle source

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

@param config [String] The switch config.

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# 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

Public Instance Methods

add_network(prefix, masklen, route_map = nil) click to toggle source

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

Commands

router bgp <bgp_as>
  network <prefix>/<masklen>
  route-map <route_map>

@param prefix [String] The IPv4 prefix to configure as part of

the network statement. The value must be a valid IPv4 prefix.

@param masklen [String] The IPv4 subnet mask length in bits.

The masklen must be in the valid range of 1 to 32.

@param route_map [String] The route-map name to apply to the

network statement when configured.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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>

@param bgp_as [String] The BGP autonomous system number to be

configured for the local BGP routing instance.

@param opts [hash] Optional keyword arguments.

@option 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

@option opts maximum_paths [Integer] Maximum number of equal cost paths.

@option opts maximum_ecmp_paths [Integer] Maximum number of installed

ECMP routes. The maximum_paths option must be set if
maximum_ecmp_paths is set.

@option opts enable [Boolean] If true then the BGP router is enabled.

If false then the BGP router is disabled.

@return [Boolean] returns true if the command completed successfully.

# 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
default() click to toggle source

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>

@return [Boolean] returns true if the command complete successfully

# File lib/rbeapi/api/bgp.rb, line 269
def default
  config = get
  return true unless config
  configure("default router bgp #{config[:bgp_as]}")
end
delete() click to toggle source

delete will delete the BGP routing instance from the node.

Commands

no router bgp <bgp_as>

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/bgp.rb, line 254
def delete
  config = get
  return true unless config
  configure("no router bgp #{config[:bgp_as]}")
end
get() click to toggle source

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

@example

{
  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>
    },
    ...
  }
}

@return [nil, Hash<Symbol, Object>] Returns the BGP resource as a

Hash.
# 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) click to toggle source

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

Commands

router bgp <bgp_as>
  {no} shutdown

@param prefix [String] The IPv4 prefix to configure as part of

the network statement.  The value must be a valid IPv4 prefix.

@param masklen [String] The IPv4 subnet mask length in bits.

The masklen must be in the valid range of 1 to 32.

@param route_map [String] The route-map name to apply to the

network statement when configured.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

Commands

router bgp <bgp_as>
  {no | default}
    maximum-paths <maximum_paths> [ecmp <maximum_ecmp_paths>]

@param maximum_paths [Integer] Maximum number of equal cost paths.

@param maximum_ecmp_paths [Integer] Maximum number of installed ECMP

routes.

@param opts [hash] Optional keyword arguments

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the maximum paths using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

set_router_id sets the router_id for the BGP routing instance.

Commands

router bgp <bgp_as>
  {no | default} router-id <router_id>

@param opts [hash] Optional keyword arguments

@option 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

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the router-id using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# File lib/rbeapi/api/bgp.rb, line 320
def set_router_id(opts = {})
  configure_bgp(command_builder('router-id', opts))
end
set_shutdown(opts = {}) click to toggle source

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>
  {no | default} shutdown

@param opts [hash] Optional keyword arguments.

@option 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.

@option opts default [Boolean] Configure the router-id using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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

Private Instance Methods

configure_bgp(cmd) click to toggle source

configure_bgp adds the command to go to BGP config mode. Then it adds the passed in command. The commands are then passed on to configure.

@api private

@param cmd [String] Command to run under BGP mode.

@return [Boolean] Returns true if the command complete successfully.

# File lib/rbeapi/api/bgp.rb, line 285
def configure_bgp(cmd)
  config = get_block('^router bgp .*')
  raise 'BGP router is not configured' unless config
  bgp_as = Bgp.parse_bgp_as(config)
  cmds = ["router bgp #{bgp_as[:bgp_as]}", cmd]
  configure(cmds)
end
parse_maximum_paths(config) click to toggle source

parse_maximum_paths scans the BGP routing configuration for the maximum paths and maximum ecmp paths.

@api private

@param config [String] The switch config.

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# File lib/rbeapi/api/bgp.rb, line 167
def parse_maximum_paths(config)
  values = config.scan(/maximum-paths\s+(\d+)\s+ecmp\s+(\d+)/).first
  { maximum_paths: values[0].to_i, maximum_ecmp_paths: values[1].to_i }
end
parse_networks(config) click to toggle source

parse_networks scans the BGP routing configuration for all the network entries.

@api private

@param config [String] The switch config.

@return [Array<Hash>] Single element hash with Array of network hashes.

# File lib/rbeapi/api/bgp.rb, line 182
def parse_networks(config)
  networks = []
  lines = config.scan(%r{network (.+)/(\d+)(?: route-map (\w+))*})
  lines.each do |prefix, mask, rmap|
    rmap = rmap == '' ? nil : rmap
    networks << { prefix: prefix, masklen: mask.to_i, route_map: rmap }
  end
  { networks: networks }
end
parse_router_id(config) click to toggle source

parse_router_id scans the BGP routing configuration for the router ID.

@api private

@param config [String] The switch config.

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# File lib/rbeapi/api/bgp.rb, line 135
def parse_router_id(config)
  value = config.scan(/router-id ([^\s]+)/).first
  value = value ? value[0] : nil
  { router_id: value }
end
parse_shutdown(config) click to toggle source

parse_shutdown scans the BGP routing configuration for the shutdown status.

@api private

@param config [String] The switch config.

@return [Hash<Symbol, Object>] resource hash attribute. Returns true if shutdown, false otherwise.

# File lib/rbeapi/api/bgp.rb, line 152
def parse_shutdown(config)
  value = config.include?('no shutdown')
  { shutdown: !value }
end