blob: fa2d4695ae0e443487f879e80e6bd8460bfa3068 [file] [log] [blame]
ilanap1965d162018-01-04 11:34:59 +02001/*
2 * Copyright © 2016-2017 European Support Limited
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 *
ilanap1965d162018-01-04 11:34:59 +02008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
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,
ilanap1965d162018-01-04 11:34:59 +020012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
ilanap1965d162018-01-04 11:34:59 +020017import showFileSaveDialog from 'nfvo-utils/ShowFileSaveDialog.js';
AviZi280f8012017-06-09 02:39:56 +030018import i18n from 'nfvo-utils/i18n/i18n.js';
19import isEqual from 'lodash/isEqual.js';
20import cloneDeep from 'lodash/cloneDeep.js';
21import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
22import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020023import {actionTypes as softwareProductsActionTypes} from '../onboarding/softwareProduct/SoftwareProductConstants.js';
AviZi280f8012017-06-09 02:39:56 +030024import {actionTypes as HeatSetupActions} from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js';
25
26
27
28const options = {
29 headers: {
talig8e9c0652017-12-20 14:30:43 +020030 USER_ID: 'validationOnlyVspUser'
AviZi280f8012017-06-09 02:39:56 +030031 }
32};
33
talig8e9c0652017-12-20 14:30:43 +020034function fetchVspIdAndVersion() {
AviZi280f8012017-06-09 02:39:56 +030035
36 let vspId = sessionStorage.getItem('validationAppVspId');
talig8e9c0652017-12-20 14:30:43 +020037 let versionId = sessionStorage.getItem('validationAppVersionId');
AviZi280f8012017-06-09 02:39:56 +030038 if (vspId) {
talig8e9c0652017-12-20 14:30:43 +020039 return Promise.resolve({value: vspId, versionId});
AviZi280f8012017-06-09 02:39:56 +030040 }else {
41 return RestAPIUtil.fetch('/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', options)
42 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020043 sessionStorage.setItem('validationAppVspId', response.itemId);
44 sessionStorage.setItem('validationAppVersionId', response.version.id);
45 return Promise.resolve({value: response.itemId, versionId: response.version.id});
AviZi280f8012017-06-09 02:39:56 +030046 });
47 }
48
49}
50
51
Michael Landoefa037d2017-02-19 12:57:33 +020052function uploadFile(formData) {
talig8e9c0652017-12-20 14:30:43 +020053 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030054 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020055 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 +030056 });
57}
58
59function loadSoftwareProductHeatCandidate(dispatch){
talig8e9c0652017-12-20 14:30:43 +020060 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030061 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020062 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 +030063 .then(response => dispatch({
64 type: HeatSetupActions.MANIFEST_LOADED,
65 response
66 }));
67 });
68}
69
70function updateHeatCandidate(dispatch, heatCandidate) {
talig8e9c0652017-12-20 14:30:43 +020071 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030072 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020073 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 +030074 heatCandidate.heatData, options)
75 .then(null, error => {
76 dispatch({
77 type: modalActionTypes.GLOBAL_MODAL_ERROR,
78 data: {
79 title: i18n('Save Failed'),
80 modalComponentName: modalContentMapper.SUMBIT_ERROR_RESPONSE,
81 modalComponentProps: {
82 validationResponse: error.responseJSON
83 },
84 cancelButtonText: i18n('Ok')
85 }
86 });
87 return Promise.reject(error);
88 });
89 });
90}
91
92function fetchSoftwareProduct() {
talig8e9c0652017-12-20 14:30:43 +020093 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +030094 .then(response => {
talig8e9c0652017-12-20 14:30:43 +020095 return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}`, options);
AviZi280f8012017-06-09 02:39:56 +030096 });
97}
98
99function downloadHeatFile() {
talig8e9c0652017-12-20 14:30:43 +0200100 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300101 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200102 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 +0300103 ...options,
104 dataType: 'binary'
105 })
ilanap1965d162018-01-04 11:34:59 +0200106 .done((response) => showFileSaveDialog({
107 blob: response.blob,
108 headers: response.headers,
AviZi280f8012017-06-09 02:39:56 +0300109 defaultFilename: 'HEAT_file.zip',
110 addTimestamp: true
111 }));
112 });
113}
114
115function processAndValidateHeatCandidate(dispatch) {
talig8e9c0652017-12-20 14:30:43 +0200116 return fetchVspIdAndVersion()
AviZi280f8012017-06-09 02:39:56 +0300117 .then(response => {
talig8e9c0652017-12-20 14:30:43 +0200118 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 +0300119 .then(response => {
120 if (response.status === 'Success') {
121 fetchSoftwareProduct().then(response => {
122 dispatch({
123 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
124 response
125 });
126 });
127 }
128 });
129 });
Michael Landoefa037d2017-02-19 12:57:33 +0200130}
131
132const UploadScreenActionHelper = {
133 uploadFile(dispatch, formData) {
134
AviZi280f8012017-06-09 02:39:56 +0300135 return Promise.resolve()
Michael Landoefa037d2017-02-19 12:57:33 +0200136 .then(() => uploadFile(formData))
137 .then(response => {
138 dispatch({
139 type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED,
140 response
141 });
Michael Landoefa037d2017-02-19 12:57:33 +0200142 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300143 type: HeatSetupActions.FILL_HEAT_SETUP_CACHE,
144 payload:{}
Michael Landoefa037d2017-02-19 12:57:33 +0200145 });
AviZi280f8012017-06-09 02:39:56 +0300146 loadSoftwareProductHeatCandidate(dispatch);
Michael Landoefa037d2017-02-19 12:57:33 +0200147 })
148 .catch(error => {
AviZi280f8012017-06-09 02:39:56 +0300149 dispatch({
150 type: modalActionTypes.GLOBAL_MODAL_ERROR,
151 data: {
152 title: i18n('File Upload Failed'),
153 msg: error.responseJSON.message,
154 cancelButtonText: i18n('Ok')
155 }
Michael Landoefa037d2017-02-19 12:57:33 +0200156 });
157 });
158 },
AviZi280f8012017-06-09 02:39:56 +0300159
160 processAndValidateHeat(dispatch, heatData, heatDataCache){
161 return isEqual(heatData, heatDataCache) ? Promise.resolve() :
162 updateHeatCandidate(dispatch, heatData)
163 .then(() => processAndValidateHeatCandidate(dispatch))
164 .then(() => dispatch({type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, payload: cloneDeep(heatData)}));
165 },
166
167 downloadHeatFile(){
168 return downloadHeatFile();
169 },
Michael Landoefa037d2017-02-19 12:57:33 +0200170};
171
172export default UploadScreenActionHelper;