2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 import rift
.downloader
as downloader
20 from gi
.repository
import RwPkgMgmtYang
23 TaskStatus
= RwPkgMgmtYang
.TaskStatus
26 class PackageFileDownloader(downloader
.UrlDownloader
):
28 downloader
.DownloadStatus
.STARTED
: TaskStatus
.QUEUED
.value_nick
.upper(),
29 downloader
.DownloadStatus
.IN_PROGRESS
: TaskStatus
.IN_PROGRESS
.value_nick
.upper(),
30 downloader
.DownloadStatus
.COMPLETED
: TaskStatus
.COMPLETED
.value_nick
.upper(),
31 downloader
.DownloadStatus
.FAILED
: TaskStatus
.FAILED
.value_nick
.upper(),
32 downloader
.DownloadStatus
.CANCELLED
: TaskStatus
.CANCELLED
.value_nick
.upper()
36 def from_rpc_input(cls
, rpc_input
, file_obj
, proxy
, log
=None, auth
=None, project
=None):
37 """Convenience class to set up an instance form RPC data
40 rpc_input
.external_url
,
42 rpc_input
.package_path
,
43 rpc_input
.package_type
,
44 rpc_input
.vnfd_file_type
,
45 rpc_input
.nsd_file_type
,
64 decompress_on_fly
=False,
71 delete_on_fail
=delete_on_fail
,
72 decompress_on_fly
=decompress_on_fly
,
76 self
.package_id
= package_id
77 self
.package_type
= package_type
78 self
.package_path
= package_path
79 self
.package_file_type
= vnfd_file_type
.lower() \
80 if package_type
== 'VNFD' else nsd_file_type
.lower()
82 self
.project
= project
84 def convert_to_yang(self
):
86 job
= RwPkgMgmtYang
.YangData_RwProject_Project_DownloadJobs_Job
.from_dict({
88 "download_id": self
.meta
.download_id
,
89 "package_id": self
.package_id
,
90 "package_path": self
.package_path
,
91 "package_type": self
.package_type
,
92 "detail": self
.meta
.detail
,
93 "progress_percent": self
.meta
.progress_percent
,
94 "bytes_downloaded": self
.meta
.bytes_downloaded
,
95 "bytes_total": self
.meta
.bytes_total
,
96 "bytes_per_second": self
.meta
.bytes_per_second
,
97 "start_time": self
.meta
.start_time
,
98 "stop_time": self
.meta
.stop_time
,
99 "status": self
.STATUS_MAP
[self
.meta
.status
]
104 # Start of delegate calls
105 def call_delegate(self
, event
):
106 if not self
.delegate
:
109 job
= self
.convert_to_yang()
110 getattr(self
.delegate
, event
)(job
)
113 def download_succeeded(self
):
116 # Add the file to package
117 self
.proxy
.package_file_add(
122 self
.package_file_type
,
125 except Exception as e
:
126 self
.log
.exception(e
)
127 self
.meta
.detail
= str(e
)
128 self
.download_failed()
131 super().download_succeeded()