Class: CPEE::NewInstance

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee/implementation.rb

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#path(e) ⇒ Object

{{{



281
282
283
284
285
286
287
288
# File 'lib/cpee/implementation.rb', line 281

def path(e)
  ret = []
  until e.qname.name == 'properties'
    ret << (e.class == XML::Smart::Dom::Attribute ? '@' : '') + e.qname.name
    e = e.parent
  end
  File.join(*ret.reverse)
end

#responseObject



290
291
292
293
294
295
296
297
298
299
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
# File 'lib/cpee/implementation.rb', line 290

def response
  opts = @a[0]
  redis = opts[:redis]
  doc = XML::Smart::open_unprotected(opts[:properties_init])
  doc.register_namespace 'p', 'http://cpee.org/ns/properties/2.0'
  name     = @p[0].value
  id       = CPEE::Persistence::new_object(opts)
  uuid     = SecureRandom.uuid
  instance = CPEE::Persistence::obj + ':' + id.to_s
  redis.multi do |multi|
    multi.zadd('instances',id,id)
    doc.root.find(PROPERTIES_PATHS_FULL.join(' | ')).each do |e|
      if e.class == XML::Smart::Dom::Element && e.element_only?
        val = e.find('*').map { |f| f.dump }.join
        multi.set(File.join(instance, path(e)), val)
      else
        multi.set(File.join(instance, path(e)), e.text)
      end
    end
    doc.root.find(PROPERTIES_PATHS_INDEX_UNORDERED.join(' | ')).each do |e|
      p = path(e)
      multi.sadd(File.join(instance, File.dirname(p)), File.basename(p))
    end
    doc.root.find(PROPERTIES_PATHS_INDEX_ORDERED.join(' | ')).each_with_index do |e,i|
      p = path(e)
      multi.zadd(File.join(instance, File.dirname(p)), i, File.basename(p))
    end
    Dir[File.join(opts[:notifications_init],'*','subscription.xml')].each do |f|
      XML::Smart::open_unprotected(f) do |doc|
        doc.register_namespace 'np', 'http://riddl.org/ns/common-patterns/notifications-producer/2.0'
        key = File.basename(File.dirname(f))
        url = doc.find('string(/np:subscription/@url)')
        multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers",key)
        multi.set(CPEE::Persistence::obj + ":#{id}/handlers/#{key}/url",url)
        doc.find('/np:subscription/np:topic/*').each do |e|
          c = File.join(e.parent.attributes['id'],e.qname.name,e.text)
          multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers/#{key}",c)
          multi.sadd(CPEE::Persistence::obj + ":#{id}/handlers/#{c}",key)
        end
      end rescue nil # all the ones that are not ok, are ignored
    end
    multi.set(File.join(instance, 'attributes', 'uuid'), uuid)
    multi.zadd(File.join(instance, 'attributes'), -2, 'uuid')
    multi.set(File.join(instance, 'attributes', 'info'), name)
    multi.zadd(File.join(instance, 'attributes'), -1, 'info')
    multi.set(File.join(instance, 'state', '@changed'), Time.now.xmlschema(3))
  end

  content = {
    :state => 'ready',
    :attributes => CPEE::Persistence::extract_list(id,opts,'attributes').to_h
  }
  CPEE::Message::send(:event,'state/change',File.join(opts[:url],'/'),id,uuid,name,content,redis)

  @headers << Riddl::Header.new("CPEE-INSTANCE", id.to_s)
  @headers << Riddl::Header.new("CPEE-INSTANCE-URL", File.join(opts[:url].to_s,id.to_s,'/'))
  @headers << Riddl::Header.new("CPEE-INSTANCE-UUID", uuid)

  Riddl::Parameter::Simple.new("id", id.to_s)
end