2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and 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';
22 function baseUrl(serviceId, artifactId = '') {
23 const restATTPrefix = Configuration.get('restATTPrefix');
24 return `${restATTPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
27 function encodeDataToBase64(dataAsString) {
28 return window.btoa(dataAsString);
31 function decodeDataToBase64(encodedData) {
32 return window.atob(encodedData);
35 function encodeContent(flowData) {
41 description: flowData.description,
42 sequenceDiagramModel: flowData.sequenceDiagramModel
45 return encodeDataToBase64(JSON.stringify(data));
48 function decodeContent(base64Contents) {
49 let description, sequenceDiagramModel;
50 let payload = JSON.parse(decodeDataToBase64(base64Contents));
52 if (payload.VERSION === undefined) {
53 description = payload.description || 'Please, provide description...';
54 sequenceDiagramModel = payload.data || payload;
55 sequenceDiagramModel = sequenceDiagramModel.model || sequenceDiagramModel;
58 else if (payload.VERSION.major === 1) {
59 description = payload.description;
60 sequenceDiagramModel = payload.sequenceDiagramModel;
69 function createOrUpdate(flowData) {
70 let createOrUpdateRequest = {
71 payloadData: encodeContent(flowData),
72 artifactLabel: flowData.artifactLabel || flowData.artifactName,
73 artifactName: flowData.artifactName,
74 artifactType: flowData.artifactType,
75 artifactGroupType: enums.INFORMATIONAL,
76 description: flowData.description
79 return RestAPIUtil.post(
80 baseUrl(flowData.serviceID, flowData.uniqueId),
81 createOrUpdateRequest,
86 const FlowsActions = Object.freeze({
88 fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID, readonly}) {
90 if (!Object.keys(artifacts).length) {
91 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
93 FlowsActions.openFlowDetailsEditor(dispatch);
97 Object.keys(artifacts).forEach(artifact => results.push({
98 artifactType: diagramType,
101 ...artifacts[artifact]
103 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
107 fetchArtifact(dispatch, {flow}){
108 let {serviceID, uniqueId, participants} = flow;
109 return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => {
111 let {artifactName, base64Contents} = response;
112 let {sequenceDiagramModel, ...other} = decodeContent(base64Contents);
114 if (!sequenceDiagramModel) {
115 sequenceDiagramModel = SequenceDiagramModelHelper.createModel({
118 lifelines: participants
122 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(sequenceDiagramModel, {
124 lifelines: participants
136 dispatch({type: actionTypes.ARTIFACT_LOADED, flow});
137 FlowsActions.openFlowDiagramEditor(dispatch, {flow});
141 createOrUpdateFlow(dispatch, {flow}, isNew) {
142 if (!isNew && flow.sequenceDiagramModel) {
143 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(flow.sequenceDiagramModel, {
144 name: flow.artifactName
147 return createOrUpdate(flow).then(response => {
148 let {uniqueId, artifactLabel} = response;
149 flow = {...flow, uniqueId, artifactLabel};
151 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel({id: uniqueId, name: flow.artifactName});
153 dispatch({type: actionTypes.ADD_OR_UPDATE_FLOW, flow});
157 deleteFlow(dispatch, {flow}) {
158 return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({
159 type: actionTypes.DELETE_FLOW,
164 openFlowDetailsEditor(dispatch, flow) {
165 dispatch({type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow});
168 closeFlowDetailsEditor(dispatch) {
169 dispatch({type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR});
172 openFlowDiagramEditor(dispatch, {flow}) {
173 dispatch({type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow});
176 closeFlowDiagramEditor(dispatch) {
177 dispatch({type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR});
181 dispatch({type: actionTypes.RESET});
185 export default FlowsActions;