Add collaboration feature
[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({softwareProductId, version, componentId, nicId}) {
42         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${nicId}/questionnaire`);
43 }
44
45 function fetchNIC({softwareProductId, version, componentId, nicId}) {
46         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${nicId}`);
47 }
48
49 function fetchNICsList({softwareProductId, version, componentId}) {
50         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}`);
51 }
52
53 function deleteNIC({softwareProductId, componentId, nicId, version}) {
54         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version, componentId)}/${nicId}`);
55 }
56 function saveNIC({softwareProductId, version, componentId, nic: {id, name, description, networkId, networkType, networkDescription}}) {
57         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${id}`,{
58                 name,
59                 description,
60                 networkId,
61                 networkDescription,
62                 networkType
63         });
64 }
65
66 function saveNICQuestionnaire({softwareProductId, version, componentId, nicId, qdata}) {
67         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${nicId}/questionnaire`, qdata);
68 }
69
70 const SoftwareProductComponentNetworkActionHelper = {
71
72         fetchNICsList(dispatch, {softwareProductId, version, componentId}) {
73                 return fetchNICsList({softwareProductId, version, componentId}).then((response) => {
74                         dispatch({
75                                 type: actionTypes.NIC_LIST_UPDATE,
76                                 response: response.results
77                         });
78                 });
79         },
80
81         openNICEditor(dispatch, {nic = {}, data = {}, softwareProductId, componentId, isReadOnlyMode, modalClassName, version}) {
82                 dispatch({
83                         type: actionTypes.NICEditor.FILL_DATA,
84                         nic: {...data, id: nic.id}
85                 });
86                 dispatch({
87                         type: GlobalModalActions.GLOBAL_MODAL_SHOW,
88                         data: {
89                                 modalClassName,
90                                 modalComponentProps: {softwareProductId, componentId, isReadOnlyMode, version},
91                                 modalComponentName: modalPagesMapper.NIC_EDITOR,
92                                 title: i18n('Edit NIC')
93                         }
94                 });
95         },
96
97         closeNICEditor(dispatch) {
98                 dispatch({
99                         type: GlobalModalActions.GLOBAL_MODAL_CLOSE
100                 });
101                 dispatch({
102                         type: actionTypes.NICEditor.CLEAR_DATA
103                 });
104         },
105
106         createNIC(dispatch, {nic, softwareProductId, componentId, version}){
107                 return createNIC({nic, vspId: softwareProductId, componentId, version}).then(() => {
108                         return SoftwareProductComponentNetworkActionHelper.fetchNICsList(dispatch, {softwareProductId, componentId, version});
109                 });
110         },
111         loadNICData({softwareProductId, version, componentId, nicId}) {
112                 return fetchNIC({softwareProductId, version, componentId, nicId});
113         },
114
115         deleteNIC(dispatch, {softwareProductId, componentId, nicId, version}) {
116                 return deleteNIC({softwareProductId, componentId, nicId, version}).then(() => {
117                         return SoftwareProductComponentNetworkActionHelper.fetchNICsList(dispatch, {softwareProductId, componentId, version});
118                 });
119         },
120         loadNICQuestionnaire(dispatch, {softwareProductId, version, componentId, nicId}) {
121                 return fetchNICQuestionnaire({softwareProductId, version, componentId, nicId}).then((response) => {
122                         ValidationHelper.qDataLoaded(dispatch, {qName: NIC_QUESTIONNAIRE ,response: {
123                                 qdata: response.data ? JSON.parse(response.data) : {},
124                                 qschema: JSON.parse(response.schema)
125                         }});
126                 });
127         },
128
129         saveNICDataAndQuestionnaire(dispatch, {softwareProductId, version, componentId, data, qdata}) {
130                 SoftwareProductComponentNetworkActionHelper.closeNICEditor(dispatch);
131                 return Promise.all([
132                         saveNICQuestionnaire({softwareProductId, version, componentId, nicId: data.id, qdata}),
133                         saveNIC({softwareProductId, version, componentId, nic: data}).then(() => {
134                                 dispatch({
135                                         type: actionTypes.NIC_LIST_EDIT,
136                                         nic: data
137                                 });
138                         })
139                 ]);
140         }
141 };
142
143 export default SoftwareProductComponentNetworkActionHelper;