blob: f5d9abd6d2e52a7fca9f59b150d2f125e35215a7 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020021import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
22import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
23import { actionTypes as softwareProductsActionTypes } from '../onboarding/softwareProduct/SoftwareProductConstants.js';
24import { actionTypes as HeatSetupActions } from '../onboarding/softwareProduct/attachments/setup/HeatSetupConstants.js';
AviZi280f8012017-06-09 02:39:56 +030025
26const options = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020027 headers: {
28 USER_ID: 'validationOnlyVspUser'
29 }
AviZi280f8012017-06-09 02:39:56 +030030};
31
talig8e9c0652017-12-20 14:30:43 +020032function fetchVspIdAndVersion() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033 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 }
AviZi280f8012017-06-09 02:39:56 +030053}
54
Michael Landoefa037d2017-02-19 12:57:33 +020055function uploadFile(formData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020056 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 });
AviZi280f8012017-06-09 02:39:56 +030065}
66
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067function 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 });
AviZi280f8012017-06-09 02:39:56 +030083}
84
85function updateHeatCandidate(dispatch, heatCandidate) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 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 });
AviZi280f8012017-06-09 02:39:56 +0300111}
112
113function fetchSoftwareProduct() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200114 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 });
AviZi280f8012017-06-09 02:39:56 +0300122}
123
124function downloadHeatFile() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 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 });
AviZi280f8012017-06-09 02:39:56 +0300143}
144
145function processAndValidateHeatCandidate(dispatch) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200146 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 Landoefa037d2017-02-19 12:57:33 +0200167}
168
169const UploadScreenActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200170 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 Landoefa037d2017-02-19 12:57:33 +0200195
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200196 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 },
AviZi280f8012017-06-09 02:39:56 +0300208
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200209 downloadHeatFile() {
210 return downloadHeatFile();
211 }
Michael Landoefa037d2017-02-19 12:57:33 +0200212};
213
214export default UploadScreenActionHelper;