RIFT 16501 get-package-endpoint now requires a valid package-type.
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / rpc.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 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
18 #
19
20 import abc
21 import asyncio
22 import tempfile
23
24 from gi.repository import (
25 RwDts as rwdts,
26 RwPkgMgmtYang)
27 import rift.tasklets
28 import rift.mano.dts as mano_dts
29
30 from . import downloader as pkg_downloader
31
32 # Shortcuts
33 RPC_PKG_ENDPOINT = RwPkgMgmtYang.YangOutput_RwPkgMgmt_GetPackageEndpoint
34 RPC_SCHEMA_ENDPOINT = RwPkgMgmtYang.YangOutput_RwPkgMgmt_GetPackageSchema
35 RPC_PACKAGE_ADD_ENDPOINT = RwPkgMgmtYang.YangOutput_RwPkgMgmt_PackageFileAdd
36 RPC_PACKAGE_DELETE_ENDPOINT = RwPkgMgmtYang.YangOutput_RwPkgMgmt_PackageFileDelete
37 RPC_PACKAGE_COPY_ENDPOINT = RwPkgMgmtYang.YangOutput_RwPkgMgmt_PackageCopy
38
39
40 class EndpointDiscoveryRpcHandler(mano_dts.AbstractRpcHandler):
41 """RPC handler to generate the endpoint for the Package manager."""
42
43 def __init__(self, log, dts, loop, proxy):
44 """
45 Args:
46 proxy: Any impl of .proxy.AbstractPackageManagerProxy
47 """
48 super().__init__(log, dts, loop)
49 self.proxy = proxy
50
51 @property
52 def xpath(self):
53 return "/rw-pkg-mgmt:get-package-endpoint"
54
55 @asyncio.coroutine
56 def callback(self, ks_path, msg):
57 """Forwards the request to proxy.
58 """
59
60 url = yield from self.proxy.endpoint(
61 msg.package_type if msg.has_field('package_type') else "",
62 msg.package_id)
63
64 rpc_op = RPC_PKG_ENDPOINT.from_dict({"endpoint": url})
65
66 return rpc_op
67
68
69 class SchemaRpcHandler(mano_dts.AbstractRpcHandler):
70 """RPC handler to generate the schema for the packages.
71 """
72 def __init__(self, log, dts, loop, proxy):
73 """
74 Args:
75 proxy: Any impl of .proxy.AbstractPackageManagerProxy
76 """
77 super().__init__(log, dts, loop)
78 self.proxy = proxy
79
80 @property
81 def xpath(self):
82 return "/rw-pkg-mgmt:get-package-schema"
83
84 @asyncio.coroutine
85 def callback(self, ks_path, msg):
86
87 package_type = msg.package_type.lower()
88 schema = yield from self.proxy.schema(msg.package_type)
89
90 rpc_op = RPC_SCHEMA_ENDPOINT()
91 for dirname in schema:
92 rpc_op.schema.append(dirname)
93
94 return rpc_op
95
96
97 class PackageOperationsRpcHandler(mano_dts.AbstractRpcHandler):
98 """File add RPC
99
100 Steps:
101 1. For a request, we schedule a download in the background
102 2. We register the downloader to a publisher to push out the download status
103 Note: The publisher starts the download automatically.
104 3. Return a tracking ID for the client to monitor the entire status
105
106 """
107 def __init__(self, log, dts, loop, proxy, publisher):
108 """
109 Args:
110 proxy: Any impl of .proxy.AbstractPackageManagerProxy
111 publisher: Instance of DownloadStatusPublisher
112 """
113 super().__init__(log, dts, loop)
114 self.proxy = proxy
115 self.publisher = publisher
116
117 @property
118 def xpath(self):
119 return "/rw-pkg-mgmt:package-file-add"
120
121 @asyncio.coroutine
122 def callback(self, ks_path, msg):
123 if not msg.external_url:
124 # For now we will only support External URL download
125 raise Exception ("No download URL provided")
126
127 # Create a tmp file to download the url
128 # We first store the data in temp and post download finish
129 # we move the file to actual location.
130 _, filename = tempfile.mkstemp()
131
132 auth = None
133 if msg.username is not None:
134 auth = (msg.username, msg.password)
135
136 url_downloader = pkg_downloader.PackageFileDownloader.from_rpc_input(
137 msg,
138 auth=auth,
139 file_obj=filename,
140 proxy=self.proxy,
141 log=self.log)
142
143 download_id = yield from self.publisher.register_downloader(url_downloader)
144
145 rpc_op = RPC_PACKAGE_ADD_ENDPOINT.from_dict({"task_id": download_id})
146
147 return rpc_op
148
149 class PackageCopyOperationsRpcHandler(mano_dts.AbstractRpcHandler):
150 def __init__(self, log, dts, loop, proxy, publisher):
151 """
152 Args:
153 proxy: Any impl of .proxy.AbstractPackageManagerProxy
154 publisher: CopyStatusPublisher object
155 """
156 super().__init__(log, dts, loop)
157 self.proxy = proxy
158 self.publisher = publisher
159
160 @property
161 def xpath(self):
162 return "/rw-pkg-mgmt:package-copy"
163
164 @asyncio.coroutine
165 def callback(self, ks_path, msg):
166 import uuid
167 copier = pkg_downloader.PackageFileCopier.from_rpc_input(msg, proxy=self.proxy, log=self.log)
168
169 transaction_id, dest_package_id = yield from self.publisher.register_copier(copier)
170 rpc_op = RPC_PACKAGE_COPY_ENDPOINT.from_dict({
171 "transaction_id":transaction_id,
172 "package_id":dest_package_id,
173 "package_type":msg.package_type})
174
175 return rpc_op
176
177 class PackageDeleteOperationsRpcHandler(mano_dts.AbstractRpcHandler):
178 def __init__(self, log, dts, loop, proxy):
179 """
180 Args:
181 proxy: Any impl of .proxy.AbstractPackageManagerProxy
182 """
183 super().__init__(log, dts, loop)
184 self.proxy = proxy
185
186 @property
187 def xpath(self):
188 return "/rw-pkg-mgmt:package-file-delete"
189
190 @asyncio.coroutine
191 def callback(self, ks_path, msg):
192
193 rpc_op = RPC_PACKAGE_DELETE_ENDPOINT.from_dict({"status": str(True)})
194
195 try:
196 package_file_type = msg.vnfd_file_type.lower() \
197 if msg.package_type == 'VNFD' else msg.nsd_file_type.lower()
198 self.proxy.package_file_delete(
199 msg.package_type,
200 msg.package_id,
201 msg.package_path,
202 package_file_type)
203 except Exception as e:
204 self.log.exception(e)
205 rpc_op.status = str(False)
206 rpc_op.error_trace = str(e)
207
208 return rpc_op