Class: Rbeapi::Api::Ospf

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

Overview

The Ospf class is a global class that provides an instance for working with the node’s OSPF configuration

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

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

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#add_network(pid, net, area) ⇒ Boolean

add_network adds network settings for router ospf and network area.

Parameters:

  • :pid (String)

    The pid for router ospf

  • :net (String)

    The network name

  • :area (String)

    The network area name

Returns:

  • (Boolean)

    returns true if the command completed successfully



169
170
171
# File 'lib/rbeapi/api/ospf.rb', line 169

def add_network(pid, net, area)
  configure ["router ospf #{pid}", "network #{net} area #{area}"]
end

#create(pid) ⇒ Boolean

create will create a router ospf with the specified pid

Parameters:

  • :pid (String)

    The router ospf to create

Returns:

  • (Boolean)

    returns true if the command completed successfully



126
127
128
# File 'lib/rbeapi/api/ospf.rb', line 126

def create(pid)
  configure "router ospf #{pid}"
end

#delete(pid) ⇒ Boolean

delete will remove the specified router ospf

Parameters:

  • :pid (String)

    The router ospf to remove

Returns:

  • (Boolean)

    returns true if the command completed successfully



136
137
138
# File 'lib/rbeapi/api/ospf.rb', line 136

def delete(pid)
  configure "no router ospf #{pid}"
end

#get(inst) ⇒ Hash

Returns the global OSPF configuration from the node

rubocop:disable Metrics/MethodLength

Examples:

{
  router_id: <string>
  areas: {
    <string>: array<string>
  },
  redistribute: {}
}

Parameters:

  • :inst (String)

    The ospf instance name

Returns:

  • (Hash)

    A Ruby hash object that provides the OSPF settings as key / value pairs.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rbeapi/api/ospf.rb', line 62

def get(inst)
  config = get_block("router ospf #{inst}")
  return nil unless config

  response = {}
  mdata = /(?<=^\s{3}router-id\s)(.+)$/.match(config)
  response['router_id'] = mdata.nil? ? '' : mdata[0]

  networks = config.scan(/^\s{3}network\s(.+)\sarea\s(.+)$/)
  areas = networks.each_with_object({}) do |cfg, hsh|
    net, area = cfg
    if hsh.include?(area)
      hsh[area] << net
    else
      hsh[area] = [net]
    end
  end
  response['areas'] = areas

  values = \
    config.scan(/(?<=^\s{3}redistribute\s)(\w+)[\s|$]*(route-map\s(.+))?/)

  response['redistribute'] = values.each_with_object({}) do |value, hsh|
    hsh[value[0]] = { 'route_map' => value[2] }
  end
  response
end

#getallHash

Returns the OSPF configuration from the node as a Ruby hash

<pid>: {
  router_id: <string>,
  areas: {,
  redistribute: {}
},
interfaces: {}

}

Returns:

  • (Hash)

    A Ruby hash object that provides the OSPF settings as key / value pairs.



105
106
107
108
109
110
111
112
# File 'lib/rbeapi/api/ospf.rb', line 105

def getall
  instances = config.scan(/(?<=^router\sospf\s)\d+$/)
  response = instances.each_with_object({}) do |inst, hsh|
    hsh[inst] = get inst
  end
  response['interfaces'] = interfaces.getall
  response
end

#interfacesObject



114
115
116
117
118
# File 'lib/rbeapi/api/ospf.rb', line 114

def interfaces
  @interfaces if @interfaces
  @interfaces = OspfInterfaces.new(node)
  @interfaces
end

#remove_network(pid, net, area) ⇒ Boolean

remove_network removes network settings for router ospf and network

area.

Parameters:

  • :pid (String)

    The pid for router ospf

  • :net (String)

    The network name

  • :area (String)

    The network area name

Returns:

  • (Boolean)

    returns true if the command completed successfully



184
185
186
# File 'lib/rbeapi/api/ospf.rb', line 184

def remove_network(pid, net, area)
  configure ["router ospf #{pid}", "no network #{net} area #{area}"]
end

#set_redistribute(pid, proto, opts = {}) ⇒ Boolean

set_redistribute sets router ospf router-id with pid and options

Parameters:

  • :pid (String)

    The router ospf name

  • :proto (String)

    The redistribute value

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command completed successfully



205
206
207
208
209
210
# File 'lib/rbeapi/api/ospf.rb', line 205

def set_redistribute(pid, proto, opts = {})
  routemap = opts[:routemap]
  cmds = ["router ospf #{pid}", "redistribute #{proto}"]
  cmds[1] << " route-map #{routemap}" if routemap
  configure cmds
end

#set_router_id(pid, opts = {}) ⇒ Boolean

set_router_id sets router ospf router-id with pid and options

Parameters:

  • :pid (String)

    The router ospf name

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command completed successfully



153
154
155
156
157
# File 'lib/rbeapi/api/ospf.rb', line 153

def set_router_id(pid, opts = {})
  cmd = command_builder('router-id', opts)
  cmds = ["router ospf #{pid}", cmd]
  configure cmds
end