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

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

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



283
284
285
286
287
288
289
290
291
# File 'lib/omf-sfa/resource/sfa_base.rb', line 283

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

#_sfa_prefix_for_namespace(urn) ⇒ Object



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

def _sfa_prefix_for_namespace(urn)
  @@sfa_namespace2prefix[urn]
end

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/omf-sfa/resource/sfa_base.rb', line 213

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



293
294
295
296
297
298
299
# File 'lib/omf-sfa/resource/sfa_base.rb', line 293

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

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



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
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
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/omf-sfa/resource/sfa_base.rb', line 131

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

  unless (href = resource_el.namespace.href) == SFA_NAMESPACE_URI
    unless prefix = _sfa_prefix_for_namespace(href)
      warn "Ignoring unknown element '#{resource_el.name}' - NS: '#{resource_el.namespace.href}'"
      return
    end
    ns_opts = @@sfa_namespaces[prefix]
    return if ns_opts[:ignore]
  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, type)
    end
  end

  # TODO: Clarify the role of 'sliver_id' vs. 'component_id'
  if comp_id_attr = resource_el.attributes['sliver_id'] || 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, type)
    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, type)
    end
  else
    # need to create a comp_gurn (the link is an example of that)
    unless client_id
      raise "Need 'client_id' for resource '#{resource_el}'"
    end
    if sliver_id_attr = resource_el.attributes['sliver_id']
      sliver_gurn = OMF::SFA::Resource::GURN.parse(sliver_id_attr.value)
      #puts "SLIVER_ID name: #{sliver_gurn.name} short: #{sliver_gurn.short_name} uuid: #{sliver_gurn.uuid}"
    else
      if type == 'request'
        sliver_gurn = OMF::SFA::Resource::GURN.create(client_id, type: resource_el.name, domain: 'unknown')
      else
        raise "Need 'sliver_id' for resource '#{resource_el}'"
      end
    end
    #puts "TYPE: #{type} - #{sliver_gurn}"
    # 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)
    comp_gurn = sliver_gurn
    if resource = OMF::SFA::Resource::OComponent.first(:urn => comp_gurn)
      context[client_id] = resource if client_id
      resource.from_sfa(resource_el, context, type)
      resource.save
      return
    end
  end

  # Appears the resource doesn't exist yet, let's see if we can create one
  type = resource_el.name #comp_gurn.type
  if res_class = @@sfa_name2class[type]
    resource = res_class.new(:name => comp_gurn.short_name, :urn => comp_gurn)
    #puts ">>> #{comp_gurn} - #{resource.to_hash}"
    context[client_id] = resource if client_id
    resource.from_sfa(resource_el, context, type)
    #puts "22>>> #{resource.to_hash}"
    return
  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)

    ????



86
87
88
89
90
91
92
# File 'lib/omf-sfa/resource/sfa_base.rb', line 86

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, options = {}) ⇒ Object



51
52
53
54
55
# File 'lib/omf-sfa/resource/sfa_base.rb', line 51

def sfa_add_namespace(prefix, urn, options = {})
  options[:urn] = urn
  @@sfa_namespaces[prefix] = options
  @@sfa_namespace2prefix[urn] = prefix
end

#sfa_add_namespaces_to_document(doc) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/omf-sfa/resource/sfa_base.rb', line 61

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

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/omf-sfa/resource/sfa_base.rb', line 256

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



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

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



252
253
254
# File 'lib/omf-sfa/resource/sfa_base.rb', line 252

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/omf-sfa/resource/sfa_base.rb', line 236

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

#sfa_suppress_idObject



69
70
71
# File 'lib/omf-sfa/resource/sfa_base.rb', line 69

def sfa_suppress_id
  @@sfa_suppress_id[self] = true
end

#sfa_suppress_id?Boolean

Returns:

  • (Boolean)


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

def sfa_suppress_id?
  @@sfa_suppress_id[self] == true
end

#to_rspec(resources, type, opts = {}) ⇒ Object

opts:

:valid_for - valid [sec] from now


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
# File 'lib/omf-sfa/resource/sfa_base.rb', line 97

def to_rspec(resources, type, 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.set_attribute('type', type)
  now = Time.now
  root.set_attribute('generated', now.iso8601)

  case opts[:type] = type = type.to_sym
  when :manifest
    schema = 'manifest.xsd'
  when :advertisement
    schema = 'ad.xsd'
    root.set_attribute('expires', (now + (opts[:valid_for] || 600)).iso8601)
  when :request
    schema = 'request.xsd'
  else
    raise "Unnown Rspec type '#{type}'"
  end

  root.add_namespace(nil, SFA_NAMESPACE_URI)
  root.add_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
  #root['xsi:schemaLocation'] = "#{SFA_NAMESPACE_URI} #{SFA_NAMESPACE_URI}/#{schema} #{@@sfa_namespaces[:ol]} #{@@sfa_namespaces[:ol]}/ad-reservation.xsd"
  @@sfa_namespaces.each do |prefix, opts|
    root.add_namespace(prefix.to_s, opts[:urn])
  end

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