[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / compute / ComputeFlavorActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and 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 {actionTypes as globalModalActionTypes, modalSizes} from 'nfvo-components/modal/GlobalModalConstants.js';
22 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
23 import {COMPONENTS_COMPUTE_QUESTIONNAIRE} from 'sdc-app/onboarding/softwareProduct/components/SoftwareProductComponentsConstants.js';
24
25 function baseUrl(softwareProductId, componentId, version) {
26         const versionId = version.id;
27         const restPrefix = Configuration.get('restPrefix');
28         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components/${componentId}/compute-flavors`;
29 }
30
31 function baseUrlVSPLevel(softwareProductId, version){
32         const versionId = version.id;
33         const restPrefix = Configuration.get('restPrefix');
34         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/compute-flavors`;
35 }
36
37 function fetchComputesList(softwareProductId, componentId, version){
38         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId, version)}`);
39 }
40
41 function fetchComputesListForVSP(softwareProductId, version){
42         return RestAPIUtil.fetch(`${baseUrlVSPLevel(softwareProductId, version)}`);
43 }
44
45 function fetchCompute(softwareProductId, componentId, computeId, version) {
46         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId, version)}/${computeId}`);
47 }
48
49 function fetchComputeQuestionnaire({softwareProductId, componentId, computeId, version}) {
50         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId, version)}/${computeId}/questionnaire`);
51 }
52
53 function postCompute({softwareProductId, componentId, compute, version}) {
54         return RestAPIUtil.post(baseUrl(softwareProductId, componentId, version), compute);
55 }
56
57 function putCompute({softwareProductId, componentId, compute, version}) {
58         const computeData = {
59                 name: compute.name,
60                 description: compute.description
61         };
62         return RestAPIUtil.put(`${baseUrl(softwareProductId, componentId, version)}/${compute.id}`, computeData);
63 }
64
65 function putComputeQuestionnaire({softwareProductId, componentId, computeId, qdata, version}) {
66         return RestAPIUtil.put(`${baseUrl(softwareProductId, componentId, version)}/${computeId}/questionnaire`, qdata);
67 }
68
69 function deleteCompute({softwareProductId, componentId, computeId, version}) {
70         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, componentId, version)}/${computeId}`);
71 }
72
73
74 const ComputeFlavorActionHelper = {
75         openComputeEditor(dispatch, {props}) {
76                 dispatch({
77                         type: actionTypes.computeEditor.LOAD_EDITOR_DATA,
78                         compute: props.compute || {}
79                 });
80                 dispatch({
81                         type: globalModalActionTypes.GLOBAL_MODAL_SHOW,
82                         data: {
83                                 modalComponentName: modalContentMapper.COMPONENT_COMPUTE_FLAVOR_EDITOR,
84                                 modalClassName: `compute-flavor-editor-modal-${props.compute ? 'edit' : 'create'}`,
85                                 modalComponentProps: {...props, size: props.compute ? modalSizes.LARGE : undefined, dialogClassName:'compute-flavor-editor-modal'},
86                                 title: `${props.compute ? i18n('Edit Compute Flavor') : i18n('Create New Compute Flavor')}`
87                         }
88                 });
89         },
90
91         closeComputeEditor(dispatch){
92                 dispatch({
93                         type: globalModalActionTypes.GLOBAL_MODAL_CLOSE
94                 });
95                 dispatch({
96                         type: actionTypes.computeEditor.CLEAR_DATA
97                 });
98         },
99
100         fetchComputesList(dispatch, {softwareProductId, componentId, version}) {
101                 return fetchComputesList(softwareProductId, componentId, version).then(response => dispatch({
102                         type: actionTypes.COMPUTE_FLAVORS_LIST_LOADED,
103                         response
104                 }));
105         },
106
107         fetchComputesListForVSP(dispatch, {softwareProductId, version}) {
108                 return fetchComputesListForVSP(softwareProductId, version).then(response => dispatch({
109                         type: actionTypes.COMPUTE_FLAVORS_LIST_LOADED,
110                         response
111                 }));
112         },
113
114         loadComputeData({softwareProductId, componentId, computeId, version}) {
115                 return fetchCompute(softwareProductId, componentId, computeId, version);
116         },
117
118         loadComputeQuestionnaire(dispatch, {softwareProductId, componentId, computeId, version}) {
119                 return fetchComputeQuestionnaire({softwareProductId, componentId, computeId, version}).then(response =>
120                         ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_COMPUTE_QUESTIONNAIRE ,response: {
121                                 qdata: response.data ? JSON.parse(response.data) : {},
122                                 qschema: JSON.parse(response.schema)
123                         }})
124                 );
125         },
126
127         loadCompute(dispatch, {softwareProductId, componentId, version, computeId, isReadOnlyMode}){
128                 return ComputeFlavorActionHelper.loadComputeData({softwareProductId, componentId, computeId, version}).then(({data}) =>
129                         ComputeFlavorActionHelper.loadComputeQuestionnaire(dispatch, {softwareProductId, componentId, computeId, version}).then(() =>
130                                 ComputeFlavorActionHelper.openComputeEditor(dispatch, {props: {softwareProductId, componentId, version, isReadOnlyMode, compute: {id: computeId, ...data}}})
131                         ));
132         },
133
134         saveComputeDataAndQuestionnaire(dispatch, {softwareProductId, componentId, data: compute, qdata, version}) {
135                 ComputeFlavorActionHelper.closeComputeEditor(dispatch);
136                 if(compute.id) {
137                         return Promise.all([
138                                 putComputeQuestionnaire({softwareProductId, componentId, computeId: compute.id, qdata, version}),
139                                 putCompute({softwareProductId, componentId, compute, version}).then(() => {
140                                         dispatch({
141                                                 type: actionTypes.COMPUTE_LIST_EDIT,
142                                                 compute
143                                         });
144                                 })
145                         ]);
146                 }
147                 else {
148                         return postCompute({softwareProductId, componentId, compute, version}).then(response =>
149                                 dispatch({
150                                         type: actionTypes.ADD_COMPUTE,
151                                         compute: {
152                                                 ...compute,
153                                                 id: response.id,
154                                                 componentId
155                                         }
156                                 })
157                         );
158                 }
159         },
160
161         deleteCompute(dispatch, {softwareProductId, componentId, computeId, version}) {
162                 return deleteCompute({softwareProductId, componentId, computeId, version}).then(() => dispatch({
163                         type: actionTypes.DELETE_COMPUTE,
164                         computeId
165                 }));
166         }
167 };
168
169 export default ComputeFlavorActionHelper;