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