react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / compute / ComputeFlavorActionHelper.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
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.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import { actionTypes } from './computeComponents/computeFlavor/ComputeFlavorConstants.js';
20 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
21 import {
22     actionTypes as globalModalActionTypes,
23     modalSizes
24 } from 'nfvo-components/modal/GlobalModalConstants.js';
25 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
26 import { COMPONENTS_COMPUTE_QUESTIONNAIRE } from 'sdc-app/onboarding/softwareProduct/components/SoftwareProductComponentsConstants.js';
27
28 function baseUrl(softwareProductId, componentId, version) {
29     const versionId = version.id;
30     const restPrefix = Configuration.get('restPrefix');
31     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components/${componentId}/compute-flavors`;
32 }
33
34 function baseUrlVSPLevel(softwareProductId, version) {
35     const versionId = version.id;
36     const restPrefix = Configuration.get('restPrefix');
37     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/compute-flavors`;
38 }
39
40 function fetchComputesList(softwareProductId, componentId, version) {
41     return RestAPIUtil.fetch(
42         `${baseUrl(softwareProductId, componentId, version)}`
43     );
44 }
45
46 function fetchComputesListForVSP(softwareProductId, version) {
47     return RestAPIUtil.fetch(`${baseUrlVSPLevel(softwareProductId, version)}`);
48 }
49
50 function fetchCompute(softwareProductId, componentId, computeId, version) {
51     return RestAPIUtil.fetch(
52         `${baseUrl(softwareProductId, componentId, version)}/${computeId}`
53     );
54 }
55
56 function fetchComputeQuestionnaire({
57     softwareProductId,
58     componentId,
59     computeId,
60     version
61 }) {
62     return RestAPIUtil.fetch(
63         `${baseUrl(
64             softwareProductId,
65             componentId,
66             version
67         )}/${computeId}/questionnaire`
68     );
69 }
70
71 function postCompute({ softwareProductId, componentId, compute, version }) {
72     return RestAPIUtil.post(
73         baseUrl(softwareProductId, componentId, version),
74         compute
75     );
76 }
77
78 function putCompute({ softwareProductId, componentId, compute, version }) {
79     const computeData = {
80         name: compute.name,
81         description: compute.description
82     };
83     return RestAPIUtil.put(
84         `${baseUrl(softwareProductId, componentId, version)}/${compute.id}`,
85         computeData
86     );
87 }
88
89 function putComputeQuestionnaire({
90     softwareProductId,
91     componentId,
92     computeId,
93     qdata,
94     version
95 }) {
96     return RestAPIUtil.put(
97         `${baseUrl(
98             softwareProductId,
99             componentId,
100             version
101         )}/${computeId}/questionnaire`,
102         qdata
103     );
104 }
105
106 function deleteCompute({ softwareProductId, componentId, computeId, version }) {
107     return RestAPIUtil.destroy(
108         `${baseUrl(softwareProductId, componentId, version)}/${computeId}`
109     );
110 }
111
112 const ComputeFlavorActionHelper = {
113     openComputeEditor(dispatch, { props }) {
114         dispatch({
115             type: actionTypes.computeEditor.LOAD_EDITOR_DATA,
116             compute: props.compute || {}
117         });
118         dispatch({
119             type: globalModalActionTypes.GLOBAL_MODAL_SHOW,
120             data: {
121                 modalComponentName:
122                     modalContentMapper.COMPONENT_COMPUTE_FLAVOR_EDITOR,
123                 modalComponentProps: {
124                     ...props,
125                     size: props.compute ? modalSizes.XLARGE : undefined,
126                     dialogClassName: 'compute-flavor-editor-modal'
127                 },
128                 title: `${
129                     props.compute
130                         ? i18n('Edit Compute Flavor')
131                         : i18n('Create New Compute Flavor')
132                 }`
133             }
134         });
135     },
136
137     closeComputeEditor(dispatch) {
138         dispatch({
139             type: globalModalActionTypes.GLOBAL_MODAL_CLOSE
140         });
141         dispatch({
142             type: actionTypes.computeEditor.CLEAR_DATA
143         });
144     },
145
146     fetchComputesList(dispatch, { softwareProductId, componentId, version }) {
147         return fetchComputesList(softwareProductId, componentId, version).then(
148             response =>
149                 dispatch({
150                     type: actionTypes.COMPUTE_FLAVORS_LIST_LOADED,
151                     response
152                 })
153         );
154     },
155
156     fetchComputesListForVSP(dispatch, { softwareProductId, version }) {
157         return fetchComputesListForVSP(softwareProductId, version).then(
158             response =>
159                 dispatch({
160                     type: actionTypes.COMPUTE_FLAVORS_LIST_LOADED,
161                     response
162                 })
163         );
164     },
165
166     loadComputeData({ softwareProductId, componentId, computeId, version }) {
167         return fetchCompute(softwareProductId, componentId, computeId, version);
168     },
169
170     loadComputeQuestionnaire(
171         dispatch,
172         { softwareProductId, componentId, computeId, version }
173     ) {
174         return fetchComputeQuestionnaire({
175             softwareProductId,
176             componentId,
177             computeId,
178             version
179         }).then(response =>
180             ValidationHelper.qDataLoaded(dispatch, {
181                 qName: COMPONENTS_COMPUTE_QUESTIONNAIRE,
182                 response: {
183                     qdata: response.data ? JSON.parse(response.data) : {},
184                     qschema: JSON.parse(response.schema)
185                 }
186             })
187         );
188     },
189
190     loadCompute(
191         dispatch,
192         { softwareProductId, componentId, version, computeId, isReadOnlyMode }
193     ) {
194         return ComputeFlavorActionHelper.loadComputeData({
195             softwareProductId,
196             componentId,
197             computeId,
198             version
199         }).then(({ data }) =>
200             ComputeFlavorActionHelper.loadComputeQuestionnaire(dispatch, {
201                 softwareProductId,
202                 componentId,
203                 computeId,
204                 version
205             }).then(() =>
206                 ComputeFlavorActionHelper.openComputeEditor(dispatch, {
207                     props: {
208                         softwareProductId,
209                         componentId,
210                         version,
211                         isReadOnlyMode,
212                         compute: { id: computeId, ...data }
213                     }
214                 })
215             )
216         );
217     },
218
219     saveComputeDataAndQuestionnaire(
220         dispatch,
221         { softwareProductId, componentId, data: compute, qdata, version }
222     ) {
223         ComputeFlavorActionHelper.closeComputeEditor(dispatch);
224         if (compute.id) {
225             return Promise.all([
226                 putComputeQuestionnaire({
227                     softwareProductId,
228                     componentId,
229                     computeId: compute.id,
230                     qdata,
231                     version
232                 }),
233                 putCompute({
234                     softwareProductId,
235                     componentId,
236                     compute,
237                     version
238                 }).then(() => {
239                     dispatch({
240                         type: actionTypes.COMPUTE_LIST_EDIT,
241                         compute
242                     });
243                 })
244             ]);
245         } else {
246             return postCompute({
247                 softwareProductId,
248                 componentId,
249                 compute,
250                 version
251             }).then(response =>
252                 dispatch({
253                     type: actionTypes.ADD_COMPUTE,
254                     compute: {
255                         ...compute,
256                         id: response.id,
257                         componentId
258                     }
259                 })
260             );
261         }
262     },
263
264     deleteCompute(
265         dispatch,
266         { softwareProductId, componentId, computeId, version }
267     ) {
268         return deleteCompute({
269             softwareProductId,
270             componentId,
271             computeId,
272             version
273         }).then(() =>
274             dispatch({
275                 type: actionTypes.DELETE_COMPUTE,
276                 computeId
277             })
278         );
279     }
280 };
281
282 export default ComputeFlavorActionHelper;