73a971ccbb4f3aa228cbb45c8b0fc1dbf8f63e2e
[sdc.git] /
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 {
20     actionTypes,
21     COMPONENTS_QUESTIONNAIRE
22 } from './SoftwareProductComponentsConstants.js';
23 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
24 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
25
26 function baseUrl(softwareProductId, version) {
27     const versionId = version.id;
28     const restPrefix = Configuration.get('restPrefix');
29     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components`;
30 }
31
32 function fetchSoftwareProductComponents(softwareProductId, version) {
33     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
34 }
35
36 function putSoftwareProductComponentQuestionnaire(
37     softwareProductId,
38     version,
39     vspComponentId,
40     vspComponent
41 ) {
42     return RestAPIUtil.put(
43         `${baseUrl(
44             softwareProductId,
45             version
46         )}/${vspComponentId}/questionnaire`,
47         vspComponent
48     );
49 }
50
51 function fetchSoftwareProductComponentQuestionnaire(
52     softwareProductId,
53     version,
54     vspComponentId
55 ) {
56     return RestAPIUtil.fetch(
57         `${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`
58     );
59 }
60
61 function fetchSoftwareProductComponent(
62     softwareProductId,
63     version,
64     vspComponentId
65 ) {
66     return RestAPIUtil.fetch(
67         `${baseUrl(softwareProductId, version)}/${vspComponentId}`
68     );
69 }
70
71 function putSoftwareProductComponent(
72     softwareProductId,
73     version,
74     vspComponentId,
75     vspComponent
76 ) {
77     return RestAPIUtil.put(
78         `${baseUrl(softwareProductId, version)}/${vspComponentId}`,
79         {
80             name: vspComponent.name,
81             displayName: vspComponent.displayName,
82             vfcCode: vspComponent.vfcCode,
83             nfcFunction: vspComponent.nfcFunction,
84             description: vspComponent.description
85         }
86     );
87 }
88
89 function deleteSoftwareProductComponent(
90     softwareProductId,
91     componentId,
92     version
93 ) {
94     return RestAPIUtil.destroy(
95         `${baseUrl(softwareProductId, version)}/${componentId}`
96     );
97 }
98
99 function postSoftwareProductComponent(
100     softwareProductId,
101     vspComponent,
102     version
103 ) {
104     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
105         name: vspComponent.displayName,
106         displayName: vspComponent.displayName,
107         description: vspComponent.description
108     });
109 }
110
111 const SoftwareProductComponentsActionHelper = {
112     fetchSoftwareProductComponents(dispatch, { softwareProductId, version }) {
113         return fetchSoftwareProductComponents(softwareProductId, version).then(
114             response => {
115                 dispatch({
116                     type: actionTypes.COMPONENTS_LIST_UPDATE,
117                     componentsList: response.results
118                 });
119                 return response;
120             }
121         );
122     },
123
124     updateSoftwareProductComponent(
125         dispatch,
126         { softwareProductId, version, vspComponentId, componentData, qdata }
127     ) {
128         return Promise.all([
129             SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(
130                 dispatch,
131                 { softwareProductId, version, vspComponentId, qdata }
132             ),
133             SoftwareProductComponentsActionHelper.updateSoftwareProductComponentData(
134                 dispatch,
135                 { softwareProductId, version, vspComponentId, componentData }
136             )
137         ]);
138     },
139
140     updateSoftwareProductComponentQuestionnaire(
141         dispatch,
142         { softwareProductId, version, vspComponentId, qdata }
143     ) {
144         return putSoftwareProductComponentQuestionnaire(
145             softwareProductId,
146             version,
147             vspComponentId,
148             qdata
149         );
150     },
151
152     updateSoftwareProductComponentData(
153         dispatch,
154         { softwareProductId, version, vspComponentId, componentData }
155     ) {
156         return putSoftwareProductComponent(
157             softwareProductId,
158             version,
159             vspComponentId,
160             componentData
161         ).then(() =>
162             dispatch({
163                 type: actionTypes.COMPONENTS_LIST_EDIT,
164                 component: {
165                     id: vspComponentId,
166                     ...componentData
167                 }
168             })
169         );
170     },
171
172     fetchSoftwareProductComponentQuestionnaire(
173         dispatch,
174         { softwareProductId, version, vspComponentId }
175     ) {
176         return fetchSoftwareProductComponentQuestionnaire(
177             softwareProductId,
178             version,
179             vspComponentId
180         ).then(response => {
181             ValidationHelper.qDataLoaded(dispatch, {
182                 qName: COMPONENTS_QUESTIONNAIRE,
183                 response: {
184                     qdata: response.data ? JSON.parse(response.data) : {},
185                     qschema: JSON.parse(response.schema)
186                 }
187             });
188         });
189     },
190
191     fetchSoftwareProductComponent(
192         dispatch,
193         { softwareProductId, version, vspComponentId }
194     ) {
195         return Promise.all([
196             fetchSoftwareProductComponent(
197                 softwareProductId,
198                 version,
199                 vspComponentId
200             ).then(response => {
201                 dispatch({
202                     type: actionTypes.COMPONENT_LOAD,
203                     component: response.data
204                 });
205                 return response;
206             }),
207             fetchSoftwareProductComponentQuestionnaire(
208                 softwareProductId,
209                 version,
210                 vspComponentId
211             ).then(response => {
212                 ValidationHelper.qDataLoaded(dispatch, {
213                     qName: COMPONENTS_QUESTIONNAIRE,
214                     response: {
215                         qdata: response.data ? JSON.parse(response.data) : {},
216                         qschema: JSON.parse(response.schema)
217                     }
218                 });
219             })
220         ]);
221     },
222
223     clearComponentsStore(dispatch) {
224         dispatch({
225             type: actionTypes.COMPONENTS_LIST_UPDATE,
226             componentsList: []
227         });
228     },
229
230     createSoftwareProductComponent(
231         dispatch,
232         { softwareProductId, componentData, version }
233     ) {
234         SoftwareProductComponentsActionHelper.closeComponentCreationModal(
235             dispatch
236         );
237         /* for mock only */
238
239         dispatch({
240             type: actionTypes.COMPONENTS_LIST_UPDATE,
241             componentsList: [{ id: '123', ...componentData }]
242         });
243
244         postSoftwareProductComponent(
245             softwareProductId,
246             componentData,
247             version
248         ).then(() => {
249             SoftwareProductComponentsActionHelper.fetchSoftwareProductComponents(
250                 dispatch,
251                 { softwareProductId, version }
252             );
253         });
254     },
255
256     clearComponentCreationData(dispatch) {
257         dispatch({
258             type: actionTypes.COMPONENT_DATA_CLEAR
259         });
260     },
261
262     closeComponentCreationModal(dispatch) {
263         dispatch({
264             type: modalActionTypes.GLOBAL_MODAL_CLOSE
265         });
266         SoftwareProductComponentsActionHelper.clearComponentCreationData(
267             dispatch
268         );
269     },
270
271     deleteComponent(dispatch, { softwareProductId, componentId, version }) {
272         deleteSoftwareProductComponent(softwareProductId, componentId, version);
273         dispatch({
274             type: actionTypes.COMPONENT_DELETE,
275             componentId: componentId
276         });
277     }
278 };
279
280 export default SoftwareProductComponentsActionHelper;