react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / network / SoftwareProductComponentsNetworkActionHelper.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
20 import { actionTypes } from './SoftwareProductComponentsNetworkConstants.js';
21 import {
22     actionTypes as GlobalModalActions,
23     modalSizes
24 } from 'nfvo-components/modal/GlobalModalConstants.js';
25 import { modalContentMapper as modalPagesMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
26 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
27 import { NIC_QUESTIONNAIRE } from 'sdc-app/onboarding/softwareProduct/components/network/SoftwareProductComponentsNetworkConstants.js';
28
29 function baseUrl(softwareProductId, version, componentId) {
30     const versionId = version.id;
31     const restPrefix = Configuration.get('restPrefix');
32     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components/${componentId}/nics`;
33 }
34
35 function createNIC({ nic, vspId, componentId, version }) {
36     return RestAPIUtil.post(baseUrl(vspId, version, componentId), {
37         name: nic.name,
38         description: nic.description,
39         networkDescription: nic.networkDescription,
40         networkType: nic.networkType
41     });
42 }
43
44 function fetchNICQuestionnaire({
45     softwareProductId,
46     version,
47     componentId,
48     nicId
49 }) {
50     return RestAPIUtil.fetch(
51         `${baseUrl(
52             softwareProductId,
53             version,
54             componentId
55         )}/${nicId}/questionnaire`
56     );
57 }
58
59 function fetchNIC({ softwareProductId, version, componentId, nicId }) {
60     return RestAPIUtil.fetch(
61         `${baseUrl(softwareProductId, version, componentId)}/${nicId}`
62     );
63 }
64
65 function fetchNICsList({ softwareProductId, version, componentId }) {
66     return RestAPIUtil.fetch(
67         `${baseUrl(softwareProductId, version, componentId)}`
68     );
69 }
70
71 function deleteNIC({ softwareProductId, componentId, nicId, version }) {
72     return RestAPIUtil.destroy(
73         `${baseUrl(softwareProductId, version, componentId)}/${nicId}`
74     );
75 }
76 function saveNIC({
77     softwareProductId,
78     version,
79     componentId,
80     nic: { id, name, description, networkId, networkType, networkDescription }
81 }) {
82     return RestAPIUtil.put(
83         `${baseUrl(softwareProductId, version, componentId)}/${id}`,
84         {
85             name,
86             description,
87             networkId,
88             networkDescription,
89             networkType
90         }
91     );
92 }
93
94 function saveNICQuestionnaire({
95     softwareProductId,
96     version,
97     componentId,
98     nicId,
99     qdata
100 }) {
101     return RestAPIUtil.put(
102         `${baseUrl(
103             softwareProductId,
104             version,
105             componentId
106         )}/${nicId}/questionnaire`,
107         qdata
108     );
109 }
110
111 const SoftwareProductComponentNetworkActionHelper = {
112     fetchNICsList(dispatch, { softwareProductId, version, componentId }) {
113         return fetchNICsList({ softwareProductId, version, componentId }).then(
114             response => {
115                 dispatch({
116                     type: actionTypes.NIC_LIST_UPDATE,
117                     response: response.results
118                 });
119             }
120         );
121     },
122
123     openNICEditor(
124         dispatch,
125         {
126             nic = {},
127             data = {},
128             softwareProductId,
129             componentId,
130             isReadOnlyMode,
131             version
132         }
133     ) {
134         dispatch({
135             type: actionTypes.NICEditor.FILL_DATA,
136             nic: { ...data, id: nic.id }
137         });
138         dispatch({
139             type: GlobalModalActions.GLOBAL_MODAL_SHOW,
140             data: {
141                 modalComponentProps: {
142                     softwareProductId,
143                     componentId,
144                     isReadOnlyMode,
145                     version,
146                     size: modalSizes.LARGE
147                 },
148                 modalComponentName: modalPagesMapper.NIC_EDITOR,
149                 title: i18n('Edit NIC')
150             }
151         });
152     },
153
154     closeNICEditor(dispatch) {
155         dispatch({
156             type: GlobalModalActions.GLOBAL_MODAL_CLOSE
157         });
158         dispatch({
159             type: actionTypes.NICEditor.CLEAR_DATA
160         });
161     },
162
163     createNIC(dispatch, { nic, softwareProductId, componentId, version }) {
164         return createNIC({
165             nic,
166             vspId: softwareProductId,
167             componentId,
168             version
169         }).then(() => {
170             return SoftwareProductComponentNetworkActionHelper.fetchNICsList(
171                 dispatch,
172                 { softwareProductId, componentId, version }
173             );
174         });
175     },
176     loadNICData({ softwareProductId, version, componentId, nicId }) {
177         return fetchNIC({ softwareProductId, version, componentId, nicId });
178     },
179
180     deleteNIC(dispatch, { softwareProductId, componentId, nicId, version }) {
181         return deleteNIC({
182             softwareProductId,
183             componentId,
184             nicId,
185             version
186         }).then(() => {
187             return SoftwareProductComponentNetworkActionHelper.fetchNICsList(
188                 dispatch,
189                 { softwareProductId, componentId, version }
190             );
191         });
192     },
193     loadNICQuestionnaire(
194         dispatch,
195         { softwareProductId, version, componentId, nicId }
196     ) {
197         return fetchNICQuestionnaire({
198             softwareProductId,
199             version,
200             componentId,
201             nicId
202         }).then(response => {
203             ValidationHelper.qDataLoaded(dispatch, {
204                 qName: NIC_QUESTIONNAIRE,
205                 response: {
206                     qdata: response.data ? JSON.parse(response.data) : {},
207                     qschema: JSON.parse(response.schema)
208                 }
209             });
210         });
211     },
212
213     saveNICDataAndQuestionnaire(
214         dispatch,
215         { softwareProductId, version, componentId, data, qdata }
216     ) {
217         SoftwareProductComponentNetworkActionHelper.closeNICEditor(dispatch);
218         return Promise.all([
219             saveNICQuestionnaire({
220                 softwareProductId,
221                 version,
222                 componentId,
223                 nicId: data.id,
224                 qdata
225             }),
226             saveNIC({
227                 softwareProductId,
228                 version,
229                 componentId,
230                 nic: data
231             }).then(() => {
232                 dispatch({
233                     type: actionTypes.NIC_LIST_EDIT,
234                     nic: data
235                 });
236             })
237         ]);
238     }
239 };
240
241 export default SoftwareProductComponentNetworkActionHelper;