[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / SoftwareProductComponentsActionHelper.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, COMPONENTS_QUESTIONNAIRE} from './SoftwareProductComponentsConstants.js';
20 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21 import SoftwareProductComponentsImageActionHelper from './images/SoftwareProductComponentsImageActionHelper.js';
22 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
23
24 function baseUrl(softwareProductId, version) {
25         const versionId = version.id;
26         const restPrefix = Configuration.get('restPrefix');
27         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components`;
28 }
29
30 function fetchSoftwareProductComponents(softwareProductId, version) {
31         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
32 }
33
34 function putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, vspComponent) {
35         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`, vspComponent);
36 }
37
38 function fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId){
39         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`);
40 }
41
42 function fetchSoftwareProductComponent(softwareProductId, version, vspComponentId){
43         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}`);
44 }
45
46 function putSoftwareProductComponent(softwareProductId, version, vspComponentId, vspComponent) {
47         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}`, {
48                 name: vspComponent.name,
49                 displayName: vspComponent.displayName,
50                 vfcCode: vspComponent.vfcCode,
51                 nfcFunction: vspComponent.nfcFunction,
52                 description: vspComponent.description
53         });
54 }
55
56 function deleteSoftwareProductComponent(softwareProductId, componentId, version) {
57         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version)}/${componentId}`,);
58 }
59
60
61 function postSoftwareProductComponent(softwareProductId, vspComponent, version) {
62
63         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
64                 name: vspComponent.displayName,
65                 displayName: vspComponent.displayName,
66                 description: vspComponent.description
67         });
68 }
69
70
71 const SoftwareProductComponentsActionHelper = {
72         fetchSoftwareProductComponents(dispatch, {softwareProductId, version, isFetchImageDetails = false}) {
73                 return fetchSoftwareProductComponents(softwareProductId, version).then(response => {
74                         let componentImagesCalls = [];
75                         if (isFetchImageDetails && response.listCount) {
76                                 response.results.map(component => {
77                                         let componentId = component.id;
78                                         componentImagesCalls[componentImagesCalls.length] =
79                                                 SoftwareProductComponentsImageActionHelper.fetchImagesList(dispatch, {
80                                                         softwareProductId,
81                                                         componentId,
82                                                         version
83                                                 });
84
85                                 });
86                                 return Promise.all(componentImagesCalls).then(() => {
87                                         dispatch({
88                                                 type: actionTypes.COMPONENTS_LIST_UPDATE,
89                                                 componentsList: response.results
90                                         });
91                                 });
92                         } else {
93                                 dispatch({
94                                         type: actionTypes.COMPONENTS_LIST_UPDATE,
95                                         componentsList: response.results
96                                 });
97                         }
98                 });
99         },
100
101         updateSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId, componentData, qdata}) {
102                 return Promise.all([
103                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}),
104                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData})
105                 ]);
106         },
107
108         updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}) {
109                 return putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, qdata);
110         },
111
112         updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData}) {
113                 return putSoftwareProductComponent(softwareProductId, version, vspComponentId, componentData).then(() => dispatch({
114                         type: actionTypes.COMPONENTS_LIST_EDIT,
115                         component: {
116                                 id: vspComponentId,
117                                 ...componentData
118                         }
119                 }));
120         },
121
122         fetchSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId}) {
123                 return fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
124                         ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
125                                 qschema: JSON.parse(response.schema)}});
126                 });
127         },
128
129         fetchSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId}) {
130                 return Promise.all([
131                         fetchSoftwareProductComponent(softwareProductId, version, vspComponentId).then(response => {
132                                 dispatch({
133                                         type: actionTypes.COMPONENT_LOAD,
134                                         component: response.data
135                                 });
136                                 return response;
137                         }),
138                         fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
139                                 ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
140                                         qschema: JSON.parse(response.schema)}});
141                         })
142                 ]);
143         },
144
145
146         clearComponentsStore(dispatch) {
147                 dispatch({
148                         type: actionTypes.COMPONENTS_LIST_UPDATE,
149                         componentsList: []
150                 });
151         },
152
153         createSoftwareProductComponent(dispatch,{softwareProductId, componentData, version}) {
154                 SoftwareProductComponentsActionHelper.closeComponentCreationModal(dispatch);
155                 /* for mock only */
156
157                 dispatch({
158                         type: actionTypes.COMPONENTS_LIST_UPDATE,
159                         componentsList: [{id: '123', ...componentData}]
160                 });
161
162                 postSoftwareProductComponent(softwareProductId, componentData, version).then(() => {
163                         SoftwareProductComponentsActionHelper.fetchSoftwareProductComponents(dispatch, {softwareProductId, version});
164                 });
165         },
166
167         clearComponentCreationData(dispatch) {
168                 dispatch({
169                         type: actionTypes.COMPONENT_DATA_CLEAR
170                 });
171         },
172
173         closeComponentCreationModal(dispatch) {
174                 dispatch({
175                         type: modalActionTypes.GLOBAL_MODAL_CLOSE
176                 });
177                 SoftwareProductComponentsActionHelper.clearComponentCreationData(dispatch);
178         },
179
180         deleteComponent(dispatch, {softwareProductId, componentId, version}) {
181                 deleteSoftwareProductComponent(softwareProductId, componentId, version);
182                 dispatch({
183                         type: actionTypes.COMPONENT_DELETE,
184                         componentId: componentId
185                 });
186         },
187
188
189
190 };
191
192 export default SoftwareProductComponentsActionHelper;