update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / downloader / url.py
1 #
2 # Copyright 2016 RIFT.IO Inc
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16
17 import gi
18
19 import rift.downloader as downloader
20 from gi.repository import RwPkgMgmtYang
21
22
23 TaskStatus = RwPkgMgmtYang.TaskStatus
24
25
26 class PackageFileDownloader(downloader.UrlDownloader):
27 STATUS_MAP = {
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()
33 }
34
35 @classmethod
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
38 """
39 url_downloader = cls(
40 rpc_input.external_url,
41 rpc_input.package_id,
42 rpc_input.package_path,
43 rpc_input.package_type,
44 rpc_input.vnfd_file_type,
45 rpc_input.nsd_file_type,
46 auth=auth,
47 proxy=proxy,
48 file_obj=file_obj,
49 log=log,
50 project=project)
51
52 return url_downloader
53
54 def __init__(self,
55 url,
56 package_id,
57 package_path,
58 package_type,
59 vnfd_file_type,
60 nsd_file_type,
61 proxy,
62 file_obj=None,
63 delete_on_fail=True,
64 decompress_on_fly=False,
65 auth=None,
66 log=None,
67 project=None):
68 super().__init__(
69 url,
70 file_obj=file_obj,
71 delete_on_fail=delete_on_fail,
72 decompress_on_fly=decompress_on_fly,
73 auth=auth,
74 log=log)
75
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()
81 self.proxy = proxy
82 self.project = project
83
84 def convert_to_yang(self):
85
86 job = RwPkgMgmtYang.YangData_RwProject_Project_DownloadJobs_Job.from_dict({
87 "url": self.meta.url,
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]
100 })
101
102 return job
103
104 # Start of delegate calls
105 def call_delegate(self, event):
106 if not self.delegate:
107 return
108
109 job = self.convert_to_yang()
110 getattr(self.delegate, event)(job)
111
112
113 def download_succeeded(self):
114
115 try:
116 # Add the file to package
117 self.proxy.package_file_add(
118 self.meta.filepath,
119 self.package_type,
120 self.package_id,
121 self.package_path,
122 self.package_file_type,
123 self.project)
124
125 except Exception as e:
126 self.log.exception(e)
127 self.meta.detail = str(e)
128 self.download_failed()
129 return
130
131 super().download_succeeded()
132