Class: Distem::Resource::Bandwidth

Inherits:
VIface::VTraffic::Property show all
Defined in:
lib/distem/resource/bandwidth.rb

Overview

Abstract representation of Network Bandwidth resource

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from VIface::VTraffic::Property

#to_s

Constructor Details

- (Bandwidth) initialize(paramshash = {})

Create a new Bandwidth

Attributes

  • paramshash The Hash of the parameters to set (in the form “paramname” => value)



14
15
16
17
18
# File 'lib/distem/resource/bandwidth.rb', line 14

def initialize(paramshash={})
  super()
  @rate = nil
  parse_params(paramshash)
end

Instance Attribute Details

- (Object) rate (readonly)

The rate (String at 'tc' form such as “10mbps”)



8
9
10
# File 'lib/distem/resource/bandwidth.rb', line 8

def rate
  @rate
end

Class Method Details

+ (Object) is_valid(rate)



49
50
51
52
53
54
55
56
# File 'lib/distem/resource/bandwidth.rb', line 49

def self.is_valid(rate)
    begin
      self.to_bytes(rate)
      return true
    rescue ArgumentError
      return false
    end
end

+ (Object) to_bytes(rate)

converts rate to integer number of bytes returns nil if ArgumentError if the rate cannot be parsed

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/distem/resource/bandwidth.rb', line 28

def self.to_bytes(rate)
  return nil if rate.nil?
  m = /^(\d+)(\w*)$/.match(rate)
  raise ArgumentError if m.nil?
  digits, units = m.captures
  mult = case units
    when 'kbps' then 1024 # kilobytes
    when 'mbps' then (1024**2) # megabytes
    when 'kbit' then (1024 / 8) # kilobits
    when 'mbit' then (1024**2 / 8) # megabits
    when 'bps', '' then 1   # bytes
    else nil
  end
  raise ArgumentError if mult.nil?
  return (digits.to_i * mult)
end

Instance Method Details

- (Object) parse_params(paramshash)

Parameters parsing method



21
22
23
24
# File 'lib/distem/resource/bandwidth.rb', line 21

def parse_params(paramshash)
  super(paramshash)
  @rate = paramshash['rate'] if paramshash['rate']
end

- (Object) to_bytes



45
46
47
# File 'lib/distem/resource/bandwidth.rb', line 45

def to_bytes
  Bandwidth.to_bytes(@rate)
end