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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 21 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 22 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 23 | import { actionTypes as softwareProductsActionTypes } from '../onboarding/softwareProduct/SoftwareProductConstants.js'; |
| 24 | import { actionTypes as HeatSetupActions } from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 25 | |
| 26 | const options = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 27 | headers: { |
| 28 | USER_ID: 'validationOnlyVspUser' |
| 29 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 30 | }; |
| 31 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 32 | function fetchVspIdAndVersion() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | let vspId = sessionStorage.getItem('validationAppVspId'); |
| 34 | let versionId = sessionStorage.getItem('validationAppVersionId'); |
| 35 | if (vspId) { |
| 36 | return Promise.resolve({ value: vspId, versionId }); |
| 37 | } else { |
| 38 | return RestAPIUtil.fetch( |
| 39 | '/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/validation-vsp', |
| 40 | options |
| 41 | ).then(response => { |
| 42 | sessionStorage.setItem('validationAppVspId', response.itemId); |
| 43 | sessionStorage.setItem( |
| 44 | 'validationAppVersionId', |
| 45 | response.version.id |
| 46 | ); |
| 47 | return Promise.resolve({ |
| 48 | value: response.itemId, |
| 49 | versionId: response.version.id |
| 50 | }); |
| 51 | }); |
| 52 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | } |
| 54 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 55 | function uploadFile(formData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 56 | return fetchVspIdAndVersion().then(response => { |
| 57 | return RestAPIUtil.post( |
| 58 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 59 | response.value |
| 60 | }/versions/${response.versionId}/orchestration-template-candidate`, |
| 61 | formData, |
| 62 | options |
| 63 | ); |
| 64 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 65 | } |
| 66 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 67 | function loadSoftwareProductHeatCandidate(dispatch) { |
| 68 | return fetchVspIdAndVersion().then(response => { |
| 69 | return RestAPIUtil.fetch( |
| 70 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 71 | response.value |
| 72 | }/versions/${ |
| 73 | response.versionId |
| 74 | }/orchestration-template-candidate/manifest`, |
| 75 | options |
| 76 | ).then(response => |
| 77 | dispatch({ |
| 78 | type: HeatSetupActions.MANIFEST_LOADED, |
| 79 | response |
| 80 | }) |
| 81 | ); |
| 82 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | function updateHeatCandidate(dispatch, heatCandidate) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 86 | return fetchVspIdAndVersion().then(response => { |
| 87 | return RestAPIUtil.put( |
| 88 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 89 | response.value |
| 90 | }/versions/${ |
| 91 | response.versionId |
| 92 | }/orchestration-template-candidate/manifest`, |
| 93 | heatCandidate.heatData, |
| 94 | options |
| 95 | ).then(null, error => { |
| 96 | dispatch({ |
| 97 | type: modalActionTypes.GLOBAL_MODAL_ERROR, |
| 98 | data: { |
| 99 | title: i18n('Save Failed'), |
| 100 | modalComponentName: |
| 101 | modalContentMapper.SUMBIT_ERROR_RESPONSE, |
| 102 | modalComponentProps: { |
| 103 | validationResponse: error.responseJSON |
| 104 | }, |
| 105 | cancelButtonText: i18n('Ok') |
| 106 | } |
| 107 | }); |
| 108 | return Promise.reject(error); |
| 109 | }); |
| 110 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | function fetchSoftwareProduct() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 114 | return fetchVspIdAndVersion().then(response => { |
| 115 | return RestAPIUtil.fetch( |
| 116 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 117 | response.value |
| 118 | }/versions/${response.versionId}`, |
| 119 | options |
| 120 | ); |
| 121 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | function downloadHeatFile() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 125 | return fetchVspIdAndVersion().then(response => { |
| 126 | RestAPIUtil.fetch( |
| 127 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 128 | response.value |
| 129 | }/versions/${response.versionId}/orchestration-template-candidate`, |
| 130 | { |
| 131 | ...options, |
| 132 | dataType: 'binary' |
| 133 | } |
| 134 | ).done(response => |
| 135 | showFileSaveDialog({ |
| 136 | blob: response.blob, |
| 137 | headers: response.headers, |
| 138 | defaultFilename: 'HEAT_file.zip', |
| 139 | addTimestamp: true |
| 140 | }) |
| 141 | ); |
| 142 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | function processAndValidateHeatCandidate(dispatch) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 146 | return fetchVspIdAndVersion().then(response => { |
| 147 | return RestAPIUtil.put( |
| 148 | `/sdc1/feProxy/onboarding-api/v1.0/vendor-software-products/${ |
| 149 | response.value |
| 150 | }/versions/${ |
| 151 | response.versionId |
| 152 | }/orchestration-template-candidate/process`, |
| 153 | {}, |
| 154 | options |
| 155 | ).then(response => { |
| 156 | if (response.status === 'Success') { |
| 157 | fetchSoftwareProduct().then(response => { |
| 158 | dispatch({ |
| 159 | type: |
| 160 | softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED, |
| 161 | response |
| 162 | }); |
| 163 | }); |
| 164 | } |
| 165 | }); |
| 166 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | const UploadScreenActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | uploadFile(dispatch, formData) { |
| 171 | return Promise.resolve() |
| 172 | .then(() => uploadFile(formData)) |
| 173 | .then(response => { |
| 174 | dispatch({ |
| 175 | type: softwareProductsActionTypes.SOFTWARE_PRODUCT_LOADED, |
| 176 | response |
| 177 | }); |
| 178 | dispatch({ |
| 179 | type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, |
| 180 | payload: {} |
| 181 | }); |
| 182 | loadSoftwareProductHeatCandidate(dispatch); |
| 183 | }) |
| 184 | .catch(error => { |
| 185 | dispatch({ |
| 186 | type: modalActionTypes.GLOBAL_MODAL_ERROR, |
| 187 | data: { |
| 188 | title: i18n('File Upload Failed'), |
| 189 | msg: error.responseJSON.message, |
| 190 | cancelButtonText: i18n('Ok') |
| 191 | } |
| 192 | }); |
| 193 | }); |
| 194 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 195 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 196 | processAndValidateHeat(dispatch, heatData, heatDataCache) { |
| 197 | return isEqual(heatData, heatDataCache) |
| 198 | ? Promise.resolve() |
| 199 | : updateHeatCandidate(dispatch, heatData) |
| 200 | .then(() => processAndValidateHeatCandidate(dispatch)) |
| 201 | .then(() => |
| 202 | dispatch({ |
| 203 | type: HeatSetupActions.FILL_HEAT_SETUP_CACHE, |
| 204 | payload: cloneDeep(heatData) |
| 205 | }) |
| 206 | ); |
| 207 | }, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 208 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 209 | downloadHeatFile() { |
| 210 | return downloadHeatFile(); |
| 211 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | export default UploadScreenActionHelper; |