Class: Distem::Resource::VPlatform

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

Overview

Abstract representation of a virtual platform resource that's describing an experimental environment (PNodes,VNodes,VNetworks,…)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (VPlatform) initialize

Create a new VPlatform



16
17
18
19
20
# File 'lib/distem/resource/vplatform.rb', line 16

def initialize
  @pnodes = {}
  @vnodes = {}
  @vnetworks = {}
end

Instance Attribute Details

- (Object) pnodes (readonly)

Hash of the physical nodes associated to this virtual platform (key: PNode.address, val: PNode)



9
10
11
# File 'lib/distem/resource/vplatform.rb', line 9

def pnodes
  @pnodes
end

- (Object) vnetworks (readonly)

Hash of the virtual networks associated to this virtual platform (key: VNetwork.name, val: VNetwork)



13
14
15
# File 'lib/distem/resource/vplatform.rb', line 13

def vnetworks
  @vnetworks
end

- (Object) vnodes (readonly)

Hash of the virtual nodes associated to this virtual platform (key: VNode.name, val: VNode)



11
12
13
# File 'lib/distem/resource/vplatform.rb', line 11

def vnodes
  @vnodes
end

Instance Method Details

- (Object) add_pnode(pnode)

Add a new physical node to the platform

Attributes

  • pnode The PNode object



26
27
28
29
30
31
32
# File 'lib/distem/resource/vplatform.rb', line 26

def add_pnode(pnode)
  raise unless pnode.is_a?(PNode)
  raise Lib::AlreadyExistingResourceError, pnode.address.to_s \
    if @pnodes[pnode.address]

  @pnodes[pnode.address] = pnode
end

- (Object) add_vnetwork(vnetwork)

Add a new virtual network to the platform

Attributes

  • vnetwork The VNetwork object

Exceptions

  • AlreadyExistingResourceError if a virtual network with the same name or the same address range already exists



149
150
151
152
153
154
155
# File 'lib/distem/resource/vplatform.rb', line 149

def add_vnetwork(vnetwork)
  raise unless vnetwork.is_a?(VNetwork)
  raise Lib::AlreadyExistingResourceError, "#{vnetwork.address.to_string}(#{vnetwork.name})" \
    if @vnetworks[vnetwork.name]

  @vnetworks[vnetwork.name] = vnetwork
end

- (Object) add_vnode(vnode)

Add a new virtual node to the platform

Attributes

  • vnode The VNode object

Exceptions

  • AlreadyExistingResourceError if a virtual node with the same name already exists



103
104
105
106
107
108
109
# File 'lib/distem/resource/vplatform.rb', line 103

def add_vnode(vnode)
  raise unless vnode.is_a?(VNode)
  raise Lib::AlreadyExistingResourceError, vnode.name \
    if @vnodes[vnode.name]

  @vnodes[vnode.name] = vnode
end

- (Object) add_vroute(vroute)

Add a new virtual network route to the platform

Attributes

  • vroute The VRoute object

Exceptions

  • ResourceNotFoundError if the source virtual network (VRoute.srcnet) is not found on the platform



211
212
213
214
215
216
# File 'lib/distem/resource/vplatform.rb', line 211

def add_vroute(vroute)
  raise unless vroute.is_a?(VRoute)
  vnetwork = @vnetworks[vroute.srcnet.name]
  raise Lib::ResourceNotFoundError, vroute.srcnet.name unless vnetwork
  vnetwork.add_vroute(vroute)
end

- (Object) DELETE pnode.address



40
# File 'lib/distem/resource/vplatform.rb', line 40

@pnodes.delete(pnode.address)

- (Object) DELETE vnetwork.name



171
# File 'lib/distem/resource/vplatform.rb', line 171

@vnetworks.delete(vnetwork.name)

- (Object) DELETE vnode.name



130
# File 'lib/distem/resource/vplatform.rb', line 130

@vnodes.delete(vnode.name)

- (Object) destroy(resource)

Delete a resource from the virtual platform

Attributes

  • resource The resource object (have to be of class: PNode,VNode,VNetwork or VRoute)



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/distem/resource/vplatform.rb', line 235

def destroy(resource)
  if resource.is_a?(PNode)
    remove_pnode(resource)
  elsif resource.is_a?(VNode)
    remove_vnode(resource)
  elsif resource.is_a?(VNetwork)
    remove_vnetwork(resource)
  elsif resource.is_a?(VRoute)
    remove_vroute(resource)
  end
end

- (Object) get_pnode_available(vnode)

Gets a physical node which is available to host a virtual node considering VCPU and VNetwork constraints

Attributes

  • vnode The virtual node

Returns

PNode object or nil if not found

Exceptions

  • UnavailableResourceError if no physical nodes are available (no PNode in this VPlatform)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/distem/resource/vplatform.rb', line 81

def get_pnode_available(vnode)
  availables = []
  # Might lead to race condition, must be executed inside a critical section
  @pnodes.each_value do |pnode|
    next if (vnode.vcpu && pnode.cpu.get_free_cores.size < vnode.vcpu.vcores.size) ||
      ((pnode.local_vifaces + vnode.vifaces.length) > Node::Admin.vifaces_max) ||
      (vnode.vmem && ((vnode.vmem.mem && (pnode.memory.get_free_capacity < vnode.vmem.mem)) &&
                       (vnode.vmem.swap && (pnode.memory.get_free_swap < vnode.vmem.swap))))
    availables << pnode
  end
  raise Lib::UnavailableResourceError, 'pnode/cpu, pnode/iface, or pnode/memory' if availables.empty?
  pnode = availables[rand(availables.size)]
  pnode.local_vifaces += vnode.vifaces.length
  return pnode
end

- (Object) get_pnode_by_address(address)

Get a physical node specifying it's address

Attributes

  • address The IP address (String)

Returns

PNode object or nil if not found

Exceptions

  • ResolvError if the address don't have a valid format



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/distem/resource/vplatform.rb', line 51

def get_pnode_by_address(address)
  # >>> TODO: validate ip address
  ret = nil
  begin
    ret = @pnodes[Resolv.getaddress(address)]
  rescue Resolv::ResolvError
    ret = nil
  ensure
    return ret
  end
end

- (Object) get_pnode_by_name(name)

Get a physical node specifying the name of a virtual node which it's connected on it

Attributes

  • name The name of the VNode (String)

Returns

PNode object or nil if not found



69
70
71
# File 'lib/distem/resource/vplatform.rb', line 69

def get_pnode_by_name(name)
  return (@vnodes[name] ? @vnodes[name].host : nil)
end

- (Object) get_vnetwork_by_address(address)

Get a virtual network specifying an IP address it's address range is including

Attributes

  • address The address (String or IPAddress)

Returns

VNetwork object or nil if not found



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/distem/resource/vplatform.rb', line 190

def get_vnetwork_by_address(address)
  raise unless (address.is_a?(String) or address.is_a?(IPAddress))
  raise if address.empty?
  ret = nil
  begin
    address = IPAddress.parse(address) if address.is_a?(String)
    address = address.network
  rescue ArgumentError
    return nil
  end

  ret = @vnetworks.values.select{ |vnet| vnet.address.include?(address) }[0]
  return ret
end

- (Object) get_vnetwork_by_name(name)

Get a virtual network specifying it's name

Attributes

  • name The name (String)

Returns

VNetwork object or nil if not found



180
181
182
# File 'lib/distem/resource/vplatform.rb', line 180

def get_vnetwork_by_name(name)
  return @vnetworks[name]
end

- (Object) get_vnode(name)

Get a virtual node specifying it's name

Attributes

  • name The name (String)

Returns

VNode object or nil if not found



139
140
141
# File 'lib/distem/resource/vplatform.rb', line 139

def get_vnode(name)
  return (@vnodes.has_key?(name) ? @vnodes[name] : nil)
end

- (Object) remove_pnode(pnode)

Remove physical node from the platform

Attributes

  • pnode The PNode object



38
39
40
41
# File 'lib/distem/resource/vplatform.rb', line 38

def remove_pnode(pnode)
  raise unless pnode.is_a?(PNode)
  @pnodes.delete(pnode.address)
end

- (Object) remove_vnetwork(vnetwork)

Remove a virtual network from the platform. Also remove all virtual routes this virtual network is playing a role in.

Attributes

  • vnetwork The VNetwork object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/distem/resource/vplatform.rb', line 161

def remove_vnetwork(vnetwork)
  raise unless vnetwork.is_a?(VNetwork)
  # Remove all associated vroutes
  @vnetworks.each_value do |vnet|
    next if vnet == vnetwork
    vnet.vroutes.each_value do |vroute|
      vnet.remove_vroute(vroute) if vroute.dstnet == vnetwork
    end
  end
  vnetwork.destroy()
  @vnetworks.delete(vnetwork.name)
end

- (Object) remove_vnode(vnode)

Remove a virtual node from the platform. If the virtual node is acting as gateway in some virtual routes, also remove this vroutes from the platform.

Attributes

  • vnode The VNode object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/distem/resource/vplatform.rb', line 115

def remove_vnode(vnode)
  raise unless vnode.is_a?(VNode)
  # Remove the vnode on each vnetwork it's connected
  @vnetworks.each_value do |vnetwork|
    if vnetwork.vnodes.keys.include?(vnode)
      # Remove every vroute vnode have a role on
      vnetwork.vroutes.each_value do |vroute|
          viface = vnetwork.get_vnode_viface(vnode)
          if viface and viface.address.to_s == vroute.gw.to_s
            vnetwork.remove_vroute(vroute)
          end
      end
      vnetwork.remove_vnode(vnode)
    end
  end
  @vnodes.delete(vnode.name)
end

- (Object) remove_vroute(vroute)

Remove a virtual network route from the platform

Attributes

  • vroute The VRoute object

Exceptions

  • ResourceNotFoundError if the source virtual network (VRoute.srcnet) is not found on the platform



224
225
226
227
228
229
# File 'lib/distem/resource/vplatform.rb', line 224

def remove_vroute(vroute)
  raise unless vroute.is_a?(VRoute)
  vnetwork = @vnetworks[vroute.srcnet.name]
  raise Lib::ResourceNotFoundError, vroute.srcnet.name unless vnetwork
  vnetwork.remove_vroute(vroute)
end