2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
24 function baseUrl(serviceId, artifactId = '') {
25 const restCatalogPrefix = Configuration.get('restCatalogPrefix');
26 return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
29 function encodeDataToBase64(dataAsString) {
30 return window.btoa(dataAsString);
33 function decodeDataToBase64(encodedData) {
34 return window.atob(encodedData);
37 function encodeContent(flowData) {
43 description: flowData.description,
44 sequenceDiagramModel: flowData.sequenceDiagramModel
47 return encodeDataToBase64(JSON.stringify(data));
50 function decodeContent(base64Contents) {
51 let description, sequenceDiagramModel;
52 let payload = JSON.parse(decodeDataToBase64(base64Contents));
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;
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
80 return RestAPIUtil.post(
81 baseUrl(flowData.serviceID, flowData.uniqueId),
82 createOrUpdateRequest,
87 const FlowsActions = Object.freeze({
90 { artifacts, diagramType, participants, serviceID, readonly }
93 if (!Object.keys(artifacts).length) {
95 type: actionTypes.FLOW_LIST_LOADED,
103 FlowsActions.openEditCreateWFModal(dispatch);
106 Object.keys(artifacts).forEach(artifact =>
108 artifactType: diagramType,
111 ...artifacts[artifact]
115 type: actionTypes.FLOW_LIST_LOADED,
125 fetchArtifact(dispatch, { flow }) {
126 let { serviceID, uniqueId, participants } = flow;
127 return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(
129 let { artifactName, base64Contents } = response;
130 let { sequenceDiagramModel, ...other } = decodeContent(
134 if (!sequenceDiagramModel) {
135 sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
139 lifelines: participants
143 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
144 sequenceDiagramModel,
147 lifelines: participants
160 dispatch({ type: actionTypes.ARTIFACT_LOADED, flow });
161 FlowsActions.openFlowDiagramEditor(dispatch, { flow });
166 createOrUpdateFlow(dispatch, { flow }, isNew) {
167 if (!isNew && flow.sequenceDiagramModel) {
168 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
169 flow.sequenceDiagramModel,
171 name: flow.artifactName
175 return createOrUpdate(flow).then(response => {
176 let { uniqueId, artifactLabel } = response;
177 flow = { ...flow, uniqueId, artifactLabel };
179 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
182 name: flow.artifactName
186 dispatch({ type: actionTypes.ADD_OR_UPDATE_FLOW, flow });
190 deleteFlow(dispatch, { flow }) {
191 return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(
194 type: actionTypes.DELETE_FLOW,
200 openFlowDiagramEditor(dispatch, { flow }) {
201 dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow });
204 closeFlowDiagramEditor(dispatch) {
205 dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR });
209 dispatch({ type: actionTypes.RESET });
211 openEditCreateWFModal(dispatch, flow) {
212 dispatch({ type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow });
214 type: modalActionTypes.GLOBAL_MODAL_SHOW,
216 modalComponentName: modalContentMapper.FLOWS_EDITOR,
217 modalComponentProps: {
218 isNewArtifact: Boolean(flow && flow.uniqueId)
221 ? i18n('Edit Workflow')
222 : i18n('Create New Workflow')
226 closeEditCreateWFModal(dispatch) {
228 type: modalActionTypes.GLOBAL_MODAL_CLOSE
230 dispatch({ type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR });
234 export default FlowsActions;