ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2017 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 17 | import showFileSaveDialog from 'nfvo-utils/ShowFileSaveDialog.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 19 | import isEqual from 'lodash/isEqual.js'; |
| 20 | import cloneDeep from 'lodash/cloneDeep.js'; |
| 21 | import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 22 | import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | import {actionTypes as softwareProductsActionTypes} from '../onboarding/softwareProduct/SoftwareProductConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 24 | import {actionTypes as HeatSetupActions} from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js'; |
| 25 | |
| 26 | |
| 27 | |
| 28 | const options = { |
| 29 | headers: { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 30 | USER_ID: 'validationOnlyVspUser' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 31 | } |
| 32 | }; |
| 33 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | function fetchVspIdAndVersion() { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 35 | |
| 36 | let vspId = sessionStorage.getItem('validationAppVspId'); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 37 | let versionId = sessionStorage.getItem('validationAppVersionId'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 38 | if (vspId) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 39 | return Promise.resolve({value: vspId, versionId}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 40 | }else { |
| 41 | return RestAPIUtil.fetch('/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', options) |
| 42 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 43 | sessionStorage.setItem('validationAppVspId', response.itemId); |
| 44 | sessionStorage.setItem('validationAppVersionId', response.version.id); |
| 45 | return Promise.resolve({value: response.itemId, versionId: response.version.id}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 46 | }); |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 52 | function uploadFile(formData) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 53 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 54 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 55 | return RestAPIUtil.post(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, formData, options); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 56 | }); |
| 57 | } |
| 58 | |
| 59 | function loadSoftwareProductHeatCandidate(dispatch){ |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 60 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 61 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 62 | return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`, options) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | .then(response => dispatch({ |
| 64 | type: HeatSetupActions.MANIFEST_LOADED, |
| 65 | response |
| 66 | })); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | function updateHeatCandidate(dispatch, heatCandidate) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 71 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 72 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 73 | return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/manifest`, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 74 | 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 | |
| 92 | function fetchSoftwareProduct() { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 93 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 94 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 95 | return RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}`, options); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 96 | }); |
| 97 | } |
| 98 | |
| 99 | function downloadHeatFile() { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 100 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 101 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 102 | RestAPIUtil.fetch(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate`, { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 103 | ...options, |
| 104 | dataType: 'binary' |
| 105 | }) |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 106 | .done((response) => showFileSaveDialog({ |
| 107 | blob: response.blob, |
| 108 | headers: response.headers, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 109 | defaultFilename: 'HEAT_file.zip', |
| 110 | addTimestamp: true |
| 111 | })); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | function processAndValidateHeatCandidate(dispatch) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 116 | return fetchVspIdAndVersion() |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 117 | .then(response => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 118 | return RestAPIUtil.put(`/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${response.value}/versions/${response.versionId}/orchestration-template-candidate/process`, {}, options) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 119 | .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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | const UploadScreenActionHelper = { |
| 133 | uploadFile(dispatch, formData) { |
| 134 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 135 | return Promise.resolve() |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 136 | .then(() => uploadFile(formData)) |
| 137 | .then(response => { |
| 138 | dispatch({ |
| 139 | type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED, |
| 140 | response |
| 141 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 142 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 143 | type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, |
| 144 | payload:{} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 145 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 146 | loadSoftwareProductHeatCandidate(dispatch); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 147 | }) |
| 148 | .catch(error => { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 149 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 156 | }); |
| 157 | }); |
| 158 | }, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 159 | |
| 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | export default UploadScreenActionHelper; |