Class: Distem::Resource::VNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/distem/resource/vnetwork.rb

Overview

Abstract representation of a virtual network

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (VNetwork) initialize(address, name, nb_pnodes, opts)

Create a new VNetwork

Attributes

  • address The address range to associate to this virtual network (ip/mask, ip/cidr format or IPAddress object)

  • name The name of the virtual network (if not precised, set to “vnetworkN” where N is a unique id)

  • nb_pnodes The number of physical nodes

  • opts Miscellaneous options



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/distem/resource/vnetwork.rb', line 28

def initialize(address,name,nb_pnodes,opts)
  @id = 0
  @name = name
  @opts = opts
  if address.is_a?(IPAddress)
    @address = address.network.clone
  else
    begin
      @address = IPAddress.parse(address).network
    rescue ArgumentError
      raise Lib::InvalidParameterError, address
    end
  end
  @vnodes = {}
  @vroutes = {}
  @visibility = []

  # Address used by the coordinator
  @alreadyusedaddr = nb_pnodes.times.map { |n| IPAddress::IPv4::parse_u32(@address.last.to_u32 - n).to_s }
  @curaddress = @address.first
end

Instance Attribute Details

- (Object) address (readonly)

The IPAddress object describing the address range of this virtual network



10
11
12
# File 'lib/distem/resource/vnetwork.rb', line 10

def address
  @address
end

- (Object) name (readonly)

The (unique) name of this virtual network



12
13
14
# File 'lib/distem/resource/vnetwork.rb', line 12

def name
  @name
end

- (Object) opts

An Hash of Miscellaneous options (network_type, root_interface, vxlan_id…)



20
21
22
# File 'lib/distem/resource/vnetwork.rb', line 20

def opts
  @opts
end

- (Object) visibility

An Array of physical nodes this virtual network is visible on



18
19
20
# File 'lib/distem/resource/vnetwork.rb', line 18

def visibility
  @visibility
end

- (Object) vnodes (readonly)

An Hash describing the VNodes connected to this virtual network (key: VNode object, val: VIface object (the VIface used by the VNode to be connected to the network)



14
15
16
# File 'lib/distem/resource/vnetwork.rb', line 14

def vnodes
  @vnodes
end

- (Object) vroutes (readonly)

An Hash of the VRoutes associated to this virtual network (key: VRoute.destnet, val: VRoute object)



16
17
18
# File 'lib/distem/resource/vnetwork.rb', line 16

def vroutes
  @vroutes
end

Instance Method Details

- (Object) ==(vnetwork)

Compare two virtual networks

Returns

Boolean value



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/distem/resource/vnetwork.rb', line 220

def ==(vnetwork)
  ret = false
  if vnetwork.is_a?(VNetwork)
    ret = (vnetwork.name == @name)
  #what follows has probably to be removed
  elsif vnetwork.is_a?(String)
    begin
      addr = IPAddress.parse(vnetwork)
      ret = (addr.to_string == @address.to_string)
    rescue ArgumentError
      ret = false
    end
  else
    ret = false
  end
  return ret
end

- (Object) add_vnode(vnode, viface, address = nil)

Connect a VNode to this vitual network

Attributes

  • vnode The VNode object

  • viface The VIface object describing which virtual network interface of the virtual node to use to connect to this network

  • address (optional) The IP address to set to the virtual node virtual network interface, if not set, picking automagically one of the IP of the range associated to this network

Exceptions

  • AlreadyExistingResourceError if the virtual node is already connected to the network

  • UnavailableResourceError if the specified IP address is already taken by another VNode



59
60
61
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
# File 'lib/distem/resource/vnetwork.rb', line 59

def add_vnode(vnode,viface,address=nil)
  #Atm one VNode can only be attached one time to a VNetwork
  raise Lib::AlreadyExistingResourceError, vnode.name if @vnodes[vnode]

  addr = nil
  if address
    begin
      address = IPAddress.parse(address) unless address.is_a?(IPAddress)
      address.prefix = @address.prefix.to_i
    rescue ArgumentError
      raise Lib::InvalidParameterError, address
    end
    raise Lib::InvalidParameterError, "#{address.to_s}->#{@address.to_string}" \
      unless @address.include?(address)

    raise Lib::UnavailableResourceError, address.to_s \
      if @alreadyusedaddr.include?(address.to_s)

    addr = address.clone
  else
    inc_curaddress() if @alreadyusedaddr.include?(@curaddress.to_s)
    addr = @curaddress.clone
    inc_curaddress()
  end
  @vnodes[vnode] = viface
  @alreadyusedaddr << addr.to_s
  viface.attach(self,addr)
end

- (Object) add_vroute(vroute)

Add a new virtual route to this virtual network

Attributes

  • vroute The VRoute object



153
154
155
156
157
158
# File 'lib/distem/resource/vnetwork.rb', line 153

def add_vroute(vroute)
  raise unless vroute.is_a?(VRoute)
  raise Lib::AlreadyExistingResourceError, vroute.to_s \
    if @vroutes[vroute.dstnet]
  @vroutes[vroute.dstnet.address.to_string] = vroute
end

- (Object) DELETE @address.last.to_s



213
# File 'lib/distem/resource/vnetwork.rb', line 213

@alreadyusedaddr.delete(@address.last.to_s)

- (Object) DELETE self



204
# File 'lib/distem/resource/vnetwork.rb', line 204

excludelist.delete(self)

- (Object) DELETE vroute.dstnet.address.to_string



166
# File 'lib/distem/resource/vnetwork.rb', line 166

@vroutes.delete(vroute.dstnet.address.to_string)

- (Object) destroy

Destroy the object (remove every association with other resources)



209
210
211
212
213
214
# File 'lib/distem/resource/vnetwork.rb', line 209

def destroy()
  @vnodes.each_key do |vnode|
    remove_vnode(vnode)
  end
  @alreadyusedaddr.delete(@address.last.to_s)
end

- (Object) get_vnode_by_address(address)

Get the VNode using a specified address on this virtual network

Attributes

  • address The address String

Returns

VNode object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/distem/resource/vnetwork.rb', line 105

def get_vnode_by_address(address)
  begin
    IPAddress.parse(address) unless address.is_a?(IPAddress)
  rescue ArgumentError
    raise Lib::InvalidParameterError, address
  end
  ret = nil
  @vnodes.each do |vnode,viface|
    if viface.address.to_s.strip == address.to_s.strip
      ret = vnode
      break
    end
  end
  return ret
end

- (Object) get_vnode_viface(vnode)

Get the VIface a VNode is using to connect to this virtual network

Attributes

  • vnode The VNode object

Returns

VIface object



94
95
96
97
# File 'lib/distem/resource/vnetwork.rb', line 94

def get_vnode_viface(vnode)
  raise unless vnode.is_a?(VNode)
  return @vnodes[vnode]
end

- (Object) get_vroute(dstnet)

Get a virtual route specifying the route destination

Attributes

  • dstnet The VNetwork object describing the destination virtual network of the virtual route



173
174
175
176
# File 'lib/distem/resource/vnetwork.rb', line 173

def get_vroute(dstnet)
  raise unless dstnet.is_a?(VNetwork)
  return @vroutes[dstnet.address.to_string]
end

- (Object) perform_vroute(vnetwork, admin_vnetwork, excludelist = [])

Get the virtual node which make the link with another virtual network

Attributes

  • vnetwork The destination VNetwork object

  • excludelist Recursive function purpose, do not use it

Returns

VNode object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/distem/resource/vnetwork.rb', line 185

def perform_vroute(vnetwork,admin_vnetwork,excludelist=[])
  ret = nil
  excludelist << self
  found = false
  @vnodes.each_key do |vnode|
    vnode.vifaces.each do |viface|
      if viface.connected_to?(vnetwork)
        found = true
      end
      if viface.vnetwork and !excludelist.include?(viface.vnetwork) and (viface.vnetwork != admin_vnetwork)
        found = true if viface.vnetwork.perform_vroute(vnetwork,admin_vnetwork,excludelist)
      end
      break if found
    end
    if found
      ret = vnode
      break
    end
  end
  excludelist.delete(self)
  return ret
end

- (Object) remove_vnode(vnode, detach = true)

Disconnect a VNode from this virtual network

Attributes

  • vnode The VNode object

  • detach (optional) Also detach the virtual network interface from this virtual network (see VIface.detach).



140
141
142
143
144
145
146
147
# File 'lib/distem/resource/vnetwork.rb', line 140

def remove_vnode(vnode,detach = true)
  #Atm one VNode can only be attached one time to a VNetwork
  if @vnodes[vnode]
    @alreadyusedaddr.delete(@vnodes[vnode].address.to_s)
    @vnodes[vnode].detach() if detach
    @vnodes.delete(vnode)
  end
end

- (Object) remove_vroute(vroute)

Remove a virtual route from this virtual network

Attributes

  • vroute The VRoute object



164
165
166
167
# File 'lib/distem/resource/vnetwork.rb', line 164

def remove_vroute(vroute)
  raise unless vroute.is_a?(VRoute)
  @vroutes.delete(vroute.dstnet.address.to_string)
end

- (Object) to_s



238
239
240
# File 'lib/distem/resource/vnetwork.rb', line 238

def to_s()
  return "#{name}(#{address.to_string})"
end