Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / network / SoftwareProductComponentsNetworkActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23
24 import {actionTypes} from './SoftwareProductComponentsNetworkConstants.js';
25
26 function baseUrl(softwareProductId, componentId) {
27         const restPrefix = Configuration.get('restPrefix');
28         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/components/${componentId}/nics`;
29 }
30
31
32 function fetchNICQuestionnaire({softwareProductId, componentId, nicId, version}) {
33         let versionQuery = version ? `?version=${version}` : '';
34         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId)}/${nicId}/questionnaire${versionQuery}`);
35 }
36
37 function fetchNIC({softwareProductId, componentId, nicId, version}) {
38         let versionQuery = version ? `?version=${version}` : '';
39         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId)}/${nicId}${versionQuery}`);
40 }
41
42 function fetchNICsList({softwareProductId, componentId, version}) {
43         let versionQuery = version ? `?version=${version}` : '';
44         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId)}${versionQuery}`);
45 }
46
47 function saveNIC({softwareProductId, componentId, nic: {id, name, description, networkId}}) {
48         return RestAPIUtil.save(`${baseUrl(softwareProductId, componentId)}/${id}`,{
49                 name,
50                 description,
51                 networkId
52         });
53 }
54
55 function saveNICQuestionnaire({softwareProductId, componentId, nicId, qdata}) {
56         return RestAPIUtil.save(`${baseUrl(softwareProductId, componentId)}/${nicId}/questionnaire`, qdata);
57 }
58
59 const SoftwareProductComponentNetworkActionHelper = {
60
61         fetchNICsList(dispatch, {softwareProductId, componentId, version}) {
62                 return fetchNICsList({softwareProductId, componentId, version}).then((response) => {
63                         dispatch({
64                                 type: actionTypes.NIC_LIST_UPDATE,
65                                 response: response.results
66                         });
67                 });
68         },
69
70         openNICEditor(dispatch, {nic = {}, data = {}}) {
71                 dispatch({
72                         type: actionTypes.NICEditor.OPEN,
73                         nic: {...data, id: nic.id}
74                 });
75         },
76
77         closeNICEditor(dispatch) {
78                 dispatch({
79                         type: actionTypes.NICEditor.CLOSE
80                 });
81         },
82
83         loadNICData({softwareProductId, componentId, nicId, version}) {
84                 return fetchNIC({softwareProductId, componentId, nicId, version});
85         },
86
87         loadNICQuestionnaire(dispatch, {softwareProductId, componentId, nicId, version}) {
88                 return fetchNICQuestionnaire({softwareProductId, componentId, nicId, version}).then((response) => {
89                         dispatch({
90                                 type: actionTypes.NICEditor.NIC_QUESTIONNAIRE_UPDATE,
91                                 payload: {
92                                         qdata: response.data ? JSON.parse(response.data) : {},
93                                         qschema: JSON.parse(response.schema)
94                                 }
95                         });
96                 });
97         },
98
99         updateNICData(dispatch, {deltaData}) {
100                 dispatch({
101                         type: actionTypes.NICEditor.DATA_CHANGED,
102                         deltaData
103                 });
104         },
105
106         updateNICQuestionnaire(dispatch, {data}) {
107                 dispatch({
108                         type: actionTypes.NICEditor.NIC_QUESTIONNAIRE_UPDATE,
109                         payload: {
110                                 qdata: data
111                         }
112                 });
113         },
114
115         saveNICDataAndQuestionnaire(dispatch, {softwareProductId, componentId, data, qdata}) {
116                 SoftwareProductComponentNetworkActionHelper.closeNICEditor(dispatch);
117                 return Promise.all([
118                         saveNICQuestionnaire({softwareProductId, componentId, nicId: data.id, qdata}),
119                         saveNIC({softwareProductId, componentId, nic: data}).then(() => {
120                                 dispatch({
121                                         type: actionTypes.NIC_LIST_EDIT,
122                                         nic: data
123                                 });
124                         })
125                 ]);
126         }
127 };
128
129 export default SoftwareProductComponentNetworkActionHelper;