lib/cpee/instantiation.rb in cpee-1.4.18 vs lib/cpee/instantiation.rb in cpee-1.4.20

- old
+ new

@@ -17,19 +17,21 @@ require 'xml/smart' require 'base64' require 'uri' require 'redis' require 'json' +require ::File.dirname(__FILE__) + '/value_helper' module CPEE module Instantiation SERVER = File.expand_path(__dir__ + '/../instantiation.xml') module Helpers #{{{ def load_testset(tdoc,cpee,name=nil) #{{{ ins = -1 + uuid = nil XML::Smart.string(tdoc) do |doc| doc.register_namespace 'desc', 'http://cpee.org/ns/description/1.0' doc.register_namespace 'prop', 'http://riddl.org/ns/common-patterns/properties/1.0' srv = Riddl::Client.new(cpee, cpee + "?riddl-description") @@ -38,14 +40,15 @@ doc.find("/testset/attributes/prop:info").each do |e| e.text = name end end - status, response = res.post Riddl::Parameter::Simple.new("info",doc.find("string(/testset/attributes/prop:info)")) + status, response, headers = res.post Riddl::Parameter::Simple.new("info",doc.find("string(/testset/attributes/prop:info)")) if status == 200 ins = response.first.value + uuid = headers['CPEE_INSTANCE_UUID'] params = [] res = srv.resource("/#{ins}/properties/values") ["handlerwrapper","positions","dataelements","endpoints","attributes","transformation"].each do |item| if doc.find("/testset/#{item}").any? @@ -74,14 +77,14 @@ pp body end end end end - return ins + [ins, uuid] end #}}} private :load_testset - def handle_waiting(cpee,instance,behavior,selfurl,cblist) #{{{ + def handle_waiting(cpee,instance,uuid,behavior,selfurl,cblist) #{{{ if behavior =~ /^wait/ condition = behavior.match(/_([^_]+)_/)&.[](1) || 'finished' @headers << Riddl::Header.new('CPEE-CALLBACK','true') cb = @h['CPEE_CALLBACK'] @@ -94,10 +97,11 @@ Riddl::Parameter::Simple.new("events","change") ] cblist.rpush(cbk, cb) cblist.rpush(cbk, condition) cblist.rpush(cbk, instance) + cblist.rpush(cbk, uuid) cblist.rpush(cbk, File.join(cpee,instance)) end end end #}}} private :handle_waiting @@ -110,22 +114,22 @@ Riddl::Parameter::Simple.new('value','running') ] end end #}}} private :handle_starting - def handle_data(cpee,instance,data) + def handle_data(cpee,instance,data) #{{{ if data srv = Riddl::Client.new(cpee, cpee + "?riddl-description") JSON::parse(data).each do |k,v| res = srv.resource("/#{instance}/properties/values/dataelements/#{k}") status, response = res.put [ Riddl::Parameter::Simple.new('value',v) ] sleep 0.42 end rescue nil end - end + end #}}} end #}}} class InstantiateUrl < Riddl::Implementation #{{{ include Helpers @@ -138,21 +142,21 @@ res[0].value.read else (@status = 500) && return end - if (instance = load_testset(tdoc,cpee,@p[0].value)) == -1 + if (instance, uuid = load_testset(tdoc,cpee,@p[0].value)).first == -1 @status = 500 else @headers << Riddl::Header.new('CPEE-INSTANTIATION',File.join(cpee,instance)) handle_data cpee, instance, @p[3]&.value - handle_waiting cpee, instance, @p[1].value, selfurl, cblist + handle_waiting cpee, instance, uuid, @p[1].value, selfurl, cblist handle_starting cpee, instance, @p[1].value return Riddl::Parameter::Simple.new("url",cpee + instance) end end - end #}}} + end #}}} class InstantiateXML < Riddl::Implementation #{{{ include Helpers def response @@ -165,16 +169,16 @@ Base64.decode64(@p[data].value.read) else @p[data].value.read end - if (instance = load_testset(tdoc,cpee)) == -1 + if (instance, uuid = load_testset(tdoc,cpee)).first == -1 @status = 500 else @headers << Riddl::Header.new('CPEE-INSTANTIATION',File.join(cpee,instance)) handle_data cpee, instance, @p[data+1]&.value - handle_waiting cpee, instance, behavior, selfurl, cblist + handle_waiting cpee, instance, uuid, behavior, selfurl, cblist handle_starting cpee, instance, behavior return Riddl::Parameter::Simple.new("url",cpee + instance) end end end #}}} @@ -186,14 +190,21 @@ cpee = @a[0] selfurl = @a[1] cblist = @a[2] instance = @p[1].value - handle_data cpee, instance, @p[2]&.value - handle_waiting cpee, instance, @p[0].value, selfurl, cblist - handle_starting cpee, instance, @p[0].value - return Riddl::Parameter::Simple.new("url",cpee + instance) + srv = Riddl::Client.new(cpee, cpee + "?riddl-description") + res = srv.resource("/#{instance}/properties/values/attributes/uuid") + status, response = res.get + + if status >= 200 && status < 300 + uuid = XML::Smart::string(response.first.value).root.text + handle_data cpee, instance, @p[2]&.value + handle_waiting cpee, instance, uuid, @p[0].value, selfurl, cblist + handle_starting cpee, instance, @p[0].value + return Riddl::Parameter::Simple.new("url",cpee + instance) + end end end #}}} class ContinueTask < Riddl::Implementation #{{{ def response @@ -202,24 +213,36 @@ topic = @p[1].value event_name = @p[2].value notification = JSON.parse(@p[3].value) key = @r.last - cb, condition, instance, instance_url = cblist.lrange(key,0,-1) + cb, condition, instance, uuid, instance_url = cblist.lrange(key,0,-1) + + send = { + 'CPEE-INSTANCE' => instance, + 'CPEE-INSTANCE-URL' => instance_url, + 'CPEE-INSTANCE-UUID' => uuid, + 'CPEE-STATE' => notification['state'] + } + if notification['state'] == condition cblist.del(key) srv = Riddl::Client.new(cpee, cpee + "?riddl-description") res = srv.resource("/#{instance}/properties/values/dataelements") status, response = res.get - send = { 'CPEE-INSTANCE' => instance_url } if status >= 200 && status < 300 doc = XML::Smart.string(response[0].value.read) doc.register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0' doc.find('/p:value/*').each do |e| - send[e.qname.name] = e.text + send[e.qname.name] = CPEE::ValueHelper::parse(e.text) end end Riddl::Client.new(cb).put [ + Riddl::Parameter::Complex.new('dataelements','application/json',JSON::generate(send)) + ] + else + Riddl::Client.new(cb).put [ + Riddl::Header.new('CPEE-UPDATE','true'), Riddl::Parameter::Complex.new('dataelements','application/json',JSON::generate(send)) ] end end end #}}}