Module: OMF::SFA::Resource::Base::InstanceMethods

Included in:
AbstractService, CompGroup, DiskImage, GroupComponent, Ip, OComponent, OLease, SliverType
Defined in:
lib/omf-sfa/resource/sfa_base.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#_to_sfa_property_hash(value, pdef, href2obj, opts) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/omf-sfa/resource/sfa_base.rb', line 438

def _to_sfa_property_hash(value, pdef, href2obj, opts)
  if !value.kind_of?(String) && value.kind_of?(Enumerable)
    value.collect do |o|
      if o.respond_to? :to_sfa_hash
        o.to_sfa_hash_short(opts)
      else
        o.to_s
      end
    end
  else
    value.to_s
  end
end

#_to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/omf-sfa/resource/sfa_base.rb', line 527

def _to_sfa_property_xml(pname, value, res_el, pdef, obj2id, opts)
  pname = self.class._sfa_add_ns(pname, pdef)
  if value.respond_to?(:to_sfa_xml)
    value.to_sfa_xml(res_el, obj2id, opts)
    return
  end

  if pdef[:attribute]
    res_el.set_attribute(pname, value.to_s)
  elsif aname = pdef[:attr_value]
    el = res_el.add_child(Nokogiri::XML::Element.new(pname, res_el.document))
    el.set_attribute(aname, value.to_s)
  else
    if pdef[:inline] == true
      cel = res_el
    else
      cel = res_el.add_child(Nokogiri::XML::Element.new(pname, res_el.document))
    end
    #puts ">>> _to_sfa_property_xml(#{pname}): class: #{value.class} string? #{value.kind_of?(String)} enumerable: #{value.kind_of?(Enumerable)} "
    if !value.kind_of?(String) && (value.kind_of?(Enumerable) || value.is_a?(OMF::SFA::Resource::OPropertyArray))
      value.each do |v|
        if v.respond_to?(:to_sfa_xml)
          v.to_sfa_xml(cel, obj2id, opts)
        else
          el = cel.add_child(Nokogiri::XML::Element.new(pname, cel.document))
          #puts (el.methods - Object.new.methods).sort.inspect
          el.content = v.to_s
          #el.set_attribute('type', (pdef[:type] || 'string').to_s)
        end
      end
    else
      cel.content = value.to_s
      #cel.set_attribute('type', (pdef[:type] || 'string').to_s)
    end
  end
end

#_to_sfa_xml(parent, obj2id, opts) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/omf-sfa/resource/sfa_base.rb', line 464

def _to_sfa_xml(parent, obj2id, opts)
  new_element = parent.add_child(Nokogiri::XML::Element.new(_xml_name(), parent.document))
  if parent.document == parent
    # first time around, add namespace
    self.class.sfa_add_namespaces_to_document(parent)
  end
  defs = self.class.sfa_defs()
  if (!(opts[:suppress_id] || self.class.sfa_suppress_id?) && id = obj2id[self])
    new_element.set_attribute('idref', id)
    return parent
  end

  id = sfa_id()
  obj2id[self] = id
  unless opts[:suppress_id] || self.class.sfa_suppress_id?
    new_element.set_attribute('id', id) #if detail_level > 0
  end

  unless opts[:suppress_uuid] || self.class.sfa_suppress_uuid?
    new_element.set_attribute('omf:uuid', self.uuid) #if detail_level > 0
  end

  #if href = self.href(opts)
  #  new_element.set_attribute('omf:href', href)
  #end
  level = opts[:level] ? opts[:level] : 0
  opts[:level] = level + 1
  sfa_type = opts[:type]
  is_request = sfa_type == :request
  defs.keys.sort.each do |key|
    next if key.start_with?('_')
    pdef = defs[key]
    #puts ">>>> PDEF(#{key}): #{pdef}"
    if (ilevel = pdef[:include_level])
      #next if level > ilevel
    end
    next if is_request && pdef[:in_request] == false

    if respond_to?(m = "_to_sfa_xml_#{key}".to_sym)
      send(m, new_element, pdef, obj2id, opts)
      next
    end
    pname = (pdef[:prop_name] || key).to_sym
    #puts ">>>> #{pname} <#{self}> #{pdef.inspect}"
    next unless respond_to? pname
    value = send(pname)
    #puts "#{key} <#{value} - #{value.class}> #{pdef.inspect}"
    if value.nil?
      value = pdef[:default]
    end
    unless value.nil?
      #if detail_level > 0 || k == 'component_name'
      if value.is_a?(Time)
        value = value.xmlschema # xs:dateTime
      end
      _to_sfa_property_xml(key, value, new_element, pdef, obj2id, opts)
      #end
    end
  end
  opts[:level] = level # restore original level
  new_element
end

#_xml_nameObject



371
372
373
374
375
376
# File 'lib/omf-sfa/resource/sfa_base.rb', line 371

def _xml_name()
  if pd = self.sfa_class
    return pd
  end
  self.class.name.gsub('::', '_')
end

#component_idObject



320
321
322
323
324
325
326
327
# File 'lib/omf-sfa/resource/sfa_base.rb', line 320

def component_id
  unless id = attribute_get(:component_id)
    #self.component_id ||= GURN.create(self.uuid.to_s, self)
    #return GURN.create(self.uuid.to_s, { :model => self.class })
    return GURN.create(self.urn, { :model => self.class })
  end
  id
end

#component_manager_idObject

end



333
334
335
336
337
338
# File 'lib/omf-sfa/resource/sfa_base.rb', line 333

def component_manager_id
  unless uuid = attribute_get(:component_manager_id)
    return (self.class.default_component_manager_id ||= GURN.create("authority+am"))
  end
  uuid
end

#default_domainObject

end



344
345
346
# File 'lib/omf-sfa/resource/sfa_base.rb', line 344

def default_domain
  self.class.default_domain()
end

#from_sfa(resource_el, context = {}, type = 'manifest') ⇒ Object

Parameters:

  • context (defaults to: {})

    Already defined resources in this context



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/omf-sfa/resource/sfa_base.rb', line 567

def from_sfa(resource_el, context = {}, type = 'manifest')
  els = {} # this doesn't work with generic namespaces
  resource_el.children.each do |el|
    next unless el.is_a? Nokogiri::XML::Element
    unless ns = el.namespace
      raise "Missing namespace declaration for '#{el}'"
    end
    name = el.name
    unless ns.href == SFA_NAMESPACE_URI
      unless prefix = self.class._sfa_prefix_for_namespace(ns.href)
        warn "#{resource_el.name}: Ignoring unknown element '#{el.name}' - NS: '#{ns.href}'"
        next
      end
      name = "#{prefix}__#{name}"
    end
    (els[el.name] ||= []) << el
  end

  #puts ">>>>> #{self} - #{self.class.sfa_defs.keys}"
  self.class.sfa_defs.each do |name, props|
    mname = "_from_sfa_#{name}_property_xml".to_sym
    #puts "#{self}; Checking for #{mname} - #{props[:attribute]} - #{self.respond_to?(mname)}"
    if self.respond_to?(mname)
      send(mname, resource_el, props, context)
    elsif props[:attribute] == true
      #puts "Checking '#{name}'"
      next if name.to_s == 'component_name' # skip that one for the moment
      if v = resource_el.attributes[props[:attribute_name] || name]
        #puts "#{name}::#{name.class} = #{v}--#{v.class} (#{props})"
        name = props[:prop_name] || name
        send("#{name}=".to_sym, v.value)
      end
    elsif arr = els[name.to_s]
      #puts "Handling #{name} -- #{props}"
      name = props[:prop_name] || name
      arr.each do |el|
        #puts "#{self}: #{name} = #{el.text}"
        send("#{name}=".to_sym, el.text)
      end
    # else
      # puts "Don't know how to handle '#{name}' (#{props})"
    end
  end
  unless self.save
    raise "Couldn't save resource '#{self}'"
  end
  return self
end

#resource_typeObject



316
317
318
# File 'lib/omf-sfa/resource/sfa_base.rb', line 316

def resource_type
  sfa_class
end

#sfa_classObject



358
359
360
# File 'lib/omf-sfa/resource/sfa_base.rb', line 358

def sfa_class()
  self.class.sfa_class()
end

#sfa_idObject

end



353
354
355
356
# File 'lib/omf-sfa/resource/sfa_base.rb', line 353

def sfa_id()
  #@sfa_id ||= "c#{object_id}"
  self.uuid.to_s
end

#sfa_property(name) ⇒ Object

end



367
368
369
# File 'lib/omf-sfa/resource/sfa_base.rb', line 367

def sfa_property(name)
  instance_variable_get("sfa_#{name}")
end

#to_sfa_hash(href2obj = {}, opts = {}) ⇒ Object

Return all SFA related properties as a hash

opts

:detail - detail to reveal about resource 0..min, 99 .. max


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/omf-sfa/resource/sfa_base.rb', line 392

def to_sfa_hash(href2obj = {}, opts = {})
  res = to_sfa_hash_short(opts)
  res['comp_gurn'] = self.urn
  href = res['href']
  if obj = href2obj[href]
    # have described myself before
    raise "Different object with same href '#{href}'" unless obj == self
    return res
  end
  href2obj[href] = self

  defs = self.class.sfa_defs()
  #puts ">> #{defs.inspect}"
  defs.keys.sort.each do |k|
    next if k.start_with?('_')
    pdef = defs[k]
    pname = pdef[:prop_name] ||k
    v = send(pname.to_sym)
    if v.nil?
      v = pdef[:default]
    end
    #puts "!#{k} => '#{v}' - #{self}"
    unless v.nil?
      m = "_to_sfa_#{k}_property_hash".to_sym
      if self.respond_to? m
        res[k] = send(m, v, pdef, href2obj, opts)
        #puts ">>>> #{k}::#{res[k]}"
      else
        res[k] = _to_sfa_property_hash(v, pdef, href2obj, opts)
      end
    end
  end
  res
end

#to_sfa_hash_short(opts = {}) ⇒ Object



427
428
429
430
431
432
433
434
435
436
# File 'lib/omf-sfa/resource/sfa_base.rb', line 427

def to_sfa_hash_short(opts = {})
  uuid = self.uuid.to_s
  href_prefix = opts[:href_prefix] ||= default_href_prefix
  {
    'name' => self.name,
    'uuid' => uuid,
    'sfa_class' => sfa_class(),
    'href' => "#{href_prefix}/#{uuid}"
  }
end

#to_sfa_short_xml(parent) ⇒ Object



378
379
380
381
382
383
384
# File 'lib/omf-sfa/resource/sfa_base.rb', line 378

def to_sfa_short_xml(parent)
  n = parent.add_child(Nokogiri::XML::Element.new('resource', parent.document))
  n.set_attribute('type', _xml_name())
  n.set_attribute('status', 'unimplemented')
  n.set_attribute('name', component_name())
  n
end

#to_sfa_xml(parent = nil, obj2id = {}, opts = {}) ⇒ Object

opts

:detail - detail to reveal about resource 0..min, 99 .. max


456
457
458
459
460
461
462
# File 'lib/omf-sfa/resource/sfa_base.rb', line 456

def to_sfa_xml(parent = nil, obj2id = {}, opts = {})
  if parent.nil?
    parent = Nokogiri::XML::Document.new
  end
  _to_sfa_xml(parent, obj2id, opts)
  parent
end