blob: 3c41b126e69253579e136840f64229d3269e7e7b [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
AviZi280f8012017-06-09 02:39:56 +030017import i18n from 'nfvo-utils/i18n/i18n.js';
18import isEqual from 'lodash/isEqual.js';
19import cloneDeep from 'lodash/cloneDeep.js';
20import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
21import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022import {actionTypes as softwareProductsActionTypes} from '../onboarding/softwareProduct/SoftwareProductConstants.js';
AviZi280f8012017-06-09 02:39:56 +030023import {actionTypes as HeatSetupActions} from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js';
24
25
26
27const options = {
28 headers: {
talig8e9c0652017-12-20 14:30:43 +020029 USER_ID: 'validationOnlyVspUser'
AviZi280f8012017-06-09 02:39:56 +030030 }
31};
32
33
34function 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())}`;
38}
39
talig8e9c0652017-12-20 14:30:43 +020040function fetchVspIdAndVersion() {
AviZi280f8012017-06-09 02:39:56 +030041
42 let vspId = sessionStorage.getItem('validationAppVspId');
talig8e9c0652017-12-20 14:30:43 +020043 let versionId = sessionStorage.getItem('validationAppVersionId');
AviZi280f8012017-06-09 02:39:56 +030044 if (vspId) {
talig8e9c0652017-12-20 14:30:43 +020045 return Promise.resolve({value: vspId, versionId});
AviZi280f8012017-06-09 02:39:56 +030046 }else {
47 return RestAPIUtil.fetch('/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', options)
48 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020049 sessionStorage.setItem('validationAppVspId', response.itemId);
50 sessionStorage.setItem('validationAppVersionId', response.version.id);
51 return Promise.resolve({value: response.itemId, versionId: response.version.id});
AviZi280f8012017-06-09 02:39:56 +030052 });
53 }
54
55}
56
57
58function showFileSaveDialog({blob, xhr, defaultFilename, addTimestamp}) {
59 let filename;
60 let contentDisposition = xhr.getResponseHeader('content-disposition');
61 let match = contentDisposition ? contentDisposition.match(/filename=(.*?)(;|$)/) : false;
62 if (match) {
63 filename = match[1];
64 } else {
65 filename = defaultFilename;
66 }
67
68 if (addTimestamp) {
69 filename = filename.replace(/(^.*?)\.([^.]+$)/, `$1_${getTimestampString()}.$2`);
70 }
71
72 let link = document.createElement('a');
73 let url = URL.createObjectURL(blob);
74 link.href = url;
75 link.download = filename;
76 link.style.display = 'none';
77 document.body.appendChild(link);
78 link.click();
79 setTimeout(function(){
80 document.body.removeChild(link);
81 URL.revokeObjectURL(url);
82 }, 0);
83}
84
Michael Landoefa037d2017-02-19 12:57:33 +020085
86function uploadFile(formData) {
talig8e9c0652017-12-20 14:30:43 +020087 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030088 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020089 return RestAPIUtil.post(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, formData, options);
AviZi280f8012017-06-09 02:39:56 +030090 });
91}
92
93function loadSoftwareProductHeatCandidate(dispatch){
talig8e9c0652017-12-20 14:30:43 +020094 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030095 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020096 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`, options)
AviZi280f8012017-06-09 02:39:56 +030097 .then(response => dispatch({
98 type: HeatSetupActions.MANIFEST_LOADED,
99 response
100 }));
101 });
102}
103
104function updateHeatCandidate(dispatch, heatCandidate) {
talig8e9c0652017-12-20 14:30:43 +0200105 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300106 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200107 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`,
AviZi280f8012017-06-09 02:39:56 +0300108 heatCandidate.heatData, options)
109 .then(null, error => {
110 dispatch({
111 type: modalActionTypes.GLOBAL_MODAL_ERROR,
112 data: {
113 title: i18n('Save Failed'),
114 modalComponentName: modalContentMapper.SUMBIT_ERROR_RESPONSE,
115 modalComponentProps: {
116 validationResponse: error.responseJSON
117 },
118 cancelButtonText: i18n('Ok')
119 }
120 });
121 return Promise.reject(error);
122 });
123 });
124}
125
126function fetchSoftwareProduct() {
talig8e9c0652017-12-20 14:30:43 +0200127 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300128 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200129 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}`, options);
AviZi280f8012017-06-09 02:39:56 +0300130 });
131}
132
133function downloadHeatFile() {
talig8e9c0652017-12-20 14:30:43 +0200134 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300135 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200136 RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, {
AviZi280f8012017-06-09 02:39:56 +0300137 ...options,
138 dataType: 'binary'
139 })
140 .done((blob, statusText, xhr) => showFileSaveDialog({
141 blob,
142 xhr,
143 defaultFilename: 'HEAT_file.zip',
144 addTimestamp: true
145 }));
146 });
147}
148
149function processAndValidateHeatCandidate(dispatch) {
talig8e9c0652017-12-20 14:30:43 +0200150 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300151 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200152 return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/process`, {}, options)
AviZi280f8012017-06-09 02:39:56 +0300153 .then(response => {
154 if (response.status === 'Success') {
155 fetchSoftwareProduct().then(response => {
156 dispatch({
157 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
158 response
159 });
160 });
161 }
162 });
163 });
Michael Landoefa037d2017-02-19 12:57:33 +0200164}
165
166const UploadScreenActionHelper = {
167 uploadFile(dispatch, formData) {
168
AviZi280f8012017-06-09 02:39:56 +0300169 return Promise.resolve()
Michael Landoefa037d2017-02-19 12:57:33 +0200170 .then(() => uploadFile(formData))
171 .then(response => {
172 dispatch({
173 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
174 response
175 });
Michael Landoefa037d2017-02-19 12:57:33 +0200176 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300177 type: HeatSetupActions.FILL_HEAT_SETUP_CACHE,
178 payload:{}
Michael Landoefa037d2017-02-19 12:57:33 +0200179 });
AviZi280f8012017-06-09 02:39:56 +0300180 loadSoftwareProductHeatCandidate(dispatch);
Michael Landoefa037d2017-02-19 12:57:33 +0200181 })
182 .catch(error => {
AviZi280f8012017-06-09 02:39:56 +0300183 dispatch({
184 type: modalActionTypes.GLOBAL_MODAL_ERROR,
185 data: {
186 title: i18n('File Upload Failed'),
187 msg: error.responseJSON.message,
188 cancelButtonText: i18n('Ok')
189 }
Michael Landoefa037d2017-02-19 12:57:33 +0200190 });
191 });
192 },
AviZi280f8012017-06-09 02:39:56 +0300193
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)}));
199 },
200
201 downloadHeatFile(){
202 return downloadHeatFile();
203 },
Michael Landoefa037d2017-02-19 12:57:33 +0200204};
205
206export default UploadScreenActionHelper;