Class: Occi::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/occi/parser.rb

Constant Summary collapse

OVF =

Declaring Class constants for OVF XML namespaces (defined in OVF specification ver.1.1)

"http://schemas.dmtf.org/ovf/envelope/1"
RASD =
"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
VSSD =
"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
OVFENV =
"http://schemas.dmtf.org/ovf/environment/1"
CIM =
"http://schemas.dmtf.org/wbem/wscim/1/common"

Class Method Summary collapse

Class Method Details

.alloc_units_bytes(alloc_units) ⇒ Object (private)



259
260
261
262
263
264
265
266
# File 'lib/occi/parser.rb', line 259

def self.alloc_units_bytes(alloc_units)
  units = alloc_units.split('*')
  #check units[1] is nil??
  units[1].strip!
  alloc_vars = units[1].split('^')
  alloc_units_bytes = (alloc_vars[0].to_i**alloc_vars[1].to_i)
  alloc_units_bytes
end

.calculate_capacity_bytes(capacity, alloc_units_bytes) ⇒ Object (private)

Helper method for calculation of storage size based on allocation units configured###########



247
248
249
250
# File 'lib/occi/parser.rb', line 247

def self.calculate_capacity_bytes(capacity, alloc_units_bytes)
  total_capacity_bytes = alloc_units_bytes * capacity.to_i
  total_capacity_bytes
end

.calculate_capacity_gb(capacity) ⇒ Object (private)



253
254
255
256
# File 'lib/occi/parser.rb', line 253

def self.calculate_capacity_gb(capacity)
  capacity_gb = capacity.to_f/(2**30)
  capacity_gb
end

.header_categories(header) ⇒ Occi::Collection (private)

Parameters:

  • header (Hash)

Returns:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/occi/parser.rb', line 73

def self.header_categories(header)
  collection = Occi::Collection.new
  category_strings = header['CATEGORY'].to_s.split(',')
  category_strings.each do |cat|
    category = OCCIANTLR::Parser.new('Category: ' + cat).category
    collection.kinds.merge category.kinds.collect { |kind| Occi::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) }
    collection.mixins.merge category.mixins.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) }
    collection.actions.merge category.actions.collect { |action| Occi::Core::Action.new(action.scheme, action.term, action.title, action.attributes) }
  end
  collection
end

.header_entity(header, entity_type) ⇒ Occi::Collection (private)

Parameters:

  • header (Hash)
  • entity_type (Class)

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/occi/parser.rb', line 88

def self.header_entity(header, entity_type)
  collection = Occi::Collection.new
  entity = Hashie::Mash.new
  category_strings = header['CATEGORY'].to_s.split(',')
  return collection if category_strings.empty?
  attribute_strings = header['X_OCCI_ATTRIBUTE'].to_s.split(',')
  categories = Hashie::Mash.new({:kinds => [], :mixins => [], :actions => []})
  category_strings.each do |category|
    cat = OCCIANTLR::Parser.new('Category: ' + category).category
    categories.kinds.concat cat.kinds
    categories.mixins.concat cat.mixins
    categories.actions.concat cat.actions
  end

  return collection if categories.kinds.empty?
  entity.kind = categories.kinds.first.scheme + categories.kinds.first.term
  entity.mixins = categories.mixins.collect { |mixin| mixin.scheme + mixin.term } if categories.mixins.any?
  attribute_strings.each { |attr| entity.attributes!.merge!(OCCIANTLR::Parser.new('X-OCCI-Attribute: ' + attr).x_occi_attribute) }
  if entity_type == Occi::Core::Link
    entity.target = link.attributes!.occi!.core!.target
    entity.source = link.attributes!.occi!.core!.source
    cats = entity.categories.split(' ')
    kind = cats.reverse!.pop
    mixins = cats.categories
    collection.links << Occi::Core::Link.new(kind, mixins, entity.attributes)
  elsif entity_type == Occi::Core::Resource
    link_strings = header['LINK'].to_s.split(',')
    link_strings.each do |link_string|
      link = OCCIANTLR::Parser.new('Link: ' + link_string).link
      if link.rel.include? 'action#'
        entity.actions = link.rel + entity.actions.to_a
      else
        link.attributes!.occi!.core!.target = link.target

        link.categories = (link.categories.presence || %w'http://schemas.ogf.org/occi/core#link')
        kind = link.categories.reverse!.pop
        mixins = link.categories

        collection.links << Occi::Core::Link.new(kind, mixins, link.attributes, link.actions, link.rel, link.target, link.source)
      end
    end
    collection.resources << Occi::Core::Resource.new(entity.kind, entity.mixins, entity.attributes, entity.actions, collection.links)
  end
  collection
end

.header_locations(header) ⇒ Array (private)

Returns list of URIs.

Parameters:

  • header (Hash)

Returns:

  • (Array)

    list of URIs



66
67
68
69
# File 'lib/occi/parser.rb', line 66

def self.header_locations(header)
  x_occi_location_strings = header['X_OCCI_LOCATION'].to_s.split(',')
  x_occi_location_strings.collect { |loc| OCCIANTLR::Parser.new('X-OCCI-Location: ' + loc).x_occi_location }
end

.json(json) ⇒ Occi::Collection (private)

Parameters:

  • json (String)

Returns:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/occi/parser.rb', line 204

def self.json(json)
  collection = Occi::Collection.new
  hash = Hashie::Mash.new(JSON.parse(json))
  collection.kinds.merge hash.kinds.collect { |kind| Occi::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) } if hash.kinds
  collection.mixins.merge hash.mixins.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) } if hash.mixins
  collection.actions.merge hash.actions.collect { |action| Occi::Core::Action.new(action.scheme, action.term, action.title, action.attributes) } if hash.actions
  collection.resources.merge hash.resources.collect { |resource| Occi::Core::Resource.new(resource.kind, resource.mixins, resource.attributes, resource.actions, resource.links) } if hash.resources
  collection.links.merge hash.links.collect { |link| Occi::Core::Link.new(link.kind, link.mixins, link.attributes, [], nil, link.target) } if hash.links

  if collection.resources.size == 1 && collection.links.size > 0
    if collection.resources.first.links.empty?
      collection.links.each { |link| link.source = collection.resources.first }
      collection.resources.first.links = collection.links
    end
  end

  # TODO: replace the following mechanism with one in the Links class
  # replace link locations with link objects in all resources
  collection.resources.each do |resource|
    resource.links.collect! do |resource_link|
      lnk = collection.links.select { |link| resource_link == link.to_s }.first
      lnk ||= resource_link
    end
  end
  collection
end

.locations(media_type, body, header) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/occi/parser.rb', line 48

def self.locations(media_type, body, header)
  locations = self.header_locations(header)
  locations << header['Location'] if !header['Location'].nil? && header['Location'].any?
  case media_type
    when 'text/uri-list'
      body.each_line { |line| locations << URI.parse(line.chomp) }
    when 'text/plain', nil
      locations.concat self.text_locations(body)
    else
      nil
  end
  locations
end

.ova(ova) ⇒ Occi::Collection (private)

Parameters:

  • ova (String)

Returns:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/occi/parser.rb', line 272

def self.ova(ova)
  tar = Gem::Package::TarReader.new(StringIO.new(ova))
  ovf = mf = cert = nil
  files = {}
  tar.each do |entry|
    tempfile = Tempfile.new(entry.full_name)
    tempfile.write(entry.read)
    tempfile.close
    files[entry.full_name] = tempfile.path
    ovf = tempfile.path if entry.full_name.end_with? '.ovf'
    mf = tempfile.path if entry.full_name.end_with? '.mf'
    cert = tempfile.path if entry.full_name.end_with? '.cert'
  end

  File.read(mf).each_line do |line|
    name = line.scan(/SHA1\(([^\)]*)\)= (.*)/).flatten.first
    sha1 = line.scan(/SHA1\(([^\)]*)\)= (.*)/).flatten.last
    Occi::Log.debug "SHA1 hash #{Digest::SHA1.hexdigest(files[name])}"
    raise "SHA1 mismatch for file #{name}" if Digest::SHA1.hexdigest(File.read(files[name])) != sha1
  end if mf

  raise 'no ovf file found' if ovf.nil?

  self.ovf(File.read(ovf), files)
end

.ovf(ovf, files = {}) ⇒ Object (private)

Parameters:

  • ovf (String)
  • files (Hash) (defaults to: {})

    key value pairs of file names and paths to the file



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/occi/parser.rb', line 300

def self.ovf(ovf, files={})
  collection = Occi::Collection.new
  doc = Nokogiri::XML(ovf)
  references = {}

  doc.xpath('envelope:Envelope/envelope:References/envelope:File', 'envelope' => "#{Parser::OVF}").each do |file|
    href = URI.parse(file.attributes['href'].to_s)
    if href.relative?
      if files[href.to_s]
        references[file.attributes['id'].to_s] = 'file://' + files[href.to_s]
      else
        references[file.attributes['id'].to_s] = 'file://' + href.to_s
      end
    else
      references[file.attributes['id'].to_s] = href.to_s
    end
  end

  doc.xpath('envelope:Envelope/envelope:DiskSection/envelope:Disk', 'envelope' => "#{Parser::OVF}").each do |disk|
    storage = Occi::Core::Resource.new('http://schemas.ogf.org/occi/infrastructure#storage')
    if disk.attributes['fileRef']
      storagelink = Occi::Core::Link.new("http://schemas.ogf.org/occi/infrastructure#storagelink")
      storagelink.attributes.occi!.core!.title = disk.attributes['fileRef'].to_s
      storagelink.attributes.occi!.core!.target = references[disk.attributes['fileRef'].to_s]
      storage.attributes.occi!.core!.title = disk.attributes['diskId'].to_s
      storage.links << storagelink
    else
      #OCCI accepts storage size in GB
      #OVF ver 1.1: The capacity of a virtual disk shall be specified by the ovf:capacity attribute with an xs:long integer
      #value. The default unit odf allocation shall be bytes. The optional string attribute
      #ovf:capacityAllocationUnits may be used to specify a particular unit of allocation.
      alloc_units = disk.attributes['capacityAllocationUnits'].to_s
      if alloc_units.empty?
        # The capacity is defined in bytes , convert to GB and pass it to OCCI
        capacity = disk.attributes['capacity'].to_s
        capacity =capacity.to_i
      else
        alloc_unit_bytes = self.alloc_units_bytes(alloc_units)
        capacity = self.calculate_capacity_bytes(disk.attributes['capacity'].to_s, alloc_unit_bytes)
      end
      capacity_gb = self.calculate_capacity_gb(capacity)
      Occi::Log.debug('capacity in gb ' + capacity_gb.to_s)
      storage.attributes.occi!.storage!.size = capacity_gb.to_s if capacity_gb
      storage.attributes.occi!.core!.title = disk.attributes['diskId'].to_s if disk.attributes['diskId']
    end
    collection.resources << storage
  end

  doc.xpath('envelope:Envelope/envelope:NetworkSection/envelope:Network', 'envelope' => "#{Parser::OVF}").each do |nw|
    network = Occi::Core::Resource.new('http://schemas.ogf.org/occi/infrastructure#network')
    network.attributes.occi!.core!.title = nw.attributes['name'].to_s
    collection.resources << network
  end

  # Iteration through all the virtual hardware sections,and a sub-iteration on each Item defined in the Virtual Hardware section
  doc.xpath('envelope:Envelope/envelope:VirtualSystem', 'envelope' => "#{Parser::OVF}").each do |virtsys|
    compute = Occi::Core::Resource.new('http://schemas.ogf.org/occi/infrastructure#compute')

    doc.xpath('envelope:Envelope/envelope:VirtualSystem/envelope:VirtualHardwareSection', 'envelope' => "#{Parser::OVF}").each do |virthwsec|
      compute.attributes.occi!.core!.summary = virthwsec.xpath("item:Info/text()", 'item' => "#{Parser::RASD}").to_s

      virthwsec.xpath('envelope:Item', 'envelope' => "#{Parser::OVF}").each do |resource_alloc|
        resType = resource_alloc.xpath("item:ResourceType/text()", 'item' => "#{Parser::RASD}")
        case resType.to_s
          # 4 is the ResourceType for memory in the CIM_ResourceAllocationSettingData
          when "4" then
            Occi::Log.debug('calculating memory in gb ')
            alloc_units = resource_alloc.xpath("item:AllocationUnits/text()", 'item' => "#{Parser::RASD}").to_s
            Occi::Log.debug('allocated units in ovf file: ' + alloc_units)
            alloc_unit_bytes = self.alloc_units_bytes(alloc_units)
            capacity = self.calculate_capacity_bytes(resource_alloc.xpath("item:VirtualQuantity/text()", 'item' => "#{Parser::RASD}").to_s, alloc_unit_bytes)
            capacity_gb = self.calculate_capacity_gb(capacity)
            Occi::Log.debug('virtual quantity of memory configured in gb: ' + capacity_gb.to_s)
            compute.attributes.occi!.compute!.memory = capacity_gb
          #  compute.attributes.occi!.compute!.memory = resource_alloc.xpath("item:VirtualQuantity/text()", 'item' => "#{Parser::RASD}").to_s.to_i
          # 3 is the ResourceType for processor in the CIM_ResourceAllocationSettingData
          when "3" then
            compute.attributes.occi!.compute!.cores = resource_alloc.xpath("item:VirtualQuantity/text()", 'item' => "#{Parser::RASD}").to_s.to_i
          when "10" then
            networkinterface = Occi::Core::Link.new('http://schemas.ogf.org/occi/infrastructure#networkinterface')
            networkinterface.attributes.occi!.core!.title = resource_alloc.xpath("item:ElementName/text()", 'item' => "#{Parser::RASD}").to_s
            id = resource_alloc.xpath("item:Connection/text()", 'item' => "#{Parser::RASD}").to_s
            network = collection.resources.select { |resource| resource.attributes.occi!.core!.title == id }.first
            raise "Network with id #{id} not found" unless network
            networkinterface.attributes.occi!.core!.target = network
          when "17" then
            storagelink = Occi::Core::Link.new("http://schemas.ogf.org/occi/infrastructure#storagelink")
            storagelink.attributes.occi!.core!.title = resource_alloc.xpath("item:ElementName/text()", 'item' => "#{Parser::RASD}").to_s
            # extract the mountpoint
            host_resource = resource_alloc.xpath("item:HostResource/text()", 'item' => "#{Parser::RASD}").to_s
            if host_resource.start_with? 'ovf:/disk/'
              id = host_resource.gsub('ovf:/disk/', '')
              storage = collection.resources.select { |resource| resource.attributes.occi!.core!.title == id }.first
              raise "Disk with id #{id} not found" unless storage
              storagelink.attributes.occi!.core!.target = storage
            elsif host_resource.start_with? 'ovf:/file/'
              id = host_resource.gsub('ovf:/file/', '')
              storagelink.attributes.occi!.core!.target = references[id]
            end
            compute.links << storagelink
        end
        ##Add the cpu architecture
        #system_sec                                      = virthwsec.xpath('envelope:System', 'envelope' => "#{Parser::OVF}")
        #virtsys_type                                    = system_sec.xpath('vssd_:VirtualSystemType/text()', 'vssd_' => "#{Parser::VSSD}")
        #compute.attributes.occi!.compute!.architecture = virtsys_type
      end
    end
    collection.resources << compute
  end
  collection
end

.parse(media_type, body, category = false, entity_type = Occi::Core::Resource, header = {}) ⇒ Occi::Collection

Parses an OCCI message and extracts OCCI relevant information

Parameters:

  • media_type (String)

    the media type of the OCCI message

  • body (String)

    the body of the OCCI message

  • category (true, false) (defaults to: false)

    for text/plain and text/occi media types information e.g. from the HTTP request location is needed to determine if the OCCI message includes a category or an entity

  • entity_type (Occi::Core::Resource, Occi::Core::Link) (defaults to: Occi::Core::Resource)

    entity type to use for parsing of text plain entities

  • header (Hash) (defaults to: {})

    optional header of the OCCI message

Returns:

  • (Occi::Collection)

    list consisting of an array of locations and the OCCI object collection



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/occi/parser.rb', line 18

def self.parse(media_type, body, category=false, entity_type=Occi::Core::Resource, header={})
  Occi::Log.debug '### Parsing request data to OCCI Collection ###'
  collection = Occi::Collection.new

  # remove trailing HTTP_ prefix if present
  header = Hash[header.map { |k, v| [k.gsub('HTTP_', '').upcase, v] }]

  category ? collection = self.header_categories(header) : collection = self.header_entity(header, entity_type)

  case media_type
    when 'text/uri-list'
      nil
    when 'text/occi'
      nil
    when 'text/plain', nil
      category ? collection = self.text_categories(body) : collection = self.text_entity(body, entity_type) if collection.empty?
    when 'application/occi+json', 'application/json'
      collection = self.json(body)
    when 'application/occi+xml', 'application/xml'
      collection = self.xml(body)
    when 'application/ovf', 'application/ovf+xml'
      collection = self.ovf(body)
    when 'application/ova'
      collection = self.ova(body)
    else
      raise "Content Type not supported"
  end
  collection
end

.text_categories(text) ⇒ Occi::Collection (private)

Parameters:

  • text (String)

Returns:



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/occi/parser.rb', line 142

def self.text_categories(text)
  collection = Occi::Collection.new
  text.each_line do |line|
    category = OCCIANTLR::Parser.new(line.chomp).category
    next if category.nil?
    collection.kinds.merge category.kinds.collect { |kind| Occi::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) }
    collection.mixins.merge category.mixins.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) }
    collection.actions.merge category.actions.collect { |action| Occi::Core::Action.new(action.scheme, action.term, action.title, action.attributes) }
  end
  collection
end

.text_entity(text, entity_type) ⇒ Occi::Collection (private)

Parameters:

  • text (String)
  • entity_type (Class)

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/occi/parser.rb', line 157

def self.text_entity(text, entity_type)
  collection = Occi::Collection.new
  entity = Hashie::Mash.new
  links = []
  categories = Hashie::Mash.new({:kinds => [], :mixins => [], :actions => []})
  text.each_line do |line|
    if line.include? 'Category'
      cat = (OCCIANTLR::Parser.new(line.chomp).category)
      categories.kinds.concat cat.kinds
      categories.mixins.concat cat.mixins
      categories.actions.concat cat.actions
    end
    entity.attributes!.merge!(OCCIANTLR::Parser.new(line.chomp).x_occi_attribute) if line.include? 'X-OCCI-Attribute'
    links << OCCIANTLR::Parser.new(line.chomp).link if line.include? 'Link'
  end
  entity.kind = categories.kinds.first.scheme + categories.kinds.first.term if categories.kinds.first
  entity.mixins = categories.mixins.collect { |mixin| mixin.scheme + mixin.term } if categories.mixins
  if entity_type == Occi::Core::Link
    entity.target = links.first.attributes!.occi!.core!.target
    entity.source = links.first.attributes!.occi!.core!.source
    cats = entity.categories.split(' ')
    kind = cats.reverse!.pop
    mixins = cats
    collection.links << Occi::Core::Link.new(kind, mixins, entity.attributes)
  elsif entity_type == Occi::Core::Resource
    links.each do |link|
      if link.rel.include? 'action#'
        entity.actions = [link.rel] + entity.actions.to_a
      else
        link.attributes!.occi!.core!.target = link.target

        link.categories = (link.categories.presence || %w'http://schemas.ogf.org/occi/core#link')
        cats = link.categories
        kind = link.categories.reverse!.pop
        mixins = link.categories

        link = Occi::Core::Link.new(kind, mixins, link.attributes, link.actions, link.rel, link.target, link.source)
        collection.links << link
      end
    end
    collection.resources << Occi::Core::Resource.new(entity.kind, entity.mixins, entity.attributes, entity.actions, collection.links)
  end unless entity.kind.nil?
  collection
end

.text_locations(text) ⇒ Array (private)

Returns list of URIs.

Parameters:

  • text (String)

Returns:

  • (Array)

    list of URIs



136
137
138
# File 'lib/occi/parser.rb', line 136

def self.text_locations(text)
  text.lines.collect { |line| OCCIANTLR::Parser.new(line.chomp).x_occi_location if line.include? 'X-OCCI-Location' }.compact
end

.xml(xml) ⇒ Occi::Collection (private)

Parameters:

  • xml (String)

Returns:



233
234
235
236
237
238
239
240
241
242
# File 'lib/occi/parser.rb', line 233

def self.xml(xml)
  collection = Occi::Collection.new
  hash = Hashie::Mash.new(Hash.from_xml(Nokogiri::XML(xml)))
  collection.kinds.merge hash.kinds.collect { |kind| Occi::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) } if hash.kinds
  collection.mixins.merge hash.mixins.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) } if hash.mixins
  collection.actions.merge hash.actions.collect { |action| Occi::Core::Action.new(action.scheme, action.term, action.title, action.attributes) } if hash.actions
  collection.resources.merge hash.resources.collect { |resource| Occi::Core::Resource.new(resource.kind, resource.mixins, resource.attributes, resource.actions, resource.links) } if hash.resources
  collection.links.merge hash.links.collect { |link| Occi::Core::Link.new(link.kind, link.mixins, link.attributes) } if hash.links
  collection
end