2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
18 import isEqual from 'lodash/isEqual.js';
19 import cloneDeep from 'lodash/cloneDeep.js';
20 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
21 import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
22 import {actionTypes as softwareProductsActionTypes} from '../onboarding/softwareProduct/SoftwareProductConstants.js';
23 import {actionTypes as HeatSetupActions} from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js';
29 USER_ID: 'validationOnlyVspUser'
34 function getTimestampString() {
35 let date = new Date();
36 let z = n => n < 10 ? '0' + n : n;
37 return `${date.getFullYear()}-${z(date.getMonth())}-${z(date.getDate())}_${z(date.getHours())}-${z(date.getMinutes())}`;
40 function fetchVspIdAndVersion() {
42 let vspId = sessionStorage.getItem('validationAppVspId');
43 let versionId = sessionStorage.getItem('validationAppVersionId');
45 return Promise.resolve({value: vspId, versionId});
47 return RestAPIUtil.fetch('/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', options)
49 sessionStorage.setItem('validationAppVspId', response.itemId);
50 sessionStorage.setItem('validationAppVersionId', response.version.id);
51 return Promise.resolve({value: response.itemId, versionId: response.version.id});
58 function showFileSaveDialog({blob, xhr, defaultFilename, addTimestamp}) {
60 let contentDisposition = xhr.getResponseHeader('content-disposition');
61 let match = contentDisposition ? contentDisposition.match(/filename=(.*?)(;|$)/) : false;
65 filename = defaultFilename;
69 filename = filename.replace(/(^.*?)\.([^.]+$)/, `$1_${getTimestampString()}.$2`);
72 let link = document.createElement('a');
73 let url = URL.createObjectURL(blob);
75 link.download = filename;
76 link.style.display = 'none';
77 document.body.appendChild(link);
79 setTimeout(function(){
80 document.body.removeChild(link);
81 URL.revokeObjectURL(url);
86 function uploadFile(formData) {
87 return fetchVspIdAndVersion()
89 return RestAPIUtil.post(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, formData, options);
93 function loadSoftwareProductHeatCandidate(dispatch){
94 return fetchVspIdAndVersion()
96 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`, options)
97 .then(response => dispatch({
98 type: HeatSetupActions.MANIFEST_LOADED,
104 function updateHeatCandidate(dispatch, heatCandidate) {
105 return fetchVspIdAndVersion()
107 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`,
108 heatCandidate.heatData, options)
109 .then(null, error => {
111 type: modalActionTypes.GLOBAL_MODAL_ERROR,
113 title: i18n('Save Failed'),
114 modalComponentName: modalContentMapper.SUMBIT_ERROR_RESPONSE,
115 modalComponentProps: {
116 validationResponse: error.responseJSON
118 cancelButtonText: i18n('Ok')
121 return Promise.reject(error);
126 function fetchSoftwareProduct() {
127 return fetchVspIdAndVersion()
129 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}`, options);
133 function downloadHeatFile() {
134 return fetchVspIdAndVersion()
136 RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, {
140 .done((blob, statusText, xhr) => showFileSaveDialog({
143 defaultFilename: 'HEAT_file.zip',
149 function processAndValidateHeatCandidate(dispatch) {
150 return fetchVspIdAndVersion()
152 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/process`, {}, options)
154 if (response.status === 'Success') {
155 fetchSoftwareProduct().then(response => {
157 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
166 const UploadScreenActionHelper = {
167 uploadFile(dispatch, formData) {
169 return Promise.resolve()
170 .then(() => uploadFile(formData))
173 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
177 type: HeatSetupActions.FILL_HEAT_SETUP_CACHE,
180 loadSoftwareProductHeatCandidate(dispatch);
184 type: modalActionTypes.GLOBAL_MODAL_ERROR,
186 title: i18n('File Upload Failed'),
187 msg: error.responseJSON.message,
188 cancelButtonText: i18n('Ok')
194 processAndValidateHeat(dispatch, heatData, heatDataCache){
195 return isEqual(heatData, heatDataCache) ? Promise.resolve() :
196 updateHeatCandidate(dispatch, heatData)
197 .then(() => processAndValidateHeatCandidate(dispatch))
198 .then(() => dispatch({type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, payload: cloneDeep(heatData)}));
202 return downloadHeatFile();
206 export default UploadScreenActionHelper;