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.
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
24 from gi
.repository
import (
28 import rift
.mano
.dts
as mano_dts
30 from . import downloader
as pkg_downloader
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
40 class EndpointDiscoveryRpcHandler(mano_dts
.AbstractRpcHandler
):
41 """RPC handler to generate the endpoint for the Package manager."""
43 def __init__(self
, log
, dts
, loop
, proxy
):
46 proxy: Any impl of .proxy.AbstractPackageManagerProxy
48 super().__init
__(log
, dts
, loop
)
53 return "/rw-pkg-mgmt:get-package-endpoint"
56 def callback(self
, ks_path
, msg
):
57 """Forwards the request to proxy.
59 url
= yield from self
.proxy
.endpoint(
63 rpc_op
= RPC_PKG_ENDPOINT
.from_dict({"endpoint": url
})
68 class SchemaRpcHandler(mano_dts
.AbstractRpcHandler
):
69 """RPC handler to generate the schema for the packages.
71 def __init__(self
, log
, dts
, loop
, proxy
):
74 proxy: Any impl of .proxy.AbstractPackageManagerProxy
76 super().__init
__(log
, dts
, loop
)
81 return "/rw-pkg-mgmt:get-package-schema"
84 def callback(self
, ks_path
, msg
):
86 package_type
= msg
.package_type
.lower()
87 schema
= yield from self
.proxy
.schema(msg
.package_type
)
89 rpc_op
= RPC_SCHEMA_ENDPOINT()
90 for dirname
in schema
:
91 rpc_op
.schema
.append(dirname
)
96 class PackageOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
100 1. For a request, we schedule a download in the background
101 2. We register the downloader to a publisher to push out the download status
102 Note: The publisher starts the download automatically.
103 3. Return a tracking ID for the client to monitor the entire status
106 def __init__(self
, log
, dts
, loop
, proxy
, publisher
):
109 proxy: Any impl of .proxy.AbstractPackageManagerProxy
110 publisher: Instance of DownloadStatusPublisher
112 super().__init
__(log
, dts
, loop
)
114 self
.publisher
= publisher
118 return "/rw-pkg-mgmt:package-file-add"
121 def callback(self
, ks_path
, msg
):
122 if not msg
.external_url
:
123 # For now we will only support External URL download
124 raise Exception ("No download URL provided")
126 # Create a tmp file to download the url
127 # We first store the data in temp and post download finish
128 # we move the file to actual location.
129 _
, filename
= tempfile
.mkstemp()
132 if msg
.username
is not None:
133 auth
= (msg
.username
, msg
.password
)
135 url_downloader
= pkg_downloader
.PackageFileDownloader
.from_rpc_input(
142 download_id
= yield from self
.publisher
.register_downloader(url_downloader
)
144 rpc_op
= RPC_PACKAGE_ADD_ENDPOINT
.from_dict({"task_id": download_id
})
148 class PackageCopyOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
149 def __init__(self
, log
, dts
, loop
, proxy
, publisher
):
152 proxy: Any impl of .proxy.AbstractPackageManagerProxy
153 publisher: CopyStatusPublisher object
155 super().__init
__(log
, dts
, loop
)
157 self
.publisher
= publisher
161 return "/rw-pkg-mgmt:package-copy"
164 def callback(self
, ks_path
, msg
):
166 copier
= pkg_downloader
.PackageFileCopier
.from_rpc_input(msg
, proxy
=self
.proxy
, log
=self
.log
)
168 transaction_id
, dest_package_id
= yield from self
.publisher
.register_copier(copier
)
169 rpc_op
= RPC_PACKAGE_COPY_ENDPOINT
.from_dict({
170 "transaction_id":transaction_id
,
171 "package_id":dest_package_id
,
172 "package_type":msg
.package_type
})
176 class PackageDeleteOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
177 def __init__(self
, log
, dts
, loop
, proxy
):
180 proxy: Any impl of .proxy.AbstractPackageManagerProxy
182 super().__init
__(log
, dts
, loop
)
187 return "/rw-pkg-mgmt:package-file-delete"
190 def callback(self
, ks_path
, msg
):
192 rpc_op
= RPC_PACKAGE_DELETE_ENDPOINT
.from_dict({"status": str(True)})
195 package_file_type
= msg
.vnfd_file_type
.lower() \
196 if msg
.package_type
== 'VNFD' else msg
.nsd_file_type
.lower()
197 self
.proxy
.package_file_delete(
202 except Exception as e
:
203 self
.log
.exception(e
)
204 rpc_op
.status
= str(False)
205 rpc_op
.error_trace
= str(e
)