deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@redhat.com
Subject [PATCH 3/7] CIMI: Adds NetworkPorts (used to be VSP)
Date Fri, 14 Sep 2012 15:42:16 GMT
From: marios <marios@redhat.com>


Signed-off-by: marios <marios@redhat.com>
---
 server/lib/cimi/collections/network_ports.rb       | 109 +++++++++++++++++++++
 server/lib/cimi/models/network_port.rb             |  96 ++++++++++++++++++
 server/lib/cimi/models/network_port_collection.rb  |  51 ++++++++++
 .../mock/data/cimi/network_port/network_port1.json |  13 +++
 .../mock/data/cimi/network_port/network_port2.json |  13 +++
 .../mock/data/cimi/network_port/network_port3.json |  15 +++
 .../mock/data/cimi/network_port/network_port4.json |  15 +++
 7 files changed, 312 insertions(+)
 create mode 100644 server/lib/cimi/collections/network_ports.rb
 create mode 100644 server/lib/cimi/models/network_port.rb
 create mode 100644 server/lib/cimi/models/network_port_collection.rb
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port1.json
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port2.json
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port3.json
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port4.json

diff --git a/server/lib/cimi/collections/network_ports.rb b/server/lib/cimi/collections/network_ports.rb
new file mode 100644
index 0000000..ef6e240
--- /dev/null
+++ b/server/lib/cimi/collections/network_ports.rb
@@ -0,0 +1,109 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module CIMI::Collections
+  class NetworkPorts < Base
+
+    set :capability, lambda { |m| driver.respond_to? m }
+
+    collection :network_ports do
+
+      description 'A NetworkPort is a realized connection point between a Network and a resource
- such as a Machine.'
+
+      operation :index, :with_capability => :network_ports do
+        description 'List all NetworkPorts in the NetworkPortCollection'
+        param :CIMISelect, :string, :optional
+        control do
+          network_ports = NetworkPortCollection.default(self).filter_by(params[:CIMISelect])
+          respond_to do |format|
+            format.xml {network_ports.to_xml}
+            format.json {network_ports.to_json}
+          end
+        end
+      end
+
+      operation :show, :with_capability => :network_ports do
+        description 'Show a specific NetworkPort'
+        control do
+          network_port = NetworkPort.find(params[:id], self)
+          respond_to do |format|
+            format.xml {network_port.to_xml}
+            format.json {network_port.to_json}
+          end
+        end
+      end
+
+      operation :create, :with_capability => :create_network_port do
+        description "Create a new NetworkPort"
+        control do
+          if request.content_type.end_with?("json")
+            network_port = CIMI::Model::NetworkPort.create(request.body.read, self, :json)
+          else
+            network_port = CIMI::Model::NetworkPort.create(request.body.read, self, :xml)
+          end
+          respond_to do |format|
+            format.xml { network_port.to_xml }
+            format.json { network_port.to_json }
+          end
+        end
+      end
+
+      operation :destroy, :with_capability => :delete_network_port do
+        description "Delete a specified NetworkPort"
+        control do
+          CIMI::Model::NetworkPort.delete!(params[:id], self)
+          no_content_with_status(200)
+        end
+      end
+
+      action :start, :with_capability => :start_network_port do
+        description "Start specific NetworkPort."
+        param :id,          :string,    :required
+        control do
+          network_port = NetworkPort.find(params[:id], self)
+          report_error(404) unless network_port
+          if request.content_type.end_with?("json")
+            action = Action.from_json(request.body.read)
+          else
+            action = Action.from_xml(request.body.read)
+          end
+          network_port.perform(action, self) do |operation|
+            no_content_with_status(202) if operation.success?
+            # Handle errors using operation.failure?
+          end
+        end
+      end
+
+      action :stop, :with_capability => :stop_network_port do
+        description "Stop specific NetworkPort."
+        control do
+          network_port = NetworkPort.find(params[:id], self)
+          report_error(404) unless network_port
+          if request.content_type.end_with?("json")
+            action = Action.from_json(request.body.read)
+          else
+            action = Action.from_xml(request.body.read)
+          end
+          network_port.perform(action, self) do |operation|
+            no_content_with_status(202) if operation.success?
+            # Handle errors using operation.failure?
+          end
+        end
+      end
+
+    end
+
+  end
+end
diff --git a/server/lib/cimi/models/network_port.rb b/server/lib/cimi/models/network_port.rb
new file mode 100644
index 0000000..92415ca
--- /dev/null
+++ b/server/lib/cimi/models/network_port.rb
@@ -0,0 +1,96 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+class CIMI::Model::NetworkPort < CIMI::Model::Base
+
+  text :state
+
+  href :network
+
+  text :port_type
+
+  text :class_of_service
+
+  href :event_log
+
+  array :meters do
+    scalar :href
+  end
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.find(id, context)
+    if id==:all
+      context.driver.network_ports(context.credentials, {:env=>context})
+    else
+      context.driver.network_ports(context.credentials, {:id=>id, :env=>context})
+    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 = CIMI::Model::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 = CIMI::Model::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
+
+  def perform(action, context, &block)
+    begin
+      if context.driver.send(:"#{action.name}_vsp", context.credentials, self.name)
+        block.callback :success
+      else
+        raise "Operation #{action.name} failed to execute on the VSP #{self.name} "
+      end
+    rescue => e
+      block.callback :failure, e.message
+    end
+  end
+
+
+  private
+
+  def self.get_by_reference(input, context)
+    vsp_template = CIMI::Model::VSPTemplate.find(context.href_id(input["vspTemplate"]["href"],
:vsp_templates), context)
+    vsp_config = CIMI::Model::VSPConfiguration.find(context.href_id(vsp_template.vsp_config.href,
:vsp_configurations), context)
+    network = CIMI::Model::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 = CIMI::Model::VSPConfiguration.from_xml(XmlSimple.xml_out(xml_arrays["vspTemplate"][0]["vspConfig"][0]))
+    else
+     json = JSON.parse(request_body)
+      vsp_config = CIMI::Model::VSPConfiguration.from_json(JSON.generate(json["vspTemplate"]["vspConfig"]))
+    end
+  end
+end
diff --git a/server/lib/cimi/models/network_port_collection.rb b/server/lib/cimi/models/network_port_collection.rb
new file mode 100644
index 0000000..32642cf
--- /dev/null
+++ b/server/lib/cimi/models/network_port_collection.rb
@@ -0,0 +1,51 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+class CIMI::Model::NetworkPortCollection < CIMI::Model::Base
+
+  act_as_root_entity :network_port
+
+  text :count
+
+  #add member array:
+  self << CIMI::Model::NetworkPort
+
+  def self.default(context)
+    network_ports = CIMI::Model::NetworkPort.all(context)
+    self.new(
+      :id => context.network_ports_url,
+      :name => 'default',
+      :created => Time.now,
+      :description => "#{context.driver.name.capitalize} NetworkPortCollection",
+      :count => network_ports.size,
+      :network_ports => network_ports
+    )
+  end
+
+  def self.for_network(network_id, context)
+    net_url = context.network_url(network_id)
+    network_ports = CIMI::Model::NetworkPort.all(context)
+    ports_collection = network_ports.inject([]){|res, current| res << current if current.network.href
== net_url ; res}
+    self.new(
+      :id => net_url+"/network_ports",
+      :name => 'default',
+      :created => Time.now,
+      :description => "#{context.driver.name.capitalize} NetworkPortCollection",
+      :count => ports_collection.size,
+      :network_ports => ports_collection
+    )
+  end
+
+end
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port1.json
b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port1.json
new file mode 100644
index 0000000..b59463d
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port1.json
@@ -0,0 +1,13 @@
+{ "id": "http://cimi.example.org/network_ports/network_port1",
+  "name": "network_port1",
+  "description": "a mock switchport",
+  "created": "Fri Mar 16 17:06:41 EET 2012",
+  "state": "STARTED",
+  "network": {"href": "http://cimi.example.org/networks/network1"},
+  "portType": "ACCESS",
+  "classOfService": "GOLD",
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_ports/network_port1" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_ports/network_port1" }
+  ]
+}
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port2.json
b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port2.json
new file mode 100644
index 0000000..80a933f
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port2.json
@@ -0,0 +1,13 @@
+{ "id": "http://cimi.example.org/network_ports/network_port2",
+  "name": "network_port2",
+  "description": "a mock switchport",
+  "created": "Fri Mar 16 17:09:27 EET 2012",
+  "state": "STARTED",
+  "network": {"href": "http://cimi.example.org/networks/network1"},
+  "portType": "ACCESS",
+  "classOfService": "GOLD",
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_ports/network_port2" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_ports/network_port2" }
+  ]
+}
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port3.json
b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port3.json
new file mode 100644
index 0000000..c62f62b
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port3.json
@@ -0,0 +1,15 @@
+{ "id": "http://cimi.example.org/network_ports/network_port3",
+  "name": "network_port3",
+  "description": "a mock switchport",
+  "created": "Fri Mar 16 17:10:36 EET 2012",
+  "state": "STARTED",
+  "network": {"href": "http://cimi.example.org/networks/network2"},
+  "portType": "ACCESS",
+  "classOfService": "GOLD",
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_ports/network_port3" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_ports/network_port3" }
+  ]
+}
+
+
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port4.json
b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port4.json
new file mode 100644
index 0000000..d64b87e
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_port/network_port4.json
@@ -0,0 +1,15 @@
+{ "id": "http://cimi.example.org/network_ports/network_port4",
+  "name": "network_port4",
+  "description": "a mock switchport",
+  "created": "Fri Mar 16 17:10:36 EET 2012",
+  "state": "STARTED",
+  "network": {"href": "http://cimi.example.org/networks/network2"},
+  "portType": "ACCESS",
+  "classOfService": "GOLD",
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_ports/network_port4" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_ports/network_port4" }
+  ]
+}
+
+
-- 
1.7.11.4


Mime
View raw message