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