2 * Copyright 2017 Huawei Technologies Co., Ltd.
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.
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19 import {actionTypes as modalActionTypes, modalSizes} from 'nfvo-components/modal/GlobalModalConstants.js';
20 import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
21 import SoftwareProductActionHelper from 'sdc-app/onboarding/softwareProduct/SoftwareProductActionHelper.js';
22 import {actionTypes} from './VNFImportConstants.js';
23 import i18n from 'nfvo-utils/i18n/i18n.js';
25 function baseUrl(selectedVendor) {
26 const restPrefix = Configuration.get('restPrefix');
27 let vspId = selectedVendor.id;
28 let version = selectedVendor.version;
29 return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${version.id}/vnfrepository`;
32 function getVNFMarketplace(dispatch, currentSoftwareProduct) {
33 return RestAPIUtil.fetch(`${baseUrl(currentSoftwareProduct)}/vnfpackages`, {
38 type: actionTypes.OPEN,
42 type: modalActionTypes.GLOBAL_MODAL_SHOW,
44 modalComponentName: modalContentMapper.VNF_IMPORT,
45 title: i18n('Browse VNF'),
46 modalComponentProps: {
47 currentSoftwareProduct,
48 size: modalSizes.LARGE
54 let errMessage = error.responseJSON ? error.responseJSON.message : i18n('VNF import failed msg');
57 type: modalActionTypes.GLOBAL_MODAL_ERROR,
59 title: i18n('VNF import failed title'),
61 cancelButtonText: i18n('Ok')
67 function downloadCSARFile(csarId, currSoftwareProduct) {
68 let url = `${baseUrl(currSoftwareProduct)}/vnfpackage/${csarId}/download`;
69 return RestAPIUtil.fetch(url, {
75 function getFileName(xhr, defaultFilename) {
77 let contentDisposition = xhr.getResponseHeader('Content-Disposition') ? xhr.getResponseHeader('Content-Disposition') : '';
78 let match = contentDisposition.match(/filename=(.*?)(;|$)/);
80 filename = match[1].replace(/['"]/g, '');;
83 filename = defaultFilename;
88 function uploadVNFData(csarId, currSoftwareProduct, dispatch) {
90 let softwareProductId = currSoftwareProduct.id;
91 let version = currSoftwareProduct.version;
93 SoftwareProductActionHelper.uploadVNFFile(dispatch, {
96 failedNotificationTitle: i18n('Upload validation failed'),
102 function getTimestampString() {
103 let date = new Date();
104 let z = n => n < 10 ? '0' + n : n;
105 return `${date.getFullYear()}-${z(date.getMonth())}-${z(date.getDate())}_${z(date.getHours())}-${z(date.getMinutes())}`;
108 function showFileSaveDialog({blob, xhr, defaultFilename, addTimestamp}) {
109 let filename = getFileName(xhr, defaultFilename);
112 filename = filename.replace(/(^.*?)\.([^.]+$)/, `$1_${getTimestampString()}.$2`);
115 let link = document.createElement('a');
116 let url = URL.createObjectURL(blob);
118 link.download = filename;
119 link.style.display = 'none';
120 document.body.appendChild(link);
122 setTimeout(function(){
123 document.body.removeChild(link);
124 URL.revokeObjectURL(url);
128 const VNFImportActionHelper = {
130 open(dispatch, currentSoftwareProduct) {
131 getVNFMarketplace(dispatch, currentSoftwareProduct);
134 download(csarId, currSoftwareProduct) {
135 downloadCSARFile(csarId, currSoftwareProduct)
136 .then((blob, statusText, xhr) => showFileSaveDialog({blob, xhr, defaultFilename: 'MyNewCSAR.csar', addTimestamp: true}));
139 resetData(dispatch) {
142 type: modalActionTypes.GLOBAL_MODAL_CLOSE
146 type: actionTypes.RESET_DATA
150 getVNFMarketplace(dispatch) {
151 return getVNFMarketplace(dispatch);
154 uploadData(currSoftwareProduct, csarId, dispatch) {
155 this.resetData(dispatch);
156 uploadVNFData(csarId, currSoftwareProduct, dispatch);
160 export default VNFImportActionHelper;