bc061469b1894d945ab6128af4d137526029a252
[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
19 import {actionTypes} from './SoftwareProductComponentsNetworkConstants.js';
20 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21 import {NIC_QUESTIONNAIRE} from 'sdc-app/onboarding/softwareProduct/components/network/SoftwareProductComponentsNetworkConstants.js';
22
23 function baseUrl(softwareProductId, version, componentId) {
24         const versionId = version.id;
25         const restPrefix = Configuration.get('restPrefix');
26         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components/${componentId}/nics`;
27 }
28
29
30 function fetchNICQuestionnaire({softwareProductId, version, componentId, nicId}) {
31         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${nicId}/questionnaire`);
32 }
33
34 function fetchNIC({softwareProductId, version, componentId, nicId}) {
35         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${nicId}`);
36 }
37
38 function fetchNICsList({softwareProductId, version, componentId}) {
39         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}`);
40 }
41
42 function saveNIC({softwareProductId, version, componentId, nic: {id, name, description, networkId}}) {
43         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${id}`,{
44                 name,
45                 description,
46                 networkId
47         });
48 }
49
50 function saveNICQuestionnaire({softwareProductId, version, componentId, nicId, qdata}) {
51         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${nicId}/questionnaire`, qdata);
52 }
53
54 const SoftwareProductComponentNetworkActionHelper = {
55
56         fetchNICsList(dispatch, {softwareProductId, version, componentId}) {
57                 return fetchNICsList({softwareProductId, version, componentId}).then((response) => {
58                         dispatch({
59                                 type: actionTypes.NIC_LIST_UPDATE,
60                                 response: response.results
61                         });
62                 });
63         },
64
65         openNICEditor(dispatch, {nic = {}, data = {}}) {
66                 dispatch({
67                         type: actionTypes.NICEditor.OPEN,
68                         nic: {...data, id: nic.id}
69                 });
70         },
71
72         closeNICEditor(dispatch) {
73                 dispatch({
74                         type: actionTypes.NICEditor.CLOSE
75                 });
76         },
77
78         loadNICData({softwareProductId, version, componentId, nicId}) {
79                 return fetchNIC({softwareProductId, version, componentId, nicId});
80         },
81
82         loadNICQuestionnaire(dispatch, {softwareProductId, version, componentId, nicId}) {
83                 return fetchNICQuestionnaire({softwareProductId, version, componentId, nicId}).then((response) => {
84                         ValidationHelper.qDataLoaded(dispatch, {qName: NIC_QUESTIONNAIRE ,response: {
85                                 qdata: response.data ? JSON.parse(response.data) : {},
86                                 qschema: JSON.parse(response.schema)
87                         }});
88                 });
89         },
90
91         saveNICDataAndQuestionnaire(dispatch, {softwareProductId, version, componentId, data, qdata}) {
92                 SoftwareProductComponentNetworkActionHelper.closeNICEditor(dispatch);
93                 return Promise.all([
94                         saveNICQuestionnaire({softwareProductId, version, componentId, nicId: data.id, qdata}),
95                         saveNIC({softwareProductId, version, componentId, nic: data}).then(() => {
96                                 dispatch({
97                                         type: actionTypes.NIC_LIST_EDIT,
98                                         nic: data
99                                 });
100                         })
101                 ]);
102         }
103 };
104
105 export default SoftwareProductComponentNetworkActionHelper;