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 HTTP_CSP_ATTUID: '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 fetchVspId() {
42 let vspId = sessionStorage.getItem('validationAppVspId');
44 return Promise.resolve({value: vspId});
46 return RestAPIUtil.fetch('/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', options)
48 sessionStorage.setItem('validationAppVspId', response.value);
49 return Promise.resolve(response);
56 function showFileSaveDialog({blob, xhr, defaultFilename, addTimestamp}) {
58 let contentDisposition = xhr.getResponseHeader('content-disposition');
59 let match = contentDisposition ? contentDisposition.match(/filename=(.*?)(;|$)/) : false;
63 filename = defaultFilename;
67 filename = filename.replace(/(^.*?)\.([^.]+$)/, `$1_${getTimestampString()}.$2`);
70 let link = document.createElement('a');
71 let url = URL.createObjectURL(blob);
73 link.download = filename;
74 link.style.display = 'none';
75 document.body.appendChild(link);
77 setTimeout(function(){
78 document.body.removeChild(link);
79 URL.revokeObjectURL(url);
84 function uploadFile(formData) {
87 return RestAPIUtil.post(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1/orchestration-template-candidate`, formData, options);
91 function loadSoftwareProductHeatCandidate(dispatch){
94 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1/orchestration-template-candidate/manifest`, options)
95 .then(response => dispatch({
96 type: HeatSetupActions.MANIFEST_LOADED,
102 function updateHeatCandidate(dispatch, heatCandidate) {
105 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1/orchestration-template-candidate/manifest`,
106 heatCandidate.heatData, options)
107 .then(null, error => {
109 type: modalActionTypes.GLOBAL_MODAL_ERROR,
111 title: i18n('Save Failed'),
112 modalComponentName: modalContentMapper.SUMBIT_ERROR_RESPONSE,
113 modalComponentProps: {
114 validationResponse: error.responseJSON
116 cancelButtonText: i18n('Ok')
119 return Promise.reject(error);
124 function fetchSoftwareProduct() {
127 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1`, options);
131 function downloadHeatFile() {
134 RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1/orchestration-template-candidate`, {
138 .done((blob, statusText, xhr) => showFileSaveDialog({
141 defaultFilename: 'HEAT_file.zip',
147 function processAndValidateHeatCandidate(dispatch) {
150 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/0.1/orchestration-template-candidate/process`, {}, options)
152 if (response.status === 'Success') {
153 fetchSoftwareProduct().then(response => {
155 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
164 const UploadScreenActionHelper = {
165 uploadFile(dispatch, formData) {
167 return Promise.resolve()
168 .then(() => uploadFile(formData))
171 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
175 type: HeatSetupActions.FILL_HEAT_SETUP_CACHE,
178 loadSoftwareProductHeatCandidate(dispatch);
182 type: modalActionTypes.GLOBAL_MODAL_ERROR,
184 title: i18n('File Upload Failed'),
185 msg: error.responseJSON.message,
186 cancelButtonText: i18n('Ok')
192 processAndValidateHeat(dispatch, heatData, heatDataCache){
193 return isEqual(heatData, heatDataCache) ? Promise.resolve() :
194 updateHeatCandidate(dispatch, heatData)
195 .then(() => processAndValidateHeatCandidate(dispatch))
196 .then(() => dispatch({type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, payload: cloneDeep(heatData)}));
200 return downloadHeatFile();
204 export default UploadScreenActionHelper;