blob: 5a751064dfca27ce924e9bd3b2c56d6f1ffcbb62 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
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';
18import {actionTypes, enums} from './FlowsConstants.js';
19import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
20
21
22function baseUrl(serviceId, artifactId = '') {
23 const restATTPrefix = Configuration.get('restATTPrefix');
24 return `${restATTPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
25}
26
27function encodeDataToBase64(dataAsString) {
28 return window.btoa(dataAsString);
29}
30
31function decodeDataToBase64(encodedData) {
32 return window.atob(encodedData);
33}
34
35function encodeContent(flowData) {
36 let data = {
37 VERSION: {
38 major: 1,
39 minor: 0
40 },
41 description: flowData.description,
42 sequenceDiagramModel: flowData.sequenceDiagramModel
43 };
44
45 return encodeDataToBase64(JSON.stringify(data));
46}
47
48function decodeContent(base64Contents) {
49 let description, sequenceDiagramModel;
50 let payload = JSON.parse(decodeDataToBase64(base64Contents));
51
52 if (payload.VERSION === undefined) {
53 description = payload.description || 'Please, provide description...';
54 sequenceDiagramModel = payload.data || payload;
55 sequenceDiagramModel = sequenceDiagramModel.model || sequenceDiagramModel;
56
AviZi280f8012017-06-09 02:39:56 +030057 }
58 else if (payload.VERSION.major === 1) {
Michael Landoefa037d2017-02-19 12:57:33 +020059 description = payload.description;
60 sequenceDiagramModel = payload.sequenceDiagramModel;
61 }
62
63 return {
64 description,
65 sequenceDiagramModel
66 };
67}
68
69function 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
77 };
78
AviZi280f8012017-06-09 02:39:56 +030079 return RestAPIUtil.post(
Michael Landoefa037d2017-02-19 12:57:33 +020080 baseUrl(flowData.serviceID, flowData.uniqueId),
81 createOrUpdateRequest,
82 {md5: true}
83 );
84}
85
86const FlowsActions = Object.freeze({
87
AviZi280f8012017-06-09 02:39:56 +030088 fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID, readonly}) {
Michael Landoefa037d2017-02-19 12:57:33 +020089 let results = [];
90 if (!Object.keys(artifacts).length) {
AviZi280f8012017-06-09 02:39:56 +030091 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
92 if (!readonly) {
93 FlowsActions.openFlowDetailsEditor(dispatch);
94 }
Michael Landoefa037d2017-02-19 12:57:33 +020095 }
96 else {
97 Object.keys(artifacts).forEach(artifact => results.push({
98 artifactType: diagramType,
99 participants,
100 serviceID,
101 ...artifacts[artifact]
102 }));
AviZi280f8012017-06-09 02:39:56 +0300103 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
Michael Landoefa037d2017-02-19 12:57:33 +0200104 }
105 },
106
107 fetchArtifact(dispatch, {flow}){
108 let {serviceID, uniqueId, participants} = flow;
AviZi280f8012017-06-09 02:39:56 +0300109 return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200110
111 let {artifactName, base64Contents} = response;
112 let {sequenceDiagramModel, ...other} = decodeContent(base64Contents);
113
114 if (!sequenceDiagramModel) {
115 sequenceDiagramModel = SequenceDiagramModelHelper.createModel({
116 id: uniqueId,
117 name: artifactName,
118 lifelines: participants
119 });
120 }
121 else {
122 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(sequenceDiagramModel, {
123 name: artifactName,
124 lifelines: participants
125 });
126 }
127
128 flow = {
129 ...flow,
130 ...other,
131 uniqueId,
132 artifactName,
133 sequenceDiagramModel
134 };
135
136 dispatch({type: actionTypes.ARTIFACT_LOADED, flow});
137 FlowsActions.openFlowDiagramEditor(dispatch, {flow});
138 });
139 },
140
141 createOrUpdateFlow(dispatch, {flow}, isNew) {
142 if (!isNew && flow.sequenceDiagramModel) {
143 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(flow.sequenceDiagramModel, {
144 name: flow.artifactName
145 });
146 }
AviZi280f8012017-06-09 02:39:56 +0300147 return createOrUpdate(flow).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200148 let {uniqueId, artifactLabel} = response;
149 flow = {...flow, uniqueId, artifactLabel};
150 if (isNew) {
151 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel({id: uniqueId, name: flow.artifactName});
152 }
153 dispatch({type: actionTypes.ADD_OR_UPDATE_FLOW, flow});
154 });
155 },
156
157 deleteFlow(dispatch, {flow}) {
AviZi280f8012017-06-09 02:39:56 +0300158 return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({
Michael Landoefa037d2017-02-19 12:57:33 +0200159 type: actionTypes.DELETE_FLOW,
160 flow
161 }));
162 },
163
164 openFlowDetailsEditor(dispatch, flow) {
165 dispatch({type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow});
166 },
167
168 closeFlowDetailsEditor(dispatch) {
169 dispatch({type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR});
170 },
171
172 openFlowDiagramEditor(dispatch, {flow}) {
173 dispatch({type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow});
174 },
175
176 closeFlowDiagramEditor(dispatch) {
177 dispatch({type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR});
178 },
179
Michael Landoefa037d2017-02-19 12:57:33 +0200180 reset(dispatch) {
181 dispatch({type: actionTypes.RESET});
182 }
183});
184
185export default FlowsActions;