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>)

    resource hash attribute



76
77
78
79
# File 'lib/rbeapi/api/bgp.rb', line 76

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.

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)

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

Returns:

  • (Boolean)

    returns true if the command complete successfully



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

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) ⇒ Boolean

create will create a new instance of BGP routing on the node.

Parameters:

  • :bgp_as (String)

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

Returns:

  • (Boolean)

    returns true if the command completed successfully



144
145
146
147
# File 'lib/rbeapi/api/bgp.rb', line 144

def create(bgp_as)
  value = bgp_as
  configure("router bgp #{value}")
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.

Returns:

  • (Boolean)

    returns true if the command complete successfully



171
172
173
174
175
# File 'lib/rbeapi/api/bgp.rb', line 171

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.

Returns:

  • (Boolean)

    returns true if the command completed successfully



156
157
158
159
160
# File 'lib/rbeapi/api/bgp.rb', line 156

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.

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the BGP resource as a Hash.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbeapi/api/bgp.rb', line 56

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

  response = Bgp.parse_bgp_as(config)
  response.merge!(parse_router_id(config))
  response.merge!(parse_shutdown(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.

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)

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

Returns:

  • (Boolean)

    returns true if the command complete successfully



290
291
292
293
294
# File 'lib/rbeapi/api/bgp.rb', line 290

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_router_id(opts = {}) ⇒ Boolean

set_router_id sets the router_id for the BGP routing instance.

Parameters:

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



222
223
224
# File 'lib/rbeapi/api/bgp.rb', line 222

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.

Parameters:

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



245
246
247
248
249
250
251
# File 'lib/rbeapi/api/bgp.rb', line 245

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