2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23 import {actionTypes, enums} from './FlowsConstants.js';
24 import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
27 function baseUrl(serviceId, artifactId = '') {
28 const restATTPrefix = Configuration.get('restATTPrefix');
29 return `${restATTPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
32 function encodeDataToBase64(dataAsString) {
33 return window.btoa(dataAsString);
36 function decodeDataToBase64(encodedData) {
37 return window.atob(encodedData);
40 function encodeContent(flowData) {
46 description: flowData.description,
47 sequenceDiagramModel: flowData.sequenceDiagramModel
50 return encodeDataToBase64(JSON.stringify(data));
53 function decodeContent(base64Contents) {
54 let description, sequenceDiagramModel;
55 let payload = JSON.parse(decodeDataToBase64(base64Contents));
57 if (payload.VERSION === undefined) {
58 description = payload.description || 'Please, provide description...';
59 sequenceDiagramModel = payload.data || payload;
60 sequenceDiagramModel = sequenceDiagramModel.model || sequenceDiagramModel;
62 } else if (payload.VERSION.major === 1) {
63 description = payload.description;
64 sequenceDiagramModel = payload.sequenceDiagramModel;
73 function createOrUpdate(flowData) {
74 let createOrUpdateRequest = {
75 payloadData: encodeContent(flowData),
76 artifactLabel: flowData.artifactLabel || flowData.artifactName,
77 artifactName: flowData.artifactName,
78 artifactType: flowData.artifactType,
79 artifactGroupType: enums.INFORMATIONAL,
80 description: flowData.description
83 return RestAPIUtil.create(
84 baseUrl(flowData.serviceID, flowData.uniqueId),
85 createOrUpdateRequest,
90 const FlowsActions = Object.freeze({
92 fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID}) {
94 if (!Object.keys(artifacts).length) {
95 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType});
96 FlowsActions.openFlowDetailsEditor(dispatch);
99 Object.keys(artifacts).forEach(artifact => results.push({
100 artifactType: diagramType,
103 ...artifacts[artifact]
105 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType});
109 fetchArtifact(dispatch, {flow}){
110 let {serviceID, uniqueId, participants} = flow;
111 RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => {
113 let {artifactName, base64Contents} = response;
114 let {sequenceDiagramModel, ...other} = decodeContent(base64Contents);
116 if (!sequenceDiagramModel) {
117 sequenceDiagramModel = SequenceDiagramModelHelper.createModel({
120 lifelines: participants
124 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(sequenceDiagramModel, {
126 lifelines: participants
138 dispatch({type: actionTypes.ARTIFACT_LOADED, flow});
139 FlowsActions.openFlowDiagramEditor(dispatch, {flow});
143 createOrUpdateFlow(dispatch, {flow}, isNew) {
144 if (!isNew && flow.sequenceDiagramModel) {
145 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(flow.sequenceDiagramModel, {
146 name: flow.artifactName
149 createOrUpdate(flow).then(response => {
150 let {uniqueId, artifactLabel} = response;
151 flow = {...flow, uniqueId, artifactLabel};
153 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel({id: uniqueId, name: flow.artifactName});
155 dispatch({type: actionTypes.ADD_OR_UPDATE_FLOW, flow});
159 deleteFlow(dispatch, {flow}) {
160 RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({
161 type: actionTypes.DELETE_FLOW,
166 openFlowDetailsEditor(dispatch, flow) {
167 dispatch({type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow});
170 closeFlowDetailsEditor(dispatch) {
171 dispatch({type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR});
174 openFlowDiagramEditor(dispatch, {flow}) {
175 dispatch({type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow});
178 closeFlowDiagramEditor(dispatch) {
179 dispatch({type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR});
182 flowDetailsDataChanged(dispatch, {deltaData}) {
183 dispatch({type: actionTypes.CURRENT_FLOW_DATA_CHANGED, deltaData});
187 dispatch({type: actionTypes.RESET});
191 export default FlowsActions;