summaryrefslogtreecommitdiffstats
path: root/zte
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-03-20 15:18:57 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-03-20 15:29:17 +0800
commit1666778165fcf65c8a7820553c31f60a2433c5fd (patch)
tree7fb70b8068c4ab1fac1f22fbb4d0c316356f99d2 /zte
parent5f924ba0d34363444ae558d6a3219551482f0fb3 (diff)
Add api adaptor for vnf pkg download
Change-Id: Ie58f4691c2eb2e433d8539bdddef2e5fb529d960 Issue-ID: VFC-1307 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
Diffstat (limited to 'zte')
-rw-r--r--zte/vmanager/driver/interfaces/urls.py1
-rw-r--r--zte/vmanager/driver/interfaces/views.py34
2 files changed, 35 insertions, 0 deletions
diff --git a/zte/vmanager/driver/interfaces/urls.py b/zte/vmanager/driver/interfaces/urls.py
index 65331fc0..82cc1ffd 100644
--- a/zte/vmanager/driver/interfaces/urls.py
+++ b/zte/vmanager/driver/interfaces/urls.py
@@ -35,5 +35,6 @@ urlpatterns = [
url(r'^api/ztevnfmdriver/v1/subscribe/(?P<subscribeId>[0-9a-zA-Z\-\_]+)$', views.SubscribeDetail.as_view(), name='subscribe_detail'),
url(r'^api/ztevnfmdriver/v1/subscribe$', views.Subscribe.as_view(), name='subscribe'),
url(r'^api/ztevnfmdriver/v1/vnfpkgs$', views.VnfPkgs.as_view(), name='VnfPkgs'),
+ url(r'^api/ztevnfmdriver/v1/vnfpkgs/(?P<packageId>[0-9a-zA-Z\-\_]+)/(?P<fileName>[0-9a-zA-Z\.\-\_]+)$', views.VnfPkg.as_view(), name='VnfPkg'),
url(r'^samples/$', views.SampleList.as_view(), name='samples')
]
diff --git a/zte/vmanager/driver/interfaces/views.py b/zte/vmanager/driver/interfaces/views.py
index 3fc6272d..7376d4d2 100644
--- a/zte/vmanager/driver/interfaces/views.py
+++ b/zte/vmanager/driver/interfaces/views.py
@@ -23,6 +23,7 @@ from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
+from django.http import StreamingHttpResponse
from driver.interfaces.serializers import HealReqSerializer, InstScaleHealRespSerializer, ScaleReqSerializer, \
NotifyReqSerializer, GrantRespSerializer, GrantReqSerializer, JobQueryRespSerializer, TerminateVnfRequestSerializer, \
@@ -31,6 +32,8 @@ from driver.pub.config.config import VNF_FTP
from driver.pub.utils import restcall
from driver.pub.utils.restcall import req_by_msb
+CHUNK_SIZE = 1024 * 8
+
logger = logging.getLogger(__name__)
@@ -42,6 +45,25 @@ def load_json_file(file_name):
return json_data
+def read(file_path, start, end):
+ fp = open(file_path, 'rb')
+ fp.seek(start)
+ pos = start
+ while pos + CHUNK_SIZE < end:
+ yield fp.read(CHUNK_SIZE)
+ pos = fp.tell()
+ yield fp.read(end - pos)
+
+
+def parse_file_range(file_path, file_range):
+ start, end = 0, os.path.getsize(file_path)
+ if file_range:
+ [start, end] = file_range.split('-')
+ start, end = start.strip(), end.strip()
+ start, end = int(start), int(end)
+ return start, end
+
+
def fun_name():
return "=================%s==================" % inspect.stack()[1][3]
@@ -662,3 +684,15 @@ class VnfPkgs(APIView):
}]
}
return Response(data=resp_data, status=status.HTTP_200_OK)
+
+
+class VnfPkg(APIView):
+ def get(self, request, packageId, fileName):
+ logger.debug("====VnfPkg get====%s, %s", packageId, fileName)
+ file_range = request.META.get('RANGE')
+ logger.debug('file_range: %s' % file_range)
+ # TODO: get filepath
+ local_file_path = fileName
+ start, end = parse_file_range(local_file_path, file_range)
+ file_iterator = read(local_file_path, start, end)
+ return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)