blob: 4ad7dcfdc8c9df9a12a8de0c3d77ea9b292dbd0c [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes, enums } from './FlowsConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020019import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030020import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
21import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
22import i18n from 'nfvo-utils/i18n/i18n.js';
Michael Landoefa037d2017-02-19 12:57:33 +020023
Michael Landoefa037d2017-02-19 12:57:33 +020024function baseUrl(serviceId, artifactId = '') {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 const restCatalogPrefix = Configuration.get('restCatalogPrefix');
26 return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
29function encodeDataToBase64(dataAsString) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 return window.btoa(dataAsString);
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
33function decodeDataToBase64(encodedData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020034 return window.atob(encodedData);
Michael Landoefa037d2017-02-19 12:57:33 +020035}
36
37function encodeContent(flowData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020038 let data = {
39 VERSION: {
40 major: 1,
41 minor: 0
42 },
43 description: flowData.description,
44 sequenceDiagramModel: flowData.sequenceDiagramModel
45 };
Michael Landoefa037d2017-02-19 12:57:33 +020046
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047 return encodeDataToBase64(JSON.stringify(data));
Michael Landoefa037d2017-02-19 12:57:33 +020048}
49
50function decodeContent(base64Contents) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 let description, sequenceDiagramModel;
52 let payload = JSON.parse(decodeDataToBase64(base64Contents));
Michael Landoefa037d2017-02-19 12:57:33 +020053
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 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 }
Michael Landoefa037d2017-02-19 12:57:33 +020063
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 return {
65 description,
66 sequenceDiagramModel
67 };
Michael Landoefa037d2017-02-19 12:57:33 +020068}
69
70function createOrUpdate(flowData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020071 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 };
Michael Landoefa037d2017-02-19 12:57:33 +020079
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020080 return RestAPIUtil.post(
81 baseUrl(flowData.serviceID, flowData.uniqueId),
82 createOrUpdateRequest,
83 { md5: true }
84 );
Michael Landoefa037d2017-02-19 12:57:33 +020085}
86
87const FlowsActions = Object.freeze({
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 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) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300103 FlowsActions.openEditCreateWFModal(dispatch);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 }
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 },
Michael Landoefa037d2017-02-19 12:57:33 +0200124
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 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 );
Michael Landoefa037d2017-02-19 12:57:33 +0200133
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200134 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 }
Michael Landoefa037d2017-02-19 12:57:33 +0200151
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200152 flow = {
153 ...flow,
154 ...other,
155 uniqueId,
156 artifactName,
157 sequenceDiagramModel
158 };
Michael Landoefa037d2017-02-19 12:57:33 +0200159
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200160 dispatch({ type: actionTypes.ARTIFACT_LOADED, flow });
161 FlowsActions.openFlowDiagramEditor(dispatch, { flow });
162 }
163 );
164 },
Michael Landoefa037d2017-02-19 12:57:33 +0200165
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200166 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 },
Michael Landoefa037d2017-02-19 12:57:33 +0200189
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200190 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 },
Michael Landoefa037d2017-02-19 12:57:33 +0200199
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200200 openFlowDiagramEditor(dispatch, { flow }) {
201 dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow });
202 },
Michael Landoefa037d2017-02-19 12:57:33 +0200203
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200204 closeFlowDiagramEditor(dispatch) {
205 dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR });
206 },
Michael Landoefa037d2017-02-19 12:57:33 +0200207
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200208 reset(dispatch) {
209 dispatch({ type: actionTypes.RESET });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300210 },
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 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200231 }
Michael Landoefa037d2017-02-19 12:57:33 +0200232});
233
234export default FlowsActions;