summaryrefslogtreecommitdiffstats
path: root/share/newton_base
diff options
context:
space:
mode:
Diffstat (limited to 'share/newton_base')
-rw-r--r--share/newton_base/openoapi/__init__.py10
-rw-r--r--share/newton_base/openoapi/flavor.py345
-rw-r--r--share/newton_base/openoapi/hosts.py85
-rw-r--r--share/newton_base/openoapi/image.py235
-rw-r--r--share/newton_base/openoapi/limits.py82
-rw-r--r--share/newton_base/openoapi/network.py171
-rw-r--r--share/newton_base/openoapi/server.py426
-rw-r--r--share/newton_base/openoapi/subnet.py169
-rw-r--r--share/newton_base/openoapi/tenants.py85
-rw-r--r--share/newton_base/openoapi/volume.py168
-rw-r--r--share/newton_base/openoapi/vport.py202
-rw-r--r--share/newton_base/util.py149
12 files changed, 2127 insertions, 0 deletions
diff --git a/share/newton_base/openoapi/__init__.py b/share/newton_base/openoapi/__init__.py
new file mode 100644
index 00000000..802f3fba
--- /dev/null
+++ b/share/newton_base/openoapi/__init__.py
@@ -0,0 +1,10 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
diff --git a/share/newton_base/openoapi/flavor.py b/share/newton_base/openoapi/flavor.py
new file mode 100644
index 00000000..27bbb821
--- /dev/null
+++ b/share/newton_base/openoapi/flavor.py
@@ -0,0 +1,345 @@
+# Copyright (c) 2017-2018 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Flavors(APIView):
+ service = {'service_type': 'compute',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("project_id", "tenantId"),
+ ("ram", "memory"),
+ ("vcpus", "vcpu"),
+ ("OS-FLV-EXT-DATA:ephemeral", "ephemeral"),
+ ("os-flavor-access:is_public", "isPublic"),
+ ("extra_specs", "extraSpecs"),
+ ]
+
+ def _convert_extra_specs(self, extraSpecs, extra_specs, reverse=False):
+ if reverse == False:
+ #from extraSpecs to extra_specs
+ for spec in extraSpecs:
+ extra_specs[spec['keyName']] = spec['value']
+ else:
+ for k,v in extra_specs.items():
+ spec={}
+ spec['keyName']=k
+ spec['value']=v
+ extraSpecs.append(spec)
+
+ def get(self, request, vimid="", tenantid="", flavorid=""):
+ logger.debug("Flavors--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = self._get_flavor(sess, request, flavorid)
+ content = resp.json()
+
+ if flavorid:
+ flavor = content.pop("flavor", None)
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
+ extraContent = extraResp.json()
+ if extraContent["extra_specs"]:
+ extraSpecs = []
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
+ flavor["extraSpecs"] = extraSpecs
+ VimDriverUtils.replace_key_by_mapping(flavor,
+ self.keys_mapping)
+ content.update(flavor)
+
+ else:
+ wanted = None
+ #check if query contains name="flavorname"
+ if query:
+ for queryone in query.split('&'):
+ k,v = queryone.split('=')
+ if k == "name":
+ wanted = v
+ break
+
+ if wanted:
+ oldFlavors = content.pop("flavors", None)
+ content["flavors"] = []
+ for flavor in oldFlavors:
+ if wanted == flavor["name"]:
+ content["flavors"].append(flavor)
+
+ #iterate each flavor to get extra_specs
+ for flavor in content["flavors"]:
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
+ extraContent = extraResp.json()
+ if extraContent["extra_specs"]:
+ extraSpecs = []
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
+ flavor["extraSpecs"] = extraSpecs
+ VimDriverUtils.replace_key_by_mapping(flavor,
+ self.keys_mapping)
+
+ #add extra keys
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+
+ return Response(data=content, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def _get_flavor_extra_specs(self, sess, flavorid):
+ if flavorid:
+ logger.debug("Flavors--get_extra_specs::> %s" % flavorid)
+ # prepare request resource to vim instance
+ req_resouce = "/flavors/%s/os-extra_specs" % flavorid
+
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ return resp
+ return {}
+
+ def _get_flavor(self, sess, request, flavorid=""):
+ logger.debug("Flavors--get basic")
+ if sess:
+ # prepare request resource to vim instance
+ req_resouce = "/flavors"
+ if flavorid:
+ req_resouce += "/%s" % flavorid
+ else:
+ req_resouce += "/detail"
+
+ query = VimDriverUtils.get_query_part(request)
+ if query:
+ req_resouce += "?%s" % query
+
+ return sess.get(req_resouce, endpoint_filter=self.service)
+ return {}
+
+ def post(self, request, vimid="", tenantid="", flavorid=""):
+ logger.debug("Flavors--post::> %s" % request.data)
+ sess = None
+ resp = None
+ resp_body = None
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #check if the flavor is already created: name or id
+ tmpresp = self._get_flavor(sess, request)
+ content = tmpresp.json()
+ #iterate each flavor to get extra_specs
+ existed = False
+ for flavor in content["flavors"]:
+ if flavor["name"] == request.data["name"]:
+ existed = True
+ break
+ elif hasattr(request.data, "id") and flavor["id"] == request.data["id"]:
+ existed = True
+ break
+
+ if existed:
+ extraResp = self._get_flavor_extra_specs(sess, flavor["id"])
+ extraContent = extraResp.json()
+ if extraContent["extra_specs"]:
+ extraSpecs = []
+ self._convert_extra_specs(extraSpecs, extraContent["extra_specs"], True)
+ flavor["extraSpecs"] = extraSpecs
+ VimDriverUtils.replace_key_by_mapping(flavor,
+ self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 0,
+ }
+ flavor.update(vim_dict)
+ return Response(data=flavor, status=tmpresp.status_code)
+
+ extraSpecs = request.data.pop("extraSpecs", None)
+ #create flavor first
+ resp = self._create_flavor(sess, request)
+ if resp.status_code == 202:
+ resp_body = resp.json()["flavor"]
+ else:
+ return resp
+
+
+ flavorid = resp_body['id']
+ if extraSpecs:
+ extra_specs={}
+ self._convert_extra_specs(extraSpecs, extra_specs, False)
+# logger.debug("extraSpecs:%s" % extraSpecs)
+# logger.debug("extra_specs:%s" % extra_specs)
+ extraResp = self._create_flavor_extra_specs(sess, extra_specs, flavorid)
+ if extraResp.status_code == 200:
+ #combine the response body and return
+ tmpSpecs = []
+ tmpRespBody = extraResp.json()
+ self._convert_extra_specs(tmpSpecs, tmpRespBody['extra_specs'], True)
+
+ resp_body.update({"extraSpecs":tmpSpecs})
+ else:
+ #rollback
+ self._delete_flavor(self, request, vimid, tenantid, flavorid)
+ return extraResp
+
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ if sess and resp and resp.status_code == 200:
+ self._delete_flavor(sess, flavorid)
+
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+
+ if sess and resp and resp.status_code == 200:
+ self._delete_flavor(sess, flavorid)
+
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def _create_flavor(self, sess, request):
+ logger.debug("Flavors--create::> %s" % request.data)
+ # prepare request resource to vim instance
+ req_resouce = "/flavors"
+
+ flavor = request.data
+
+ VimDriverUtils.replace_key_by_mapping(flavor,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"flavor": flavor})
+ return sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+
+ def _create_flavor_extra_specs(self, sess, extraspecs, flavorid):
+ logger.debug("Flavors extra_specs--post::> %s" % extraspecs)
+ # prepare request resource to vim instance
+ req_resouce = "/flavors"
+ if flavorid:
+ req_resouce += "/%s/os-extra_specs" % flavorid
+ else:
+ raise VimDriverNewtonException(message="VIM newton exception",
+ content="internal bug in creating flavor extra specs",
+ status_code=500)
+
+ req_body = json.JSONEncoder().encode({"extra_specs": extraspecs})
+
+ return sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+
+ def delete(self, request, vimid="", tenantid="", flavorid=""):
+ logger.debug("Flavors--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #delete extra specs one by one
+ resp = self._delete_flavor_extra_specs(sess, flavorid)
+
+ #delete flavor
+ resp = self._delete_flavor(sess, flavorid)
+
+ #return results
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def _delete_flavor_extra_specs(self, sess, flavorid):
+ logger.debug("Flavors--delete extra::> %s" % flavorid)
+
+ #delete extra specs one by one
+ resp = self._get_flavor_extra_specs(sess, flavorid)
+ extra_specs = resp.json()
+ if extra_specs and extra_specs["extra_specs"]:
+ for k, _ in extra_specs["extra_specs"].items():
+ # just try to delete extra spec, but do not care if succeeded
+ self._delete_flavor_one_extra_spec(sess, flavorid, k)
+ return resp
+
+ def _delete_flavor_one_extra_spec(self, sess, flavorid, extra_spec_key):
+ logger.debug("Flavors--delete 1 extra::> %s" % extra_spec_key)
+ # prepare request resource to vim instance
+ try:
+ req_resouce = "/flavors"
+ if flavorid and extra_spec_key:
+ req_resouce += "/%s" % flavorid
+ req_resouce += "/os-extra_specs/%s" % extra_spec_key
+ else:
+ raise VimDriverNewtonException(message="VIM newton exception",
+ content="internal bug in deleting flavor extra specs: %s" % extra_spec_key,
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ return sess.delete(req_resouce, endpoint_filter=self.service)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def _delete_flavor(self, sess, flavorid):
+ logger.debug("Flavors--delete basic::> %s" % flavorid)
+ # prepare request resource to vim instance
+ req_resouce = "/flavors"
+ if flavorid:
+ req_resouce += "/%s" % flavorid
+ else:
+ raise VimDriverNewtonException(message="VIM newton exception",
+ content="internal bug in deleting flavor",
+ status_code=500)
+
+ return sess.delete(req_resouce, endpoint_filter=self.service)
diff --git a/share/newton_base/openoapi/hosts.py b/share/newton_base/openoapi/hosts.py
new file mode 100644
index 00000000..720ba8e2
--- /dev/null
+++ b/share/newton_base/openoapi/hosts.py
@@ -0,0 +1,85 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Hosts(APIView):
+ service = {'service_type': 'compute',
+ 'interface': 'public'}
+
+ hosts_keys_mapping = [
+ ("host_name", "name"),
+ ]
+ host_keys_mapping = [
+ ("host", "name"),
+ ]
+
+ def get(self, request, vimid="", tenantid="", hostname=""):
+ logger.debug("Hosts--get::> %s" % request.data)
+ try:
+ #prepare request resource to vim instance
+ req_resouce = "/os-hosts"
+ if hostname:
+ req_resouce += "/%s" % hostname
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+
+ if not hostname:
+ # convert the key naming in hosts
+ for host in content["hosts"]:
+ VimDriverUtils.replace_key_by_mapping(host,
+ self.hosts_keys_mapping)
+ else:
+ #restructure host data model
+ old_host = content["host"]
+ content["host"] = []
+ # convert the key naming in resources
+ for res in old_host:
+ VimDriverUtils.replace_key_by_mapping(res['resource'],
+ self.host_keys_mapping)
+ content["host"].append(res['resource'])
+
+ return Response(data=content, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
diff --git a/share/newton_base/openoapi/image.py b/share/newton_base/openoapi/image.py
new file mode 100644
index 00000000..9ddb5f55
--- /dev/null
+++ b/share/newton_base/openoapi/image.py
@@ -0,0 +1,235 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+from six.moves import urllib
+import threading
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+running_threads = {}
+running_thread_lock = threading.Lock()
+
+class imageThread (threading.Thread):
+ service = {'service_type': 'image',
+ 'interface': 'public'}
+ def __init__(self, vimid, tenantid, imageid, imagefd):
+ threading.Thread.__init__(self)
+ self.vimid = vimid
+ self.tenantid = tenantid
+ self.imageid = imageid
+ self.imagefd = imagefd
+
+ def run(self):
+ logger.debug("start imagethread %s, %s, %s" % (self.vimid, self.tenantid, self.imageid))
+ self.transfer_image(self.vimid, self.tenantid, self.imageid, self.imagefd)
+ logger.debug("stop imagethread %s, %s, %s" % (self.vimid, self.tenantid, self.imageid))
+ running_thread_lock.acquire()
+ running_threads.pop(self.imageid)
+ running_thread_lock.release()
+
+ def transfer_image(self, vimid, tenantid, imageid, imagefd):
+ logger.debug("Images--transfer_image::> %s" % (imageid))
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "v2/images/%s/file" % imageid
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #open imageurl
+ resp = sess.put(req_resouce, endpoint_filter=self.service, data=imagefd.read(),
+ headers={"Content-Type": "application/octet-stream",
+ "Accept": ""})
+
+ logger.debug("response status code of transfer_image %s" % resp.status_code)
+ return None
+ except HttpError as e:
+ logger.error("transfer_image, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return None
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ logger.error("Failed to transfer_image:%s" % str(e))
+ return None
+
+class Images(APIView):
+ service = {'service_type': 'image',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("project_id", "tenantId"),
+ ("disk_format", "imageType"),
+ ("container_format", "containerFormat")
+ ]
+
+ def get(self, request, vimid="", tenantid="", imageid=""):
+ logger.debug("Images--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self.get_images(query, vimid, tenantid, imageid)
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def get_images(self, query="", vimid="", tenantid="", imageid=""):
+ logger.debug("Images--get_images::> %s" % imageid)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2/images"
+ if imageid:
+ req_resouce += "/%s" % imageid
+ elif query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not imageid:
+ # convert the key naming in images
+ for image in content["images"]:
+ VimDriverUtils.replace_key_by_mapping(image,
+ self.keys_mapping)
+ else:
+ # convert the key naming in the image specified by id
+ #image = content.pop("image", None)
+ VimDriverUtils.replace_key_by_mapping(content,
+ self.keys_mapping)
+ #content.update(image)
+
+ return content, resp.status_code
+
+ def post(self, request, vimid="", tenantid="", imageid=""):
+ logger.debug("Images--post::> %s" % request.data)
+ try:
+ #check if created already: check name
+ query = "name=%s" % request.data["name"]
+ content, status_code = self.get_images(query, vimid, tenantid)
+ existed = False
+ if status_code == 200:
+ for image in content["images"]:
+ if image["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ image.update(vim_dict)
+ return Response(data=image, status=status_code)
+
+ imageurl = request.data.pop("imagePath", None)
+ imagefd = None
+ if not imageurl:
+ return Response(data={'error': 'imagePath is not specified'}, status=500)
+
+ #valid image url
+ imagefd = urllib.request.urlopen(imageurl)
+ if not imagefd:
+ logger.debug("image is not available at %s" % imageurl)
+ return Response(data={'error': 'cannot access to specified imagePath'}, status=500)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2/images"
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ image = request.data
+ VimDriverUtils.replace_key_by_mapping(image,
+ self.keys_mapping, True)
+ #req_body = json.JSONEncoder().encode({"image": image})
+ req_body = json.JSONEncoder().encode(image)
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+ #resp_body = resp.json()["image"]
+ resp_body = resp.json()
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+
+ #launch a thread to download image and upload to VIM
+ if resp.status_code == 201:
+ imageid = resp_body["id"]
+ logger.debug("launch thread to upload image: %s" % imageid)
+ tmp_thread = imageThread(vimid, tenantid,imageid,imagefd)
+ running_thread_lock.acquire()
+ running_threads[imageid] = tmp_thread
+ running_thread_lock.release()
+ tmp_thread.start()
+ else:
+ logger.debug("resp.status_code: %s" % resp.status_code)
+
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except urllib.error.URLError as e:
+ return Response(data={'error': 'image is not accessible:%s' % str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def delete(self, request, vimid="", tenantid="", imageid=""):
+ logger.debug("Images--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "v2/images"
+ if imageid:
+ req_resouce += "/%s" % imageid
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/openoapi/limits.py b/share/newton_base/openoapi/limits.py
new file mode 100644
index 00000000..3f62afa3
--- /dev/null
+++ b/share/newton_base/openoapi/limits.py
@@ -0,0 +1,82 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Limits(APIView):
+ service = {'service_type': 'compute',
+ 'interface': 'public'}
+
+ service_network = {'service_type': 'network',
+ 'interface': 'public'}
+
+ service_volume = {'service_type': 'volumev2',
+ 'interface': 'public'}
+
+ def get(self, request, vimid="", tenantid=""):
+ logger.debug("Limits--get::> %s" % request.data)
+ try:
+ #get limits first
+ # prepare request resource to vim instance
+ req_resouce = "/limits"
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ content_all =content['limits']['absolute']
+
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content_all.update(vim_dict)
+
+ #now get quota
+ # prepare request resource to vim instance
+ req_resouce = "/v2.0/quotas/%s" % tenantid
+ resp = sess.get(req_resouce, endpoint_filter=self.service_network)
+ content = resp.json()
+ content_all.update(content['quota'])
+
+ #now get volume limits
+ # prepare request resource to vim instance
+ req_resouce = "/limits"
+ resp = sess.get(req_resouce, endpoint_filter=self.service_volume)
+ content = resp.json()
+ content_all.update(content['limits']['absolute'])
+
+ return Response(data=content_all, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
diff --git a/share/newton_base/openoapi/network.py b/share/newton_base/openoapi/network.py
new file mode 100644
index 00000000..a1ac550d
--- /dev/null
+++ b/share/newton_base/openoapi/network.py
@@ -0,0 +1,171 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Networks(APIView):
+ service = {'service_type': 'network',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("provider:segmentation_id", "segmentationId"),
+ ("provider:physical_network", "physicalNetwork"),
+ ("router:external", "routerExternal"),
+ ("provider:network_type", "networkType"),
+ ("vlan_transparent", "vlanTransparent"),
+ ("project_id", "tenantId"),
+ ]
+
+ def get(self, request, vimid="", tenantid="", networkid=""):
+ logger.debug("Networks--get::> %s" % request.data)
+ try:
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self.get_networks(query, vimid, tenantid, networkid)
+ return Response(data=content, status=status_code)
+
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def get_networks(self, query, vimid="", tenantid="", networkid=""):
+ logger.debug("Networks--get_networks::> %s" % networkid)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/networks"
+ if networkid:
+ req_resouce += "/%s" % networkid
+
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not networkid:
+ # convert the key naming in networks
+ for network in content["networks"]:
+ VimDriverUtils.replace_key_by_mapping(network,
+ self.keys_mapping)
+ else:
+ # convert the key naming in the network specified by id
+ network = content.pop("network", None)
+ VimDriverUtils.replace_key_by_mapping(network,
+ self.keys_mapping)
+ content.update(network)
+
+ return content, resp.status_code
+
+ def post(self, request, vimid="", tenantid="", networkid=""):
+ logger.debug("Networks--post::> %s" % request.data)
+ try:
+ #check if created already: check name
+ query = "name=%s" % request.data["name"]
+ content, status_code = self.get_networks(query, vimid, tenantid)
+ existed = False
+ if status_code == 200:
+ for network in content["networks"]:
+ if network["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ network.update(vim_dict)
+ return Response(data=network, status=status_code)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/networks"
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ network = request.data
+ VimDriverUtils.replace_key_by_mapping(network,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"network": network})
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+ resp_body = resp.json()["network"]
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def delete(self, request, vimid="", tenantid="", networkid=""):
+ logger.debug("Networks--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/networks"
+ if networkid:
+ req_resouce += "/%s" % networkid
+ query = VimDriverUtils.get_query_part(request)
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+
+class Subnets(APIView):
+ pass
diff --git a/share/newton_base/openoapi/server.py b/share/newton_base/openoapi/server.py
new file mode 100644
index 00000000..c53e43e8
--- /dev/null
+++ b/share/newton_base/openoapi/server.py
@@ -0,0 +1,426 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+
+import logging
+import json
+import threading
+import traceback
+
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+running_threads = {}
+running_thread_lock = threading.Lock()
+
+
+#assume volume is attached on server creation
+class ServerVolumeAttachThread (threading.Thread):
+ service = {'service_type': 'compute',
+ 'interface': 'public'}
+ def __init__(self, vimid, tenantid, serverid, is_attach, *volumeids):
+ threading.Thread.__init__(self)
+ self.vimid = vimid
+ self.tenantid = tenantid
+ self.serverid = serverid
+ self.volumeids = volumeids
+ self.is_attach = is_attach
+
+ def run(self):
+ logger.debug("start server thread %s, %s, %s" % (self.vimid, self.tenantid, self.serverid))
+ if (self.is_attach):
+ self.attach_volume(self.vimid, self.tenantid, self.serverid, *self.volumeids)
+ else:
+ self.detach_volume(self.vimid, self.tenantid, self.serverid, *self.volumeids)
+ logger.debug("stop server thread %s, %s, %s" % (self.vimid, self.tenantid, self.serverid))
+ running_thread_lock.acquire()
+ running_threads.pop(self.serverid)
+ running_thread_lock.release()
+
+ def attach_volume(self, vimid, tenantid, serverid, *volumeids):
+ logger.debug("Server--attach_volume::> %s, %s" % (serverid, volumeids))
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #check if server is ready to attach
+ logger.debug("Servers--attach_volume, wait for server to be ACTIVE::>%s" % serverid)
+ req_resouce = "servers/%s" % serverid
+ while True:
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ if content and content["server"] and content["server"]["status"] == "ACTIVE":
+ break;
+
+ for volumeid in volumeids:
+ req_resouce = "servers/%s/os-volume_attachments" % serverid
+ req_data = {"volumeAttachment": {
+ "volumeId": volumeid
+ }}
+ logger.debug("Servers--attach_volume::>%s, %s" % (req_resouce, req_data))
+ req_body = json.JSONEncoder().encode(req_data)
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service,
+ headers={"Content-Type": "application/json",
+ "Accept": "application/json"})
+ logger.debug("Servers--attach_volume resp::>%s" % resp.json())
+
+ return None
+ except HttpError as e:
+ logger.error("attach_volume, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return None
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ logger.debug("Failed to attach_volume:%s" % str(e))
+ return None
+
+
+ def detach_volume(self, vimid, tenantid, serverid, *volumeids):
+ logger.debug("Server--detach_volume::> %s, %s" % (serverid, volumeids))
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #wait server to be ready to detach volume
+
+ # assume attachment id is the same as volume id
+ for volumeid in volumeids:
+ req_resouce = "servers/%s/os-volume_attachments/%s" % (serverid, volumeid)
+
+ logger.debug("Servers--dettachVolume::>%s" % (req_resouce))
+ resp = sess.delete(req_resouce,
+ endpoint_filter=self.service,
+ headers={"Content-Type": "application/json",
+ "Accept": "application/json"})
+
+ logger.debug("Servers--dettachVolume resp::>%s" % resp.json())
+
+ return None
+ except HttpError as e:
+ logger.error("detach_volume, HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return None
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ logger.debug("Failed to detach_volume:%s" % str(e))
+ return None
+
+
+class Servers(APIView):
+ service = {'service_type': 'compute',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("tenant_id", "tenantId"),
+ ("flavorRef", "flavorId"),
+ ("user_data", "userdata"),
+ ("security_groups", "securityGroups"),
+ ("availability_zone ", "availabilityZone"),
+ ("os-extended-volumes:volumes_attached", "volumeArray"),
+ ]
+
+ def _attachVolume(self, vimid, tenantid, serverId, *volumeIds):
+ #has to be async mode to wait server is ready to attach volume
+ logger.debug("launch thread to attach volume: %s" % serverId)
+ tmp_thread = ServerVolumeAttachThread(vimid, tenantid, serverId, True, *volumeIds)
+ running_thread_lock.acquire()
+ running_threads[serverId] = tmp_thread
+ running_thread_lock.release()
+ tmp_thread.start()
+
+ def _dettach_volume(self, vimid, tenantid, serverId, *volumeIds):
+ # assume attachment id is the same as volume id
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ for volumeid in volumeIds:
+ req_resouce = "servers/%s/os-volume_attachments/%s" % (serverId, volumeid)
+ logger.debug("Servers--dettachVolume::>%s" % (req_resouce))
+ resp = sess.delete(req_resouce,
+ endpoint_filter=self.service,
+ headers={"Content-Type": "application/json",
+ "Accept": "application/json"})
+ logger.debug("Servers--dettachVolume resp status::>%s" % resp.status_code)
+
+ def _convert_metadata(self, metadata, metadata_output, reverse=True):
+ if not reverse:
+ # from extraSpecs to extra_specs
+ for spec in metadata:
+ metadata_output[spec['keyName']] = spec['value']
+ else:
+ for k, v in metadata_output.items():
+ spec = {}
+ spec['keyName'] = k
+ spec['value'] = v
+ metadata.append(spec)
+
+ def _convert_resp(self, server):
+ #convert volumeArray
+ volumeArray = server.pop("volumeArray", None)
+ tmpVolumeArray = []
+ if volumeArray and len(volumeArray) > 0:
+ for vol in volumeArray:
+ tmpVolumeArray.append({"volumeId": vol["id"]})
+ server["volumeArray"] = tmpVolumeArray if len(tmpVolumeArray) > 0 else None
+
+ #convert flavor
+ flavor = server.pop("flavor", None)
+ server["flavorId"] = flavor["id"] if flavor else None
+
+ #convert nicArray
+
+ #convert boot
+ imageObj = server.pop("image", None)
+ imageId = imageObj.pop("id", None) if imageObj else None
+ if imageId:
+ server["boot"] = {"type":2, "imageId": imageId}
+ else:
+ server["boot"] = {"type":1, "volumeId":tmpVolumeArray.pop(0)["volumeId"] if len(tmpVolumeArray) > 0 else None}
+
+ #convert OS-EXT-AZ:availability_zone
+ server["availabilityZone"] = server.pop("OS-EXT-AZ:availability_zone", None)
+
+ def get(self, request, vimid="", tenantid="", serverid=""):
+ logger.debug("Servers--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self._get_servers(query, vimid, tenantid, serverid)
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def _get_ports(self, vimid="", tenantid="", serverid=None):
+ # query attached ports
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ req_resouce = "servers/%s/os-interface" % serverid
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ ports = resp.json()
+ if ports and ports["interfaceAttachments"] and len(ports["interfaceAttachments"]) > 0:
+ return [{"portId":port["port_id"]} for port in ports["interfaceAttachments"]]
+ return None
+
+ def _get_servers(self, query="", vimid="", tenantid="", serverid=None):
+ logger.debug("Servers--get_servers::> %s,%s" % (tenantid, serverid))
+
+ # prepare request resource to vim instance
+ req_resouce = "servers"
+ if serverid:
+ req_resouce += "/%s" % serverid
+ else:
+ req_resouce += "/detail"
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not serverid:
+ # convert the key naming in servers
+ for server in content["servers"]:
+ metadata = server.pop("metadata", None)
+ if metadata:
+ meta_data = []
+ self._convert_metadata(metadata, meta_data, False)
+ server["metadata"] = meta_data
+ VimDriverUtils.replace_key_by_mapping(server,
+ self.keys_mapping)
+ self._convert_resp(server)
+ server["nicArray"] = self._get_ports(vimid, tenantid, server["id"])
+
+ else:
+ # convert the key naming in the server specified by id
+ server = content.pop("server", None)
+ metadata = server.pop("metadata", None)
+ if metadata:
+ meta_data = []
+ self._convert_metadata(metadata, meta_data)
+ server["metadata"] = meta_data
+ VimDriverUtils.replace_key_by_mapping(server,
+ self.keys_mapping)
+ self._convert_resp(server)
+ server["nicArray"] = self._get_ports(vimid, tenantid, serverid)
+ content.update(server)
+
+ return content, resp.status_code
+
+ def post(self, request, vimid="", tenantid="", serverid=""):
+ logger.debug("Servers--post::> %s" % request.data)
+ try:
+ # check if created already: check name
+ servername = request.data["name"]
+ query = "name=%s" % servername
+ content, status_code = self._get_servers(query, vimid, tenantid)
+ existed = False
+ if status_code == status.HTTP_200_OK:
+ for server in content["servers"]:
+ if server["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed and server:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ server.update(vim_dict)
+ return Response(data=server, status=status_code)
+
+ # prepare request resource to vim instance
+ req_resouce = "servers"
+ server = request.data
+
+ # convert parameters
+ boot = server.pop("boot", None)
+ if not boot:
+ return Response(data={'error': "missing boot paramters"},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ if boot["type"] == 1:
+ # boot from volume
+ server["block_device_mapping_v2"] = [{"uuid": boot["volumeId"],
+ "source_type": "volume",
+ "destination_type": "volume",
+ "delete_on_termination": "false",
+ "boot_index": "0"}]
+ else:
+ # boot from image
+ server["imageRef"] = boot["imageId"]
+
+ nicarray = server.pop("nicArray", None)
+ if not nicarray:
+ return Response(data={'error': "missing nicArray paramters"},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ networks = []
+ for nic in nicarray:
+ networks.append({"port": nic["portId"]})
+ if len(networks) > 0:
+ server["networks"] = networks
+
+ meta_data = server.pop("metadata", None)
+ if meta_data:
+ metadata = {}
+ self._convert_metadata(metadata, meta_data, False)
+ server["metadata"] = metadata
+
+ contextarray = server.pop("contextArray", None)
+ volumearray = server.pop("volumeArray", None)
+ if contextarray:
+ # now set "contextArray" array
+ personalities = []
+ for context in contextarray:
+ personalities.append({"path": context["fileName"], "contents": context["fileData"]})
+ if len(personalities) > 0:
+ server["personality"] = personalities
+
+ VimDriverUtils.replace_key_by_mapping(server,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"server": server})
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service,
+ headers={"Content-Type": "application/json",
+ "Accept": "application/json"})
+
+ resp_body = resp.json().pop("server", None)
+
+ logger.debug("Servers--post status::>%s, %s" % (resp_body["id"], resp.status_code))
+ if resp.status_code in [status.HTTP_200_OK, status.HTTP_201_CREATED, status.HTTP_202_ACCEPTED]:
+ if volumearray and len(volumearray) > 0:
+ # server is created, now attach volumes
+ volumeIds = [extraVolume["volumeId"] for extraVolume in volumearray]
+ self._attachVolume(vimid, tenantid, resp_body["id"], *volumeIds)
+
+ metadata = resp_body.pop("metadata", None)
+ if metadata:
+ meta_data = []
+ self._convert_metadata(metadata, meta_data)
+ resp_body["metadata"] = meta_data
+
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ resp_body["boot"] = boot
+ resp_body["volumeArray"] = volumearray
+ resp_body["nicArray"] = nicarray
+ resp_body["contextArray"] = contextarray
+ resp_body["name"] = servername
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def delete(self, request, vimid="", tenantid="", serverid=""):
+ logger.debug("Servers--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ #check and dettach them if volumes attached to server
+ server, status_code = self._get_servers("", vimid, tenantid, serverid)
+ volumearray = server.pop("volumeArray", None)
+ if volumearray and len(volumearray) > 0:
+ volumeIds = [extraVolume["volumeId"] for extraVolume in volumearray]
+ self._dettach_volume(vimid, tenantid, serverid, *volumeIds)
+
+ #delete server now
+ req_resouce = "servers"
+ if serverid:
+ req_resouce += "/%s" % serverid
+
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/openoapi/subnet.py b/share/newton_base/openoapi/subnet.py
new file mode 100644
index 00000000..1310ab50
--- /dev/null
+++ b/share/newton_base/openoapi/subnet.py
@@ -0,0 +1,169 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Subnets(APIView):
+ service = {'service_type': 'network',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("project_id", "tenantId"),
+ ("network_id", "networkId"),
+ ("ip_version", "ipVersion"),
+ ("enable_dhcp", "enableDhcp"),
+ ("gateway_ip", "gatewayIp"),
+ ("dns_nameservers", "dnsNameservers"),
+ ("host_routes", "hostRoutes"),
+ ("allocation_pools", "allocationPools"),
+ ]
+
+ def get(self, request, vimid="", tenantid="", subnetid=""):
+ logger.debug("Subnets--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self.get_subnets(query, vimid, tenantid, subnetid)
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def get_subnets(self, query="", vimid="", tenantid="", subnetid=""):
+ logger.debug("Subnets--get_subnets::> %s" % subnetid)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/subnets"
+ if subnetid:
+ req_resouce += "/%s" % subnetid
+
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not subnetid:
+ # convert the key naming in subnets
+ for subnet in content["subnets"]:
+ VimDriverUtils.replace_key_by_mapping(subnet,
+ self.keys_mapping)
+ else:
+ # convert the key naming in the subnet specified by id
+ subnet = content.pop("subnet", None)
+ VimDriverUtils.replace_key_by_mapping(subnet,
+ self.keys_mapping)
+ content.update(subnet)
+
+ return content, resp.status_code
+
+ def post(self, request, vimid="", tenantid="", subnetid=""):
+ logger.debug("Subnets--post::> %s" % request.data)
+ try:
+ #check if created already: check name
+ query = "name=%s" % request.data["name"]
+ content, status_code = self.get_subnets(query, vimid, tenantid)
+ existed = False
+ if status_code == 200:
+ for subnet in content["subnets"]:
+ if subnet["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ subnet.update(vim_dict)
+ return Response(data=subnet, status=status_code)
+
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/subnets"
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ subnet = request.data
+ VimDriverUtils.replace_key_by_mapping(subnet,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"subnet": subnet})
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+ resp_body = resp.json()["subnet"]
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def delete(self, request, vimid="", tenantid="", subnetid=""):
+ logger.debug("Subnets--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/subnets"
+ if subnetid:
+ req_resouce += "/%s" % subnetid
+ query = VimDriverUtils.get_query_part(request)
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/openoapi/tenants.py b/share/newton_base/openoapi/tenants.py
new file mode 100644
index 00000000..bcd8f2f2
--- /dev/null
+++ b/share/newton_base/openoapi/tenants.py
@@ -0,0 +1,85 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+
+import logging
+import traceback
+
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Tenants(APIView):
+ service = {
+ 'service_type': 'identity',
+ 'interface': 'public'
+ }
+
+ keys_mapping = [
+ ("projects", "tenants"),
+ ]
+
+ def get(self, request, vimid=""):
+ logger.debug("Tenants--get::> %s" % request.data)
+ try:
+ #prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ req_resouce = "/projects"
+ if '/v2' in vim["url"]:
+ req_resouce = "/v2.0/tenants"
+
+ sess = VimDriverUtils.get_session(vim)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ }
+ content.update(vim_dict)
+
+ VimDriverUtils.replace_key_by_mapping(
+ content, self.keys_mapping)
+
+ if query:
+ _, tenantname = query.split('=')
+ if tenantname:
+ tmp = content["tenants"]
+ content["tenants"] = []
+ # convert the key naming in hosts
+ for tenant in tmp:
+ if tenantname == tenant['name']:
+ content["tenants"].append(tenant)
+
+ return Response(data=content, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(
+ data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (
+ e.http_status, e.response.json()))
+ return Response(data=e.response.json(),
+ status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(
+ data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/openoapi/volume.py b/share/newton_base/openoapi/volume.py
new file mode 100644
index 00000000..a048dda4
--- /dev/null
+++ b/share/newton_base/openoapi/volume.py
@@ -0,0 +1,168 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Volumes(APIView):
+ service = {'service_type': 'volumev2',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("project_id", "tenantId"),
+ ("created_at", "createTime"),
+ ("size", "volumeSize"),
+ ("volume_type", "volumeType"),
+ ("imageRef", "imageId"),
+ ("availability_zone", "availabilityZone"),
+ ("server_id", "serverId"),
+ ("attachment_id", "attachmentId"),
+ ]
+
+ def get(self, request, vimid="", tenantid="", volumeid=""):
+ logger.debug("Volumes--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self.get_volumes(query, vimid, tenantid, volumeid)
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def get_volumes(self, query="", vimid="", tenantid="", volumeid=None):
+ logger.debug("Volumes--get_volumes::> %s,%s" % (tenantid, volumeid))
+
+ # prepare request resource to vim instance
+ req_resouce = "volumes"
+ if volumeid:
+ req_resouce += "/%s" % volumeid
+ else:
+ req_resouce += "/detail"
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not volumeid:
+ # convert the key naming in volumes
+ for volume in content["volumes"]:
+ VimDriverUtils.replace_key_by_mapping(volume,
+ self.keys_mapping)
+ else:
+ # convert the key naming in the volume specified by id
+ volume = content.pop("volume", None)
+ VimDriverUtils.replace_key_by_mapping(volume,
+ self.keys_mapping)
+ content.update(volume)
+
+ return content, resp.status_code
+
+ def post(self, request, vimid="", tenantid="", volumeid=""):
+ logger.debug("Volumes--post::> %s" % request.data)
+ try:
+ #check if created already: check name
+ query = "name=%s" % request.data["name"]
+ content, status_code = self.get_volumes(query, vimid, tenantid)
+ existed = False
+ if status_code == 200:
+ for volume in content["volumes"]:
+ if volume["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ volume.update(vim_dict)
+ return Response(data=volume, status=status_code)
+
+ # prepare request resource to vim instance
+ req_resouce = "volumes"
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ volume = request.data
+ VimDriverUtils.replace_key_by_mapping(volume,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"volume": volume})
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service, headers={"Content-Type": "application/json",
+ "Accept": "application/json" })
+ resp_body = resp.json()["volume"]
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ return Response(data=resp_body, status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def delete(self, request, vimid="", tenantid="", volumeid=""):
+ logger.debug("Volumes--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "volumes"
+ if volumeid:
+ req_resouce += "/%s" % volumeid
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/openoapi/vport.py b/share/newton_base/openoapi/vport.py
new file mode 100644
index 00000000..8f9b0d62
--- /dev/null
+++ b/share/newton_base/openoapi/vport.py
@@ -0,0 +1,202 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+import logging
+import json
+import traceback
+from keystoneauth1.exceptions import HttpError
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from newton.pub.exceptions import VimDriverNewtonException
+
+from newton_base.util import VimDriverUtils
+
+logger = logging.getLogger(__name__)
+
+
+class Vports(APIView):
+ service = {'service_type': 'network',
+ 'interface': 'public'}
+ keys_mapping = [
+ ("project_id", "tenantId"),
+ ("network_id", "networkId"),
+ ("binding:vnic_type", "vnicType"),
+ ("security_groups", "securityGroups"),
+ ("mac_address", "macAddress"),
+ ("subnet_id", "subnetId"),
+ ("ip_address", "ip"),
+ ]
+
+ def get(self, request, vimid="", tenantid="", portid=""):
+ logger.debug("Ports--get::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ query = VimDriverUtils.get_query_part(request)
+ content, status_code = self.get_ports(query, vimid, tenantid, portid)
+
+ return Response(data=content, status=status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def get_ports(self, query="", vimid="", tenantid="", portid=""):
+ logger.debug("Ports--get_ports::> %s" % portid)
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+
+ if sess:
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/ports"
+ if portid:
+ req_resouce += "/%s" % portid
+
+ if query:
+ req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.get(req_resouce, endpoint_filter=self.service)
+ content = resp.json()
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ }
+ content.update(vim_dict)
+
+ if not portid:
+ # convert the key naming in ports
+ for port in content["ports"]:
+ # use only 1st entry of fixed_ips
+ tmpips = port.pop("fixed_ips", None) if port else None
+ port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
+ VimDriverUtils.replace_key_by_mapping(port,
+ self.keys_mapping)
+ else:
+ # convert the key naming in the port specified by id
+ port = content.pop("port", None)
+ #use only 1st entry of fixed_ips
+ tmpips = port.pop("fixed_ips", None) if port else None
+ port.update(tmpips[0]) if tmpips and len(tmpips) > 0 else None
+
+ VimDriverUtils.replace_key_by_mapping(port,
+ self.keys_mapping)
+ content.update(port)
+ return content, resp.status_code
+ return {}, 500
+
+ def post(self, request, vimid="", tenantid="", portid=""):
+ logger.debug("Ports--post::> %s" % request.data)
+ try:
+ #check if already created: name
+ query = "name=%s" % request.data["name"]
+ content, status_code = self.get_ports(query, vimid, tenantid, portid)
+ existed = False
+ if status_code == 200:
+ for port in content["ports"]:
+ if port["name"] == request.data["name"]:
+ existed = True
+ break
+ if existed == True:
+ vim_dict = {
+ "returnCode": 0,
+ }
+ port.update(vim_dict)
+ return Response(data=port, status=status_code)
+
+ #otherwise create a new one
+ return self.create_port(request, vimid, tenantid)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ def create_port(self, request, vimid, tenantid):
+ logger.debug("Ports--create::> %s" % request.data)
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ if sess:
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/ports"
+
+ port = request.data
+ #handle ip and subnetId
+ tmpip = port.pop("ip", None)
+ tmpsubnet = port.pop("subnetId", None)
+ if tmpip and tmpsubnet:
+ fixed_ip = {
+ "ip_address": tmpip,
+ "subnet_id": tmpsubnet,
+ }
+ port["fixed_ips"] = []
+ port["fixed_ips"].append(fixed_ip)
+
+ VimDriverUtils.replace_key_by_mapping(port,
+ self.keys_mapping, True)
+ req_body = json.JSONEncoder().encode({"port": port})
+ resp = sess.post(req_resouce, data=req_body,
+ endpoint_filter=self.service)
+ resp_body = resp.json()["port"]
+ #use only 1 fixed_ip
+ tmpips = resp_body.pop("fixed_ips", None)
+ if tmpips and len(tmpips) > 0:
+ resp_body.update(tmpips[0])
+
+ VimDriverUtils.replace_key_by_mapping(resp_body, self.keys_mapping)
+ vim_dict = {
+ "vimName": vim["name"],
+ "vimId": vim["vimId"],
+ "tenantId": tenantid,
+ "returnCode": 1,
+ }
+ resp_body.update(vim_dict)
+ return Response(data=resp_body, status=resp.status_code)
+ return {}
+
+ def delete(self, request, vimid="", tenantid="", portid=""):
+ logger.debug("Ports--delete::> %s" % request.data)
+ try:
+ # prepare request resource to vim instance
+ req_resouce = "v2.0/ports"
+ if portid:
+ req_resouce += "/%s" % portid
+# query = VimDriverUtils.get_query_part(request)
+# if query:
+# req_resouce += "?%s" % query
+
+ vim = VimDriverUtils.get_vim_info(vimid)
+ sess = VimDriverUtils.get_session(vim, tenantid)
+ resp = sess.delete(req_resouce, endpoint_filter=self.service)
+ return Response(status=resp.status_code)
+ except VimDriverNewtonException as e:
+ return Response(data={'error': e.content}, status=e.status_code)
+ except HttpError as e:
+ logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
+ return Response(data=e.response.json(), status=e.http_status)
+ except Exception as e:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': str(e)},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/share/newton_base/util.py b/share/newton_base/util.py
new file mode 100644
index 00000000..d2363252
--- /dev/null
+++ b/share/newton_base/util.py
@@ -0,0 +1,149 @@
+# Copyright (c) 2017 Wind River Systems, Inc.
+#
+# Licensed 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.
+
+import logging
+
+from django.conf import settings
+from django.core.cache import cache
+from keystoneauth1.identity import v2 as keystone_v2
+from keystoneauth1.identity import v3 as keystone_v3
+from keystoneauth1 import session
+
+from newton.pub.msapi import extsys
+
+logger = logging.getLogger(__name__)
+
+
+class VimDriverUtils(object):
+ @staticmethod
+ def get_vim_info(vimid):
+ """
+ Retrieve VIM information.
+
+ :param vimid: VIM Identifier
+ :return: VIM information
+ """
+ # TODO: get vim info from local cache firstly later from ESR
+ return extsys.get_vim_by_id(vimid)
+
+ @staticmethod
+ def delete_vim_info(vimid):
+ return extsys.delete_vim_by_id(vimid)
+
+ @staticmethod
+ def get_query_part(request):
+ query = ""
+ full_path = request.get_full_path()
+ if '?' in full_path:
+ _, query = full_path.split('?')
+ return query
+
+ @staticmethod
+ def get_session(
+ vim, tenant_id=None, tenant_name=None, auth_state=None):
+ """
+ get session object and optionally preload auth_state
+ """
+ auth = None
+
+ params = {
+ "auth_url": vim["url"],
+ "username": vim["userName"],
+ "password": vim["password"],
+ }
+
+ # tenantid takes precedence over tenantname
+ if tenant_id:
+ params["tenant_id"] = tenant_id
+ else:
+ # input tenant name takes precedence over the default one
+ # from AAI data store
+ params["tenant_name"] = (tenant_name if tenant_name
+ else vim['tenant'])
+
+ if '/v2' in params["auth_url"]:
+ auth = keystone_v2.Password(**params)
+ else:
+ params["user_domain_name"] = vim["domain"]
+ params["project_domain_name"] = vim["domain"]
+
+ if 'tenant_id' in params:
+ params["project_id"] = params.pop("tenant_id")
+ if 'tenant_name' in params:
+ params["project_name"] = params.pop("tenant_name")
+ if '/v3' not in params["auth_url"]:
+ params["auth_url"] = params["auth_url"] + "/v3",
+ auth = keystone_v3.Password(**params)
+
+ #preload auth_state which was acquired in last requests
+ if auth_state:
+ auth.set_auth_state(auth_state)
+
+ return session.Session(auth=auth)
+
+ @staticmethod
+ def get_auth_state(session_obj):
+ """
+ Retrieve the authorization state
+ :param session: OpenStack Session object
+ :return: return a string dump of json object with token and
+ resp_data of authentication request
+ """
+ auth = session_obj._auth_required(None, 'fetch a token')
+ if auth:
+ #trigger the authenticate request
+ session_obj.get_auth_headers(auth)
+
+ # norm_expires = utils.normalize_time(auth.expires)
+ return auth.get_auth_state()
+
+ @staticmethod
+ def get_token_cache(token):
+ """
+ get auth_state and metadata fromm cache
+ :param token:
+ :return:
+ """
+ return cache.get(token), cache.get("meta_%s" % token)
+
+ @staticmethod
+ def update_token_cache(token, auth_state, metadata):
+ """
+ Stores into the cache the auth_state and metadata_catalog
+ information.
+
+ :param token: Base token to be used as an identifier
+ :param auth_state: Authorization information
+ :param metadata: Metadata Catalog information
+ """
+ if metadata and not cache.get(token):
+ cache.set(
+ token, auth_state, settings.CACHE_EXPIRATION_TIME)
+ cache.set(
+ "meta_%s" % token, metadata,
+ settings.CACHE_EXPIRATION_TIME)
+
+ @staticmethod
+ def _replace_a_key(dict_obj, key_pair, reverse):
+ old_key = key_pair[1] if reverse else key_pair[0]
+ new_key = key_pair[0] if reverse else key_pair[1]
+
+ old_value = dict_obj.pop(old_key, None)
+ if old_value:
+ dict_obj[new_key] = old_value
+
+ @staticmethod
+ def replace_key_by_mapping(dict_obj, mapping, reverse=False):
+ for k in mapping:
+ VimDriverUtils._replace_a_key(dict_obj, k, reverse)