From: marios <[email protected]>

Signed-off-by: marios <[email protected]>
---
 server/lib/cimi/model/vsp.rb                       |   39 ++++++++++++++++++++
 server/lib/cimi/server.rb                          |   24 ++++++++++++
 server/lib/deltacloud/drivers/mock/mock_client.rb  |    3 +-
 .../drivers/mock/mock_driver_cimi_methods.rb       |   28 ++++++++++++++
 4 files changed, 93 insertions(+), 1 deletions(-)

diff --git a/server/lib/cimi/model/vsp.rb b/server/lib/cimi/model/vsp.rb
index 33e5d5b..9507dd5 100644
--- a/server/lib/cimi/model/vsp.rb
+++ b/server/lib/cimi/model/vsp.rb
@@ -47,4 +47,43 @@ class CIMI::Model::VSP < CIMI::Model::Base
     end
   end
 
+  def self.create(request_body, context, type)
+    input = (type == :xml)? XmlSimple.xml_in(request_body, 
{"ForceArray"=>false, "NormaliseSpace"=>2}) : JSON.parse(request_body)
+    if input["vspTemplate"]["href"] #template by reference
+      vsp_config, network = get_by_reference(input, context)
+    else
+      if input["vspTemplate"]["vspConfig"]["href"] # configuration by reference
+        vsp_config = 
VSPConfiguration.find(context.href_id(input["vspTemplate"]["vspConfig"]["href"],:vsp_configurations),
 context)
+      else #configuration by value
+        vsp_config = get_by_value(request_body, type)
+      end
+      network = 
Network.find(context.href_id(input["vspTemplate"]["network"]["href"], 
:networks), context)
+    end
+    params = {:vsp_config => vsp_config, :network => network, 
:name=>input["name"], :description=>input["description"], :env=>context}
+    raise CIMI::Model::BadRequest.new("Bad request - missing required 
parameters. Client sent: #{request_body} which produced #{params.inspect}")  if 
params.has_value?(nil)
+    context.driver.create_vsp(context.credentials, params)
+  end
+
+  def self.delete!(id, context)
+    context.driver.delete_vsp(context.credentials, id)
+  end
+
+  private
+
+  def self.get_by_reference(input, context)
+    vsp_template = 
VSPTemplate.find(context.href_id(input["vspTemplate"]["href"], :vsp_templates), 
context)
+    vsp_config = 
VSPConfiguration.find(context.href_id(vsp_template.vsp_config.href, 
:vsp_configurations), context)
+    network = Network.find(context.href_id(vsp_template.network.href, 
:networks), context)
+    return vsp_config, network
+  end
+
+  def self.get_by_value(request_body, type)
+    if type == :xml
+      xml_arrays = XmlSimple.xml_in(request_body, {"NormaliseSpace"=>2})
+      vsp_config = 
VSPConfiguration.from_xml(XmlSimple.xml_out(xml_arrays["vspTemplate"][0]["vspConfig"][0]))
+    else
+     json = JSON.parse(request_body)
+      vsp_config = 
VSPConfiguration.from_json(JSON.generate(json["vspTemplate"]["vspConfig"]))
+    end
+  end
 end
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 2891df6..63523ba 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -750,6 +750,30 @@ global_collection :vsps do
     end
   end
 
+  operation :create do
+    description "Create a new VSP"
+    control do
+      if request.content_type.end_with?("json")
+        vsp = CIMI::Model::VSP.create(request.body.read, self, :json)
+      else
+        vsp = CIMI::Model::VSP.create(request.body.read, self, :xml)
+      end
+      respond_to do |format|
+        format.xml { vsp.to_xml }
+        format.json { vsp.to_json }
+      end
+    end
+  end
+
+  operation :destroy do
+    description "Delete a specified VSP"
+    param :id, :string, :required
+    control do
+      CIMI::Model::VSP.delete!(params[:id], self)
+      no_content_with_status(200)
+    end
+  end
+
 end
 
 global_collection :vsp_configurations do
diff --git a/server/lib/deltacloud/drivers/mock/mock_client.rb 
b/server/lib/deltacloud/drivers/mock/mock_client.rb
index 3de8254..94c56af 100644
--- a/server/lib/deltacloud/drivers/mock/mock_client.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_client.rb
@@ -98,7 +98,8 @@ module Deltacloud::Drivers::Mock
 
     def destroy_cimi(collection, id)
       fname = cimi_file(collection, id)
-      FileUtils.rm(fname) if File::exists?(fname)
+      raise "No such object: #{id} in #{collection} collection" unless 
File::exists?(fname)
+      FileUtils.rm(fname)
     end
 
     def load_all_cimi(model_name)
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb 
b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
index 28b74a0..fdd501a 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -130,6 +130,33 @@ module Deltacloud::Drivers::Mock
       end
     end
 
+    def create_vsp(credentials, opts={})
+      check_credentials(credentials)
+      id = "#{opts[:env].send("vsps_url")}/#{opts[:name]}"
+      vsp_hash = { "id"    => id,
+                    "name"  => opts[:name],
+                    "description" => opts[:description],
+                    "state" => "STARTED",
+                    "created" => Time.now,
+                    
"bandwidthReservation"=>opts[:vsp_config].bandwidth_reservation,
+                    "trafficPriority"=>opts[:vsp_config].traffic_priority,
+                    "maxTrafficDelay"=>opts[:vsp_config].max_traffic_delay,
+                    "maxTrafficLoss"=>opts[:vsp_config].max_traffic_loss,
+                    "maxTrafficJitter"=>opts[:vsp_config].max_traffic_jitter,
+                    "network" => {"href" => opts[:network].id},
+                    "operations" => [{"rel"=>"edit", "href"=> id},
+                                     {"rel"=>"delete", "href"=> id}]
+                   }
+      vsp = CIMI::Model::VSP.from_json(JSON.generate(vsp_hash))
+      @client.store_cimi(:vsp, vsp)
+      vsp
+    end
+
+    def delete_vsp(credentials, id)
+      check_credentials(credentials)
+      @client.destroy_cimi(:vsp, id)
+    end
+
     def vsp_configurations(credentials, opts={})
       check_credentials(credentials)
       if opts[:id].nil?
@@ -169,6 +196,7 @@ module Deltacloud::Drivers::Mock
       addr_hash = { "id"    => id,
                     "name"  => opts[:name],
                     "description" => opts[:description],
+                    "created" => Time.now,
                     "hostName" => opts[:address_template].hostname,
                     "allocation" => opts[:address_template].allocation,
                     "defaultGateway" => 
opts[:address_template].default_gateway,
-- 
1.7.6.5

Reply via email to