Integrate VNF Repository with SDC
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / vnfMarketPlace / VNFImportActionHelper.js
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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 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';
24
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`;
30 }
31
32 function getVNFMarketplace(dispatch, currentSoftwareProduct) {
33         return RestAPIUtil.fetch(`${baseUrl(currentSoftwareProduct)}/vnfpackages`, {
34                 isAnonymous: false
35         })
36         .then((response) => {
37                 dispatch({
38                         type: actionTypes.OPEN,
39                         response
40                 });
41                 dispatch({
42                         type: modalActionTypes.GLOBAL_MODAL_SHOW,
43                         data: {
44                                 modalComponentName: modalContentMapper.VNF_IMPORT,
45                                 title: i18n('Browse VNF'),                              
46                                 modalComponentProps: {
47                                         currentSoftwareProduct,
48                                         size: modalSizes.LARGE                                  
49                                 }
50                         }
51                 });
52         })
53         .catch((error) => {
54                 let errMessage = error.responseJSON ? error.responseJSON.message : i18n('VNF import failed msg');
55                 
56                 dispatch({                                      
57                         type: modalActionTypes.GLOBAL_MODAL_ERROR,
58                         data: {
59                                 title: i18n('VNF import failed title'),
60                                 msg: errMessage,
61                                 cancelButtonText: i18n('Ok')
62                         }
63                 });
64         });
65 }
66
67 function downloadCSARFile(csarId, currSoftwareProduct) {
68         let url = `${baseUrl(currSoftwareProduct)}/vnfpackage/${csarId}/download`;
69         return RestAPIUtil.fetch(url, {
70                 dataType: 'binary',
71                 isAnonymous: false
72         });
73 }
74
75 function getFileName(xhr, defaultFilename) {
76         let filename = '';
77         let contentDisposition = xhr.getResponseHeader('Content-Disposition') ? xhr.getResponseHeader('Content-Disposition') : '';
78         let match = contentDisposition.match(/filename=(.*?)(;|$)/);
79         if (match) {
80                 filename = match[1].replace(/['"]/g, '');;
81         }
82         else {
83                 filename = defaultFilename;
84         }
85         return filename;
86 }
87
88 function uploadVNFData(csarId, currSoftwareProduct, dispatch) {
89
90         let softwareProductId = currSoftwareProduct.id;
91         let version = currSoftwareProduct.version;
92
93         SoftwareProductActionHelper.uploadVNFFile(dispatch, {
94                 csarId,
95                 currSoftwareProduct,    
96                 failedNotificationTitle: i18n('Upload validation failed'),
97                 softwareProductId,
98                 version
99         });
100 }
101
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())}`;
106 }
107
108 function showFileSaveDialog({blob, xhr, defaultFilename, addTimestamp}) {
109         let filename = getFileName(xhr, defaultFilename);
110
111         if (addTimestamp) {
112                 filename = filename.replace(/(^.*?)\.([^.]+$)/, `$1_${getTimestampString()}.$2`);
113         }
114
115         let link = document.createElement('a');
116         let url = URL.createObjectURL(blob);
117         link.href = url;
118         link.download = filename;
119         link.style.display = 'none';
120         document.body.appendChild(link);
121         link.click();
122         setTimeout(function(){
123                 document.body.removeChild(link);
124                 URL.revokeObjectURL(url);
125         }, 0);
126 }
127
128 const VNFImportActionHelper = {
129
130         open(dispatch, currentSoftwareProduct) {
131                 getVNFMarketplace(dispatch, currentSoftwareProduct);
132         },
133
134         download(csarId, currSoftwareProduct) {
135                 downloadCSARFile(csarId, currSoftwareProduct)
136                         .then((blob, statusText, xhr) => showFileSaveDialog({blob, xhr, defaultFilename: 'MyNewCSAR.csar', addTimestamp: true}));
137         },
138
139         resetData(dispatch) {
140
141                 dispatch({
142                         type: modalActionTypes.GLOBAL_MODAL_CLOSE
143                 });
144
145                 dispatch({
146                         type: actionTypes.RESET_DATA
147                 });
148         },
149
150         getVNFMarketplace(dispatch) {
151                 return getVNFMarketplace(dispatch);
152         },
153
154         uploadData(currSoftwareProduct, csarId, dispatch) {
155                 this.resetData(dispatch);
156                 uploadVNFData(csarId, currSoftwareProduct, dispatch);
157         }
158 };
159
160 export default VNFImportActionHelper;