Module: OMF::SFA::Resource::Base::ClassMethods

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

Constant Summary collapse

@@sfa_defs =
{}
@@sfa_namespaces =
{}
@@sfa_classes =
{}
@@sfa_name2class =
{}

Instance Method Summary collapse

Instance Method Details

#_sfa_add_ns(name, opts = {}) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/omf-sfa/resource/sfa_base.rb', line 244

def _sfa_add_ns(name, opts = {})
  if ns = opts[:namespace]
    unless @@sfa_namespaces[ns]
      raise "Unknown namespace '#{ns}'"
    end
    name = "#{ns}:#{name}"
  end
  name
end

#_to_sfa_xml(resources, root, obj2id, opts = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/omf-sfa/resource/sfa_base.rb', line 174

def _to_sfa_xml(resources, root, obj2id, opts = {})
  #puts "RRRXXX> #{resources}"
  if resources.kind_of? Enumerable
    #puts "RRRXXX2> #{resources}"
    resources.each do |r|
      #puts "R3> #{r}"
      _to_sfa_xml(r, root, obj2id, opts)
    end
  # elsif resources.kind_of? OMF::SFA::Resource::OGroup
    # # NOTE: Should be adding a GROUP element!
    # resources.each_resource do |r|
      # _to_sfa_xml(r, root, obj2id, opts)
    # end
  else
    resources.to_sfa_xml(root, obj2id, opts)
  end
  root.document
end

#default_component_manager_idObject



24
25
26
# File 'lib/omf-sfa/resource/sfa_base.rb', line 24

def default_component_manager_id()
  Constants.default_component_manager_id
end

#default_domainObject



20
21
22
# File 'lib/omf-sfa/resource/sfa_base.rb', line 20

def default_domain()
  Constants.default_domain
end

#descendantsObject



254
255
256
257
258
259
260
# File 'lib/omf-sfa/resource/sfa_base.rb', line 254

def descendants
  result = []
  ObjectSpace.each_object(Class) do |klass|
    result = result << klass if klass < self
  end
  result
end

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/omf-sfa/resource/sfa_base.rb', line 110

def from_sfa(resource_el, context = {})
  resource = nil
  uuid = nil
  comp_gurn = nil

  unless resource_el.namespace.href == SFA_NAMESPACE_URI
    warn "WARNING: '#{resource_el.name}' Can't handle non-default namespaces '#{resource_el.namespace.href}'"
    return
  end

  client_id_attr = resource_el.attributes['client_id']
  client_id = client_id_attr ? client_id_attr.value : nil

  if uuid_attr = (resource_el.attributes['uuid'] || resource_el.attributes['idref'])
    uuid = UUIDTools::UUID.parse(uuid_attr.value)
    if resource = OMF::SFA::Resource::OResource.first(:uuid => uuid)
      context[client_id] = resource if client_id
      return resource.from_sfa(resource_el, context)
    end
  end

  if comp_id_attr = resource_el.attributes['component_id']
    comp_id = comp_id_attr.value
    comp_gurn = OMF::SFA::Resource::GURN.parse(comp_id)
    #begin
    if uuid = comp_gurn.uuid
      resource = OMF::SFA::Resource::OResource.first(:uuid => uuid)
      context[client_id] = resource if client_id
      return resource.from_sfa(resource_el, context)
    end
    if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn)
      context[client_id] = resource if client_id
      return resource.from_sfa(resource_el, context)
    end
  else
    # need to create a comp_gurn (the link is an example of that)
    unless sliver_id_attr = resource_el.attributes['sliver_id']
      raise "Need 'sliver_id' for resource '#{resource_el}'"
    end
    sliver_gurn = OMF::SFA::Resource::GURN.parse(sliver_id_attr.value)
    unless client_id
      raise "Need 'client_id' for resource '#{resource_el}'"
    end
    opts = {
      :domain => sliver_gurn.domain,
      :type => resource_el.name  # TODO: This most likely will break with NS
    }
    comp_gurn = OMF::SFA::Resource::GURN.create("#{sliver_gurn.short_name}:#{client_id}", opts)
    if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn)
      context[client_id] = resource if client_id
      return resource.from_sfa(resource_el, context)
    end
  end

  # Appears the resource doesn't exist yet, let's see if we can create one
  type = comp_gurn.type
  if res_class = @@sfa_name2class[type]
    resource = res_class.new(:name => comp_gurn.short_name)
    context[client_id] = resource if client_id
    return resource.from_sfa(resource_el, context)
  end
  raise "Unknown resource type '#{type}' (#{@@sfa_name2class.keys.join(', ')})"
end

#sfa(name, opts = {}) ⇒ Object

Define a SFA property

Parameters:

  • name (Symbol)

    name of resource in RSpec

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

    options to further describe mappings

Options Hash (opts):

  • :inline (Boolean)

    ????

  • :has_manny (Boolean)

    If true, can occur multiple time forming an array

  • :attribute (Boolean)

    If true, ????

  • :attr_value (String)

    ????



70
71
72
73
74
75
76
# File 'lib/omf-sfa/resource/sfa_base.rb', line 70

def sfa(name, opts = {})
  name = name.to_s
  props = sfa_defs() # get all the sfa properties of this class
  props[name] = opts
  # recalculate sfa properties of the descendants
  descendants.each do |c| c.sfa_defs(false) end
end

#sfa_add_namespace(prefix, urn) ⇒ Object



49
50
51
# File 'lib/omf-sfa/resource/sfa_base.rb', line 49

def sfa_add_namespace(prefix, urn)
  @@sfa_namespaces[prefix] = urn
end

#sfa_add_namespaces_to_document(doc) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/omf-sfa/resource/sfa_base.rb', line 53

def sfa_add_namespaces_to_document(doc)
  root = doc.root
  root.add_namespace(nil, SFA_NAMESPACE_URI)
  @@sfa_namespaces.each do |name, uri|
    root.add_namespace(name.to_s, uri) #'omf', 'http://tenderlovemaking.com')
  end
end

#sfa_advertisement_xml(resources, opts = {}) ⇒ Object

opts:

:valid_for - valid [sec] from now


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/omf-sfa/resource/sfa_base.rb', line 81

def sfa_advertisement_xml(resources, opts = {})
  doc = Nokogiri::XML::Document.new
  #<rspec expires="2011-09-13T09:07:09Z" generated="2011-09-13T09:07:09Z" type="advertisement" xmlns="http://www.geni.net/resources/rspec/3" xmlns:ol="http://nitlab.inf.uth.gr/schema/sfa/rspec/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/ad.xsd http://nitlab.inf.uth.gr/schema/sfa/rspec/1 http://nitlab.inf.uth.gr/schema/sfa/rspec/1/ad-reservation.xsd">
  root = doc.add_child(Nokogiri::XML::Element.new('rspec', doc))
  root.add_namespace(nil, SFA_NAMESPACE_URI)
  root.add_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")

  opts[:type] = 'advertisement' unless opts[:type]
  if opts[:type] == 'manifest'
    schema = 'manifest.xsd'
  else
    schema = 'ad.xsd'
  end
  root['xsi:schemaLocation'] = "#{SFA_NAMESPACE_URI} #{SFA_NAMESPACE_URI}/#{schema} #{@@sfa_namespaces[:ol]} #{@@sfa_namespaces[:ol]}/ad-reservation.xsd"
  @@sfa_namespaces.each do |prefix, urn|
    root.add_namespace(prefix.to_s, urn)
  end

  root.set_attribute('type', opts[:type])
  now = Time.now
  root.set_attribute('generated', now.iso8601)
  root.set_attribute('expires', (now + (opts[:valid_for] || 600)).iso8601)

  #root = doc.create_element('rspec', doc)
  #doc.add_child root
  obj2id = {}
  _to_sfa_xml(resources, root, obj2id, opts)
end

#sfa_cast_property_value(value, property_name, context, type = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/omf-sfa/resource/sfa_base.rb', line 217

def sfa_cast_property_value(value, property_name, context, type = nil)
  name = property_name.to_s
  unless type
    pdef = sfa_def_for(name)
    raise "Unknow SFA property '#{name}'" unless pdef
    type = pdef[:type]
  end
  if type.kind_of?(Symbol)
    if type == :boolean
      unless value.kind_of?(TrueClass) || value.kind_of?(FalseClass)
        raise "Wrong type for '#{name}', is #{value.type}, but should be #{type}"
      end
    else
      raise "Unknown type '#{type}', use real Class"
    end
  elsif !(value.kind_of?(type))
    if type.respond_to? :sfa_create
      value = type.sfa_create(value, context)
    else
      raise "Wrong type for '#{name}', is #{value.class}, but should be #{type}"
    end
  #          puts "XXX>>> #{name}--#{! value.kind_of?(type)}--#{value.class}||#{type}||#{pdef.inspect}"

  end
  value
end

#sfa_class(name = nil, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/omf-sfa/resource/sfa_base.rb', line 37

def sfa_class(name = nil, opts = {})
  if name
    name = _sfa_add_ns(name, opts)
    #sfa_defs()['_class_'] = name
    @@sfa_classes[self] = name
    @@sfa_name2class[name] = self
  else
    @@sfa_classes[self]
    #sfa_def_for('_class_')
  end
end

#sfa_def_for(name) ⇒ Object



213
214
215
# File 'lib/omf-sfa/resource/sfa_base.rb', line 213

def sfa_def_for(name)
  sfa_defs()[name.to_s]
end

#sfa_defs(cached = true) ⇒ Object

Return all the property definitions for this class.

cached - If false, recalculate



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/omf-sfa/resource/sfa_base.rb', line 197

def sfa_defs(cached = true)
  unless cached && props = @@sfa_defs[self]
    # this assumes that all the properties of the super classes are already set
    props = {}
    klass = self
    while klass = klass.superclass
      if sp = @@sfa_defs[klass]
        props = sp.merge(props)
      end
    end
    #puts "PROP #{self}:#{props.keys.inspect}"
    @@sfa_defs[self] = props
  end
  props
end