react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / flows / FlowsActions.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes, enums } from './FlowsConstants.js';
19 import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
20 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
21 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
22 import i18n from 'nfvo-utils/i18n/i18n.js';
23
24 function baseUrl(serviceId, artifactId = '') {
25     const restCatalogPrefix = Configuration.get('restCatalogPrefix');
26     return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
27 }
28
29 function encodeDataToBase64(dataAsString) {
30     return window.btoa(dataAsString);
31 }
32
33 function decodeDataToBase64(encodedData) {
34     return window.atob(encodedData);
35 }
36
37 function encodeContent(flowData) {
38     let data = {
39         VERSION: {
40             major: 1,
41             minor: 0
42         },
43         description: flowData.description,
44         sequenceDiagramModel: flowData.sequenceDiagramModel
45     };
46
47     return encodeDataToBase64(JSON.stringify(data));
48 }
49
50 function decodeContent(base64Contents) {
51     let description, sequenceDiagramModel;
52     let payload = JSON.parse(decodeDataToBase64(base64Contents));
53
54     if (payload.VERSION === undefined) {
55         description = payload.description || 'Please, provide description...';
56         sequenceDiagramModel = payload.data || payload;
57         sequenceDiagramModel =
58             sequenceDiagramModel.model || sequenceDiagramModel;
59     } else if (payload.VERSION.major === 1) {
60         description = payload.description;
61         sequenceDiagramModel = payload.sequenceDiagramModel;
62     }
63
64     return {
65         description,
66         sequenceDiagramModel
67     };
68 }
69
70 function createOrUpdate(flowData) {
71     let createOrUpdateRequest = {
72         payloadData: encodeContent(flowData),
73         artifactLabel: flowData.artifactLabel || flowData.artifactName,
74         artifactName: flowData.artifactName,
75         artifactType: flowData.artifactType,
76         artifactGroupType: enums.INFORMATIONAL,
77         description: flowData.description
78     };
79
80     return RestAPIUtil.post(
81         baseUrl(flowData.serviceID, flowData.uniqueId),
82         createOrUpdateRequest,
83         { md5: true }
84     );
85 }
86
87 const FlowsActions = Object.freeze({
88     fetchFlowArtifacts(
89         dispatch,
90         { artifacts, diagramType, participants, serviceID, readonly }
91     ) {
92         let results = [];
93         if (!Object.keys(artifacts).length) {
94             dispatch({
95                 type: actionTypes.FLOW_LIST_LOADED,
96                 results,
97                 participants,
98                 serviceID,
99                 diagramType,
100                 readonly
101             });
102             if (!readonly) {
103                 FlowsActions.openEditCreateWFModal(dispatch);
104             }
105         } else {
106             Object.keys(artifacts).forEach(artifact =>
107                 results.push({
108                     artifactType: diagramType,
109                     participants,
110                     serviceID,
111                     ...artifacts[artifact]
112                 })
113             );
114             dispatch({
115                 type: actionTypes.FLOW_LIST_LOADED,
116                 results,
117                 participants,
118                 serviceID,
119                 diagramType,
120                 readonly
121             });
122         }
123     },
124
125     fetchArtifact(dispatch, { flow }) {
126         let { serviceID, uniqueId, participants } = flow;
127         return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(
128             response => {
129                 let { artifactName, base64Contents } = response;
130                 let { sequenceDiagramModel, ...other } = decodeContent(
131                     base64Contents
132                 );
133
134                 if (!sequenceDiagramModel) {
135                     sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
136                         {
137                             id: uniqueId,
138                             name: artifactName,
139                             lifelines: participants
140                         }
141                     );
142                 } else {
143                     sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
144                         sequenceDiagramModel,
145                         {
146                             name: artifactName,
147                             lifelines: participants
148                         }
149                     );
150                 }
151
152                 flow = {
153                     ...flow,
154                     ...other,
155                     uniqueId,
156                     artifactName,
157                     sequenceDiagramModel
158                 };
159
160                 dispatch({ type: actionTypes.ARTIFACT_LOADED, flow });
161                 FlowsActions.openFlowDiagramEditor(dispatch, { flow });
162             }
163         );
164     },
165
166     createOrUpdateFlow(dispatch, { flow }, isNew) {
167         if (!isNew && flow.sequenceDiagramModel) {
168             flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
169                 flow.sequenceDiagramModel,
170                 {
171                     name: flow.artifactName
172                 }
173             );
174         }
175         return createOrUpdate(flow).then(response => {
176             let { uniqueId, artifactLabel } = response;
177             flow = { ...flow, uniqueId, artifactLabel };
178             if (isNew) {
179                 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
180                     {
181                         id: uniqueId,
182                         name: flow.artifactName
183                     }
184                 );
185             }
186             dispatch({ type: actionTypes.ADD_OR_UPDATE_FLOW, flow });
187         });
188     },
189
190     deleteFlow(dispatch, { flow }) {
191         return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(
192             () =>
193                 dispatch({
194                     type: actionTypes.DELETE_FLOW,
195                     flow
196                 })
197         );
198     },
199
200     openFlowDiagramEditor(dispatch, { flow }) {
201         dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow });
202     },
203
204     closeFlowDiagramEditor(dispatch) {
205         dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR });
206     },
207
208     reset(dispatch) {
209         dispatch({ type: actionTypes.RESET });
210     },
211     openEditCreateWFModal(dispatch, flow) {
212         dispatch({ type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow });
213         dispatch({
214             type: modalActionTypes.GLOBAL_MODAL_SHOW,
215             data: {
216                 modalComponentName: modalContentMapper.FLOWS_EDITOR,
217                 modalComponentProps: {
218                     isNewArtifact: Boolean(flow && flow.uniqueId)
219                 },
220                 title: flow
221                     ? i18n('Edit Workflow')
222                     : i18n('Create New Workflow')
223             }
224         });
225     },
226     closeEditCreateWFModal(dispatch) {
227         dispatch({
228             type: modalActionTypes.GLOBAL_MODAL_CLOSE
229         });
230         dispatch({ type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR });
231     }
232 });
233
234 export default FlowsActions;